gpt4 book ai didi

html - 具有 "overflow: hidden"溢出祖 parent 边缘的 Flexbox child

转载 作者:技术小花猫 更新时间:2023-10-29 12:20:47 27 4
gpt4 key购买 nike

我正在尝试将两个子元素嵌套在一个包装器中,该包装器指定边距,以便在显示较窄时其内容与屏幕两侧之间有空间,而 max-width 则用于显示器很宽。

第二个 child 有一些溢出应该是可见的,而第一个 child 应该严格留在包装器的内容框中。删除第一个 child 后,第二个 child 的行为将如预期。但是,当我添加第一个 child 时,它似乎完全忽略了包装器的边距,拉伸(stretch)了包装器的内容框并破坏了第二个 child 。

overflow: hidden 应用到包装器可以修复边距问题,但会剪掉第二个子元素。将边距应用于第一个子项不会使其与父项一起折叠,因为它处于新的 block 格式上下文中。

到目前为止我发现的唯一解决方法是:

.wrapper {
> * {
margin-left: 1.5rem;
margin-right: 1.5rem;
}
}

并将包装器的最大宽度增加 3rem,但我希望有一些解决方案不需要我将边距从包装器转移到它的子项。

https://codepen.io/HybridCore/pen/jjoWmd

body {
background-color: #000;
color: #FFF;
display: flex;
justify-content: center;
}

.wrapper {
margin: 0 1.5rem;
max-width: 40rem;
width: 100%;
}

.fit_content_box {
display: flex;
align-items: center;
}

.L {
min-width: 0;
flex: 1 0;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}

.R {
margin-left: 1rem;
height: 1rem;
width: 1rem;
background-color: #FFF;
}

.overflow {
display: flex;
justify-content: space-between;
}

.overflow>div {
width: 0;
display: flex;
justify-content: center;
}
<body>
<div class="wrapper">
<div class="fit_content_box">
<p class="L">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

<div class="R"></div>
</div>

<div class="overflow">
<div>
<p>0</p>
</div>
<div>
<p>12</p>
</div>
<div>
<p>24</p>
</div>
</div>
</div>
</body>

最佳答案

你主要有两个问题:

  1. 您正在设置 width:100%到包装器,这不考虑 margin ,所以逻辑上你会溢出,因为主体是一个带有 justify-content:center 的 flex 容器边距将从两侧均等溢出,这就是您认为未应用边距的原因。
  2. 你面对的是the min-width constraint of flexbox这迫使您设置 width:100%认为这是好的解决方案。同样的约束也防止元素收缩低于您指定的 100%(相关:Why is a flex item limited to parent size?)

要解决此问题,您需要删除 width:100%从包装器中考虑 min-width:0反而。您还可以删除 min-width适用于.L你需要考虑flex-shrink:0.R (或将其宽度替换为 min-width )

body {
background-color: #000;
color: #FFF;
display: flex;
justify-content: center;
}

.wrapper {
margin: 0 1.5rem;
max-width: 40rem;
min-width:0;
}

.fit_content_box {
display: flex;
align-items: center;
}

.L {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}

.R {
margin-left: 1rem;
flex-shrink:0;
height: 1rem;
width: 1rem;
background-color: #FFF;
}

.overflow {
display: flex;
justify-content: space-between;
}

.overflow>div {
width: 0;
display: flex;
justify-content: center;
}
<body>
<div class="wrapper">
<div class="fit_content_box">
<p class="L">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

<div class="R"></div>
</div>

<div class="overflow">
<div>
<p>0</p>
</div>
<div>
<p>12</p>
</div>
<div>
<p>24</p>
</div>
</div>
</div>
</body>

如果你希望元素至少保持等于max-width文本量少时加flex-grow:1 :

body {
background-color: #000;
color: #FFF;
display: flex;
justify-content: center;
}

.wrapper {
margin: 0 1.5rem;
max-width: 40rem;
min-width:0;
flex-grow:1;
}

.fit_content_box {
display: flex;
align-items: center;
}

.L {
flex-grow:1;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}

.R {
margin-left: 1rem;
flex-shrink:0;
height: 1rem;
width: 1rem;
background-color: #FFF;
}

.overflow {
display: flex;
justify-content: space-between;
}

.overflow>div {
width: 0;
display: flex;
justify-content: center;
}
<body>
<div class="wrapper">
<div class="fit_content_box">
<p class="L">Lorem ipsum dolor sit e dolor sit e</p>

<div class="R"></div>
</div>

<div class="overflow">
<div>
<p>0</p>
</div>
<div>
<p>12</p>
</div>
<div>
<p>24</p>
</div>
</div>
</div>
</body>


为了更好地说明 (1),这里是另一个您几乎不会注意到的边距溢出的示例:

.container {
width:200px;
margin:auto;
display:flex;
justify-content:center;
}
.box {
height:50px;
width:100%;
background:red;
}
<div class="container">
<div class="box" style="margin:0 5966px">a_long_text_to_avoid_the_shrink</div>
</div>

<div class="container">
<div class="box">a_long_text_to_avoid_the_shrink</div>
</div>

你可以看到我们有一个长文本迫使我们的元素不收缩(最小宽度约束),元素占据全宽并且我们将内容居中。这将使边距像没有边距一样溢出。

如果您违反了一条规则,那么您将看到边距的影响。

删除长文本:

.container {
width:200px;
margin:auto;
display:flex;
justify-content:center;
}
.box {
width:100%;
height:50px;
background:red;
}
<div class="container">
<div class="box" style="margin:0 5966px">a long text to avoid the shrink</div>
</div>

<div class="container">
<div class="box">a long text to avoid the shrink</div>
</div>

移除居中:

.container {
width:200px;
margin:auto;
display:flex;
}
.box {
width:100%;
height:50px;
background:red;
}
<div class="container">
<div class="box" style="margin:0 5966px">a_long_text_to_avoid_the_shrink</div>
</div>

<div class="container">
<div class="box">a_long_text_to_avoid_the_shrink</div>
</div>

每边做不同的边距

.container {
width:200px;
margin:auto;
display:flex;
justify-content:center;
}
.box {
width:100%;
height:50px;
background:red;
}
<div class="container">
<div class="box" style="margin:0 500px 0 400px">a_long_text_to_avoid_the_shrink</div>
</div>

<div class="container">
<div class="box">a_long_text_to_avoid_the_shrink</div>
</div>


(2) white-space正在创建最小宽度约束以防止元素缩小。

举个例子来说明:

.body {
display: flex;
justify-content: center;
margin: 10px 100px;
border: 2px solid red;
}

.wrapper {
border: 1px solid;
margin: 0 20px;
}
.box {
display:flex;
}
The below is a logical behavior where the text will wrap and the margin are respected
<div class="body">
<div class="wrapper">
<div class="box">
<div>some long text here some long text here some long text here some long text here</div>
</div>
</div>
</div>
Let's add white-space:nowrap. We add a min-width contraint since we said to the text to never wrap thus our flex element will not shrink and overflow.
<div class="body">
<div class="wrapper">
<div class="box">
<div style="white-space:nowrap">some long text here some long text here some long text here some long text here</div>
</div>
</div>
</div>
If we add width:100% we force its width to be the same as the container BUT the margin aren't included and are kept outside (the text will logically overflow)
<div class="body">
<div class="wrapper" style="width:100%">
<div class="box">
<div style="white-space:nowrap">some long text here some long text here some long text here some long text here</div>
</div>
</div>
</div>
Now if we add min-width:0 we remove the constaint of minimum sizing and we can see the margin again even if we keep width:100% because the element will shrink by default
<div class="body">
<div class="wrapper" style="width:100%;min-width:0">
<div class="box">
<div style="white-space:nowrap">some long text here some long text here some long text here some long text here</div>
</div>
</div>
</div>

诀窍是我们将元素居中并在两侧应用相同的边距,这会产生边距折叠的错觉,但它只是从两侧均等地溢出边距。

让我们稍微改变一侧的边距,看看另一侧有一点偏移:

.body {
display: flex;
justify-content: center;
margin: 10px 100px;
border: 2px solid red;
}

.wrapper {
border: 1px solid;
margin: 0 20px 0 40px;
}
.box {
display:flex;
}
The below is a logical behavior where the text will wrap and the margin are respected
<div class="body">
<div class="wrapper">
<div class="box">
<div>some long text here some long text here some long text here some long text here</div>
</div>
</div>
</div>
Let's add white-space:nowrap. We add a min-width contraint since we said to the text to never wrap thus our flex element will not shrink and overflow.
<div class="body">
<div class="wrapper">
<div class="box">
<div style="white-space:nowrap">some long text here some long text here some long text here some long text here</div>
</div>
</div>
</div>
If we add width:100% we force its width to be the same as the container BUT the margin aren't included and are kept outside (the text will logically overflow)
<div class="body">
<div class="wrapper" style="width:100%">
<div class="box">
<div style="white-space:nowrap">some long text here some long text here some long text here some long text here</div>
</div>
</div>
</div>
Now if we add min-width:0 we remove the constaint of minimum sizing and we can see the margin again even if we keep width:100% because the element will shrink by default
<div class="body">
<div class="wrapper" style="width:100%;min-width:0">
<div class="box">
<div style="white-space:nowrap">some long text here some long text here some long text here some long text here</div>
</div>
</div>
</div>

关于html - 具有 "overflow: hidden"溢出祖 parent 边缘的 Flexbox child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57021064/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com