找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索本站精品资源

首页 教程频道 查看内容

CSS中常见的布局有哪些?

作者:模板之家 2021-7-23 18:15 125人关注

CSS中常见的布局有:1、水平居中,内联元素水平居中、块级元素水平居中和多块级元素水平居中;2、垂直居中,单行内联元素垂直居中和多行元素垂直居中;3、利用flex布局;4、单列布局;5、两列布局。

CSS中常见的布局有:1、水平居中,内联元素水平居中、块级元素水平居中和多块级元素水平居中;2、垂直居中,单行内联元素垂直居中和多行元素垂直居中;3、利用flex布局;4、单列布局;5、两列布局。

本教程操作环境:windows7系统、CSS3&&HTML5版、Dell G3电脑。

1、水平居中:

内联元素水平居中

利用 text-align: center 可以实现在块级元素内部的内联元素水平居中。此方法对内联元素(inline), 内联块(inline-block), 内联表(inline-table), inline-flex元素水平居中都有效。

核心代码:

.center-text {
  text-align: center;
}

块级元素水平居中

通过把固定宽度块级元素的margin-left和margin-right设成auto,就可以使块级元素水平居中。

核心代码:

.center-block {
  margin: 0 auto;
}

多块级元素水平居中

利用inline-block

如果一行中有两个或两个以上的块级元素,通过设置块级元素的显示类型为inline-block和父容器的text-align属性从而使多块级元素水平居中。

核心代码:

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

2、垂直居中

单行内联(inline-)元素垂直居中

通过设置内联元素的高度(height)和行高(line-height)相等,从而使元素垂直居中。

核心代码:

#v-box {
    height: 120px;
    line-height: 120px;
}

多行元素垂直居中

利用表布局(table)

利用表布局的vertical-align: middle可以实现子元素的垂直居中。

核心代码:

.center-table {
    display: table;
}
.v-cell {
    display: table-cell;
    vertical-align: middle;
}

3、利用flex布局(flex)

利用flex布局实现垂直居中,其中flex-direction: column定义主轴方向为纵向。因为flex布局是CSS3中定义,在较老的浏览器存在兼容性问题。

核心代码:

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

4、单列布局

主要有两种:

- header, content, footer宽度相同,有一个max-width

- header和footer占满浏览器100%宽度,content有一个max-width

第一种

<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>

5、两列布局

float + margin

用float将边栏与主要内容拉到一行,然后设置主要内容的margin。

<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>

推荐学习:css视频教程

以上就是CSS中常见的布局有哪些?的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章!


路过

雷人

握手

鲜花

鸡蛋
来自: 网络收集

全部回复(0)