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

I am looking for guidance on removing the bottom line from the ionic 4 segment indicator. Any advice or tips on

.segment-button-indicator { -ms-flex-item-align: end; align-self: flex-end; width: 100%; height: 2px; background-color: var(--indicator-color); opacity: 1; } I am a beginner in hybrid app development and ...

Guide to removing selected value from a combobox in Angular

I am working on a simple HTML file that consists of one combobox and one clear button. I want the clear button to remove the selected value from the combobox when clicked. Below is my code: mat-card-content fxLayout="row wrap" fxLayoutAlign="left" fxLayou ...

The process involves transferring information from a specific div element to a database. This particular div element obtains its data after receiving an appended ID

It's a bit complicated, but I'm working on creating a tag system. I'm fetching data from a database with an AJAX call using the "@" symbol. Everything is working fine - the tags are being generated, but I'm having trouble retrieving and ...

The issue of selecting multiple classes simultaneously is not being resolved

Here is my JavaScript code snippet: $('.normal-box').click(function(){ //unique row id var id = $(this).data('row-id'); //its index according to the content array var index = $(this).data('index'); //which car ...

Alter the Default Visibility on an HTML Page from Visible to Hidden with JavaScript

I found this code snippet on a website and made some modifications. On my webpage, I have implemented links that toggle the visibility of hidden text. The functionality is working fine, but the issue is that the hidden text is initially visible when the p ...

Personalizing the arrow positioning of the Angular8 date picker (both top and bottom arrow)

I am interested in enhancing the design of the Angular 8 date picker by adding top and bottom arrows instead of the default left and right arrows. Can someone guide me on how to customize it? Check out the Angular 8 date picker here ...

The necessary min-width for the media query has not been implemented

Despite creating a basic example using media queries, I'm puzzled that the effect is not being applied at the exact min-width, but rather at (offset + min-width). The HTML document is currently empty. <!DOCTYPE html> <html lang=""> <h ...

Is it possible to keep my JavaScript scripts running continuously within my HTML code?

I recently set up a JavaScript file that continuously queries an API for updates. It's currently linked to my index.html, but I'm looking for a way to keep it live and running 24/7 without requiring the browser to be open. Any suggestions on how ...

Looking to retrieve the <td> element using the class name?

When a specific condition is met in the class name, I want to apply styles to an entire row. The table structure I am working with is as follows: <table> <tbody> <tr> <td><div><span class='level2'></span&g ...

Creating circular borders in CSS: A step-by-step guide

Is it possible to create a circular border in CSS? Currently, the shape is square but I would like it to be circular. color: white; left: 52px; position: absolute; top: 65px; padding: 3px; background-color: rgb(0, 195, 255); ...

Can you provide me with instructions on utilizing PNGs to create edge masks for an image?

After experimenting with border-image and PNGs to achieve textured borders, I'm curious if there is a method for masking in a similar fashion? I recently came across the Rumchata website which showcases what I have in mind. Their background has a blu ...

Accordions housing Bootstrap 3 pills in a sleek design

I'm working on integrating pills and an accordion in Bootstrap 3. Everything seems to be functioning correctly, but there are a couple of issues I am encountering: When the accordion is open, clicking on another tab closes the accordion. I would li ...

The functionality for dragging elements is not functioning properly in JavaScript

Here is the code I used in an attempt to make a div element draggable: let div = document.querySelector('div') div.onmousedown = function() { div.addEventListener('mousemove', move, true) } window.onmouseup = function() { window. ...

What is the reason behind receiving email alerts whenever visitors access my website?

While developing a website, I ran some tests and noticed that whenever I visit the URL, an email notification is generated. However, the notification received is just a blank one. Could this issue be related to what I entered in the action field? <for ...

Contemplating the Sequence of DOM Execution

In the world of html/javascript, the order of execution is typically serial, meaning that the browser reads the code line by line and interprets it accordingly. This is a common practice in most programming languages. Some javascript programmers prefer to ...

Ways to repair an element

Can anyone help me troubleshoot the issue with my count text on a div block? It seems to be affected by changes in screen size. Normal https://i.stack.imgur.com/RZkgr.png On screen resize https://i.stack.imgur.com/hTdCG.png The Code: span.right-cor ...

Tips for centering a paragraph vertically within a div element

I'm struggling to achieve automatic vertical alignment for this. While I can manually center it using padding-bottom, I prefer a way to position it vertically on its own. How can I accomplish this while retaining the display: inline-block feature? &l ...

How to play audio with a source path that includes the special character "%" in JavaScript

Having an issue with playing an audio file that has '%' in its name. I've tried using the tag <audio src="test%320.mp3" controls></audio>, but it seems like the file is not being found when I try to play it. Can anyone ...

What is the best way to implement CSS styles from a parent component?

Within a React component using Emotion, we have a component called OtherComponent: OtherComponent: ... return <div css={otherComponentStyles}> <div className='something'> </div> </div> There is also another comp ...

Loads a PHP file automatically using the URL from a jQuery Ajax method

Using the jquery ajax method, I am trying to populate a dropdown menu with a set of option values from my title.php file. However, upon loading the page, in the Chrome Android browser, it redirects to the title.php. Does anyone have any insights into what ...