【CSS】水平居中和垂直居中

水平居中和垂直居中



2019-11-12  15:35:37  by冲冲

1、水平居中

(1)父级元素是行内元素,子级元素是行内元素,子级元素水平居中

① 设置父级元素为块级元素 display:block;

② 给父级元素添加 text-aglin:center;

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>居中测试</title>
 6 <style>
 7 #father {
 8   background-color: skyblue;
 9   display:block;
10   text-align:center;
11 }
12 #son {
13   background-color: green;
14 }
15 </style>
16 </head>
17
18 <body>
19 <span id="father">
20   <span id="son">我将居中</span>
21 </span>
22 </body>
23 </html>

(2)父级元素是块级元素,子级元素是行内元素,子级元素水平居中

① 给父级元素添加 text-aglin:center;

(3)父级元素是块级元素,子级元素是块级元素,子级元素水平居中

方案一

(31)父级元素和子级元素有宽度

① 给子级元素添加 margin: 0 auto;

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>居中测试</title>
 6 <style>
 7 #father {
 8   width: 500px;
 9   height: 300px;
10   background-color: skyblue;
11 }
12 #son {
13   width: 200px;
14   height: 100px;
15   background-color: green;
16   margin: 0 auto;
17 }
18 </style>
19 </head>
20
21 <body>
22 <div id="father">
23   <div id="son">我将居中</div>
24 </div>
25 </body>
26 </html>

(32)父级元素有宽度,子级元素没有宽度(特点:会默认子元素的宽度和父元素一样)

① 设置子级元素为行内块元素 display:inline-block;

② 给父级元素添加 text-align:center;

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>居中测试</title>
 6 <style>
 7 #father {
 8   width: 500px;
 9   height: 300px;
10   background-color: skyblue;
11   text-align: center;
12 }
13 #son {
14   background-color: green;
15   display: inline-block;
16 }
17 </style>
18 </head>
19
20 <body>
21 <div id="father">
22   <div id="son">我将居中</div>
23 </div>
24 </body>
25 </html>

方案二 -- 使用定位属性

(31)父级元素和子级元素有宽度

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>居中测试</title>
<style>
#father {
  width: 500px;
  height: 300px;
  background-color: skyblue;
  position: relative;
}
#son {
  width: 200px;
  height: 100px;
  background-color: green;
  position: absolute;
  left: 150px;
}
</style>
</head>

<body>
<div id="father">
  <div id="son">我将居中</div>
</div>
</body>
</html>

(32)父级元素有宽度,子级元素没有宽度

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>居中测试</title>
 6 <style>
 7 #father {
 8   width: 500px;
 9   height: 300px;
10   background-color: skyblue;
11   position: relative;
12 }
13 #son {
14   background-color: green;
15   position: absolute;
16   left: 50%;/*元素的左侧居中,而非元素的中心居中*/
17   transform: translateX(-50%);/*把元素沿着横向(x轴)移动自身宽度的50%*/
18 }
19 </style>
20 </head>
21
22 <body>
23 <div id="father">
24   <div id="son">我将居中</div>
25 </div>
26 </body>
27 </html>

注意:left:50%;不会中心居中,而是左边居中

方案三 -- flexbox布局

使用flex布局,宽度有或无都OK

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>居中测试</title>
 6 <style>
 7 #father {
 8   width: 500px;
 9   height: 300px;
10   background-color: skyblue;
11   display: flex;
12   justify-content: center;
13 }
14 #son {
15   background-color: green;
16 }
17 </style>
18 </head>
19
20 <body>
21 <div id="father">
22   <div id="son">我将居中</div>
23 </div>
24 </body>
25 </html>

2、垂直居中

(1)父级元素是块元素,子级元素是行内元素,子级元素垂直居中

(11)单行文本居中

① 父级元素设置行高等于盒子高 height=500px;line-height:500px;

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>居中测试</title>
 6 <style>
 7 #father {
 8   width: 500px;
 9   height: 300px;
10   background-color: skyblue;
11   line-height: 300px;
12 }
13 #son {
14   background-color: green;
15 }
16 </style>
17 </head>
18
19 <body>
20 <div id="father">
21   <div id="son">我将居中</div>
22 </div>
23 </body>
24 </html>

(12)多行文本居中

① 父级元素设置 display:table-cell;vertical-align:middle;

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>居中测试</title>
 6 <style>
 7 #father {
 8   width: 500px;
 9   height: 300px;
10   background-color: skyblue;
11   display:table-cell;
12   vertical-align: middle;
13 }
14 #son {
15   background-color: green;
16 }
17 </style>
18 </head>
19
20 <body>
21 <div id="father">
22   <div id="son">我将居中我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素</div>
23 </div>
24 </body>
25 </html>

(2)父级元素和子级元素都是块级元素

(21)父级元素和子级元素都有高度

方案一 -- 使用定位

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>居中测试</title>
 6 <style>
 7 #father {
 8   width: 500px;
 9   height: 300px;
10   background-color: skyblue;
11   position: relative;
12 }
13 #son {
14   background-color: green;
15   height: 100px;
16   position: absolute;
17   top: 100px;
18 }
19 </style>
20 </head>
21
22 <body>
23 <div id="father">
24   <div id="son">我将居中</div>
25 </div>
26 </body>
27 </html>

方案二 -- flexbox布局(子级元素高度有或无都OK)

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>居中测试</title>
 6 <style>
 7 #father {
 8   width: 500px;
 9   height: 300px;
10   background-color: skyblue;
11   display: flex;
12   align-items: center;
13 }
14 #son {
15   background-color: green;
16   height: 100px;
17 }
18 </style>
19 </head>
20
21 <body>
22 <div id="father">
23   <div id="son">我将居中</div>
24 </div>
25 </body>
26 </html>

(22)父级元素有高度,子级元素没有高度

方案一 -- 定位属性

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>居中测试</title>
 6 <style>
 7 #father {
 8   width: 500px;
 9   height: 300px;
10   background-color: skyblue;
11   position: relative;
12 }
13 #son {
14   background-color: green;
15   position: absolute;
16   top: 50%;
17   transform: translateY(-50%);
18 }
19 </style>
20 </head>
21
22 <body>
23 <div id="father">
24   <div id="son">我将居中</div>
25 </div>
26 </body>
27 </html>

方案二 -- flexbox布局

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>居中测试</title>
 6 <style>
 7 #father {
 8   width: 500px;
 9   height: 300px;
10   background-color: skyblue;
11   display: flex;
12   align-items: center;
13 }
14 #son {
15   background-color: green;
16 }
17 </style>
18 </head>
19
20 <body>
21 <div id="father">
22   <div id="son">我将居中</div>
23 </div>
24 </body>
25 </html>

3、同时水平和垂直居中

(1)父级元素和子级元素都已知高度和宽度

① flexbox布局推荐

② 定位属性

(2)父级元素已知高度和宽度,子级元素没有高度和宽度

① flexbox布局

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>居中测试</title>
 6 <style>
 7 #father {
 8   width: 500px;
 9   height: 300px;
10   background-color: skyblue;
11   display: flex;
12   justify-content: center;
13   align-items: center;
14 }
15 #son {
16   background-color: green;
17 }
18 </style>
19 </head>
20
21 <body>
22 <div id="father">
23   <div id="son">我将居中</div>
24 </div>
25 </body>
26 </html>

② 定位属性

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>居中测试</title>
 6 <style>
 7 #father {
 8   width: 500px;
 9   height: 300px;
10   background-color: skyblue;
11   position: relative;
12 }
13 #son {
14   background-color: green;
15   position: absolute;
16   left:50%;
17   top:50%;
18   transform:translateX(-50%) translateY(-50%);
19 }
20 </style>
21 </head>
22
23 <body>
24 <div id="father">
25   <div id="son">我将居中</div>
26 </div>
27 </body>
28 </html>

原文地址:https://www.cnblogs.com/yadiel-cc/p/11844386.html

时间: 2024-08-01 06:30:46

【CSS】水平居中和垂直居中的相关文章

CSS 水平居中和垂直居中

转自:http://www.cnblogs.com/fu277/archive/2012/09/13/2400118.html 1.水平居中 (1) 文本.图片等行内元素的水平居中 给父元素设置text-align:center可以实现文本.图片等行内元素的水平居中. (2) 确定宽度的块级元素的水平居中 通过设置margin-left:auto;和margin-right:auto;来实现的. (3) 不确定宽度的块级元素的水平居中 方法一: 使用table标签,table本身并不是块级元素,

CSS水平居中和垂直居中解决方案

一.CSS 居中 — 水平居中 DIV等标签本身没有定义自己居中的属性,网上很多的方法都是介绍用上级的text-align: center,然后嵌套一层DIV来解决问题.可是这个方法有时候完全不起作用,而且对于布局是非常不科学的方法.正确的的设置写法如下(对页面构造没有影响):div {margin-left: auto; margin-right: auto; }这句CSS居中的意思就是让div自己调整左右margin间隔的距离以达到水平居中的效果.有时候我们还可以简写为 div { marg

css水平居中和垂直居中

水平居中:内联元素:text-align:center;相对于父级居中显示块级元素:margin:0 auto;但是需要同时width,否则无法看到效果多个块级元素居中:在此想要探讨一下display:inline-block;和display:flex;方法1:在父级上设置text-align:center;在元素上设置:display:inline-block;同时为了使文字向左显示,设置了text-align:left;在浏览器中的显示为,子元素高度自适应,底部对齐.方法2:在子元素上设置

css水平居中与垂直居中

一.水平居中 1.inline元素用tet-align:center; 2.block元素用margin:auto: 二.垂直居中 1.flex弹性盒子 父元素定义 display:flex;align-item:center; 2.absolute绝对定位 i.position:absolute;top:50%;left:50%;width:x;height:y;margin-left:-x/2;margin-top:-y/2; 缺点是必须指定元素的宽高,否则无法给定margin,显得不够灵活

html+css水平居中和垂直居中

水平居中 1.定宽.左右:margin:auto 2.弹性盒设置 justify-content:center 3.父元素设置 text-align:center 4相对定位元素 left:0:right:0:定宽:左右margin:auto 5.相对定位元素 margin-left:50%:transform:translateX(-50%)[margin,padding相对于包含块宽度的百分比] 垂直居中 1.line-height[单行文本垂直居中] 2.弹性盒设置 align-items

css水平居中和垂直居中的方法

html <div class="father"> <div class="child"></div></div> 法一:已知道两个盒子的宽高的情况,可以用margin来居中盒子: .father{ height: 200px; width: 200px; border: 1px solid red; } .child{ width: 100px; height: 100px; margin-top: 50px; ma

css图片居中(水平居中和垂直居中)

http://www.51xuediannao.com/html+css/htmlcssjq/css_img_center.html css图片居中分css图片水平居中和垂直居中两种情况,有时候还需要图片同时水平垂直居中,下面分几种居中情况分别介绍 css图片居中分css图片水平居中和垂直居中两种情况,有时候还需要图片同时水平垂直居中,下面分几种居中情况分别介绍. css图片水平居中 利用margin: 0 auto实现图片水平居中 利用margin: 0 auto实现图片居中就是在图片上加上c

CSS设置行内元素和块级元素的水平居中、垂直居中

CSS设置行内元素的水平居中 div{text-align:center} /*DIV内的行内元素均会水平居中*/ CSS设置行内元素的垂直居中 div{height:30px; line-height:30px} /*DIV内的行内元素均会垂直居中*/ PS:当然,如果既要水平居中又要垂直居中,那么综合一下 div{text-align:center; height:30px; line-height:30px} CSS设置块级元素的水平居中 div p{margin:0 auto; widt

CSS中元素水平居中和垂直居中的方法

#CSS中水平居中和垂直居中的方法 一. 水平居中 1.行内元素(文本,图片等) 如果被设置元素为文本.图片等行内元素时,可以通过给父元素设置` text-align:center;` 来实现 2.定宽块级元素 可以通过设置“左右margin”值为“auto”来实现居中 常用的有 `margin:0 auto; ` 也可以写成`margin-left:auto;margin-right:auto;` 3.不定宽块级元素 有三种实现方法: * 加入 table 标签   为需要设置的居中的元素外面

css确定元素水平居中和垂直居中

---恢复内容开始--- 首先,我们在了解如何通过css了解元素水平和垂直居中之前,先要了解下html都有哪些元素,这些元素与偶有哪些分类,因为不同类别的元素的水平垂直居中方法是完全不同的,究其根本当然是因为不同类别元素的性质不同.所以我们先来了解下html的元素类别. 一.HTML元素分类 1)内联(inline)元素: <a>--锚点 <abbr>--缩写 <acronym>--首字母缩写(HTML5不支持,请使用<abbr>代替) <b>-