Steps to correct alignment in a numbered list when the dot is removed using CSS

Currently, I'm working on developing a rule page for a game. The specific layout requirements dictate that the rules be presented in an ordered list without periods following each number:

1 Rule one
2 Rule two
3 Rule three

To achieve this format, I utilized the following code snippet:

ol {
    list-style-type: none;
    margin-left: 0px;
  }
  
  ol > li {
    counter-increment: customlistcounter;
  }
  
  ol > li:before {
    content: counter(customlistcounter) " ";
    font-weight: bold;
    float: left;
    width: 3em;
  }
  
  ol:first-child {
    counter-reset: customlistcounter;
  }

An issue arises when the text of the rules extends to a second line, positioning the subsequent line directly below the number instead of aligning it with the beginning of the first line of text.

1 this is how I envision the formatting to display

2 this is how it currently appears
on the screen

In my attempt to rectify this matter, adjusting margins and padding proved ineffective, resulting in either overflow or negligible change.

If useful, here's a snippet of the relevant HTML:


<ol>
        <h2 class="uppercase title fs-500 text-mid">How to Play</h2>
          <li class="fs-400">
            Red makes the initial move in the inaugural game.
          </li>
          <li class="fs-400">
            Players take turns consecutively, dropping a single disc per turn. 
          </li>
          <li class="fs-400">
            The game concludes upon achieving a 4-in-a-row or reaching a stalemate.
          </li>
          <li class="fs-400">
            The initiator of the preceding game assumes the second position in the subsequent game.
          </li>
        </ol>

Answer №1

The @counter-style rule allows you to create custom counter styles that are not included in the default set.

You can customize the appearance of the numbers using the ::marker pseudo-element.

By the way, nesting an h2 element inside an ol element is invalid. To achieve the desired styling, you must adjust the margin and/or padding to match the padding of the ol.

@counter-style no-decimal-point {
  system: extends decimal;
  suffix: '';
}

::marker {
  font-weight: bold;
}

Ol {
  list-style: no-decimal-point;
}

li {
  padding-inline-start: 3em;
}
<h2>How to Play</h2>
<ol>
  <li>Red goes first in the first game.</li>
  <li>Players must alternate turns, and only one disc can be dropped in each turn.</li>
  <li>The game ends when there is a 4-in-a-row or a stalemate.</li>
  <li>The starter of the previous game goes second on the next game.</li>
</ol>

Answer №2

Give this a try:

 <ul>
    <h3 class="text-uppercase title fs-600 text-mid">Instructions</h3>
      <li class="fs-400" data-value="1">
        Start with red in the first round.
      </li>
      <li class="fs-400" data-value="2">
        Players must take turns and can only drop one disc per turn.
      </li>
      <li class="fs-400" data-value="3">
        The game ends when there is a 4-in-a-row or a tie.
      </li>
      <li class="fs-400" data-value="4">
        The player who started the previous game goes second in the next game.
      </li>
</ul>

CSS:

ul {
   list-style: none;
   margin-left: 0px;
}

ul > li::before {
   content: attr(data-value) ' ';
}

Note: :before is represented by two colons: ::before

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 consistent height for elements in different columns using Bootstrap 5

I'm struggling with keeping the ".item" elements equal height in different columns. Does anyone have a solution for this? <div class="container"> <div class="row"> <div class="col-8"> <h3& ...

Mobile values in tailwindcss take precedence over breakpoints values, overriding them

I have encountered a puzzling issue that seems to have no answers thus far. Whenever I set paddings or margins for mobile devices and attempt to adjust these values for larger screens, the original mobile base value stubbornly persists and overrides my sp ...

Learn how to implement the JQuery ReplaceWith function with the resources provided by materials.io icon

I would like the favorite_border icon to switch to the favorite icon when clicked. As we are using material.io and both icons have the class material-icons, I am unsure about how to implement this using jQuery. What steps should I take to achieve this? (w ...

Creating Dynamic Videos with PHP and HTML

Trying to display the user-submitted video, but all my attempts failed for some reason: 1. echo "<video width='500px'>"; echo "<source type='video/mp4' src=$var autoplay>"; echo "</video>"; echo "<video width=& ...

The Chrome application does not retain cookies

Why is the cookie url_user not being stored? Below is the content of index.html: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial- ...

jquery dialog box displaying a webpage

I am looking to implement a feature where, upon clicking the edit link in the last column of my dataTable, a separate page for editing should open within a jQuery dialog box. The goal is to seamlessly submit data while providing a user-friendly experienc ...

Developing an Easy-to-Use PDF Popup Interface

I need a simple PDF modal where I can input the text: 'The sky is blue' and have it link to a PDF that opens up when clicked. I found a similar sample online, but it includes an image plus an image modal that pops up. I want to replace the imag ...

Modifying an image's src using JavaScript is not possible

I'm attempting to modify the source of an image using a JavaScript function, but it doesn't seem to be working. The function is being executed within a mounted() method in Framework7. Here is my current setup: HTML: <div> <span> &l ...

Customize the CSS styling of third-party components in different pages using NextJS

When working with third-party components, you can include their styles by importing their stylesheet into your component or _app.tsx file. For detailed instructions on how to do this, you can refer to Next.js documentation. Another option is to add the sty ...

Review Limited Screen Size for a Smartphone Website

I have been working on optimizing a mobile website and I utilized CSS to adjust the layout design for screen widths between 150px and 480px. However, during testing on an actual phone, the desktop layout is appearing instead of the custom layout set with C ...

A bottom border that spans the entire width, complete with padding

I am attempting to create a uniform border for each item on my list. However, due to the nested structure of the list, I have used padding to achieve this. As a result, the appearance is as expected and behaves normally. Check out the JSFiddle Example C ...

Imitate a style using jQuery without deleting other elements

This is an example of HTML code: <div id="id1" style="width:100px;background: rgb(196, 14, 14);">1</div> <div id="id2" style="margin-top:10px">2</div> to <div id="id1" style=&qu ...

Ways to incorporate a dynamic value into a chart created with chart.js?

I want to create a doughnut chart representing the number of individuals who have tested positive for Coronavirus and the number of deaths related to it. How can I transfer the same data from the top container into the chart? The confirmedCases and deaths ...

Adaptive design exhibits varying appearances across various screen sizes

Currently, I am working on a responsive design WordPress site and here is the markup: <div class="contact-wrap"> <div class="contact-number">123-456-7890</div><!--.contact-number--> <div class="user-login"><a href="#"& ...

Troubleshooting Bootstrap select box design discrepancies

Currently revamping a website and encountered an unusual issue with select boxes. There seems to be an overlapping white space where the option values should be. Here's a visual reference: View Image of Select Box Issue Utilizing Bootstrap for the re ...

"Revolutionary AJAX-enabled PHP social commenting system with multi-form support

Why is it that when I submit forms using these ajax functions in PHP, they only send to the first form on the page? I have multiple forms under each article and I want them to be submitted separately. What am I doing wrong? ...

CSS Dynamix - Include the parameter ?value to resolve caching issues

I came across a discussion on the issue of Dynamic CSS caching problem where it was suggested to append ?value to the end of the css file name for better caching. I am currently using themes and the css files are loaded automatically. Is it possible to use ...

Guide to highlighting a particular letter in a string variable using JavaScript and AngularJS

Looking to highlight specific letters within a string variable. I have the string stored as an AngularJS variable in a table like this: <td>{{variable}}<td> In my JavaScript file, I am passing a variable for Angular that contains the string & ...

Achieving Left and Right Alignment of Navigation Elements using Flex Basis in Chakra UI with Next JS

For my project, I am working on creating a straightforward Navigation bar using Chakra UI and Next JS. The goal is to have an svg logo positioned at the top-left, while the menu and UI buttons are aligned to the top-right. However, I'm facing challeng ...

As I scroll, I hope the texts will stay fixed against the backdrop image

Is it possible to make the #home section fixed to the background while scrolling, including all the texts and buttons? I envision it like having texts embedded in an image. The text is located in the middle left of the page (let's keep this as default ...