What steps are needed to develop a proper HTML structure for this specific box in order to achieve the desired aesthetics?

Currently working on a box that includes various tags and an input field for adding new tags. The goal is to make this box resemble the tag form used on YouTube:

https://i.stack.imgur.com/RSj2p.png

This particular layout features a box with existing tags displayed, along with a hidden input field for adding more tags. The input field should stretch across the remaining width of its parent block.

Here's the HTML code I'm using:

<div class="box">
    <ul>
        <li>added tag 1</li>
        <li>added tag 2</li>
    </ul>

    <input type="text" placeholder="input new tag" />
</div>

I've chosen to style the added tags as list items within the <ul> element since they essentially function as a list of items, making it a natural choice for structuring them this way.

My query pertains to styling: in this case, how can I ensure that the input behaves similarly to the example seen on YouTube? If I apply display: inline-flex to the <ul> and display: flex to the box div, the input field aligns itself on the next row rather than occupying the remaining space on the current partially filled row. Placing the input inside the <ul> and setting the display mode of the <ul> to flex could work, but it's not valid HTML since <ul> should only contain <li> elements.

Answer №1

Your input on Douwe de Haan's article suggests that using display: contents can effectively bypass the ul container. Take a look at the application below.

References to css-tricks and MDN

.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>

Answer №2

Utilizing display: contents

By using display: contents, we have the ability to substitute <ul> with its child elements in relation to CSS layout. This approach allows us to effectively arrange the input field and list-items together.

Please note: Some browsers currently experience accessibility concerns when employing display: contents. If this issue did not exist, I would recommend utilizing display: contents.

Alternative to using a list

If we decide to disregard the requirement of organizing the tags within a list structure, we can opt to place them as siblings alongside the input field. This flat hierarchy facilitates easier styling, for instance, leveraging CSS Flexbox:

.box {display: flex}
.box input {flex-grow: 1}

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

/*Excluded for formatting purposes*/
.box {border: 1px solid black}
.box>ul {
  margin: 0;
  padding: 0;
  list-style-type: none; /*Eliminate list bullets*/
}
.box>ul>li {background-color: #ddd}
<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>

Embracing inline elements

To achieve a seamless flow between tags and the input field akin to inline-level elements, it is advisable to employ display: inline (or similar):

.box > ul {display: inline}
.box > ul > li {display: inline-block}

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

/*Excluded for formatting purposes*/
.box {border: 1px solid black}
.box>ul {
  margin: 0;
  padding: 0;
  list-style-type: none; /*Remove list-markers*/
}
.box>ul>li {background-color: #ddd}
<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>

Note: Consider using inline-block or a similar approach for the tags if you require a different formatting context than outlined in inline formatting specifications, which may not fully accommodate certain vertical styles (e.g., margin, padding, border).


The reason why applying display: inline-flex on <ul> does not position the input field in the last partially filled line is:

The inline-flex value treats <ul> as one (flex) container within the flow. Another element cannot be inserted into that container's sequence.

In contrast, the inline value converts <ul> into an inline-level element, allowing it to adapt to its surrounding flow by forming line boxes. Line boxes might occupy only part of a line, enabling subsequent elements to share the same line.

Answer №3

To incorporate the input, you can simply include it as the last item in the existing list:

.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;
}
<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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display only the initial word in a hidden input value of an HTML document using NodeJS Express

I have a Node.js application that utilizes EJS as its template engine. On multiple pages, I incorporate hidden inputs and retrieve their values using req.body. Although my body parser in Express is configured correctly, I've noticed an issue where hid ...

The displayed database results will appear in the opposite order of what was originally assigned for both inline and block elements

I have a database with information that I am trying to display using a while loop. My goal is to show the results in this format... Firstname Lastname - Firstname Lastname - Firstname Lastname player1 ---------------player1-----------------------pla ...

Aligning a div vertically in the center of its parent container

I am trying to vertically align a child element within its parent element <!DOCTYPE html> <html> <head> <title>Test</title> <style type="text/css"> #body { font-family: sans-serif, arial, 'Roboto'; } #outer ...

Locate button elements with the class "button" that do not have any images inside them

Below is the code I am currently using for buttons (I prefer not to change it): <a href="#" class="button"> Button text <img src="someimage.png" /> </a> This CSS styled code creates a sleek button with an icon. To round the left sid ...

Adding an additional stroke to a Material-UI Circular Progress component involves customizing the styling properties

I am attempting to create a circular progress bar with a determinate value using Material-UI, similar to the example shown in this circular progress image. However, the following code does not display the additional circle as the background: <CircularP ...

EU directive on cookies: What is the best way to store your preferences?

While there are numerous third-party solutions available to comply with the cookie EU directive, I prefer to learn how to implement it myself. My specific requirement is that when a user clicks on "Accept", their choice should be remembered for the entire ...

Creating a dynamic navigation bar that adjusts to changing content requires careful planning and implementation

Struggling with achieving my visual mockup while designing a webpage: Check out my web design mockup Currently focusing on section 2 of the page. Using Angular 5, Bootstrap 3, and ngx-bootstrap library to integrate Bootstrap components into Angular. Here ...

Including a logo and navigation bar in the header while collaborating with different subregions

I'm having trouble getting a picture to display in the header alongside a horizontal menu. I've divided the header into two sections: img and navbar (html). The navbar is showing up correctly, but the image isn't appearing. Any suggestions o ...

Retrieving information from a text document by means of data-src with the assistance of jQuery

Trying to extract text from a .txt file using jQuery while utilizing Bootstrap. New to web design with some coding experience. Button in HTML: <button type="button" class="btn btn-primary-outline-btn text-btn" data-toggle="modal ...

What sets the Virtual DOM apart from the Shadow DOM?

During my exploration of Vue.js, I delved into the realm of shadow DOM to grasp the essence of a fundamental Vue.js Component. As I studied shadow DOM and its similarities to virtual DOM, I embarked on a quest for diverse information pertaining to both con ...

sequentially animating elements using animate css in a choreographed manner

I have created a unique jsfiddle with 20 boxes that I am trying to animate using the Animate.css plugin. The plugin can be found at daneden.me/animate. My goal is to animate each box one after the other in a sequential manner, but I seem to be having trou ...

Top Margin: Supported by Chrome and Safari

After troubleshooting why the margin-top is not working on Safari, but works fine on Chrome, I discovered a discrepancy. Chrome Upon hovering over an item in the image, the cursor changes as expected. This feature operates smoothly in Chrome. https://i. ...

Looking to incorporate HTML5 videos into a responsive Bootstrap carousel?

I've been working on creating a carousel that includes videos. When it comes to images, everything works smoothly as they are responsive even in mobile view. However, I'm encountering an issue with videos where the width is responsive but the hei ...

Adjusting the background color of the list item

I'm looking for a way to change the background color of the li tag when the user focuses on the input, similar to what can be seen at the bottom of this page here. After researching similar questions, it appears that achieving this effect in pure CSS ...

What advantages does creating a class that can freely float in either direction offer?

Recently, I noticed a common trend on websites like : .left { float: left; } <div class="col two left" /> Is this considered best practice? It goes against the idea of creating classes solely for styling purposes. If we wanted to change the floa ...

When coding on Github pages, the behavior of code blocks can vary depending on whether

I am interested in obtaining the offline version, which can be seen here: On the other hand, the online version has a different appearance: If you want to view the incorrect version, click on this direct link to visit the blog. A notable issue is the da ...

Splitting the page in half

Is there a way to ensure that the LeftArea and wuiMainPageNav sections stay side by side regardless of the browser window size? When I resize the window, the layout gets messed up with wuiMainPageNav moving down. Any suggestions on how to address this issu ...

Prevent typing in text box when drawer is activated by pressing a button

update 1 : After removing unnecessary files, I need assistance with https://codesandbox.io/s/0pk0z5prqn I am attempting to disable a textbox. When clicking the advanced sports search button, a drawer opens where I want to display a textbox. The toggleDra ...

Creating eight equal sections within HTML <div> elements using Bootstrap

I am brand new to Angular. I need to design something like this: https://i.sstatic.net/Zcb9i.png However, I'm struggling to find the right solution. Here is what I have so far: https://i.sstatic.net/7hsrk.png. Having been a backend developer, I&ap ...

Beneath the footer lies a blank space in white

Visit our site for information on various towns. However, we are facing an issue with the CSS when it comes to new or less populated towns like this one, where the footer is not aligning properly and leaving a large white gap below. We attempted to implem ...