Tips for aligning numbers in a monospaced ordered list

Is there a way to align the numbers in a numbered list within a table using monospace font so that they are at the same level as bullets on a bullet list?

Simplest example:

ol {
  font-family: monospace;
}

ul,
ol {
  margin-top: 10px;
  margin-bottom: 0;
  padding: 0 0 0 1.4em;
  font-size: 15px;
  line-height: 24px;
  list-style: none;
}

ol {
  list-style-type: decimal;
}

ul {
  list-style: disc;
}

table {
  font-family: monospace;
  width: 100%;
  table-layout: fixed;
  border-spacing: 0;
  border-collapse: collapse;
  td {
    position: relative;
    min-width: 137px;
    padding: 7px 6px;
    border: 1px solid #ccc;
    line-height: 20px;
  }
}
<div contenteditable="true">
  <ol>
    <li>Hello</li>
    <li>World</li>
  </ol>

  <ul>
    <li>Hello</li>
    <li>World</li>
  </ul>

  <table>
    <tr>
      <td>
        <ol>
          <li>Hello</li>
          <li>World</li>
        </ol>

        <ul>
          <li>Hello</li>
          <li>World</li>
        </ul>
      </td>
      <td>Another cell</td>
    </tr>
  </table>
</div>

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

Answer №1

We have the ability to craft a personalized counter-style that eliminates the trailing space found in the default counter-style: numeric.

ul,
ol {
  margin-top: 10px;
  margin-bottom: 0;
  padding: 0 0 0 1.4em;
  font-size: 15px;
  line-height: 24px;
  list-style: none;
}

ol {
  list-style-type: custom-style;
}

ul {
  list-style: disc;
}

table {
  font-family: monospace;
  width: 100%;
  table-layout: fixed;
  border-spacing: 0;
  border-collapse: collapse;
  td {
    position: relative;
    min-width: 137px;
    padding: 7px 6px;
    border: 1px solid #ccc;
    line-height: 20px;
  }
}

@counter-style custom-style {
  /* These lines essentially mimic the `counter-style: numeric` */
  system: numeric;
  symbols: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9";

  /* This comes after the number */
  suffix: ".";
  /* The default `numeric` style is like */
  /* suffix: ". "; */
}
<div contenteditable="true">
  <table>
    <tr>
      <td>
        <ol>
          <li>Hello</li>
          <li>World</li>
        </ol>

        <ul>
          <li>Hello</li>
          <li>World</li>
        </ul>
      </td>
      <td>Another cell</td>
    </tr>
  </table>
</div>

Other options for slightly varied outcomes:

Option 1: Append spacing to the right of the bullet manually

The concept is to ensure all list styles possess equal prefix lengths, which will align them when using a monospaced font.

ul,
ol {
  margin-top: 10px;
  margin-bottom: 0;
  padding: 0 0 0 1.4em;
  font-size: 15px;
  line-height: 24px;
  list-style: none;
}

ol {
  list-style-type: decimal;
}

ul {
  list-style: custom-style;
}

table {
  font-family: monospace;
  width: 100%;
  table-layout: fixed;
  border-spacing: 0;
  border-collapse: collapse;
  td {
    position: relative;
    min-width: 137px;
    padding: 7px 6px;
    border: 1px solid #ccc;
    line-height: 20px;
  }
}

@counter-style custom-style {
  system: cyclic;
  symbols: "•";
  suffix: "  ";
}
<div contenteditable="true">
  <table>
    <tr>
      <td>
        <ol>
          <li>Hello</li>
          <li>World</li>
        </ol>

        <ul>
          <li>Hello</li>
          <li>World</li>
        </ul>
      </td>
      <td>Another cell</td>
    </tr>
  </table>
</div>

Option 2: Only apply monospacing to the text (bypassing the bullets)

This approach ensures the list-style (::marker) sections of the elements appear the same as they did in the initial non-monospaced example.

ul,
ol {
  margin-top: 10px;
  margin-bottom: 0;
  padding: 0 0 0 1.4em;
  font-size: 15px;
  line-height: 24px;
  list-style: none;
}

ol {
  list-style-type: decimal;
}

ul {
  list-style-type: disc;
}

table {
  width: 100%;
  table-layout: fixed;
  border-spacing: 0;
  border-collapse: collapse;
  td {
    position: relative;
    min-width: 137px;
    padding: 7px 6px;
    border: 1px solid #ccc;
    line-height: 20px;
  }
}

span {
    font-family: monospace;
}
<div contenteditable="true">
  <table>
    <tr>
      <td>
        <ol>
          <li><span>Hello</span></li>
          <li><span>World</span></li>
        </ol>

        <ul>
          <li><span>Hello</span></li>
          <li><span>World</span></li>
        </ul>
      </td>
      <td>Another cell</td>
    </tr>
  </table>
</div>

Option 3: Apply list-style-position: inside

This aligns the beginnings but not the actual text itself.

ul,
ol {
  margin-top: 10px;
  margin-bottom: 0;
  padding: 0 0 0 1.4em;
  font-size: 15px;
  line-height: 24px;
  list-style: none;
  list-style-position: inside;
}

ol {
  list-style-type: decimal;
}

ul {
  list-style-type: disc;
}

table {
  font-family: monospace;
  width: 100%;
  table-layout: fixed;
  border-spacing: 0;
  border-collapse: collapse;
  td {
    position: relative;
    min-width: 137px;
    padding: 7px 6px;
    border: 1px solid #ccc;
    line-height: 20px;
  }
}
<div contenteditable="true">
  <table>
    <tr>
      <td>
        <ol>
          <li>Hello</li>
          <li>World</li>
        </ol>

        <ul>
          <li>Hello</li>
          <li>World</li>
        </ul>
      </td>
      <td>Another cell</td>
    </tr>
  </table>
</div>

Answer №2

Here is a CSS snippet you can try:

ol > li::marker {
  white-space: inherit;
}

ul,
ol {
  margin-top: 10px;
  margin-bottom: 0;
  padding: 0 0 0 1.4em;
  font-size: 15px;
  line-height: 24px;
  list-style: none;
}

ol {
  list-style-type: decimal;
}

ol > li::marker {
  white-space: inherit;
}

ul {
  list-style-type: disc;
}

table {
  font-family: monospace;
  width: 100%;
  table-layout: fixed;
  border-spacing: 0;
  border-collapse: collapse;
}

td {
  position: relative;
  min-width: 137px;
  padding: 7px 6px;
  border: 1px solid #ccc;
  line-height: 20px;
}
<div contenteditable="true">
  <table>
    <tr>
      <td>
        <ol>
          <li>Hello</li>
          <li>World</li>
        </ol>

        <ul>
          <li>Hello</li>
          <li>World</li>
        </ul>
      </td>
      <td>Another cell</td>
    </tr>
  </table>
</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

Securing Your WordPress Custom Form with Captcha

I have a unique form on my WordPress website that tracks IP addresses, checks referer pages, and validates email addresses. Despite these measures, I am still receiving spam subscriptions. I have tried implementing both Bestwebsoft CAPTCHA and really sim ...

What issues could arise from utilizing PHP for generating variables within my CSS file?

One major limitation of CSS is the absence of variables. It would be beneficial to have the ability to use variables for controlling things like the location of imported CSS and color schemes within a design. An alternative solution could involve using a ...

What is the proper way to add comments within CSS code inline?

Can anyone provide instructions on how to comment an inline css property, such as height, in the following code snippet? <div style="width:100%; height:30%;"> Any help is appreciated. Thank you! ...

Java servlet is unable to interpret the name of an html select tag

Currently, I am implementing a feature where table rows with select boxes are added dynamically and the values of these select boxes are retrieved in a servlet. The process of adding the select boxes is functioning properly; however, when attempting to ret ...

Is there a way to adjust the spacing between a specific background image in a design pattern? The image must be sourced online

I am currently working on creating a background pattern using only online images, and I need to adjust the spacing between them. Is there a way to specifically control the space between the images in the pattern using CSS? I know about the round and space ...

What is the method for setting the doctype to HTML in JavaScript or React?

I created a canvas with a height equal to window.innerHeight, but unexpectedly it seems to have 100% screen height plus an extra 4 pixels coming from somewhere. I came across a solution suggesting that I need to declare doctype html, but I'm unsure ho ...

Other elements are unable to conceal Material UI InputBase

Displayed below is a navigation bar that sticks to the top, followed by rows of InputBase components from material-ui. Despite setting the background color of the nav bar to white, the input always appears above the nav. This strange behavior stopped when ...

I am having trouble getting the border-radius to display properly in my email table

I'm in the process of designing a responsive email template. For the white content area, I've applied border-radius: 5px;. While it's working fine on the bottom corners of the table, the top corners don't seem to display the border-rad ...

Tips for positioning content next to a sidebar using basic CSS

I'm having an issue where my content is being hidden behind the sidebar and I want the sidebar to stay fixed on the left side. When I changed position:fixed to float:left in the CSS, it gave me the desired result but the sidebar isn't fixed when ...

Using CSS to align the position of a div with the last word position of an h4 element

Trying to figure out how to position a div at the end of an h4 text element has been challenging. When the h4 text is on a single line, it's easy to place the div correctly. However, things get complicated when the text spans multiple lines. In that c ...

Determine the number of visible li elements using jQuery

Currently, I am utilizing a jQuery script to count the number of li elements in my HTML code. Here is an example of the setup: HTML: <ul class="relatedelements"> <li style="display:none;" class="1">anything</li> <li style="disp ...

I am encountering an issue with the jquery.min.js file not loading when I try to utilize the Google CDN for jQuery

Currently in the process of learning about jQuery and testing its functionality, however, I am facing an issue where changes are not being reflected. The error message states that it is unable to load. I have confirmed that I am using Google CDN and also h ...

Encountered a runtime error while trying to insert a list item <li> into a paragraph <p> element

Take a look at this code snippet: <%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication18._Default" %> <!DOCTYPE html> <html> <body> <p id="someId"></p& ...

Is it possible for me to designate a specific class for the rev value?

<a href="img1.jpg" rel="zoom-id:bike" rev="img1.jpg"> <img src="" class="img-thumb"/></a> Shown above is a snippet of HTML code. I wonder if it's possible for the attribute rev="img1.jpg" to have a class that only affects the s ...

issue with arranging content in a two-column layout with headers

Currently working on my first website and running into some CSS issues. I'm aiming for a two-column + header layout that fills the entire screen space of the website. I have the following specifications in mind: Header takes up 20% of the screen he ...

How can we use JavaScript to add a class to the <html> element?

What is the method to apply a class to the root element <html> in JavaScript? ...

Placing a div with absolute positioning inside another div with absolute positioning raises the question of how it functions when the outer div is not set to position: relative

Trying to grasp the concept, I experimented with the following setup: <div id="Outer"> <div id="Inner"></div> </div> In this scenario, the outer div has a relative position and the inner div has an absolute position. This allo ...

Enhance the aesthetic appeal of the Search and Filters component in ASP.NET Core using Bootstrap styling

I am struggling to enhance the style and layout of multiple search filters on the page so that the User Experience is improved. I'm unsure how to effectively design this search component. <section class="search-sec"> <div class=&qu ...

What is the best way to implement JavaScript for loading and removing content based on button clicks on a webpage?

I'm in need of a vanilla JavaScript solution (I know JQuery options exist, but I want to stick to vanilla JS for now)? Currently, I am using a simple page as a testing ground for my ongoing project. The page consists of two buttons that load HTML pag ...

Using Selenium to scroll down to an element until its 'style' changes

I'm in the process of scraping a review page similar to this one. While this specific page has only a few reviews, there are others with a larger volume that require extensive scrolling. Upon observation, I noticed that when the page is not complete ...