资讯专栏INFORMATION COLUMN

【芝士整理】CSS经典布局

chavesgu / 3245人阅读

摘要:水平居中行内元素定宽块元素常规流中浮动块元素负边距绝对定位元素负边距居中绝对居中不定宽块元素完整垂直居中单行和一致定高块元素负边距居中绝对居中不定高块元素完整水平垂直居中行内元素

水平居中 行内元素

text-align

.parent {
    text-align: center;
}
定宽块元素 常规流中

margin: 0 auto;

.child {
    width: 200px;
    margin: 0 auto;
}
浮动块元素

relative + 负边距

.child {
    width: 200px;
    float: left;
    position: relative;
    left: 50%;
    margin-left: -100px;
}
绝对定位元素

负边距居中

.parent {
    position: relative;
}
.child {
    width: 200px;
    position: absolute;
    left: 50%;
    margin-left: -100px;
}

绝对居中

.parent {
    position: relative;
}
.child {
    width: 200px;
    position: absolute;
    left: 0;
    right: 0;
    margin: 0 auto;
}
不定宽块元素

inline-block + text-align

.parent{
    text-align: center;
}
.child{
    display: inline-block;
}

transform

.parent {
    position: relative;
    height: 300px;
}
.child {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}

flexbox

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

table + margin:auto

.parent {
    display: table;
      margin: 0 auto;
}

table-cell + text-align

.table {
  display: table;
  width: 100%;
}
.table-cell {
  display: table-cell;
  text-align: center;
}

PS: 完整Demo - https://codepen.io/curlywater...

垂直居中 单行

line-height和height一致

.child {
    height: 100px;
    line-height: 100px;
}
定高块元素

负边距居中

.parent {
    position: relative;
}
.child {
    width: 200px;
    position: absolute;
    top: 50%;
    margin-top: -100px;
}

绝对居中

.parent {
    position: relative;
}
.child {
    width: 200px;
    position: absolute;
    top: 0;
    bottom: 0;
    margin: auto;
}
不定高块元素

transform

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

flex

.parent {
    display: flex;
    align-items: center;
    height: 300px;
}

table-cell + vertical-align

.parent {
    display: table-cell;
      vertical-align: middle;
      height: 300px;
}

PS: 完整Demo - https://codepen.io/curlywater...

水平垂直居中 行内元素

text-align: center + line-height一致 + vertical-align: middle (近似居中)

.parent {
    text-align: center;
    line-height: 300px;
    height: 300px;
    text-align: center;
}
.child {
    vertical-align: middle;
}
定高定宽块元素

负边距居中

.parent {
    position: relative;
}
.child {
    width: 200px;
    height: 200px;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-left: -100px;
    margin-top: -100px;
}

绝对居中定位

.parent {
    position: relative;
}
.child {
    width: 200px;
    height: 200px;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
}
不定块元素

transform

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

flex

.parent {
    display: flex;
    justify-content: center;
    align-items: center;
}

table-cell + text-align + vertical-align

.parent {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
.child {
    display: inline-block;
}

PS: 完整Demo - https://codepen.io/curlywater...

上下布局

calc()

.header {
    height: 200px;
}
.content {
    height: calc(100% - 200px);
}

content absolute

.header {
    height: 200px;
}
.content {
    position: absolute;
    width: 100%;
    top: 200px;
    bottom: 0;
}

header absolute

.content {
    height: 100%;
    padding-top: 100px;
    box-sizing: border-box;
}
.header {
    position: absolute;
    height: 100px;
    width: 100%;
}

flex

.container {
    display: flex;
    flex-direction: column;
}
.content {
    flex: 1;
}

table(table-row里需要有内容)

.container {
    display: table;
}
.header, .content {
    display: table-row;
}

PS: 完整Demo - https://codepen.io/curlywater...

左右布局

float + margin

.left {
    width: 200px;
    float: left;
}
.right {
    margin-left: -200px;
}

float + BFC(左右两边都可自适应)

.left {
    float: left;
}
.right {
    overflow: auto;
}

PS: 完整Demo - https://codepen.io/curlywater...

左中右布局

left + right + margin(注意DOM节点顺序)

.left {
    float: left;
    width: 100px;
}
.right {
    float: right;
    width: 200px;
}c
.main {
    margin-left: 100px;
    margin-right: 200px;
    height: 100%;
}

float + BFC

.left {
    float: left;
     width: 100px;
}
.right {
    float: right;
    width: 200px;
}
.main {
    overflow: auto;
    height: 100%;
}

圣杯

https://codepen.io/curlywater...

双飞翼

https://codepen.io/curlywater...

flex

.container {
    display: flex;
}
.main {
    flex: 1;
}

table

.container{
    display: table; 
    width: 100%;
    table-layout: fixed;
}
.left,.main,.right{
    display: table-cell;
}
.main {
    width: 100%;
}

https://codepen.io/curlywater...

等高

padding + margin

.container {
  overflow: hidden;
  .left,
  .right {
    padding-bottom: 9999px;
    margin-bottom: -9999px;
  }
  .left {
    float: left;
    width: 300px;
    background: #333;
    color: #fff;
  }
  .right {
    overflow: hidden;
  }
}

border

.container {
  border-left: 300px solid #333;
  .left {
    float: left;
    margin-left: -300px;
    color: #fff;
  }
  .right {
    height: 100%;
  }
}

table

.container {
  display: table;
  width: 100%;
  .left, .right {
    display: table-cell;
  }
  .left {
    width: 300px;
    background: #333;
    color: #fff;
  }
}

flex

.container {
  display: flex;
  .left {
    width: 300px;
    background: #333;
    color: #fff;
  }
}

https://codepen.io/curlywater...

等分

百分比

.container {
    margin-left: -20px;
    font-size: 0;
}
.item {
    width: 25%;
    padding-left: 20px;
    display: inline-block;
    box-sizing: border-box;
}

flex

.container {
    display: flex;
}
.item {
    flex: 1;
}
.item + .item {
    margin-left: 20px;
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/117049.html

相关文章

  • 芝士整理CSS基础图谱

    摘要:为了实现文字环绕效果,规范规定的措施是使父容器塌陷,元素脱离文档流浮动产生,元素周围的内容转换为围绕元素排列。 选择器注意点 属性选择器 [attr^=value] - 开头或全等 [attr$=value] - 结尾或全等 [attr*=value] - 包含值 [attr~=value] - 字符串包含 选择器组 A > B - 直接子节点 A + B - 下一个兄弟节点 A...

    iOS122 评论0 收藏0
  • 【前端芝士树】如何对元素块实现居中(垂直和水平方向都居中)?

    摘要:前端芝士树如何对元素块实现垂直居中水平居中和垂直居中是前端开发过程中肯定会遇到的问题,下面我讲解几种常见的方式。 【前端芝士树】如何对元素块实现垂直居中? showImg(https://segmentfault.com/img/bVbisNT?w=430&h=183); 水平居中和垂直居中是前端开发过程中肯定会遇到的问题,下面我讲解几种常见的方式。 1/ 利用Flex布局来实现极速居...

    _Dreams 评论0 收藏0
  • 【前端芝士树】如何对元素块实现居中(垂直和水平方向都居中)?

    摘要:前端芝士树如何对元素块实现垂直居中水平居中和垂直居中是前端开发过程中肯定会遇到的问题,下面我讲解几种常见的方式。 【前端芝士树】如何对元素块实现垂直居中? showImg(https://segmentfault.com/img/bVbisNT?w=430&h=183); 水平居中和垂直居中是前端开发过程中肯定会遇到的问题,下面我讲解几种常见的方式。 1/ 利用Flex布局来实现极速居...

    baoxl 评论0 收藏0
  • 芝士整理】JS基础图谱

    摘要:没有找到的话,看上级函数作用域,向上查找到,找到为止。将会在执行上下文栈中保留上级作用域的执行上下文。若在闭包使用完毕之后不手动解除引用,相关执行上下文将会一直保留于执行上下文栈中,占据内存空间,若持续积累,容易造成内存泄漏。 JS有哪些基本数据类型呢? 值类型:undefined, Number, Boolean, String,null 引用类型:Object 值类型存放在栈中 引...

    netScorpion 评论0 收藏0
  • CSS 布局经典问题初步整理

    摘要:布局经典问题初步整理标签前端本文主要对布局中常见的经典问题进行简单说明,并提供相关解决方案的参考链接,涉及到三栏式布局,负,清除浮动,居中布局,响应式设计,布局,等等。 CSS 布局经典问题初步整理 标签 : 前端 [TOC] 本文主要对 CSS 布局中常见的经典问题进行简单说明,并提供相关解决方案的参考链接,涉及到三栏式布局,负 margin,清除浮动,居中布局,响应式设计,Fl...

    Amos 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<