博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何使用CSS将绝对定位的元素居中
阅读量:2507 次
发布时间:2019-05-11

本文共 3258 字,大约阅读时间需要 10 分钟。

This article was written in 2010 and remains one of our most popular posts. If you’re keen to learn more about CSS techniques, you may find this of great interest.

本文写于2010年,至今仍是我们最受欢迎的帖子之一。 如果您想了解有关CSS技术的更多信息,您可能会发现这篇有关CSS 。

Centering an absolutely positioned element is a CSS challenge that occurs now and then. The solution seems obvious once I’ve done it, but I still find myself googling the problem every few months.

将绝对定位的元素居中是不时出现CSS挑战。 完成后,解决方案似乎很明显,但是我仍然发现自己每隔几个月就会搜索一次该问题。

Horizontally centering a static element in CSS is normally handled by setting the left and right margins to auto, for example:

通常通过将左右边距设置为auto来处理静态元素在CSS中的水平居中,例如:

.myelement {  margin: 0 auto;}

However, this won’t work on an absolutely positioned element. Its location is determined in relation to the most immediate parent element that has a position value of absolute, relative, or fixed.

但是,这不适用于绝对定位的元素。 相对于具有absoluterelativefixed position值的最直接父元素确定其位置。

In the following example, the relative red square has a width set to 40% of the available space. The top-left of the absolutely positioned blue square is positioned 30px across and 10px down:

在下面的示例中,相对的红色正方形的width设置为可用空间的40%。 绝对定位的蓝色方块的左上角跨度为30px,向下为10px:

.outer {  position: relative;  width: 40%;  height: 120px;  margin: 20px auto;  border: 2px solid #c00;}.inner {	  position: absolute;  width: 100px;  height: 100px;  top: 10px;  left: 30px;  background-color: #00c;}

If we’re not concerned about the exact dimensions of our blue box, we could omit the width setting and set the same left and right values. This would effectively center our blue box:

如果我们不关心我们的蓝色方块的确切尺寸,我们可以省略width设置,并设置相同的leftright的值。 这将有效地使我们的蓝框居中:

.outer {  position: relative;  width: 40%;  height: 120px;  margin: 20px auto;  border: 2px solid #c00;}.inner {  position: absolute;  height: 100px;  left: 30px;  top: 10px;  right: 30px;  background-color: #00c;}

So, how can we center our box if it has fixed dimensions? The answer requires a little lateral thinking:

那么,如果盒子有固定的尺寸,我们如何居中呢? 答案需要一些横向思考:

  1. First, we use left: 50%. Unlike background image positions, this will move the left-hand edge of the blue box to the center.

    首先,我们使用left: 50% 。 与背景图像位置不同,这会将蓝色框的左边缘移到中心。

  2. Since our box is too far to the right, we use a negative left margin that’s half the box’s width. In our example, we must set margin-left to -50px to shift the box back to the right place.

    由于我们的盒子距离右边太远,因此我们使用负的左边距作为盒子宽度的一半。 在我们的示例中,我们必须将margin-left设置为-50px以将框移回右侧。

Here is the code:

这是代码:

.outer {  position: relative;  width: 40%;  height: 120px;  margin: 20px auto;  border: 2px solid #c00;}.inner {  position: absolute;  width: 100px;  height: 100px;  top: 10px;  left: 50%;  margin-left: -50px;  background-color: #00c;}

The blue box will remain centered no matter how the width of the outer element changes.

无论外部元素的宽度如何变化,蓝色框都将居中。

If you enjoyed reading this post, you’ll love ; the place to learn fresh skills and techniques from experienced developers. Members get instant access to all of SitePoint’s ebooks and interactive online courses, like .

如果您喜欢阅读这篇文章,您将爱上 ; 向经验丰富的开发人员学习新鲜技能的地方。 成员可以立即访问所有SitePoint的电子书和交互式在线课程,例如 。

Comments on this article are closed. Have a question about CSS? Why not ask it on our ?

本文的评论已关闭。 对CSS有疑问吗? 为什么不在我们的上提问呢?

翻译自:

转载地址:http://uergb.baihongyu.com/

你可能感兴趣的文章
MongoDB基本语法和操作入门
查看>>
学习笔记_vnpy实战培训day04_作业
查看>>
OCO订单(委托)
查看>>
学习笔记_vnpy实战培训day06
查看>>
回测引擎代码分析流程图
查看>>
Excel 如何制作时间轴
查看>>
matplotlib绘图跳过时间段的处理方案
查看>>
vnpy学习_04回测评价指标的缺陷
查看>>
iOS开发中遇到的问题整理 (一)
查看>>
Linux(SUSE 12)安装jboss4并实现远程访问
查看>>
Neutron在给虚拟机分配网络时,底层是如何实现的?
查看>>
netfilter/iptables全攻略
查看>>
Overlay之VXLAN架构
查看>>
Eclipse : An error occurred while filtering resources(Maven错误提示)
查看>>
在eclipse上用tomcat部署项目404解决方案
查看>>
web.xml 配置中classpath: 与classpath*:的区别
查看>>
suse如何修改ssh端口为2222?
查看>>
详细理解“>/dev/null 2>&1”
查看>>
suse如何创建定时任务?
查看>>
suse搭建ftp服务器方法
查看>>