话说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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,204评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,091评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,548评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,657评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,689评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,554评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,302评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,216评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,661评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,851评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,977评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,697评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,306评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,898评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,019评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,138评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,927评论 2 355

推荐阅读更多精彩内容

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