未分类 · 2023年3月24日 0

CSS 布局 – 水平 & 垂直对齐

CSS 布局 - 水平 & 垂直对齐


水平 & 垂直居中对齐


元素居中对齐

要水平居中对齐一个元素(如

), 可以使用 margin: auto;

设置到元素的宽度将防止它溢出到容器的边缘。

元素通过指定宽度,并将两边的空外边距平均分配:

div 元素是居中的

实例

.center {
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
}

尝试一下 »

注意: 如果没有设置 width 属性(或者设置 100%),居中对齐将不起作用。


文本居中对齐

如果仅仅是为了文本在元素内居中对齐,可以使用 text-align: center;

文本居中对齐

实例

.center {
text-align: center;
border: 3px solid green;
}

尝试一下 »

提示: 更多文本对齐实例,请参阅 CSS 文本 章节。


图片居中对齐

要让图片居中对齐, 可以使用 margin: auto; 并将它放到 元素中:

实例

img {
display: block;
margin: auto;
width: 40%;
}

尝试一下 »


左右对齐 - 使用定位方式

我们可以使用 position: absolute; 属性来对齐元素:

菜鸟教程 -- 学的不仅是技术,更是梦想!!!

实例

.right {
position: absolute;
right: 0px;
width: 300px;
border: 3px solid #73AD21;
padding: 10px;
}

尝试一下 »

注释:绝对定位元素会被从正常流中删除,并且能够交叠元素。

提示: 当使用 position 来对齐元素时, 通常 元素会设置
marginpadding 。 这样可以避免在不同的浏览器中出现可见的差异。
当使用 position 属性时,IE8 以及更早的版本存在一个问题。如果容器元素(在我们的案例中是

)设置了指定的宽度,并且省略了 !DOCTYPE 声明,那么 IE8 以及更早的版本会在右侧增加 17px 的外边距。这似乎是为滚动条预留的空间。当使用 position 属性时,请始终设置 !DOCTYPE 声明:

实例

body {
margin: 0;
padding: 0;
}

.container {
position: relative;
width: 100%;
}

.right {
position: absolute;
right: 0px;
width: 300px;
background-color: #b0e0e6;
}

尝试一下 »


左右对齐 - 使用 float 方式

我们也可以使用 float 属性来对齐元素:

实例

.right {
float: right;
width: 300px;
border: 3px solid #73AD21;
padding: 10px;
}

尝试一下 »

当像这样对齐元素时,对

元素的外边距和内边距进行预定义是一个好主意。这样可以避免在不同的浏览器中出现可见的差异。

注意:如果子元素的高度大于父元素,且子元素设置了浮动,那么子元素将溢出,这时候你可以使用 "clearfix(清除浮动)" 来解决该问题。

我们可以在父元素上添加 overflow: auto; 来解决子元素溢出的问题:

实例

.clearfix {
overflow: auto;
}

尝试一下 »

当使用 float 属性时,IE8 以及更早的版本存在一个问题。如果省略 !DOCTYPE 声明,那么 IE8 以及更早的版本会在右侧增加 17px 的外边距。这似乎是为滚动条预留的空间。当使用 float 属性时,请始终设置 !DOCTYPE 声明:

实例

body {
margin: 0;
padding: 0;
}

.right {
float: right;
width: 300px;
background-color: #b0e0e6;
}

尝试一下 »


垂直居中对齐 - 使用 padding

CSS 中有很多方式可以实现垂直居中对齐。 一个简单的方式就是头部顶部使用 padding:

我是垂直居中。

实例

.center {
padding: 70px 0;
border: 3px solid green;
}

尝试一下 »

如果要水平和垂直都居中,可以使用 paddingtext-align: center:

我是水平和垂直都居中的。

实例

.center {
padding: 70px 0;
border: 3px solid green;
text-align: center;
}

尝试一下 »


垂直居中 - 使用 line-height

我是垂直居中的。

实例

.center {
line-height: 200px;
height: 200px;
border: 3px solid green;
text-align: center;
}

/* 如果文本有多行,添加以下代码: */
.center p {
line-height: 1.5;
display: inline-block;
vertical-align: middle;
}

尝试一下 »


垂直居中 - 使用 position 和 transform

除了使用 paddingline-height 属性外,我们还可以使用 transform 属性来设置垂直居中:

实例

.center {
height: 200px;
position: relative;
border: 3px solid green;
}

.center p {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

尝试一下 »

提示: 更多 transform 属性内容可以参阅 2D 翻转章节


更多实例

CSS 使用 margin 让 div 居中对齐
CSS 使用绝对定位 让 div 右对齐

    <div class="previous-next-links">
        <div class="previous-design-link">

    <style>

.wrapper {
/text-transform: uppercase; */
background: #ececec;
color: #555;
cursor: help;
font-family: "Gill Sans", Impact, sans-serif;
font-size: 20px;
position: relative;
text-align: center;
width: 200px;
-webkit-transform: translateZ(0); /
webkit flicker fix /
-webkit-font-smoothing: antialiased; /
webkit text rendering fix */
}

.wrapper .tooltip {
white-space: nowrap;
font-size: 14px;
text-align: left;
background: #96b97d;
bottom: 100%;
color: #fff;
display: block;
left: -25px;
margin-bottom: 15px;
opacity: 0;
padding: 14px;
pointer-events: none;
position: absolute;

-webkit-transform: translateY(10px);
-moz-transform: translateY(10px);
-ms-transform: translateY(10px);
-o-transform: translateY(10px);
transform: translateY(10px);
-webkit-transition: all .25s ease-out;
-moz-transition: all .25s ease-out;
-ms-transition: all .25s ease-out;
-o-transition: all .25s ease-out;
transition: all .25s ease-out;
-webkit-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
-moz-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
-ms-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
-o-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
}
.tooltip a {
color:#fff;
}
/* This bridges the gap so you can mouse into the tooltip without it disappearing */
.wrapper .tooltip:before {
bottom: -20px;
content: " ";
display: block;
height: 20px;
left: 0;
position: absolute;
width: 100%;
}

/* CSS Triangles - see Trevor's post /
.wrapper .tooltip:after {
border-left: solid transparent 10px;
border-right: solid transparent 10px;
border-top: solid #96b97d 10px;
bottom: -10px;
content: " ";
height: 0;
left: 20%;
margin-left: -13px;
position: absolute;
width: 0;
}
.wrapper .tooltip1 {
margin-left: 50px;
padding-top: 0px;
}
/

.wrapper:hover .tooltip {
opacity: 1;
pointer-events: auto;
-webkit-transform: translateY(0px);
-moz-transform: translateY(0px);
-ms-transform: translateY(0px);
-o-transform: translateY(0px);
transform: translateY(0px);
}
/
/
IE can just show/hide with no transition */
.lte8 .wrapper .tooltip {
display: none;
}

.lte8 .wrapper:hover .tooltip {
display: block;
}


1 篇笔记
写笔记

  1. #0

       初心不负

      136***0939@qq.com

    401
    设置容器上下 padding 相同实现垂直居中和使用 line-height=height 实现垂直居中仅对单行文本有效,当文本行数超过单行时:

    • 1)padding:文本仍然处于容器垂直居中的位置,但是容器的 height 会随着文本行数的增加而增大;
    • 2)line-height=height:容器的 height 不变,line-height 是文本的行间距,文本会溢出容器显示;

    多行文本可使用 vertical-align: middle; 来实现元素的垂直居中,但是如果子元素的内容体积大于父元素的内容体积时,仍然会溢出,后面需要使用文字溢出处理来解决。

    初心不负

       初心不负

      136***0939@qq.com

    3年前 (2019-11-20)

打赏 赞(0) 分享'
分享到...
微信
支付宝
微信二维码图片

微信扫描二维码打赏

支付宝二维码图片

支付宝扫描二维码打赏

文章目录