CSS中常见的布局有哪些?
时间:2022-02-11 17:12
CSS中常见的布局有:1、水平居中,内联元素水平居中、块级元素水平居中和多块级元素水平居中;2、垂直居中,单行内联元素垂直居中和多行元素垂直居中;3、利用flex布局;4、单列布局;5、两列布局。 本教程操作环境:windows7系统、CSS3&&HTML5版、Dell G3电脑。 1、水平居中: 内联元素水平居中 利用 text-align: center 可以实现在块级元素内部的内联元素水平居中。此方法对内联元素(inline), 内联块(inline-block), 内联表(inline-table), inline-flex元素水平居中都有效。 核心代码: 块级元素水平居中 通过把固定宽度块级元素的margin-left和margin-right设成auto,就可以使块级元素水平居中。 核心代码: 多块级元素水平居中 利用inline-block 如果一行中有两个或两个以上的块级元素,通过设置块级元素的显示类型为inline-block和父容器的text-align属性从而使多块级元素水平居中。 核心代码: 2、垂直居中 单行内联(inline-)元素垂直居中 通过设置内联元素的高度(height)和行高(line-height)相等,从而使元素垂直居中。 核心代码: 多行元素垂直居中 利用表布局(table) 利用表布局的vertical-align: middle可以实现子元素的垂直居中。 核心代码: 3、利用flex布局(flex) 利用flex布局实现垂直居中,其中flex-direction: column定义主轴方向为纵向。因为flex布局是CSS3中定义,在较老的浏览器存在兼容性问题。 核心代码: 4、单列布局 主要有两种: - header, content, footer宽度相同,有一个max-width - header和footer占满浏览器100%宽度,content有一个max-width 第一种 第二种: 5、两列布局 float + margin 用float将边栏与主要内容拉到一行,然后设置主要内容的margin。 推荐学习:css视频教程 以上就是CSS中常见的布局有哪些?的详细内容,更多请关注gxlsystem.com其它相关文章!.center-text {
text-align: center;
}
.center-block {
margin: 0 auto;
}
.container {
text-align: center;
}
.inline-block {
display: inline-block;
}
#v-box {
height: 120px;
line-height: 120px;
}
.center-table {
display: table;
}
.v-cell {
display: table-cell;
vertical-align: middle;
}
.center-flex {
display: flex;
flex-direction: column;
justify-content: center;
}
<header style="background-color: red; width: 600px; margin: 0 auto;">头部</header>
<main style="background-color: green; width: 600px; margin: 0 auto;">内容</main>
<footer style="background-color: yellow; width: 600px; margin: 0 auto;">尾部</footer>
<header style="background-color: red;">头部</header>
<main style="background-color: green; width: 600px; margin: 0 auto;">内容</main>
<footer style="background-color: yellow;">尾部</footer>
<main style="background-color: red;">
<aside style="background-color: yellow; float: left; width: 50px;">边栏</aside>
<section style="background-color: green; margin-left: 50px;">主要内容</section>
</main>