在浮动容器内居中对齐浮动 div

发布于 2024-12-10 08:20:22 字数 193 浏览 1 评论 0原文

我有一个带有多个子浮动 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄意 2024-12-17 08:20:22

您无法相对于父元素对齐浮动元素。

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 to position:absolute/fixed). As a result of this, the element cannot be positioned "at the center of the parent".

椵侞 2024-12-17 08:20:22

我今天正在为父容器内的三个 div 寻找解决方案,我发现了这篇旧文章,其中似乎没有提供好的解决方案。

我的情况:我有一个宽度为100%的父容器,以便它适合屏幕。我还设置了最大宽度,以便父容器不会变得太大。在最大宽度下,我希望三个子 div 全部显示在一行中。

随着页面缩小并且父容器宽度缩小,我希望三个子 div 回流,每个子 div 都被推到前一个子 div 下方,直到它们全部垂直堆叠。当发生这种情况时,我希望子 div 在父容器内保持中心对齐。

解决方案使用浮动,正如Rob W真正所说的那样“从上下文中剥离元素”,而是使用内联块:

.parentContainer{
   margin:0 auto; /*keep centered in page*/
   width:100%; /*stretch to page*/
   max-width:800px; /*expand up to 800px*/
   text-align:center; /*keep my children centered*/
}
.childDiv{
   display:inline-block /*treat me as a block that flows like text*/
   width:230px; /*set my size*/
   vertical-align:top; /*keep me aligned at the top of my parent even if my siblings are taller than me*/
}

<div class='parentContainer'>
   <div class='childDiv'>Content 1</div>
   <div class='childDiv'>Content 2</div>
   <div class='childDiv'>Content 3</div>
</div>

您可以浮动父容器,给它绝对定位,几乎任何东西,子 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:

.parentContainer{
   margin:0 auto; /*keep centered in page*/
   width:100%; /*stretch to page*/
   max-width:800px; /*expand up to 800px*/
   text-align:center; /*keep my children centered*/
}
.childDiv{
   display:inline-block /*treat me as a block that flows like text*/
   width:230px; /*set my size*/
   vertical-align:top; /*keep me aligned at the top of my parent even if my siblings are taller than me*/
}

<div class='parentContainer'>
   <div class='childDiv'>Content 1</div>
   <div class='childDiv'>Content 2</div>
   <div class='childDiv'>Content 3</div>
</div>

You can float the parent container, give it absolute positioning, pretty much whatever, and the child divs will keep their same behavior.

cheers

一刻暧昧 2024-12-17 08:20:22

如果您一次只显示一个子 div,它们可能不需要浮动。将非浮动块在另一个块内居中的最佳方法是指定以下样式:

.child {
    margin: 0 auto;
}

这将使 div.child 居中,因为左右边距将相等地跨度以适合父容器。同样,您可以将 div 向左和向右对齐:

.left {
    margin-right: auto;
}
.right {
    margin-left: auto;
}

If you show only one child div at once, probably they don't need to float. The best way to center a non-floated block inside of another block is to assign the following style:

.child {
    margin: 0 auto;
}

This will center the div.child because both left and right margins will span equally to fit the parent container. Similarly, you can align divs to the left and to the right:

.left {
    margin-right: auto;
}
.right {
    margin-left: auto;
}
愁以何悠 2024-12-17 08:20:22

如果容器和子容器的宽度都是固定的,则可以将 margin-leftmargin-right 设置为(容器 div 宽度 - 包含的宽度宽度)/2 。当然,如果您有填充和边框,则必须在公式中考虑它们。

If you have a fixed width for both container and children, then you can use margin-left and margin-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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文