CSS垂直置中

absolute + top/left + tranform

利用top/left将子元素的左上角对齐中间,再利用transform平移元素自身50%的宽高达到置中。

.father
{
  border:1px solid black;
  height:100px;
  width:100px;
  position:relative;
}
.child
{
   border:1px solid black;
   height:50px;
   width:50px;
   position:absolute;
   top:50%;
   left:50%;
   transform:translate(-50%,-50%)
}

Flex

将父元素设定为Flex,同时使用justify-content让主轴置中,再使用align-items让次轴置中。

.father
{
  border:1px solid black;
  height:100px;
  width:100px;
  display:flex;
  justify-content: center;
  align-items: center;
}

relative + calc

利用calc计算出置中,父元素(100% - 自身高) / 2 = 垂直置中。
margin:auto 水平置中。

.father
{
  border:1px solid black;
  height:100px;
  width:100px;
  

}
.child
{
   border:1px solid black;
   height:30px;
   width:30px;
   position:relative; 
   top : calc((100% - 30px) / 2);
   margin:auto;
}

<<:  [Day 18] 重构我们的测试程序码

>>:  Day 08 Section Summary 2

用 Python 畅玩 Line bot - 29:Line Notify(二)

到 Line Notify 点进连动好的服务後,可以看到他有产生一个 Client ID 和 Cli...

[day4] 安全签章 - 产生订单 & 签章(Sign)

准备讯息文本 依照参数说明,建立订单的资料结构(DAY3-参考),详细参数规格可以在永丰API技术规...

Day.9 备份还原 - 还原资料 (MYSQL binlog )-下

前情提要: 在前一天的部分我们备份好user资料库数据,和准备资料的动作 << 那就直接...

[Day 10] - Spring Boot 实作登入验证(四)(JWT登入验证)

今天就来完成登入验证的部分! 昨天已经完成发送帐号密码到api,验证ok即发送一笔JWT给clien...

【Day 20】Google Apps Script - API 篇回顾整理

停留回想:要进入下一篇前,整理回顾一下 API 篇的笔记思绪。 今日要点: 》API 篇回顾整理 ...