-
HTML+CSS,让div在屏幕中居中(水平居中+垂直居中)方法总结
2017-08-11 18:25:02当然也可以用下面的方法下面说两种在屏幕正中(水平居中+垂直居中)的方法 放上示范的html代码:<body> <div class="main">最近写网页经常需要将div在屏幕中居中显示,遂记录下几个常用的方法,都比较简单。
水平居中直接加上<center>
标签即可,或者设置margin:auto;
当然也可以用下面的方法下面说两种在屏幕正中(水平居中+垂直居中)的方法
放上示范的html代码:<body> <div class="main"> <h1>MAIN</h1> </div> </body>
- 方法一:
div使用绝对布局,设置
margin:auto;
并设置top、left、right、bottom的值相等即可,不一定要都是0。.main{ text-align: center; /*让div内部文字居中*/ background-color: #fff; border-radius: 20px; width: 300px; height: 350px; margin: auto; position: absolute; top: 0; left: 0; right: 0; bottom: 0; }
效果如图:
- 方法二:
仍然是绝对布局,让left和top都是50%,这在水平方向上让div的最左与屏幕的最左相距50%,垂直方向上一样,所以再用transform向左(上)平移它自己宽度(高度)的50%,也就达到居中效果了,效果图和上方相同。
.main{ text-align: center; background-color: #fff; border-radius: 20px; width: 300px; height: 350px; position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%); }
- 方法三:
对于水平居中,可以使用最简单的<center>
标签,不过已经过时了,用法如下:
<p><center>123</center></p>
这个
<center>
标签就是相对于<p>
标签里的文字,可以使其居中。由于center标签已经过时了,所以正规一点的话还是不建议使用的,可以使用如下的方式代替:
<p style="text-align:center;">123</p>
欢迎大家加入QQ群一起交流讨论,「吟游」程序人生——YinyouPoet -
实现div里的img图片水平垂直居中
2017-03-31 09:53:09body结构<body> <div> <...将display设置成table-cell,然后水平居中设置text-align为center,垂直居中设置vertical-align为middle。<style type="text/css"> *{body结构
<body> <div> <img src="1.jpg" alt="haha"> </div> </body>
方法一:
将display设置成table-cell,然后水平居中设置text-align为center,垂直居中设置vertical-align为middle。<style type="text/css"> *{margin: 0;padding: 0;} div{ width:150px; height: 100px; display: table-cell; vertical-align: middle; text-align: center; border:1px solid #000; } img { width: 50px; height: 50px; } </style>
结果如下图所示:
方法二:
通过position定位来实现。将div设置成相对定位relative,将img设置成绝对定位absolute,left:50%,top:50%,此时图片的左上角位于div的中心,要是图片的中心位于div的中心,就需要将图片向上移动图片高度的一半,并向左移动图片宽度的一半。<style type="text/css"> *{margin: 0;padding:0;} div{ width:150px; height: 100px; position: relative; border:1px solid #000; } img { width: 50px; height: 50px; position: absolute; top: 50%; left: 50%; margin-top: -25px; /* 高度的一半 */ margin-left: -25px; /* 宽度的一半 */ } </style>
结果如下图所示:
很久以前的文章了,看到浏览量这么高,我再补充几种实现方法
方法三:可以用在不清楚图片图片或元素的真实宽高情况下
还是通过position定位来实现。将div设置成相对定位relative,将img设置成绝对定位absolute,left:50%,top:50%,此时图片的左上角位于div的中心,要是图片的中心位于div的中心,就需要将图片向上移动图片高度的一半,并向左移动图片宽度的一半,如果不知道元素的宽高,可以用transform: translate(-50%,-50%);<style type="text/css"> *{margin: 0;padding:0;} div{ width:150px; height: 100px; position: relative; border:1px solid #000; } img { width: 50px; height: 50px; position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); } </style>
方法四:
<style type="text/css"> *{margin: 0;padding:0;} div{ width:150px; height: 100px; position: relative; border:1px solid #000; } img { width: 50px; height: 50px; position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; } </style>
方法五:弹性布局flex
<style type="text/css"> *{margin: 0;padding:0;} div{ width:150px; height: 100px; border:1px solid #000; display: flex; justify-content: center; align-items: center; } img { width: 50px; height: 50px; } </style>
效果都一样,希望能帮到大家!
读后有收获并有兴趣的可以微信打赏哈哈:
-
垂直居中
2017-07-25 02:04:51垂直居中方式 (1)父元素高度确定的内联元素垂直居中 通过给父元素设置line-height来实现,line-height值和父元素高度值相同<!DOCTYPE html> 垂直居中方式 div { width垂直居中方式
(1)父元素高度确定的内联元素垂直居中
通过给父元素设置line-height来实现,line-height值和父元素高度值相同<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>垂直居中方式</title> <style> div { width: 480px; height: 100px; line-height: 100px; border: 1px solid #000; } span { border: 1px solid #000;s } </style> </head> <body> <div> <span>内联元素</span> </div> </body> </html>
效果如下图所示
缺点:此方法不适用于块状元素,而且需要知道父元素的高度
(2)块状元素垂直居中
设置position属性后,设置top:为50%,margin-top为元素高度的一半<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>垂直居中方式</title> <style> div { width: 480px; height: 100px; border: 1px solid #000; } h3 { position: relative; top: 50%; left: 50%; width: 90px; height: 30px; margin-left: -45px; margin-top: -15px; border:1px solid #000; } span { border: 1px solid #000;s } </style> </head> <body> <div> <h3>块级元素</h3> <span>内联元素</span> </div> </body> </html>
效果如下图所示
缺点:元素定位后带来一定的副作用
(3)父元素高度不确定的内联元素、块状元素垂直居中
通过给父元素设置相同的padding-top与padding-bottom来实现垂直居中<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>垂直居中方式</title> <style> div { width: 480px; padding: 20px 0; border: 1px solid #000; } h3 { margin: 0; padding: 0; border:1px solid #000; } span { border: 1px solid #000;s } </style> </head> <body> <div> <h3>块级元素</h3> <span>内联元素</span> </div> </body> </html>
效果如下图所示
优点:不仅适用于内联元素,还适用于块状元素
(4)父元素高度确定的内联元素、块级元素的垂直居中
通过给父元素设置vertical-align为middle<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>垂直居中方式</title> <style> div { display: table-cell; width: 480px; height: 100px; vertical-align: middle; border: 1px solid #000; } h3 { margin: 0; padding: 0; border:1px solid #000; } span { border: 1px solid #000;s } </style> </head> <body> <div> <h3>块级元素</h3> <span>内联元素</span> </div> </body> </html>
效果如下图所示
(完)
-
CSS水平居中+垂直居中+水平/垂直居中的方法总结
2018-09-02 19:28:12垂直居中 单行的行内元素 多行的行内元素 块级元素 水平垂直居中 已知高度和宽度的元素 未知高度和宽度的元素 方案一:使用定位属性 方案二:使用flex布局实现 水平居中 行内元素 首先看它的父元....目录
水平居中
-
行内元素
首先看它的父元素是不是块级元素,如果是,则直接给父元素设置 text-align: center;
<style> #father { width: 500px; height: 300px; background-color: skyblue; text-align: center; } </style> <div id="father"> <span id="son">我是行内元素</span> </div>
如果不是,则先将其父元素设置为块级元素,再给父元素设置 text-align: center;
<style> #father { display: block; width: 500px; height: 300px; background-color: skyblue; text-align: center; } </style> <span id="father"> <span id="son">我是行内元素</span> </span>
效果:
-
块级元素
方案一:(分宽度定不定两种情况)
定宽度:需要谁居中,给其设置 margin: 0 auto; (作用:使盒子自己居中)
<style> #father { width: 500px; height: 300px; background-color: skyblue; } #son { width: 100px; height: 100px; background-color: green; margin: 0 auto; } </style> <div id="father"> <div id="son">我是块级元素</div> </div>
效果:
不定宽度:默认子元素的宽度和父元素一样,这时需要设置子元素为display: inline-block; 或 display: inline;即将其转换成行内块级/行内元素,给父元素设置 text-align: center;
<style> #father { width: 500px; height: 300px; background-color: skyblue; text-align: center; } #son { background-color: green; display: inline; } </style> <div id="father"> <div id="son">我是块级元素</div> </div>
效果:(将#son转换成行内元素,内容的高度撑起了#son的高度,设置高度无用)
方案二:使用定位属性
首先设置父元素为相对定位,再设置子元素为绝对定位,设置子元素的left:50%,即让子元素的左上角水平居中;
定宽度:设置绝对子元素的 margin-left: -元素宽度的一半px; 或者设置transform: translateX(-50%);
<style> #father { width: 500px; height: 300px; background-color: skyblue; position: relative; } #son { width: 100px; height: 100px; background-color: green; position: absolute; left: 50%; margin-left: -50px; } </style> <div id="father"> <div id="son">我是块级元素</div> </div>
不定宽度:利用css3新增属性transform: translateX(-50%);
<style> #father { width: 500px; height: 300px; background-color: skyblue; position: relative; } #son { height: 100px; background-color: green; position: absolute; left: 50%; transform: translateX(-50%); } </style> <div id="father"> <div id="son">我是块级元素</div> </div>
效果:
方案三:使用flexbox布局实现(宽度定不定都可以)
使用flexbox布局,只需要给待处理的块状元素的父元素添加属性 display: flex; justify-content: center;
<style> #father { width: 500px; height: 300px; background-color: skyblue; display: flex; justify-content: center; } #son { width: 100px; height: 100px; background-color: green; } </style> <div id="father"> <div id="son">我是块级元素</div> </div>
效果:
垂直居中
-
单行的行内元素
只需要设置单行行内元素的"行高等于盒子的高"即可;
<style> #father { width: 500px; height: 300px; background-color: skyblue; } #son { background-color: green; height: 300px; } </style> <div id="father"> <span id="son">我是单行的行内元素</span> </div>
效果:
-
多行的行内元素
使用给父元素设置display:table-cell;和vertical-align: middle;属即可;
<style> #father { width: 500px; height: 300px; background-color: skyblue; display: table-cell; vertical-align:middle; } #son { background-color: green; } </style> <div id="father"> <span id="son">我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素</span> </div>
效果:
-
块级元素
方案一:使用定位
首先设置父元素为相对定位,再设置子元素为绝对定位,设置子元素的top: 50%,即让子元素的左上角垂直居中;
定高度:设置绝对子元素的 margin-top: -元素高度的一半px; 或者设置transform: translateY(-50%);
<style> #father { width: 500px; height: 300px; background-color: skyblue; position: relative; } #son { height: 100px; background-color: green; position: absolute; top: 50%; margin-top: -50px; } </style> <div id="father"> <div id="son">我是块级元素</div> </div>
不定高度:利用css3新增属性transform: translateY(-50%);
<style> #father { width: 500px; height: 300px; background-color: skyblue; position: relative; } #son { width: 100px; background-color: green; position: absolute; left: 50%; transform: translateY(-50%); } </style> <div id="father"> <div id="son">我是块级元素</div> </div>
效果:
方案二:使用flexbox布局实现(高度定不定都可以)
使用flexbox布局,只需要给待处理的块状元素的父元素添加属性 display: flex; align-items: center;
<style> #father { width: 500px; height: 300px; background-color: skyblue; display: flex; align-items: center; } #son { width: 100px; height: 100px; background-color: green; } </style> <div id="father"> <div id="son">我是块级元素</div> </div>
效果:
水平垂直居中
-
已知高度和宽度的元素
方案一:设置父元素为相对定位,给子元素设置绝对定位,top: 0; right: 0; bottom: 0; left: 0; margin: auto;
<style> #father { width: 500px; height: 300px; background-color: skyblue; position: relative; } #son { width: 100px; height: 100px; background-color: green; position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto; } </style> <div id="father"> <div id="son">我是块级元素</div> </div>
效果:
方案二:设置父元素为相对定位,给子元素设置绝对定位,left: 50%; top: 50%; margin-left: --元素宽度的一半px; margin-top: --元素高度的一半px;
<style> #father { width: 500px; height: 300px; background-color: skyblue; position: relative; } #son { width: 100px; height: 100px; background-color: green; position: absolute; left: 50%; top: 50%; margin-left: -50px; margin-top: -50px; } </style> <div id="father"> <div id="son">我是块级元素</div> </div>
效果:
-
未知高度和宽度的元素
方案一:使用定位属性
设置父元素为相对定位,给子元素设置绝对定位,left: 50%; top: 50%; transform: translateX(-50%) translateY(-50%);
<style> #father { width: 500px; height: 300px; background-color: skyblue; position: relative; } #son { background-color: green; position: absolute; left: 50%; top: 50%; transform: translateX(-50%) translateY(-50%); } </style> <div id="father"> <div id="son">我是块级元素</div> </div>
效果:
方案二:使用flex布局实现
设置父元素为flex定位,justify-content: center; align-items: center;
<style> #father { width: 500px; height: 300px; background-color: skyblue; display: flex; justify-content: center; align-items: center; } #son { background-color: green; } </style> <div id="father"> <div id="son">我是块级元素</div> </div>
效果:
-
-
div垂直居中-CSS元素垂直居中方法的探究
2018-10-29 22:17:50针对学员疑问“div垂直居中?”引出的“CSS元素垂直居中一系列方法的探究”的针对性课程,课程包含两方面:1、文本垂直居中的解决方案;2、块级元素垂直居中的解决方案。 -
垂直居中,水平居中,水平垂直居中
2020-06-13 18:35:08文章目录一、垂直居中二、水平居中三、水平垂直居中 参考 一、垂直居中 二、水平居中 三、水平垂直居中 -
浅谈CSS居中问题(水平居中、垂直居中、水平垂直居中)
2020-12-10 14:40:56文章目录水平居中块元素水平居中行内元素水平居中垂直居中块元素垂直居中行内元素垂直居中img垂直居中input(radio、checkbox)垂直居中多行文本垂直居中水平垂直居中固定宽高不定宽高 水平居中 块元素水平居中 行内... -
h5文字垂直居中_CSS中垂直居中和水平垂直居中的方法
2020-11-21 03:55:16flex垂直居中:第一种:使用flex布局,让居中元素的父元素为flex属性,让它在交叉轴上center就可以达到居中效果了: ...我要垂直居中</p> </div> css代码: .father { display:flex; align-items: ... -
多行,单行文字垂直居中,图片垂直居中,图文垂直居中
2017-05-10 11:50:431.单行文字垂直居中,图片垂直居中: (1)用简单的一行代码即可实现单行文字垂直居中:设置 line-height 的值 等于高度的值。代码如下: .div { height: 500px; line-height :500px; } (2)图片垂直居中:... -
div中的a标签垂直居中_CSS中垂直居中和水平垂直居中的方法
2020-12-27 22:56:24flex垂直居中:第一种:使用flex布局,让居中元素的父元素为flex属性,让它在交叉轴上center就可以达到居中效果了: ...我要垂直居中</p> </div> css代码: .father { display:flex; align-items: ... -
CSS水平居中,垂直居中,水平垂直居中
2019-04-24 17:06:53本文主要介绍水平居中,垂直居中,还有水平垂直居中各种办法,思维导图如下: 水平居中 1.行内元素水平居中 利用 text-align: center 可以实现在块级元素内部的行内元素水平居中。此方法对inline、... -
居中布局:水平居中 + 垂直居中 + 水平垂直居中
2017-08-03 19:49:07布局:5种水平居中 + 3种垂直居中 + 3种水平垂直居中 -
水平居中/垂直居中/水平垂直居中总结
2020-08-27 23:44:25水平居中/垂直居中/水平垂直居中总结 1.1 内联元素水平居中 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>内联元素水平居中</title> <style> div {... -
div水平垂直居中 div相对于父元素水平垂直居中 div相对于网页水平垂直居中
2020-09-03 09:57:11div相对于父元素水平垂直居中(相对网页水平垂直居中在下面) 弹性布局 父元素作为容器设置宽高、弹性布局、水平轴和交叉轴居中 display:flex; justify-content:center; align-items:center; 使用CSS3 transform ... -
CSS布局对齐方式--水平居中、垂直居中、水平垂直居中
2018-07-04 11:09:39前言:在网页布局中,经常遇到需要使元素居中对齐的时候,居中对齐的方式有:水平居中、垂直居中和水平垂直居中。这次,借此回顾总结一下,并在此记录下相关内容。 一、水平居中: (1)行内元素的水平居中 ... -
css垂直居中怎么设?文字上下居中和图片垂直居中
2020-06-17 11:22:02css垂直居中怎么设?文字上下居中和图片垂直居中 css 居中分css垂直居中和css水平居中,水平居中平时比较常用,这里我们主要讲css上下居中的问题。垂直居中又分为css文字上下居中和css图片垂直居中,下面我们就分别来... -
简单DIV垂直居中在可视区域、DIV垂直居中在可视区域、DIV垂直居中、导航菜单以下可视区域垂直居中
2018-09-13 10:46:04简单了解一下: <!DOCTYPE html> <html lang="...DIV垂直居中在可视区域/有固定定位的导航菜单以下可视区域垂直居中</title> <script type=&quo -
div内图片和文字水平垂直居中
2017-11-29 17:32:36大小不固定的图片、多行文字的水平垂直居中 本文综述 想必写css的都知道如何让单行文字在高度固定的容器内垂直居中,但是您知道或者想过让行数不固定的文字在高度固定的容器内垂直居中呢?本文将会告诉你如何实现... -
Android进阶(二十二)设置TextView文字水平垂直居中
2016-06-30 17:54:52设置TextView文字水平垂直居中 有2种方法可以设置TextView文字居中: 一:在xml文件设置:android:gravity="center" 二:在程序中设置:m_TxtTitle.setGravity(Gravity.CENTER); 备注:android:gravity和android:... -
让div在屏幕中居中(水平居中+垂直居中)的几种方法
2019-05-21 10:07:26【让div在屏幕中居中(水平居中+垂直居中)的几种方法】 水平居中方法: 1.inline,inline-block元素的水平居中,在父级块级元素中设置text-align:center; 2.确定宽度的块级元素水平居中方法 margin:0 auto... -
div垂直居中 css div盒子上下垂直居中
2017-01-21 16:46:28div垂直居中 css div盒子上下垂直居中,让DIV盒子在任何浏览器中任何分辨率的显示屏浏览器中处于水平居中和上下垂直居中。 div垂直居中常用于单个盒子,如一个页面里只有一个登录布局,使用div css让这个登录布局... -
CSS实现盒子模型水平居中、垂直居中、水平垂直居中的多种方法
2018-07-25 22:26:30CSS实现盒子模型水平居中、垂直居中、水平垂直居中的多种方法 CSS实现盒子模型水平居中的方法 水平居中效果图 水平居中全局样式 .parent { color: #FFFFFF; height: 200px; width: 200px; margin: 0 auto; ... -
DIV 垂直居中
2011-11-14 21:41:36DIV 垂直居中DIV 垂直居中DIV 垂直居中DIV 垂直居中DIV 垂直居中DIV 垂直居中 -
CSS——水平居中、垂直居中、水平垂直居中
2018-09-20 13:16:44这里父元素和子元素的宽高都是不确定的,想实现子元素在父元素中的水平居中、垂直居中、水平垂直居中。下面列出了部分解决方法,如果大家有好的方法,欢迎留言~ 一、水平居中 html代码如下: <div class=... -
css图片垂直居中 让html img图片垂直居中的三种方法
2018-12-03 14:08:51本教程为thinkcss教大家三种让img元素图片在盒子内垂直居中的方法教程,根据代码与文章教程理解掌握并加以使用。 一、使用flex实现垂直居中 利用css flex实现垂直居中。flex可能不是实现垂直居中最好的选择,因为... -
CSS3/CSS之居中解析(水平+垂直居中、水平居中,垂直居中)
2018-06-14 15:24:06居中总体来说可以分为水平居中还有垂直居中,顾名思义,定义这里就不解释了!首先,我们来看下垂直居中:(1)、如果是单行文本,则可以设置的line-height的数值,让其等于父级元素的高度!<!DOCTYPE ... -
bootstrap 页面垂直居中_bootstrap div垂直居中+水平居中保持响应式
2020-12-19 19:58:20引入bootstrap4 css文件,只在bootstrap4有效,bs3效果不太行:垂直居中:为需要垂直居中的div新建如下样式.col-center-block {position: absolute;top: 50%;-webkit-transform: translateY(-50%);-moz-transform: ...
-
FFmpeg4.3黄金系列课程:c++版
-
如何多平台批量压缩或者加压文件
-
【数据分析-随到随学】数据分析基础及方法论
-
对公账户编码规则
-
三维地图GIS大数据可视化
-
计算机硬件系统设计.rar
-
页面自动化--浏览器的常见操作方法
-
【数据分析-随到随学】机器学习模型及应用
-
Hutool - Java 8 日期时间相关代码
-
css弹幕发射界面可随机发送,弹幕样式大小颜色随机
-
ubuntu gufw防火墙阻塞已建立的连接
-
WH80QD调质型高强钢板产地舞钢执行舞钢推荐标准调质热处理.doc
-
学习日志
-
C++ 基础知识总结_终稿_124.pdf
-
HTTP响应码.xlsx
-
【数据分析-随到随学】Spark理论及实战
-
ntsys.zip
-
lsof-master
-
python办公自动化技巧
-
数据结构李春葆版期末试卷,复习资料,ppt