中心对齐固定位置 div
我正在尝试获取一个在页面上居中对齐 position:fixed
的 div。
我一直能够使用这个“hack”来使用绝对定位的 div 来做到这一点
left: 50%;
width: 400px;
margin-left: -200px;
......其中 margin-left 的值是 div 宽度的一半。
这似乎不适用于固定位置的 div,相反,它只是将它们的最左边的角放置在 50% 处,并忽略 margin-left 声明。
有什么想法可以解决这个问题,以便我可以居中对齐固定定位的元素吗?
如果你能告诉我一种比我上面概述的方式更好的居中对齐绝对定位元素的方法,我会加一个额外的 M&M。
I'm trying to get a div that has position:fixed
center aligned on my page.
I've always been able to do it with absolutely positioned divs using this "hack"
left: 50%;
width: 400px;
margin-left: -200px;
...where the value for margin-left is half the width of the div.
This doesn't seem to work for fixed position divs, instead it just places them with their left-most corner at 50% and ignores the margin-left declaration.
Any ideas of how to fix this so I can center align fixed positioned elements?
And I'll throw in a bonus M&M if you can tell me a better way to center align absolutely positioned elements than the way I've outlined above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
Koen 的答案 并没有完全将元素居中。
正确的方法是使用 CSS3
transform
属性,尽管它在某些旧浏览器中不支持< /a>.我们甚至不需要设置固定或相对宽度。Koen's answer doesn't exactly center the element.
The proper way is to use CSS3
transform
property, although it's not supported in some old browsers. We don't even need to set a fixed or relativewidth
.对于那些遇到同样问题但采用响应式设计的人,您还可以使用:
这样做将使您的固定
div
始终保持在屏幕中央,即使采用响应式设计也是如此。For the ones having this same problem, but with a responsive design, you can also use:
Doing this will always keep your fixed
div
centered on the screen, even with a responsive design.您也可以使用 Flexbox 来实现此目的。
You could use flexbox for this as well.
从上面的帖子来看,我认为最好的方法是
margin-right: auto
,或者对于表格,将其设为align="center"
。希望这会有所帮助。
From the post above, I think the best way is
width: 100%
margin-left: auto
andmargin-right: auto
, or for table make italign="center"
.Hope this will help.
水平居中:
水平和垂直居中(如果高度与宽度相同):
无副作用:在 Flexbox 中使用边距时不会限制元素的宽度
Center it horizontally:
Center it horizontally and vertically (if its height is same as width):
No side effect: It will not limit element's width when using margins in flexbox
这应该起到同样的作用。
This should do the same.
普通 div 应该使用
margin-left: auto
和margin-right: auto
,但这不适用于固定 div。解决此问题的方法类似于 Andrew 的答案,但不使用已弃用的东西。基本上,只需给固定 div 一个包装器即可。
这会将固定 div 置于 div 中居中,同时允许 div 与浏览器做出反应。即如果有足够的空间,div 将居中,但如果没有足够的空间,则会与浏览器边缘发生碰撞;类似于常规居中 div 的反应。
Normal divs should use
margin-left: auto
andmargin-right: auto
, but that doesn't work for fixed divs. The way around this is similar to Andrew's answer, but doesn't use the deprecated<center>
thing. Basically, just give the fixed div a wrapper.This will center a fixed div within a div while allowing the div to react with the browser. i.e. The div will be centered if there's enough space, but will collide with the edge of the browser if there isn't; similar to how a regular centered div reacts.
如果您想在垂直和水平方向上居中对齐固定位置 div,请使用此
If you want to center aligning a fixed position div both vertically and horizontally use this
如果你知道宽度是 400px,我想这将是最简单的方法。
If you know the width is 400px this would be the easiest way to do it I guess.
您可以简单地执行以下操作:
You can simply do the following:
常规做法是
.center {position:fixed;左:50%;顶部:50%;变换:翻译(-50%,-50%); }
尝试在居中元素周围保留一些空白空间,这通常会导致居中元素的宽度不理想,例如小于应有的宽度。@proseosoc 的 答案 在这种情况下效果很好,并将居中元素的范围扩展到视口两侧的末端。然而,居中的元素会以整个视口(包括滚动条)为中心。但是,如果您的用例需要在没有滚动条的情况下将元素在空间内居中,则可以使用此修改后的答案。这种方法也类似于前面提到的传统方法,其将元素在空间中居中而没有滚动条。
水平居中
垂直居中
水平和垂直居中
The conventional approach of
.center { position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); }
tries to keep some empty space around the centered element which often causes the centered element's width to not be ideal like being smaller than what it should be.@proseosoc's answer works well in that scenario and extends the centered element's reach to the end of the viewport sides. The centered element, however, gets centered to the entire viewport including the scrollbar. But if your use case requires centering elements within space without the scrollbar, you can use this modified answer. This approach is also similar to the aforementioned conventional approach which centers elements in space without the scrollbar.
Center horizontally
Center vertically
Center horizontally and vertically
要将位置固定元素居中,您可以使用以下 CSS:
To center a position fixed element you can use the following CSS:
如果您希望元素像另一个导航栏一样跨越页面,则此方法有效。
width: calc (width: 100% - 任何偏离中心的宽度)
例如,如果你的侧面导航栏是 200px:
width: calc(100% - 200px);
This works if you want the element to span across the page like another navigation bar.
width: calc (width: 100% - width whatever else is off centering it)
For example if your side navigation bar is 200px:
width: calc(100% - 200px);
使用起来非常简单
宽度:70%;
左:15%;
将元素宽度设置为窗口的 70%,并在两侧保留 15%
It is quite easy using
width: 70%;
left:15%;
Sets the element width to 70% of the window and leaves 15% on both sides
我将以下内容与 Twitter Bootstrap (v3) 一起使用,
即制作一个固定位置的全宽度元素,然后将一个容器推入其中,该容器相对于全宽度居中。反应也应该表现得很好。
I used the following with Twitter Bootstrap (v3)
I.e make a full width element that is fixed position, and just shove a container in it, the container is centered relative to the full width. Should behave ok responsively too.
我总是更喜欢在 div 居中时使用
margin: auto
。对于
position:fixed
的元素,如果指定top:0, right:0,bottom:0, left:0
,则可以使用margin: auto
就可以了。I always prefer using
margin: auto
when centering divs.For an element that has
position: fixed
, if you specifytop:0, right:0, bottom:0, left:0
, you can usemargin:auto
and that works.使用flex box的解决方案;完全响应:
A solution using flex box; fully responsive:
我想分享具有居中 div、居中文本内容以及明确宽度和高度的答案。如果您不关心尺寸,您可以通过 特马尼·阿菲夫。
I want to share answer that have centered div, centered text content AND explicit width and height. If you don't care about sizing you may take a look to padding solution by Temani Afif.
如果您不想使用包装方法。那么你可以这样做:
if you don't want to use the wrapper method. then you can do this: