英文:
How to create a valid HTML for this box to achieve the described styling?
问题
I'm making a box containing multiple tags and an input field that allows to enter a new tag. This box should look and function alike the following tag form from YouTube:
这是一个包含多个标签和一个允许输入新标签的输入字段的框。此框应该看起来和功能与YouTube中的以下标签表单相似:
This layout is a box that contains the list of already added tags, as well as an invisible input field that allows to add more tags to the list. This input field stretches to the leftover width of the parent block.
这个布局是一个包含已经添加的标签列表的框,以及一个允许添加更多标签到列表中的不可见输入字段。这个输入字段会延伸到父块的剩余宽度。
Here is the HTML code that I have:
以下是我拥有的HTML代码:
<div class="box">
<ul>
<li>added tag 1</li>
<li>added tag 2</li>
</ul>
<input type="text" placeholder="input new tag" />
</div>
I style already added tags as list items inside <ul>
because, essentially, added tags are a list of items, and this seems the most natural markup for them.
我将已经添加的标签样式化为<ul>
中的列表项,因为从本质上来说,已添加的标签是一个项目列表,这似乎是它们最自然的标记。
My question is about styling: in that scenario, how do I make the input behave just like it does in YouTube's example provided above? If I assign display: inline-flex
to this <ul>
and display: flex
to the box div, then the input will get aligned in the next row instead of occupying the remaining place of the last partially filled row. Putting the input inside of the <ul>
and setting the <ul>
's display mode to flex
would work, but it is invalid HTML because <ul>
should only contain <li>
's.
我的问题是关于样式:在这种情况下,如何使输入框的行为与上面提供的YouTube示例相同?如果我将display: inline-flex
分配给<ul>
,并将display: flex
分配给框<div>
,那么输入框将与下一行对齐,而不是占据最后部分填充行的剩余位置。将输入框放在<ul>
内,并将<ul>
的显示模式设置为flex
将起作用,但这是无效的HTML,因为<ul>
应只包含<li>
。
英文:
I'm making a box containing multiple tags and an input field that allows to enter a new tag. This box should look and function alike the following tag form from YouTube:
This layout is a box that contains the list of already added tags, as well as an invisible input field that allows to add more tags to the list. This input field stretches to the leftover width of the parent block.
Here is the HTML code that I have:
<div class="box">
<ul>
<li>added tag 1</li>
<li>added tag 2</li>
</ul>
<input type="text" placeholder="input new tag" />
</div>
I style already added tags as list items inside <ul>
because, essentially, added tags are a list of items, and this seems the most natural markup for them.
My question is about styling: in that scenario, how do I make the input behave just like it does in YouTube's example provided above? If I assign display: inline-flex
to this <ul>
and display: flex
to the box div, then the input will get aligned in the next row instead of occupying the remaining place of the last partially filled row. Putting the input inside of the <ul>
and setting the <ul>
's display mode to flex
would work, but it is invalid HTML because <ul>
should only contain <li>
's.
答案1
得分: 3
根据您对Douwe de Haan的帖子的评论,您确实可以使用display: contents
来忽略ul
容器。请参见下面的示例。
在css-tricks和MDN上查看Display: contents的示例。
.box {
width: 20rem;
height: 12rem;
border: 2px solid #0E68D3;
border-radius: 0.25rem;
padding: 0.5rem;
display: flex;
align-items: baseline;
align-content: flex-start;
flex-wrap: wrap;
}
.box ul {
display: contents;
}
.box ul li {
list-style-type: none;
margin: 0.25rem;
padding: 0.5rem 1rem;
background-color: #E9E9E9;
border-radius: 100vh;
white-space: nowrap;
}
.box input {
flex-grow: 1;
}
<div class="box">
<ul>
<li>added tag 1</li>
<li>added tag 2</li>
<li>added tag three</li>
</ul>
<input type="text" placeholder="input new tag" />
</div>
英文:
As per your comment to Douwe de Haan's post, you can indeed use display: contents
to ignore the ul
container. See the example below.
Display: contents on css-tricks and MDN
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.box {
width: 20rem;
height: 12rem;
border: 2px solid #0E68D3;
border-radius: 0.25rem;
padding: 0.5rem;
display: flex;
align-items: baseline;
align-content: flex-start;
flex-wrap: wrap;
}
.box ul {
display: contents;
}
.box ul li {
list-style-type: none;
margin: 0.25rem;
padding: 0.5rem 1rem;
background-color: #E9E9E9;
border-radius: 100vh;
white-space: nowrap;
}
.box input {
flex-grow: 1;
}
<!-- language: lang-html -->
<div class="box">
<ul>
<li>added tag 1</li>
<li>added tag 2</li>
<li>added tag three</li>
</ul>
<input type="text" placeholder="input new tag" />
</div>
<!-- end snippet -->
答案2
得分: 2
使用 display: contents
使用 display: contents
,我们可以将 <ul>
替换为其子元素,以便在 CSS 布局方面进行布局。这样我们可以将输入和列表项一起布局。
注意:在撰写本文时,一些浏览器在使用 display: contents
时存在辅助功能问题。如果不是这种情况,那么我建议使用 display: contents
。
不使用列表
如果我们忽略将标签放在列表中的要求,那么我们可以将它们作为输入的兄弟元素。这种扁平结构更容易进行样式设置,例如使用 CSS Flexbox:
<div class="box">
<span>added tag 1</span>
<span>added tag 2</span>
<span>added tag 3</span>
<span>added tag 4</span>
<input placeholder="input new tag">
</div>
使用内联元素
我们希望标签和输入像内联级元素一样流动,因此我建议使用 display: inline
(或类似的方式):
<div class="box">
<ul>
<li>added tag 1</li>
<li>added tag 2</li>
<li>added tag 3</li>
<li>added tag 4</li>
</ul>
<input placeholder="input new tag">
</div>
注意:您可能希望为标签使用 inline-block
或类似的样式,以便使用除内联格式之外的格式上下文,该格式上下文不支持一些垂直样式(例如 margin
、padding
、border
)。
display: inline-flex
在 <ul>
上不会将输入放在其最后部分填满的行中的原因是:
inline-flex
值将 <ul>
作为一个(flex)_容器_内联显示。不能将另一个元素放在该容器的流中。
然而,inline
值将 <ul>
变成_内联级_元素。它可以根据其周围的流进行包装,形成_行框_。行框可以部分填充一行,因此后续元素可以放在同一行上。
英文:
Using display: contents
With display: contents
, we can replace <ul>
with its children in regards to CSS layout. That way we can lay out the input and the list-items together.
Note: At the time of writing, some browsers come with accessibility issues when using display: contents
. If this was not the case, then I'd suggest using display: contents
.
Not using a list
If we were to ignore the requirement of placing the tags in a list, then we could have them as siblings to the input. That flat structure allows for easier styling, e.g. with CSS Flexbox:
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-css -->
.box {display: flex}
.box input {flex-grow: 1}
/*Allow resizing; observe the behaviour!*/
.box {
overflow: hidden;
resize: both;
}
/*Ignore; presentational*/
.box {border: 1px solid black}
.box>ul {
margin: 0;
padding: 0;
list-style-type: none; /*Remove list-markers*/
}
.box>ul>li {background-color: #ddd}
<!-- language: lang-html -->
<div class="box">
<span>added tag 1</span>
<span>added tag 2</span>
<span>added tag 3</span>
<span>added tag 4</span>
<input placeholder="input new tag">
</div>
<!-- end snippet -->
Using inline elements
We want the tags and the input to flow like inline-level elements, therefore I suggest using display: inline
(or similar):
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-css -->
.box > ul {display: inline}
.box > ul > li {display: inline-block}
/*Allow resizing; observe the behaviour!*/
.box {
overflow: hidden;
resize: both;
}
/*Ignore; presentational*/
.box {border: 1px solid black}
.box>ul {
margin: 0;
padding: 0;
list-style-type: none; /*Remove list-markers*/
}
.box>ul>li {background-color: #ddd}
<!-- language: lang-html -->
<div class="box">
<ul>
<li>added tag 1</li>
<li>added tag 2</li>
<li>added tag 3</li>
<li>added tag 4</li>
</ul>
<input placeholder="input new tag">
</div>
<!-- end snippet -->
Note: You may want to prefer inline-block
or similar for the tags to use a formatting context other than inline formatting, which doesn't honour some vertical styling (e.g. margin
, padding
, border
).
The reason that display: inline-flex
on <ul>
won't place the input in its last partially filled line is:
The inline-flex
value inlines <ul>
as one (flex) container. Another element cannot be placed in that container's flow.
However, the inline
value makes <ul>
an inline-level element. It can be wrapped according to its surrounding flow into line boxes. Line boxes may partially fill a line, so following elements can be placed along the same line.
答案3
得分: 0
你可以将输入添加到列表中的最后一项:
<div class="box">
<ul>
<li>stack overflow</li>
<li>how to use stack overflow</li>
<li>tutorial</li>
<li>css overflow</li>
<li>talent on stack overflow</li>
<li>buffer overflow tutorial</li>
<li>ask stack overflow</li>
<li>stack overflow rude</li>
<li>stack overflow jobs</li>
<li>tech talent on stack overflow</li>
<li>buffer overflow tutorial step by step</li>
<li>recruit on stack overflow</li>
<li>stack overflow visual studio code</li>
<li>what is stack overflow</li>
<li>stack overflow vs code</li>
<li>stack overflow search</li>
<li>css overflow tutorial in hindi</li>
<li><input type="text" placeholder="input new tag" /></li>
</ul>
</div>
英文:
You could add the input to the last item in the list:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.box {
background: #333;
}
.box ul,
.box input {
display: inline-block;
}
.box ul li {
display: inline-block;
background: #FFF;
padding: 5px;
border-radius: 20px;
margin-top: 5px;
}
<!-- language: lang-html -->
<div class="box">
<ul>
<li>stack overflow</li>
<li>how to use stack overflow</li>
<li>tutorial</li>
<li>css overflow</li>
<li>talent on stack overflow</li>
<li>buffer overflow tutorial</li>
<li>ask stack overflow</li>
<li>stack overflow rude</li>
<li>stack overflow jobs</li>
<li>tech talent on stack overflow</li>
<li>buffer overflow tutorial step by step</li>
<li>recruit on stack overflow</li>
<li>stack overflow visual studio code</li>
<li>what is stack overflow</li>
<li>stack overflow vs code</li>
<li>stack overflow search</li>
<li>css overflow tutorial in hindi</li>
<li><input type="text" placeholder="input new tag" /></li>
</ul>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论