话说CSS居中和CSS布局

  1. 水平居中
    (1)内联元素
.center-children {
  text-align: center;
}

(2)块级元素

.center-me {
  margin: 0 auto;
}

(3)多个块级元素
第一种方法:我们将多个块级元素运用display:inline-block,转换为内联元素,然后运用text-align:center来处理。

<main class="inline-block-center">
    <div>
        I'm an element that is block-like with my siblings and we're centered in a row.
    </div>
    <div>
        I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
    </div>
    <div>
        I'm an element that is block-like with my siblings and we're centered in a row.
    </div>
</main>
.inline-block-center {
    text-align: center;
}
.inline-block-center div {
    display: inline-block;
    text-align: left;
}

第二种方法:运用flex。

<main class="flex-center">
    <div>
        I'm an element that is block-like with my siblings and we're centered in a row.
    </div>
    <div>
        I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
    </div>
    <div>
        I'm an element that is block-like with my siblings and we're centered in a row.
    </div>
</main>
.flex-center {
    display: flex;
    justify-content: center;
}
  1. 垂直居中
    (1) 单行内联元素
    第一种方法:上下设置相同的padding。
.link {
  padding-top: 30px;
  padding-bottom: 30px;
}

第二种方法:运用行高, 注意是不能换行的。

.link {
  height: 30px;
  line-height: 30px;
  white-space: nowrap;
}

(2) 多行内联元素

.parent-center {
  display: flex;
  justify-content: center;
  flex-direction: column;
}

(3) 块级元素

情况一: 定高

.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 50%;
  height: 100px;
  margin-top: -50px; /* account for padding and border if not using box-sizing: border-box; */
}

情况二:不定高

.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

情况三:运用flex

.parent {
  display: flex;
  flex-direction: column;
  justify-content: center;
}
  1. 水平垂直居中

情况一:定高定宽

.parent {
  position: relative;
}

.child {
  width: 300px;
  height: 100px;
  padding: 20px;

  position: absolute;
  top: 50%;
  left: 50%;

  margin: -70px 0 0 -170px;
}

情况二:不定宽高

.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

情况三:运用flex

.parent {
  display: flex;
  //水平居中
  justify-content: center;
  //垂直居中
  align-items: center;
}

情况四:运用grid

body, html {
  height: 100%;
  display: grid;
}
span {
  margin: auto;
}
  1. 固定宽度布局

4.1 左右布局(两列布局)
html代码:

<div class="container clearfix">
    <div class="left-box">左侧列</div>
    <div class="right-box">右侧列</div>
 </div>

CSS代码:

.left-box {
    float: left;
    width: 100px;
    height: 100px;
    background-color: blue;
}
.right-box {
    float: left;
    width: 200px;
    height: 100px;
    background-color: red;
}

4.2 左中右布局(三列布局)
三列布局同两列布局的原理,使用float浮动。
html代码:

<div class="container clearfix">
    <div class="left-box">左侧列</div>
    <div class="mid-box">中间列</div>
    <div class="right-box">右侧列</div>
 </div>

CSS代码:

.left-box {
    float: left;
    width: 100px;
    height: 100px;
    background-color: blue;
}
.mid-box {
    float: left;
    width: 100px;
    height: 100px;
    background-color: green;
}
.right-box {
    float: left;
    width: 200px;
    height: 100px;
    background-color: red;
}
  1. 自适应布局

5.1左右布局(两列布局)
假设左侧列定宽,右侧列自适应。
(1) 自适应宽度运用calc计算。
CSS代码:

.left-box {
    float: left;
    width: 100px;
    height: 100px;
    background-color: blue;
}
.right-box {
    float: left;
    width: calc(100% - 100px);
    height: 100px;
    background-color: red;
}

(2)结合绝对定位
CSS代码:

.container {
  position: relative;
  height: 100px;
}
.left-box {
    position: absolute;
    top: 0;
    left: 0;
    width: 100px;
    height: 100px;
    background-color: blue;
}
.right-box {
    margin-left: 100px;
    height: 100px;
    background-color: red;
}

5.2 左中右布局(三列布局,中间栏自适应)

(1) 使用float浮动
html代码:

<div class="container clearfix">
    <div class="left-box">左侧列</div>
    <div class="right-box">右侧列</div>
    <div class="mid-box">中侧列</div>
</div>

CSS代码:

.container {
    height: 100px;
}

.left-box {
    float: left;
    width: 300px;
    height: 100%;
    background-color: red;
}
.mid-box {
    margin: 0 300px;
    height: 100%;
    background-color: green;
}
.right-box {
    float: right;
    width: 300px;
    height: 100%;
    background-color: blue;
}

注意:在html代码中,右侧列和中间列位置对换,如果不换,右侧列会换行。原因是:浮动的三栏布局(中间栏自适应)中,因为中间列是没有设置浮动定位,所以中间div没有脱离文档流,默认是block,块状元素会占满整行,导致右侧列是从下一行开始向右浮动。要使浮动的三列布局起效,要调整一下html的布局。

(2) 结合定位布局
html代码:

<div class="container clearfix">
    <div class="left-box">左侧列</div>
    <div class="right-box">右侧列</div>
    <div class="mid-box">中侧列</div>
</div>

CSS代码:

.left-box {
    position: absolute;
    left: 0;
    width: 300px;
    height: 100%;
    background-color: red;
}
.mid-box {
    margin: 0 300px;
    height: 100%;
    background-color: green;
}
.right-box {
    position: absolute;
    right: 0;
    width: 300px;
    height: 100%;
    background-color: blue;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,790评论 1 92
  • 1. 前言 前端圈有个“梗”:在面试时,问个css的position属性能刷掉一半人,其中不乏工作四五年的同学。在...
    YjWorld阅读 4,523评论 5 15
  • H5移动端知识点总结 阅读目录 移动开发基本知识点 calc基本用法 box-sizing的理解及使用 理解dis...
    Mx勇阅读 4,611评论 0 26
  • 果戈里说过:写作的人像画家不应该停止画笔一样,也是不应该停止笔头的。随便他写什么,必须每天写,要紧的是叫手学会完全...
    沈同学的伪哲学阅读 266评论 0 7
  • 刚做了父母的朋友,常常为孩子的英语启蒙问题,感到焦虑。在这里,简单分享一些个人的观点和经验。 一、英语启蒙很重要,...
    howhowfire阅读 3,663评论 5 15