What is the best way to include a colored square before multiple items on a list?

Is there a way to customize list items with colored squares before the text?

I've been searching for a solution, but haven't found it yet:

http://www.w3schools.com/cssref/pr_list-style-type.asp

Although they don't necessarily need to be list items, it seemed convenient as I want to style and display them in line, not on separate lines like in the image above.

I've attempted the following code:

<ul class="ordering">
<li><div></div><span>One</span></li>
<li><div></div><span>Two</span></li>
<li><div></div><span>Three</span></li>
</ul>

along with this CSS:

.ordering {
    list-style:none;
    min-width:250px;
}
.ordering div{
         width: 12px; height: 12px; float: left;
    }
.ordering span{
        display: block;
        float: left;
        font-size: 11px;
        line-height: 11px;
        padding-left: 5px;
    }

The issue I'm facing is that I can't separate them (they're stuck together), and when I have many items that exceed the size of the ordering class, they break onto a new line while the square remains on the previous line, making the square + text appear disjointed.

Answer №1

You're definitely on the right track. One suggestion is to simplify the HTML structure and utilize the nth-child() property.

HTML

<ul class="sortable">
<li><span></span>One</li>
<li><span></span>Two</li>
<li><span></span>Three</li>
</ul>

CSS

ul.sortable {
    list-style: none;
    min-width: 250px;
}
ul.sortable li {
    display: block;
    clear: left;
    vertical-align: middle;
    font-size: 20px;
    line-height: 20px;
    padding-left: 5px;
}
ul.sortable li span {
    width: 12px;
    height: 12px;
    background: red;
    display: inline-block;
    margin-right: 6px;
}
ul.sortable li:nth-child(2) span {
    background: green !important;
}
ul.sortable li:nth-child(3) span {
    background: blue !important;
}

VIEW DEMO

Answer №2

There is no need to use the float property in this scenario:

.arrangement div {
     width: 12px; height: 12px; display: inline-block;
}

.arrangement span{
    display: inline-block;
    font-size: 11px;
    line-height: 11px;
    margin-left: 5px;
}

(In case you require compatibility with IE 7 and earlier versions, consider changing the <div> to a <span>, or another inline element) for example:

.arrangement span.coloredSquare {
     width: 12px; height: 12px; display: inline-block;
}

Answer №3

One issue I'm facing is the inability to separate glued together items, with the problem that when there are multiple items in a size-constrained ordering class, they break into new lines but the square remains on the previous line, causing the square and text to not act as one unit.

To resolve this spacing issue and ensure the square and text move together as a "block," simply assign your li elements a display property of display: inline-block.

.ordering li
{
    display: inline-block;
}

Answer №4

Enhance your markup effortlessly using pseudo elements.

HTML (remember: only one child element per list item)

<ul>
  <li><p>Bender</p></li>
  <li><p>Fry</p></li>
  <li><p>Leela</p></li>
</ul>


CSS

ul { margin: 10px; }
li { position: relative; }

p:before {
  background: #bb3333;
  content: ".";
  display: inline-block;
  height: 10px;
  margin: 0 5px 0 0;
  text-indent: -10000px;
  width: 10px;
}

li:nth-child(2) p:before { background: #33bb33; }
li:nth-child(3) p:before { background: #3333bb; }

Check out my fiddle here: http://jsfiddle.net/uNR2M/2/

Instead of using nth-child, I suggest applying classes for better performance optimization.

Answer №5

Here's a simple solution that might help:

<li>
    <div></div><span>One</span>
    <div class="clearfix"></div>
</li>

Make sure to include the clearfix class before closing the li tag.

.clearfix {
    clear: both;
}

Answer №6

If you want to have stylish bullet points for your list, it's best not to use the native list styles that are hard to customize. Instead, you can opt for nested divs and spans, but a cleaner approach would be styling the :before pseudo-element. Here's an example:

Check out the demo here.

This is how you can structure your HTML:

<ul>
    <li>item</li>
    <li class="red">item</li>
    <li class="blue">item</li>
</ul>

And here's the corresponding CSS:

li { 
    position: relative; 
    list-style: none; 
    display: inline-block; 
    margin-left: 30px;
}

li:before {
    content: ' ';
    width: 10px;
    height: 10px;
    position: absolute;
    left: -20px;
    top: 5px;
    background: green;
}

.red:before { background: red; }

.blue:before { background: blue; }

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

Text off-center in Bootstrap Badge

I'm struggling to find a way to center my text with the icon properly. This issue is only happening in Google Chrome. You can view the problem in action on this code snippet. @import url('https://fonts.googleapis.com/icon?family=Material+Icons ...

Loop through each JSON object and insert it into an HTML element

I am working on looping through a JSON object and converting it into HTML. While I can iterate through all the objects in the JSON, I am having trouble extracting just the first object. var res = '[{"ID":"246","mobile":"samsung","feedback":"feedback ...

The sidebar remains fixed in height and does not expand vertically

Need Help: My sidebar won't expand in height when I update content. Is there a way to make it adjust using just HTML and CSS? html code: <div id="main"> <section> More text goes here... </section> <div id="sideb ...

Ways to expand the width of a div on hover - Angular navbar dropdown utilizing Bootstrap 4

I am attempting to make the div full width while also implementing a slideDown/Up effect. I am struggling to identify the specific CSS needed to adjust the width accordingly. Currently, this is what I have: I can see that the class open is dynamically ad ...

Unable to collapse the bootstrap navbar on a targeted screen size

My intention was to collapse the Bootstrap navbar when the screen width reaches 1280px. However, instead of collapsing, the navbar appears at the top. Here is the code snippet I am using: body { padding-top: 90px; ...

CSS code influencing unintended elements

www.geo-village.com <- Live Example After setting up my main webpage with CSS and PHP files, I noticed an issue with the navigation bar. Despite having a separate PHP and CSS file for the navigation bar items, the Login and Register buttons on the main ...

Navigate down the page until you reach the section that is set to display as a block

Is it possible to make the page automatically scroll to a specific element located in the middle of the page when changing its display property from none to block? ...

HTML does not occupy the entire width of the page

I'm struggling with an issue on my website where the <html> element doesn't span full width on mobile devices. Currently, I am using Bootstrap and Jquery for my website. If you have any insights on what may be causing this problem, please ...

What is the best way to align a modal with a layout when it appears far down the components hierarchy?

Struggling with creating a React modal and facing some issues. Let's consider the structure below: React structure <ComponentUsingModal> <Left> <ButtonToActivateModal> ... </ButtonToActivateModa ...

The multi-level dropdown feature in the BootStrap-4 navbar extends off the screen when the dropdown menu is opened

I have been struggling to make this work. I used BS4 for responsiveness, and my navigation items are aligned to the right in the navbar. Despite trying various methods to position the submenu on the left of the parent menu, I still can't get it to dis ...

What is the best way to transfer data from an HTML input field to a PHP script?

Within my patient_display.php file, I am calling the patient_history.php script and passing along the P_ID. Here is an outline of my code. patient_display.php: echo '<form name="Patient" action="patient_history_display.php" method="get">'; ...

How to customize Material UI Autocomplete options background color

Is there a way to change the background color of the dropdown options in my Material UI Autocomplete component? I've checked out some resources, but they only explain how to use the renderOption prop to modify the option text itself, resulting in a a ...

Having trouble understanding the differences between Bootstrap 4's .list-inline class and .list-inline-item class?

I am currently utilizing Bootstrap 4 within Wordpress. Strangely, I am facing an issue where I am unable to have list items appear inline (horizontally) solely by using the class .list-inline on the list itself, as shown below: <ul id="dances" class="l ...

Creating a seamless video texture loop using PixiJS

I have successfully loaded a video texture and rendered it using pixi. // initialize a new pixi stage var stage = new PIXI.Stage(0x66FF99); // create a renderer instance var renderer = PIXI.autoDetectRenderer(window.innerWidth, window.inner ...

Trouble experienced with the window.open() function on Safari

When using Safari, it can sometimes block the opening of a new tab through the window.open() function during an ajax call. To bypass this blocking, we must first call window.open() to open a new tab before making the ajax call. Refer to this Stack Overflow ...

The function iframe.scrollTo() is not effective for scrolling through Excel or PowerPoint documents on an iPad

I am trying to create a custom scrolling feature for iframe content specifically for iPad. Currently, I have set up my iframe like this: <div id="scroller" style="height: 300px; width: 100%; overflow: auto;"> <iframe height="100%" id="iframe" ...

Automatically adjust multiple images on an HTML webpage

Just starting out with html programming here. Looking to add top margin using a span class, any tips on how to tackle this issue? ...

Utilizing Angular for Webcam Integration

After trying out this code snippet: <video autoplay playsinline style="width: 100vw; height: 100vh;"></video> <script> navigator.mediaDevices.getUserMedia({ video: { facingMode: 'user' } }) .then(stream =&g ...

The flex-basis property seems to be malfunctioning as the image suddenly vanishes when

While my question may seem similar to others, the issue at hand is actually quite different. Here's how it appears in Chrome https://i.sstatic.net/tngvr.jpg Safari https://i.sstatic.net/eE74k.png In Safari, you'll notice that the map disappear ...

What could be causing the bootstrap file to fail loading after adding a double mapping in the href attribute?

<!-- This code is functioning correctly and loading all bootstrap files --> <li class="nav-item" id="products"> <a class="nav-link" href="/productlist">Products</a> </li> <!-- The issue arises when ...