gpt4 book ai didi

html - CSS:阻止第二个 child (段落)扩大 parent 的宽度

转载 作者:太空宇宙 更新时间:2023-11-04 06:46:39 24 4
gpt4 key购买 nike

我有 flexbox 框列表,其宽度由选择下拉列表和旁边的按钮控制。当我在其下方显示错误并且错误很长时,框会变大(宽度)。看这个jsFiddle example .我怎么能阻止它。我希望错误换行并且框与选择+按钮一样宽。它必须仍然是灵活的,所以没有段落的最大宽度。而且没有绝对的立场。此外,真正的代码不是 jQuery (AngularJS)。

html:

<div class="container">
<ul class="items">
<li class="item">
<h2>Title 1</h2>
<div>
<select name="item-1-options" id="item-1-options">
<option value="1">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</option>
<option value="2">Lorem ipsum dolor sit amet</option>
<option value="3">Lorem ipsum dolor</option>
</select>
<button>Select</button>
</div>
<div class="errors">
<p class="error">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ultricies.</p>
</div>
</li>
<li class="item">
<h2>Title 1</h2>
<div>
<select name="item-1-options" id="item-1-options">
<option value="1">Lorem ipsum .</option>
<option value="2">Lorem ipsum dolor sit amet</option>
<option value="3">Lorem ipsum dolor</option>
</select>
<button>Select</button>
</div>
<div class="errors">
<p class="error">Lorem ipsum dolor sit amet, consectetur adipiscingLorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ultricies. Lorem ipsum dolor</p>
</div>
</li>
</ul>

盒子的 CSS:

.items {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.item {
display: block;
padding: 1em;
margin: 1em;
border: 1px grey solid;
background: white;
flex-grow: 1;
}

最佳答案

如果我是正确的,您只需要将您的子 div 包装起来,这将是下拉菜单和按钮作为子 1,错误消息作为子 2,将它们包装在带有内联 block 的父 div 中。设置 child 2 flex-grow 为 1 和 width 为 0,然后 ta-da

$("#toggle-errors").on("click", function(){
$(".errors").toggle();
});
.wrapper > div {
border: 1px solid tomato;
}

.errors {
display: flex;
}

.errors p {
flex-grow: 1;
width: 0;
}

.wrapper {
display: inline-block;
}

.items {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.item {
display: block;
padding: 1em;
margin: 1em;
border: 1px grey solid;
background: white;
flex-grow: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="toggle-errors">Toggle Errors</button>
<div class="container">
<ul class="items">
<li class="item">
<h2>Title 1</h2>
<div class="wrapper">
<div>
<select name="item-1-options" id="item-1-options">
<option value="1">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</option>
<option value="2">Lorem ipsum dolor sit amet</option>
<option value="3">Lorem ipsum dolor</option>
</select>
<button>Select</button>
</div>

<div class="errors">
<p class="error">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ultricies.</p>
</div>
</li>
<li class="item">
<h2>Title 1</h2>
<div class="wrapper">
<div>
<select name="item-1-options" id="item-1-options">
<option value="1">Lorem ipsum dolor</option>
<option value="2">Lorem ipsum dolor sit amet</option>
<option value="3">Lorem ipsum dolor</option>
</select>
<button>Select</button>
</div>
<div class="errors">
<p class="error">Lorem ipsum dolor sit amet, consectetur adipiscingLorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ultricies. Lorem ipsum dolor</p>
</div>
</li>
<li class="item">
<h2>Title 1</h2>
<div>
<select name="item-1-options" id="item-1-options">
<option value="1">Lorem ipsum dolor sit amet, consectetur.</option>
<option value="2">Lorem ipsum dolor sit amet</option>
<option value="3">Lorem ipsum dolor</option>
</select>
<button>Select</button>
</div>
</li>
</ul>
</div>

关于html - CSS:阻止第二个 child (段落)扩大 parent 的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53232844/

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