在浮动容器内居中对齐浮动 div
我有一个带有多个子浮动 div 的浮动容器。
我想将所有这些浮动子 div 相对于浮动容器居中对齐(不是文本对齐)。
我该怎么做?
一次,浏览器上只有 1 个子 div 可见。用户单击上一个/下一个来查看其他子 div(有点像轮播)
显然,如果我对子元素使用 float:left ,我将面临中心对齐问题分区
I have a floated container with multiple child floated divs.
I want to center align (not text-align) all these floated child divs with respect to the floated container.
How do I do that ?
At a time, only 1 of these child div is visible on the browser ..User clicks on Prev/Next to view other child divs (kind of like Carousel)
Apparently I am facing issues center aligning if I use float:left for the child div.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法相对于父元素对齐浮动元素。
float
CSS 属性将元素从文档中的上下文中分离出来(类似于position:absolute/fixed
)。因此,该元素无法定位在“父元素的中心”。You cannot align a floated element relative to a parent element.
The
float
CSS property rips an element from its context in the document (similarly toposition:absolute/fixed
). As a result of this, the element cannot be positioned "at the center of the parent".我今天正在为父容器内的三个 div 寻找解决方案,我发现了这篇旧文章,其中似乎没有提供好的解决方案。
我的情况:我有一个宽度为100%的父容器,以便它适合屏幕。我还设置了最大宽度,以便父容器不会变得太大。在最大宽度下,我希望三个子 div 全部显示在一行中。
随着页面缩小并且父容器宽度缩小,我希望三个子 div 回流,每个子 div 都被推到前一个子 div 下方,直到它们全部垂直堆叠。当发生这种情况时,我希望子 div 在父容器内保持中心对齐。
解决方案是不使用浮动,正如Rob W真正所说的那样“从上下文中剥离元素”,而是使用内联块:
您可以浮动父容器,给它绝对定位,几乎任何东西,子 div 都会保持相同的行为。
干杯
I was searching for a solution today for my three divs inside a parent container and I came across this old post where a good solution doesn't seem to have been offered.
My situation: I have a parent container with width:100% so that it fits the screen. I also set a max-width so that the parent container doesn't grow too large. At the max-width I want the three child divs to display all in one row.
As the page is reduced and the parent container shrinks in width, I want the three child divs to reflow, each child div being pushed under the previous child div until they are all stacked vertically. As this happens I want the child divs to stay center aligned within the parent container.
The solution is to not use float which "rips an element from its context" as Rob W truly states, but instead use inline-blocks:
You can float the parent container, give it absolute positioning, pretty much whatever, and the child divs will keep their same behavior.
cheers
如果您一次只显示一个子
div
,它们可能不需要浮动
。将非浮动块在另一个块内居中的最佳方法是指定以下样式:这将使
div.child
居中,因为左右边距将相等地跨度以适合父容器。同样,您可以将div
向左和向右对齐:If you show only one child
div
at once, probably they don't need tofloat
. The best way to center a non-floated block inside of another block is to assign the following style:This will center the
div.child
because both left and right margins will span equally to fit the parent container. Similarly, you can aligndiv
s to the left and to the right:如果容器和子容器的宽度都是固定的,则可以将
margin-left
和margin-right
设置为(容器 div 宽度 - 包含的宽度宽度)/2 。当然,如果您有填充和边框,则必须在公式中考虑它们。If you have a fixed width for both container and children, then you can use
margin-left
andmargin-right
set to (container div width - contained width width)/2. Of course, if you have paddings and borders, you have to account for them in the formula.