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

Creating Flexible Divs with Gradient Background for Older Versions of Internet Explorer - IE8 and IE7

I'm attempting to achieve the following. https://i.stack.imgur.com/YYOZv.jpg The issue I am encountering is that the elements in row1 have a different gradient background compared to those in row2. Additionally, row1 is populated dynamically with c ...

Can HTML/CSS be used to specifically target handheld mobile devices?

I am looking to optimize my video display in HTML by only showing it on desktop browsers. The difference in bandwidth between desktop and mobile devices is affecting the performance of mobile browsers, so I want to target only desktop users. Is there a way ...

Is there a way to insert a record upon the user clicking on the Add Record button?

// Here is my Component code // I want to figure out how to add a new row to the table with fresh values. import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-uom', templateUrl: './uom.component.html ...

Can I use a custom font in an HTML5 canvas?

Has anyone had success importing a custom font for use in HTML5 canvas? I've been trying to do this by loading the font file on my computer, but all my attempts have failed so far. The canvas keeps showing the default font instead of the one I want to ...

The thumb of the range input is misaligned with the axis markers

The handle, also known as the knob in this input[type=range] codepen, is not aligning correctly with the ticks. It appears to be off by varying amounts (either left or right) depending on the value. Move the handle to see for yourself. What CSS properties ...

Ensure that a select input, dynamically generated, is marked as 'required' only when other inputs have been filled out

Displayed below is a snapshot of a page I'm developing that allows users to input details about various rooms in a property. Users have the ability to 'add' multiple rooms, triggering javascript/jQuery to dynamically create additional input ...

Steps to decode a data URL into an image and render it on a canvas

With 3 canvases at my disposal, I decided to get creative. First, I cropped a region from canvas1 and showcased it on canvas2. Then, I challenged myself to convert the image in canvas2 into a URL and then reverse the process back to an image, all to be d ...

The select box in CSS is optimized for Chrome but poorly displayed in Firefox

Struggling with styling the select box for both webkit and moz browsers. It looks great in Chrome, but terrible in Firefox - the default option is not vertically aligned with the select box. Can someone help me pinpoint the issue in this code? CSS: sele ...

Display a particular div upon clicking a specific link, while concealing the other divs

As a beginner in the world of jQuery and coding, I'm encountering some difficulties that I need help with. My goal is to have the 'Vlogging' link activated and show 'Details 1' when the page loads. Then, upon clicking on either &a ...

"PHP Dilemma: Navigating the Ajax Button Press Situation

I've been attempting to create a button that triggers a PHP script on a webpage and updates only a specific div tag, but so far I haven't had any success. Removing the $ajax section of the script allows my buttons to change states, but as soon as ...

The issue with loading scripts in a ReactJS NextJS app is related to the inline condition not working

I'm having trouble with an inline condition for loading scripts. The condition seems to be working because the tag is displaying text, but when it comes to scripts, it doesn't work. How can I resolve this issue? const cookie = new Cookies().get ...

Adjusting the input label to be aligned inside the input box and positioned to the right side

I am currently working on aligning my input label inside the input box and positioning it to float right. The input boxes I am using have been extended from b4. Currently, this is how it appears (see arrow for desired alignment): https://i.stack.imgur.co ...

Checkbox: Customize the appearance of the root element

I am trying to modify the root styles of a Checkbox component, but it is not working as expected. Here is my code: <CheckboxItem onChange={()} checked={isChecked} label="Show Checkb ...

Leverage the calc() function within the makeStyles method

const useStyles = makeStyles((theme) => ({ dialog: { '& .MuiTextField-root': { margin: theme.spacing(1), } }, address: { width:"calc(24vw + 8px)" }, })); <div> <TextField id="contact ...

What is the best way to pass an array to a child component in React?

I am having an issue where only the first element of inputArrival and inputBurst is being sent to the child component Barchart.js, instead of all elements. My goal is for the data to be instantly reflected as it is entered into Entrytable.js. EntryTable.js ...

What is the best way to display text from a header box across multiple line boxes in real time on an HTML page?

Looking to enhance an HTML layout with a header box and line comment boxes. <textarea name="order[header_comments]"></textarea> The line comment boxes are structured as follows: <textarea name="line_comments[0][line_comments]"></tex ...

Issues with the functionality of customisable list items in a list

I am attempting to customize the appearance of specific list items within a ul element. One li element should be displayed in red font, while another li element should have a hyperlink styled with a different color and an underline rollover effect. The thi ...

The font fails to load

I've hit a roadblock with this issue. I'm attempting to add a custom font to my project. The CSS file can be found in the directory project/css/. The font is located at project/fonts/iconfont/. In that folder, I have the following font files (alt ...

Altering the ASP DropDownList's background color solely with CSS modifications

Is there a way to change the background colors of dropdownlists in my C# web app using CSS instead of specifying each one individually? In simpler terms, I'm trying to avoid having to write out code like this for multiple dropdowns: private void For ...

Footer refuses to stay anchored at the bottom of the page

I'm struggling to keep my footer at the bottom of all pages on my blog. I am facing two main issues. If I use position:absolute, the footer ends up in the middle of the main blog page. Alternatively, when I don't use it, the footer sticks to the ...