发表于: 2019-06-26 23:44:39

1 806


今天开始做任务八,在试着用框架来布局,对框架了解的不多导致出了好多问题。

明天的计划是把首页和第二页用框架写出来。

遇到的问题:主要是还是一些居中对齐的问题,有些在框架里用的和自己写的有冲突。

今天的收获:《css 有哪些方式可以实现垂直居中》

1. 背景介绍

 

居中是前端排版的一个重要话题,今天我们就来梳理一下垂直居中的方法。

 

2. 知识剖析

 

布局的解决方案,基于盒状模型,依赖 display 属性 + position 属性 + float 属性 + flexbox,咱们就从这几点入手。

 

3. 常见问题

 

垂直居中的方式?

 

4. 解决方案

 

HTML 如下:

  1. <div class="out">
  2. <div class="in">nihaoa</div>
  3. </div>

默认的 CSS 如下:

  1. .out {
  2. width: 300px;
  3. height: 300px;
  4. background-color: red;
  5. }
  6. .in {
  7. background-color: yellow;
  8. }

①子元素 line-height: 100%,代码少,兼容性好,但子元素背景色覆盖了父元素:

  1. .in {
  2. line-height: 100%;
  3. }

②子元素 absolute+auto,兼容性好,但需要设置子元素的高度:

  1. .out {
  2. position: relative;
  3. }
  4. .in {
  5. position: absolute;
  6. top: 0;
  7. bottom: 0;
  8. margin: auto;
  9. height: 20px;
  10. }

③ 子元素 top+margin-top,兼容性好,但需要设置子元素的高度 :

  1. .out {
  2. position: relative;
  3. }
  4. .in {
  5. position: absolute;
  6. top: 50%;
  7. height: 20px;
  8. margin-top: -10px;
  9. }

④ 子元素 absolute+ translateY,无需设置子元素的高度,但兼容性不好:

  1. .out {
  2. position: relative;
  3. }
  4. .in {
  5. position: absolute;
  6. top: 50%;
  7. transform: translateY(-50%);
  8. }

⑤ 子元素 top: calc(),只需设置子元素,但需要知道子元素的高度,且兼容性不好:

  1. .in {
  2. position: relative;
  3. height: 20px;
  4. top: calc(50% - 10px);
  5. }

⑥ 父元素 display: flex; 设置很简单,但兼容性不好

  1. .out {
  2. display: flex;
  3. align-items: center;
  4. }

⑦ 父元素 display: flex;+ 子元素 margin:auto;设置简单,比 6 稍微复杂,但同样兼容性不好:

  1. .out {
  2. display: flex;
  3. }
  4. .in {
  5. margin: auto 0;
  6. }

⑧ table 布局,兼容性好,但子元素背景色会覆盖父元素,且性能不好:

  1. .out {
  2. display: table;
  3. }
  4. .in {
  5. display: table-cell;
  6. vertical-align: middle;
  7. }

⑨ 兼容性好,但需要知道父元素高度,且比较复杂:

  1. .out:after {
  2. content: '';
  3. display: inline-block;
  4. height: 100%;
  5. vertical-align: middle;
  6. }
  7. .in {
  8. display: inline;
  9. vertical-align: middle;
  10. }

5. 编码实战

 

如上

6. 扩展思考

 

如何水平居中?

方法如下:
①子元素 margin: auto;

  1. .in {
  2. margin: auto;
  3. width: 4em;
  4. }

②对于行内元素 text-align: center;

  1. .in {
  2. text-align: center;
  3. }

③和②相似

  1. .out {
  2. text-align: center;
  3. }
  4. .in {
  5. display: inline-block;
  6. }

④绝对定位 + left+ margin-left

  1. .out {
  2. position: relative;
  3. }
  4. .in {
  5. position: absolute;
  6. left: 50%;
  7. width: 5em;
  8. margin-left: -2.5em;
  9. }

⑤绝对定位 + left+ translatex

  1. .out {
  2. position: relative;
  3. }
  4. .in {
  5. position: absolute;
  6. left: 50%;
  7. transform: translatex(-50%);
  8. }

⑥子元素 display: table;

  1. .in {
  2. display: table;
  3. margin: auto;
  4. }

⑦父元素 display: flex;+ 子元素 margin:auto;

  1. .out {
  2. display: flex;
  3. }
  4. .in {
  5. margin: 0 auto;
  6. height: 20px;
  7. }

⑧父元素 display: flex;

  1. .out {
  2. display: flex;
  3. justify-content: center;
  4. }
  5. .in {
  6. height: 20px;
  7. }

 https://blog.csdn.net/jnshu_it/article/details/89349543


返回列表 返回列表
评论

    分享到