如何创建一个有效的HTML以实现所描述的样式?

huangapple go评论67阅读模式
英文:

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中的以下标签表单相似:

如何创建一个有效的HTML以实现所描述的样式?

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:

如何创建一个有效的HTML以实现所描述的样式?

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:

&lt;div class=&quot;box&quot;&gt;
    &lt;ul&gt;
        &lt;li&gt;added tag 1&lt;/li&gt;
        &lt;li&gt;added tag 2&lt;/li&gt;
    &lt;/ul&gt;

    &lt;input type=&quot;text&quot; placeholder=&quot;input new tag&quot; /&gt;
&lt;/div&gt;

I style already added tags as list items inside &lt;ul&gt; 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 &lt;ul&gt; 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 &lt;ul&gt; and setting the &lt;ul&gt;'s display mode to flex would work, but it is invalid HTML because &lt;ul&gt; should only contain &lt;li&gt;'s.

答案1

得分: 3

根据您对Douwe de Haan的帖子的评论,您确实可以使用display: contents来忽略ul容器。请参见下面的示例。

css-tricksMDN上查看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 -->

&lt;div class=&quot;box&quot;&gt;
    &lt;ul&gt;
      &lt;li&gt;added tag 1&lt;/li&gt;
      &lt;li&gt;added tag 2&lt;/li&gt;
      &lt;li&gt;added tag three&lt;/li&gt;
    &lt;/ul&gt;
    &lt;input type=&quot;text&quot; placeholder=&quot;input new tag&quot; /&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 2

使用 display: contents

使用 display: contents,我们可以将 &lt;ul&gt; 替换为其子元素,以便在 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 或类似的样式,以便使用除内联格式之外的格式上下文,该格式上下文不支持一些垂直样式(例如 marginpaddingborder)。


display: inline-flex&lt;ul&gt; 上不会将输入放在其最后部分填满的行中的原因是:

inline-flex 值将 &lt;ul&gt; 作为一个(flex)_容器_内联显示。不能将另一个元素放在该容器的流中。

然而,inline 值将 &lt;ul&gt; 变成_内联级_元素。它可以根据其周围的流进行包装,形成_行框_。行框可以部分填充一行,因此后续元素可以放在同一行上。

英文:

Using display: contents

With display: contents, we can replace &lt;ul&gt; 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&gt;ul {
  margin: 0;
  padding: 0;
  list-style-type: none; /*Remove list-markers*/
}
.box&gt;ul&gt;li {background-color: #ddd}

<!-- language: lang-html -->

&lt;div class=&quot;box&quot;&gt;
  &lt;span&gt;added tag 1&lt;/span&gt;
  &lt;span&gt;added tag 2&lt;/span&gt;
  &lt;span&gt;added tag 3&lt;/span&gt;
  &lt;span&gt;added tag 4&lt;/span&gt;
  &lt;input placeholder=&quot;input new tag&quot;&gt;
&lt;/div&gt;

<!-- 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 &gt; ul {display: inline}
.box &gt; ul &gt; li {display: inline-block}

/*Allow resizing; observe the behaviour!*/
.box {
  overflow: hidden;
  resize: both;
}

/*Ignore; presentational*/
.box {border: 1px solid black}
.box&gt;ul {
  margin: 0;
  padding: 0;
  list-style-type: none; /*Remove list-markers*/
}
.box&gt;ul&gt;li {background-color: #ddd}

<!-- language: lang-html -->

&lt;div class=&quot;box&quot;&gt;
  &lt;ul&gt;
    &lt;li&gt;added tag 1&lt;/li&gt;
    &lt;li&gt;added tag 2&lt;/li&gt;
    &lt;li&gt;added tag 3&lt;/li&gt;
    &lt;li&gt;added tag 4&lt;/li&gt;
  &lt;/ul&gt;
  &lt;input placeholder=&quot;input new tag&quot;&gt;
&lt;/div&gt;

<!-- 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 &lt;ul&gt; won't place the input in its last partially filled line is:

The inline-flex value inlines &lt;ul&gt; as one (flex) container. Another element cannot be placed in that container's flow.

However, the inline value makes &lt;ul&gt; 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 -->

&lt;div class=&quot;box&quot;&gt;
  &lt;ul&gt;
    &lt;li&gt;stack overflow&lt;/li&gt;
    &lt;li&gt;how to use stack overflow&lt;/li&gt;
    &lt;li&gt;tutorial&lt;/li&gt;
    &lt;li&gt;css overflow&lt;/li&gt;
    &lt;li&gt;talent on stack overflow&lt;/li&gt;
    &lt;li&gt;buffer overflow tutorial&lt;/li&gt;
    &lt;li&gt;ask stack overflow&lt;/li&gt;
    &lt;li&gt;stack overflow rude&lt;/li&gt;
    &lt;li&gt;stack overflow jobs&lt;/li&gt;
    &lt;li&gt;tech talent on stack overflow&lt;/li&gt;
    &lt;li&gt;buffer overflow tutorial step by step&lt;/li&gt;
    &lt;li&gt;recruit on stack overflow&lt;/li&gt;
    &lt;li&gt;stack overflow visual studio code&lt;/li&gt;
    &lt;li&gt;what is stack overflow&lt;/li&gt;
    &lt;li&gt;stack overflow vs code&lt;/li&gt;
    &lt;li&gt;stack overflow search&lt;/li&gt;
    &lt;li&gt;css overflow tutorial in hindi&lt;/li&gt;
    &lt;li&gt;&lt;input type=&quot;text&quot; placeholder=&quot;input new tag&quot; /&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月3日 21:26:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627674.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定