【原创】码路工人 Coder-Power

大家好,这里是码路工人有力量,我是码路工人,你们是力量。

github-pages

博客园cnblogs


做Web开发少不了做页面布局。码路工人给大家总结一下水平居中,垂直居中,水平垂直居中的布局实现。


1.水平居中

通过以下四种方式,将实现下图中的效果

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlUAAAJICAIAAADHLIagAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAgAElEQVR42u3dCXTW9aHnYe7c05naTttpe9vbc9rTaXu7nJl7jp0uR+pSoWprtdVa53pvO/bWIiJYUaGKFi3UYls3KptLQaCoUBWUHdkXBdlkCxKQLRtkIQsBsm9kfuGvL5EAed8s5E3yfM9ze5I3ueHXlPf95H3zJvT4zEO7AKC76RH+Ly0zBwC6D/0DQP8AQP8AQP8AQP8AQP8AQP8AoAv1Lz0zd/+JF/ZnZrfrmaKP3/BnZeU6SVKdxDE64zFy8gqPlpRVVFbV1tXVt9vCBw9/RPiDsvMK3bDSpfrX3rfvZ5ThJMl6EsfoDMcoPlpSf85XfKTEbWvX0OOsO5cn+eznPjf/tSVNLw8Xhjd13cc/M5wkWU/iGMl9jMqq6voOWmVltXh0jf614E3tIercKQk87YVt27/sDv/f4L0vpZ0k6U7iGEl7jOIjx+o7dIePHGvFf4V4L6Sb9K9p7VoZv+b7l540/zOkn+F7G07iGI7RVHZeYX0S7GBugYToX3sksPXxa75/+zOyk+R/BidJ2pM4RhIe41hJWTL072hJmYToX3sksPXx0z8ncYyueYyKyqpk6F9FRVU7fvc0oytXJ0meddK9+5c0fyHmLljiJMl5EsdIwmPU1NYmQ/9qa+vchXL/r7M+/pk8HntirJMk50kcIwmPUZ80a/e7cRkq5fkv+uckjuEYnbF/6F8S//xDM1J27nnwj4/0u+3OX91yWxBeCK+GC8/9zYqTdMhJHCMJj6F/dLHvRCbjz7+/OH1W31tvv6nvgD79bht41z13/ua+cH0Or4YLw5vO5c2Kk3TUSRwjCY/RZfp3y4Dbw+dJh2gPrepfuLqGq26ffr+eM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0zMzPTPzMzM/0zMzPTPzMzM/0zMzPTPzMzM/0zMzPTPzMzM/0zMzPTPzMzM/0zMzPTPzMz0z8zMzP9MzMz0z8zMzP9MzMz0z8zMzP9MzMz0z8zMzP9MzMz0z8zMzP9MzMz0z8zMzP9MzMz0z8zMzP9MzMz0z8zMzP9MzMz0z8zMzP9MzMz/dM/MzPTPzMzM/0zMzPTPzMzM/0zMzPTPzMzM/0zMzPTPzMzM/0zMzPTPzMzM/0zMzPTPzMzM/0zMzPTPzMzM/0zMzPTPzMzM/0zMzPTPzMzM/0zMzP9MzMz0z8zMzP9MzMz0z8zMzP9MzMz0z8zMzP9MzMz0z8zMzP9MzMz0z8zMzP9MzMz0z8zMzP9MzMz0z8zMzP9MzMz0z8zMzP9MzMz0z8zM9M/nwgzM9M/MzMz/TMzM9M/MzMz/TMzM9M/MzMz/TMzM9M/MzMz/TMzM9M/MzMz/TMzM9M/MzMz/TMzM9M/MzMz/TMzM9M/MzMz/TMzM9M/MzPTP/0zMzP9MzMz0z8zMzP9MzMz0z8zM7PO3z8A6G7+P6jc4FW9188hAAAAAElFTkSuQmCC" alt="Horizontal-Center">

1.1 利用父级 text-align: center; 加子级 display: inline-block;(只要是inline-的都可以)实现子元素水平居中

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>layout-h-center-1.html</title>
<style>
/* 为了不影响理解,对于布局非关键的样式单独放在这里 */
body {
background-color: lightgray;
}
.parent {
background-color: lightblue;
width: 500px;
height:400px;
}
.child {
background-color: lightyellow;
width: 300px;
height:200px;
} /* 布局关键样式在这里 */
.parent {
text-align: center;
}
.child {
display: inline-block;
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
</html>

1.2 利用弹性布局 flex,将父级设为 display: flex;justify-content: center; 实现子元素水平居中

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>layout-h-center-2.html</title>
<style>
/* 为了不影响理解,对于布局非关键的样式单独放在这里 */
body {
background-color: lightgray;
}
.parent {
background-color: lightblue;
width: 500px;
height:400px;
}
.child {
background-color: lightyellow;
width: 300px;
height:200px;
} /* 布局关键样式在这里 */
.parent {
display: flex;
justify-content: center;
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
</html>

1.3 利用相对布局加平移,将子级设为 position: relative;left: 50%;transform: translateX(-50%); 实现子元素水平居中

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>layout-h-center-3.html</title>
<style>
/* 为了不影响理解,对于布局非关键的样式单独放在这里 */
body {
background-color: lightgray;
}
.parent {
background-color: lightblue;
width: 500px;
height:400px;
}
.child {
background-color: lightyellow;
width: 300px;
height:200px;
} /* 布局关键样式在这里 */
.child {
position: relative;
left: 50%;
transform: translateX(-50%);
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
</html>

1.4 将子元素左右间距设为 auto 实现子元素水平居中

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>layout-h-center-4.html</title>
<style>
/* 为了不影响理解,对于布局非关键的样式单独放在这里 */
body {
background-color: lightgray;
}
.parent {
background-color: lightblue;
width: 500px;
height:400px;
}
.child {
background-color: lightyellow;
width: 300px;
height:200px;
} /* 布局关键样式在这里 */
.child {
margin: 0 auto;
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
</html>

2. 垂直居中

通过以下四种方式,将实现下图中的效果

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoEAAAJtCAIAAADGtz0WAAAACXBIW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AAADQYAABoMAIAGAwCgwRoMAIAGAwCgwQAAQIMBANBgAACgwQAAaDAAANBgAAA0GAAAaDAAABoMAAA0GAAADQYAQIMBAIAGAwCgwQAAQIMBANBgAACgwQAAaDAAANBgAAA0GAAAaDAAABoMAIAGAwAADQYAQIMBAIAGAwCgwQAAQIMBANBgAACgwQAAaDAAANBgAAA0GAAADdZgAAA0GAAADQYAABoMAIAGAwAADQYAQIMBAIAGAwCgwQAAQIMBANBgAACgwQAAaDAAABoMAAA0GAAADQYAABoMAIAGAwAADQYAQIMBAIAGAwCgwQAAQIMBANBgAAA0GAAAaDAAABoMAAA0GAAADQYAAKlqMEmSzLz/H7K58TtOdbDtAAAAAElFTkSuQmCC" alt="Vertical-Center">

2.1 利用 table-cell ,将父级设为 display: table-cell;vertical-align: middle; 实现子元素垂直居中

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>layout-v-center-1.html</title>
<style>
/* 为了不影响理解,对于布局非关键的样式单独放在这里 */
body {
background-color: lightgray;
}
.parent {
background-color: lightblue;
width: 500px;
height:400px;
}
.child {
background-color: lightyellow;
width: 300px;
height:200px;
} /* 布局关键样式在这里 */
.parent {
display: table-cell;
vertical-align: middle;
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
</html>

2.2 利用弹性布局 flex,将父级设为 display: flex;align-items: center; 实现子元素垂直居中

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>layout-v-center-2.html</title>
<style>
/* 为了不影响理解,对于布局非关键的样式单独放在这里 */
body {
background-color: lightgray;
}
.parent {
background-color: lightblue;
width: 500px;
height:400px;
}
.child {
background-color: lightyellow;
width: 300px;
height:200px;
} /* 布局关键样式在这里 */
.parent {
display: flex;
align-items: center;
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
</html>

2.3 利用相对布局加平移,将子级设为 position: relative;top: 50%;transform: translateY(-50%); 实现子元素垂直居中

子元素的左上角顶点固定到父元素的垂直中点,

然后再以自身的一半向上平移,实现垂直居中。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>layout-v-center-3.html</title>
<style>
/* 为了不影响理解,对于布局非关键的样式单独放在这里 */
body {
background-color: lightgray;
}
.parent {
background-color: lightblue;
width: 500px;
height:400px;
}
.child {
background-color: lightyellow;
width: 300px;
height:200px;
} /* 布局关键样式在这里 */
.child {
position: relative;
top: 50%;
transform: translateY(-50%);
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
</html>

2.4 通过绝对布局加 margin ,将父级设为 position: relative; ,加子元素设上下左右均为 0 并且 margin: auto 0; 实现子元素垂直居中

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>layout-v-center-4.html</title>
<style>
/* 为了不影响理解,对于布局非关键的样式单独放在这里 */
body {
background-color: lightgray;
}
.parent {
background-color: lightblue;
width: 500px;
height:400px;
}
.child {
background-color: lightyellow;
width: 300px;
height:200px;
} /* 布局关键样式在这里 */
.parent {
position: relative;
}
.child {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto 0;
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
</html>

3. 水平垂直居中

通过以下四种方式,将实现下图中的效果

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoEAAAJtCAIAAADGtz0WAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAgAElEQVR42u3dCZRU9Z3ocebN8b2YvCQTM8nkHI1JjCbnneTo0yQS1AhRE6NGn8m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02F8EAAAaDACABgMAAA0GAECDAQCABgMAoMEAAECDAQDQYAAAoMEAAGgwAADQYAAANBgAAA0GAAAaDACABgMAAA0GAECDAQCABgMAoMEAAECDAQDQYAAAoMEAAGgwAAAaDAAANBgAAA0GAAAaDACABgMAAA0GAECDAQCABgMAoMEAAECDAQDQYAAANFiDAQDQYAAANBgAAGgwAAAaDAAANBgAAA0GAAAaDACABgMAAA0GAECDAQCABgMAoMEAAGgwAADQYAAANBgAAGgwAAAaDAAANBgAAA0GAAAaDACABgMAAA0GAECDAQDQYAAAoMEAAGgwAADQYAAANBgAAGgwAAAaDAAANBgAAA0GAAAaDACABgMAoMEaDACABgMAoMEAAECDAQDQYAAAoMEAAGgwAADQYAAANBgAAGgwAAAaDAAANBgAAA0GAECDAQCABgMAoMEAAECDAQDQYAAAoMEAAGgwAADQYAAANBgAAGgwAAAaDACABgMAAA0GAECDAQCABgMAoMEAACBVDSZJkpn3/wPDgcH/4CA+cgAAAABJRU5ErkJggg==" alt="Both-Center">

上面分别列举了水平居中与垂直居中的4种实现方式,那么要实现水平+垂直的居中,就很容易了,把上面的组合起来就可以。为了统一好看,我们还是列举4个代码样例片段吧。

3.1 与前文的第一种对应

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>layout-h-center-1.html</title>
<style>
/* 为了不影响理解,对于布局非关键的样式单独放在这里 */
body {
background-color: lightgray;
}
.parent {
background-color: lightblue;
width: 500px;
height:400px;
}
.child {
background-color: lightyellow;
width: 300px;
height:200px;
} /* 布局关键样式在这里 */
.parent {
text-align: center; display: table-cell;
vertical-align: middle;
}
.child {
display: inline-block;
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
</html>

3.2 与前文的第二种对应

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>layout-h-center-1.html</title>
<style>
/* 为了不影响理解,对于布局非关键的样式单独放在这里 */
body {
background-color: lightgray;
}
.parent {
background-color: lightblue;
width: 500px;
height:400px;
}
.child {
background-color: lightyellow;
width: 300px;
height:200px;
} /* 布局关键样式在这里 */
.parent {
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
</html>

3.3 与前文的第三种对应

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>layout-h-center-1.html</title>
<style>
/* 为了不影响理解,对于布局非关键的样式单独放在这里 */
body {
background-color: lightgray;
}
.parent {
background-color: lightblue;
width: 500px;
height:400px;
}
.child {
background-color: lightyellow;
width: 300px;
height:200px;
} /* 布局关键样式在这里 */
.child {
position: relative;
left: 50%;
top: 50%;
transform: translateX(-50%) translateY(-50%);
/* translateX 与 translateY 要写在一个 transform 里 */
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
</html>

3.4 与前文的第四种对应

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>layout-h-center-1.html</title>
<style>
/* 为了不影响理解,对于布局非关键的样式单独放在这里 */
body {
background-color: lightgray;
}
.parent {
background-color: lightblue;
width: 500px;
height:400px;
}
.child {
background-color: lightyellow;
width: 300px;
height:200px;
} /* 布局关键样式在这里 */
.parent {
position: relative;
}
.child {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
</html>

列举了这么多,大家都掌握了吗?

其实也不必全部掌握,上面的布局实现,以 flex 实现方式作为重点。

这里主要总结几种方式,没有过多纠结其中细节。

码路工人认为,将来的话 flexgrid 布局方式会成为主流。这两个不是哪个好的问题,而是相互补充,flex 简单说就是一行一列(实际上里面也有很多),grid 的话就理解为类似 Bootstrap 的栅格吧,多行多列。

建议布局方面的话,学习 flexgrid 。码路工人作为转前端的新手,CSS 知识浅薄,就暂时不献丑了。以后,也可以做个 CSS3基础 的学习系列。不过暂时呢,还是要把我们的 ES6基础系列 写完。(感觉有太多事情要做有木有?。。)

以上。


希望对你能有帮助,下期再见。

欢迎关注分享,一起学习提高吧。

QRCode/微信订阅号二维码


[CSS布局基础]居中布局的实现方式总结的更多相关文章

  1. 【前端】使用CSS使元素居中的几种方式

    Precondition: <div class="parent"> <div class="item">居中</div> ...

  2. 通过纯css实现图片居中的多种实现方式

    html结构: <div class="demo" style="width: 800px;height: 600px; border:1px solid #ddd ...

  3. 前端总结·基础篇·CSS(一)布局

    目录 这是<前端总结·基础篇·CSS>系列的第一篇,主要总结一下布局的基础知识. 一.显示(display) 1.1 盒模型(box-model) 1.2 行内元素(inline) &am ...

  4. css布局基础总结

    前端css布局知识繁杂,实现方式多种多样.想写出高效.合理的布局,必须以深厚的css基础为前提.为了方便记忆和复习,将css布局要点记录如下.内容较多,应用方面说的不太详细,但都是很实用的点. 所谓布 ...

  5. css布局---各种居中

    居中是我们使用css来布局时常遇到的情况.使用css来进行居中时,有时一个属性就能搞定,有时则需要一定的技巧才能兼容到所有浏览器,本文就居中的一些常用方法做个简单的介绍. 注:本文所讲方法除了特别说明 ...

  6. CSS基础和布局复习

    table布局 div布局优势   浏览器支持完善   表现和结构分离   样式设计控制功能强大   可以继承,层叠处理 Transitional // 松散过度型 Strict //严格型 Fram ...

  7. CSS布局和居中常用技巧

    1.常用居中方法 居中在布局中很常见,我们假设DOM文档结构如下,子元素要在父元素中居中: <div class="parent"> <div class=&qu ...

  8. css布局之居中

    CSS布局之居中 本文主要是介绍水平居中,垂直居中,还有水平垂直居中的方法 水平居中 1.行内元素水平居中 使用text-align:center;就可以实现行内元素的水平居中,但是记得要在父元素中设 ...

  9. 【网页布局基础】css布局学习总结

    三种定位机制 1.标准文档流:从上到下,从左到右输出文档内容 2.浮动 3.绝对定位 需要注意的是块级元素如div p ul 等 行级元素如span strong img input等 块级元素与行级 ...

随机推荐

  1. PHP版DES算法加密数据(3DES)另附openssl_encrypt版本

    PHP版DES算法加密数据(3DES) 可与java的DES(DESede/CBC/PKCS5Padding)加密方式兼容 <?php /** * Created by PhpStorm. * ...

  2. Damaged Hard Drive and Reinstall System

    0 缘由 我是ACER笔记本,电脑从桌子上重摔,之后几天可以正常使用.可是后来看完视频准备退出的时候,发现所有页面已经卡死了,内存占用已经超过了80%,任务管理器没有反应,不得已按了电源键强制关机. ...

  3. JSP、ASP、PHP Web应用程序怎么这么多P!

    之前我们说完了计算机网络应用程序的两种结构:C/S,B/S(传送门)今天我们详细说一说B/S开发中的这么多P是干什么的. 1.什么是Web应用程序 一个Web应用程序是由完成特定任务的各种Web组件( ...

  4. CF思维联系– CodeForces -CodeForces - 992C Nastya and a Wardrobe(欧拉降幂+快速幂)

    Nastya received a gift on New Year - a magic wardrobe. It is magic because in the end of each month ...

  5. 从0开始搭建精灵宝可梦的检测APP

    从0开始搭建精灵宝可梦的检测APP 本文为本人原创,转载请注明来源链接 环境要求 Tensorflow1.12.0 cuda 9.0 python3.6.10 Android Studio Anaco ...

  6. P4370 [Code+#4]组合数问题2

    题目要求当\(0\leq a\leq b\leq n\)时,\(k\)个\(\tbinom{b}{a}\)的和的最大值 观察杨辉三角形,可以发现,最大的\(\tbinom{b}{a}\),为\(\tb ...

  7. SQL 文件导入数据库

    1.首先通过 xshell 连接数据库服务器,执行命令 mysql -u root -p 命令,按照提示输入密码,连接上数据库 2.在连接终端上执行命令 create database JD_Mode ...

  8. VUE生命周期中的钩子函数及父子组件的执行顺序

    先附一张官网上的vue实例的生命周期图,每个Vue实例在被创建的时候都需要经过一系列的初始化过程,例如需要设置数据监听,编译模板,将实例挂载到DOM并在数据变化时更新DOM等.同时在这个过程中也会运行 ...

  9. Docker安装各种服务

    一. centos7设置固定IP 查看当前正在使用的网络情况 [root@localhost ~]# nmcli dev status 显示情况 : DEVICE TYPE     STATE   C ...

  10. neo4j企业版集群搭建

    一.HA高可用集群搭建 版本采用的是neo4j-enterprise-3.5.3-unix.tar.gz 1.1.集群ip规划 192.168.56.10 neo4j-node1 192.168.56 ...