css3让盒子水平居中的方法:1、使用margin属性,给盒子元素添加“margin: 0 auto;”样式即可水平居中;2、利用flex弹性布局来实现水平居中;3、利用position和transform属性实现水平居中。
本教程操作环境:windows7系统、CSS3&&HTML5版、Dell G3电脑。
在CSS中如何让盒子水平居中是很常见的面试题,盒子居中是相对于父元素来说的,因此我们让盒子居中时,往往采用嵌套的方式,让父盒子套着子盒子 。
在父子盒子嵌套下,让子盒子居中的方式:
第一种方法:margin: 0 auto,使用边框,但是margin使用会影响其他盒子的使用,不太推荐使用;
第二种方法:position, 使用定位,子绝父相,再left:50%,margin-left:负的盒子宽度的一半,这是最常用的方法;
第三种方法:flex,弹性布局,让子盒子居中,但是样式要写在父盒子中,display:flex,just-content:center;
第四种方法:在position基础上,把margin-left换成CSS3中的transform:translate(-50px);
-
第五种方法:在position的基础上,只保留子绝父相,然后在子盒子中加上margin:auto、left:0、right:0;
补充:在第五种方法上,加上top:0,bottom:0,可以实现垂直和水平都居中
<div id="father"> <div id="son"></div> </div>
<style> #father{ width: 400px; height: 200px; border: 3px solid pink; } #son{ width: 100px; height: 100px; border: 2px solid red; } </style>
使用margin实现水平居中:
<style> #father{ width: 400px; height: 200px; border: 3px solid pink; margin: 30px auto; /* 让父元素相对于body居中 */ } #son{ width: 100px; height: 100px; border: 2px solid red; margin: 0 auto;/* 让子元素相对于father居中 */ } </style>
使用定位,子绝父相,再left:50%,margin-left:负的盒子宽度的一半:
<style> #father{ width: 400px; height: 200px; border: 3px solid pink; margin: 0 auto; position: relative; } #son{ width: 100px; height: 100px; border: 2px solid red; position: absolute; left: 50%; margin-left: -50px; } </style>
flex,弹性布局,让子盒子居中,但是样式要写在父盒子中:
<style> #father{ width: 400px; height: 200px; border: 3px solid pink; margin: 0 auto; display: flex; justify-content: center; } #son{ width: 100px; height: 100px; border: 2px solid red; } </style>
在position的基础上,只保留子绝父相,然后在子盒子中加上margin:auto、left:0、right:0:
<style> #father{ width: 400px; height: 200px; border: 3px solid pink; margin: 0 auto; position: relative; } #son{ width: 100px; height: 100px; border: 2px solid red; position: absolute; margin: auto; left: 0; right: 0; } </style>
以上几种方法都可以实现盒子的水平居中,如果大家有其它优(奇)秀(葩)方法,欢迎交流鸭!
第五种方法补充:再加上top:0,bottom:0可以实现水平和垂直都居中 :
<style> #father{ width: 400px; height: 200px; border: 3px solid pink; margin: 0 auto; position: relative; } #son{ width: 100px; height: 100px; border: 2px solid red; position: absolute; margin: auto; left: 0; right: 0; top: 0; bottom: 0; } </style>
(学习视频分享:css视频教程)
以上是css3如何让盒子水平居中的详细内容。更多信息请关注PHP中文网其他相关文章!

我最近找到了一种动态更新任何产品图像的颜色的解决方案。因此,只有一种产品之一,我们可以以不同的方式对其进行着色以显示

在本周的综述中,灯塔在第三方脚本上阐明了灯光,不安全的资源将在安全站点上被阻止,许多国家连接速度

有很多分析平台可帮助您跟踪网站上的访问者和使用数据。也许最著名的是Google Analytics(广泛使用)

文档负责人可能不是网站上最迷人的部分,但是其中所处的内容对于您的网站的成功也一样重要

当您看到一些称为super()的JavaScript时,在子类中,您会使用super()调用其父母的构造函数和超级。访问它的


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

Atom编辑器mac版下载
最流行的的开源编辑器