gpt4 book ai didi

html - 使 CSS Flex 元素忽略容器元素

转载 作者:太空狗 更新时间:2023-10-29 12:33:21 28 4
gpt4 key购买 nike

我正在努力创建一个类似于 Chosen 的组件,但我遇到了 flex 和 wrapping 的问题。要点是伪文本框中的每个“芯片”都需要位于一个不包含实际文本输入的容器中,但我需要将这些芯片视为与 输入相同的基础上的 flex 元素

我的 HTML 结构如下所示:

<div class="box">
<div class="chipContainer">
<div class="chip">Chip1</div>
<div class="chip">Chip2</div>
<div class="chip">Chip3</div>
<div class="chip">Chip4</div>
</div>
<input class="textbox" type="text">
</div>

Plunker example更完整,样式与我目前正在使用的样式大致相同。

.box {
background: #fff;
border: 1px solid #ccc;
width: 500px;
overflow: auto;
padding: 2px;
display: flex;
flex-wrap: wrap;
}
.chip {
display: inline-block;
border: 1px solid #ccc;
background: #eee;
padding: 2px;
margin-right: 1px;
flex: auto;
}
.textbox {
border: none;
background: #fdd;
font-size: 18px;
flex: auto;
}
.textbox:focus {
outline: none;
}
.chipContainer {
display: inline;
}
<div class="box">
<div class="chipContainer">
<div class="chip">Chip1</div>
<div class="chip">Chip2</div>
<div class="chip">Chip3</div>
<div class="chip">Chip4</div>
</div>
<input class="textbox" type="text">
</div>
<br>
<div class="box">
<div class="chipContainer">
<div class="chip">Chip1</div>
<div class="chip">Chip2</div>
<div class="chip">Chip3</div>
<div class="chip">Chip4</div>
<div class="chip">Chip5</div>
<div class="chip">Chip6</div>
<div class="chip">Chip7</div>
<div class="chip">Chip8</div>
<div class="chip">Chip9</div>
<div class="chip">Chip10</div>
<div class="chip">Chip11</div>
</div>
<input class="textbox" type="text">
</div>

如您所见,在 .box 容器中添加了足够多的筹码后,.chipContainer 将开始包裹它们,但是 .chipContainer 仍然是一个 block 元素,并将 input 完全向下推到一个新行。

Plunker example证明我的目标。删除 .chipContainer 后可以很容易地实现布局,但它是组件结构中相当重要的元素,如果可能的话,我更愿意在不删除它的情况下解决这个问题。

.box {
background: #fff;
border: 1px solid #ccc;
width: 500px;
overflow: auto;
padding: 2px;
display: flex;
flex-wrap: wrap;
}
.chip {
display: inline-block;
border: 1px solid #ccc;
background: #eee;
padding: 2px;
margin-right: 1px;
flex: auto;
min-width: 50px;
max-width: 60px;
}
.textbox {
border: none;
background: #fdd;
font-size: 18px;
flex: auto;
min-width: 250px;
}
.textbox:focus {
outline: none;
}
.chipContainer {
display: inline;
}
<div class="box">
<div class="chip">Chip1</div>
<div class="chip">Chip2</div>
<div class="chip">Chip3</div>
<div class="chip">Chip4</div>
<input class="textbox" type="text">
</div>
<br>
<div class="box">
<div class="chip">Chip1</div>
<div class="chip">Chip2</div>
<div class="chip">Chip3</div>
<div class="chip">Chip4</div>
<div class="chip">Chip5</div>
<div class="chip">Chip6</div>
<div class="chip">Chip7</div>
<div class="chip">Chip8</div>
<div class="chip">Chip9</div>
<input class="textbox" type="text">
</div>

最佳答案

Display Module Level 3介绍 display: contents :

The element itself does not generate any boxes, but its children and pseudo-elements still generate boxes as normal. For the purposes of box generation and layout, the element must be treated as if it had been replaced with its children and pseudo-elements in the document tree.

所以你只需要

.chipContainer {
display: contents;
}

.box {
background: #fff;
border: 1px solid #ccc;
width: 500px;
overflow: auto;
padding: 2px;
display: flex;
flex-wrap: wrap;
}
.chip {
display: inline-block;
border: 1px solid #ccc;
background: #eee;
padding: 2px;
margin-right: 1px;
flex: auto;
min-width: 50px;
max-width: 60px;
}
.textbox {
border: none;
background: #fdd;
font-size: 18px;
flex: auto;
}
.textbox:focus {
outline: none;
}
.chipContainer {
display: contents;
}
<div class="box">
<div class="chipContainer">
<div class="chip">Chip1</div>
<div class="chip">Chip2</div>
<div class="chip">Chip3</div>
<div class="chip">Chip4</div>
</div>
<input class="textbox" type="text">
</div>
<br>
<div class="box">
<div class="chipContainer">
<div class="chip">Chip1</div>
<div class="chip">Chip2</div>
<div class="chip">Chip3</div>
<div class="chip">Chip4</div>
<div class="chip">Chip5</div>
<div class="chip">Chip6</div>
<div class="chip">Chip7</div>
<div class="chip">Chip8</div>
<div class="chip">Chip9</div>
<div class="chip">Chip10</div>
<div class="chip">Chip11</div>
</div>
<input class="textbox" type="text">
</div>

但是,请注意,它尚未得到广泛支持。

关于html - 使 CSS Flex 元素忽略容器元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31033164/

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