饥人谷技术博客

使用CSS做出常用布局

2019-03-19  本文已影响13人  陈光展_Gz

以下用CSS介绍如何做出

  • 左右布局
  • 左中右布局
  • 水平居中
  • 垂直居中
  • 其他小技巧

一、左右布局

方法一:对于块元素采用给爸爸元素加clearfix,给儿子元素加float: left;

html

   <div class="father clearfix"> /* 爸爸元素添加clearfix */
       <div class="son">龟儿子1</div>
       <div class="son">龟儿子2</div>
       <div class="son">龟儿子3</div>
   </div>

CSS

.clearfix::after {
    content: '';
    display: block;
    clear: both;
}
.son {
    float: left; /* 儿子元素添加float: left; */
}

运行结果

左右布局

方法二:对于块元素设置display: inline-block;(display属性规定元素该生成的框类型。inline-block是让定义元素作为行内块元素。),这样会出现行距上下不平均的BUG,故还要添加vertical-align: top;解决这个BUG,然后给父元素加入text-align: center;可以实现块状子元素水平居中。

html

   <div class="father">
       <div class="son">龟儿子1</div>
       <div class="son">龟儿子2</div>
       <div class="son">龟儿子3</div>
   </div>

CSS

.father {
    text-align: center;
}
.son {
    display: inline-block;
    vertical-align: top; /* 为了解决bug */
}

运行结果

左右布局

二、左中右布局

方法一:在左右布局中添加margin实现左中右布局;并采用伪类。
思路示意图:

image.png

html

   <div class="father clearfix"> /* 爸爸元素添加clearfix */
       <div class="son">龟儿子1</div>
       <div class="son">龟儿子2</div>
       <div class="son">龟儿子3</div>
   </div>

CSS

.clearfix::after {
    content:'';
    display:block;
    clear:both;
}
.son {
    float:left; 
    border:1px solid green
}
.son:nth-child(even) { /* 此处不能有空格 */
    margin-left:20px;
    margin-right:20px;
}

运行结果

左中右.png

方法二:给爸爸元素postion: relative;,儿子元素postion: absolute;
思路示意图:

image.png

html

   <div class="father"> /* 爸爸元素添加clearfix */
       <div class="son">龟儿子1</div>
       <div class="son">龟儿子2</div>
       <div class="son">龟儿子3</div>
   </div>

CSS

.father {
  postion: relavtive;
}
.son {
  postion: absloute;
  display:inline-block;
  border: 1px solid red;
}
.son:nth-child(even) {
  postion: absloute;
  display:inline-block;
  margin-left: 20px;
  margin-right: 20px;
}

运行结果

左中右

三、水平居中

CSS

.father {
    position: relative;
}
.son {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}

方法二:Flex布局

CSS

.father {
    display: flex;
    justify-content: center;
}

Tips:适用于子元素为浮动,绝对定位,内联元素,均可水平居中。

CSS

.father {
    text-align: center;
}

Tips:此方法同样适用于 display: inline-block 的元素。

CSS

.parent {
    width: 100%;
}
.child {
    width: 600px;
    height: 50px;
    margin: 0 auto;
    background: #999;
}

Tips:此方法只能进行水平的居中,且对浮动元素或绝对定位元素无效。

方式二:修改为 inline-block 属性

CSS

.father {
    text-align: center;
}
.son {
    display: inline-block;
}

CSS

.son {
    width: 100px;
    float: left;
    position: relative;
    left: 50%;
    margin-left: -50px;
}

方式一:

.father {
    position: relative;
}
.son {
    position: absolute;
    width: 100px;
    left: 50%;
    margin-left: -50px;
}

方式二:

.father {
    position: relative;
}
.child {
    position: absolute;
    width: 100px;
    left: 0;
    right: 0;
    margin: 0 auto;
}

四、垂直居中

.text {
    line-height: 200px;
    height: 200px;
}

TIips:把文字的 line-height 设为文字父容器的高度,适用于只有一行文字的情况。

.father {
    position: relative;
}
.son {
    position: absolute;
    top: 0;
    bottom: 0;
    height: 100px;
    margin: auto 0;

方式二:

.father {
    position: relative;
}
.son {
    position: absolute;
    top: 50%;
    height: 100px;
    margin-top: -50px;
}

其他小技巧

写页面时,经常遇到需要扩大文字间间距的情况,尤其是标题,如果用空格去填充那就太麻烦了,代码也不够优雅。好在css提供了扩大文字间距的属性letter-spacing

image

但是由于最后一个字符后也跟了一个间距,导致文字并没有完全居中。那么现在要使用css提供的第二个间距相关的属性text-indent,可以控制首行文本前的间距。设置与letter-spacing的值相同即可。

image

代码如下:

  letter-spacing: 0.5em;
  text-indent: 0.5em;
上一篇 下一篇

猜你喜欢

热点阅读