Ways to align the `<ol>` number at the top vertically

I'm having trouble aligning my <ol> numbers vertically at the top of the list. I've created a sample on CodePen.

ol {
  list-style-type: decimal-leading-zero;
  font-size: 11px;
}

a {
  font-size: 80px;
  text-decoration: none;
}
<nav>
  <ol>
    <li><a href="#">Header One</a><span>This is some text, don't move me</span></li>
    <li><a href="#">Header Two</a><span>This is some text, don't move me</span></li>
    <li><a href="#">Header Three</a><span>This is some text, don't move me</span></li>
    <li><a href="#">Header Four</a><span>This is some text, don't move me</span></li>
  </ol>
</nav>

Answer №1

To create a custom counter, you can utilize the counter-reset and counter-increment CSS properties. To display the counter, use the before pseudo-element of each li element. You can align the counter to the top of the text by using vertical-align: top and adjusting the line-height.

Check out this demo:

ol {
  list-style: none;
  counter-reset: items;
  font-size: 11px;
}

ol li {
  counter-increment: items;
}

ol li:before {
  vertical-align: top;
  line-height: 45px;
}

ol li:before {
  content: "0" counter(items) ". ";
}

ol li:nth-child(n+10):before {
  content: counter(items) ". ";
}

a {
  font-size: 80px;
  text-decoration: none;
}
<nav>
  <ol>
    <li><a href="#">Header One</a><span>This is some text, don't move me</span></li>
    <li><a href="#">Header Two</a><span>This is some text, don't move me</span></li>
    <li><a href="#">Header Three</a><span>This is some text, don't move me</span></li>
    <li><a href="#">Header Four</a><span>This is some text, don't move me</span></li>
  </ol>
</nav>

Answer №2

To resolve the issue with the tall anchor element, you can utilize vertical-align:top on the anchor tag. This adjustment should work well. Additionally, you may need to modify the line-height for the <a> tag and specify both line-height and margin for the <li> to ensure it appears as desired. If your goal is to align the <span>, consider adding vertical-align:bottom. After reviewing your CodePen example, I believe these changes will achieve the desired effect. Feel free to check out my version on CodePen.

ol {
  list-style-type: decimal-leading-zero;
  font-size: 11px;
  width: 300px;
}

li {
  margin: .6em 0;
  line-height: 1.6;
}

a {
  font-size: 80px;
  text-decoration: none;
  vertical-align: top;
  line-height: .8;
}

span {
  vertical-align: bottom;
}
<nav>
  <ol>
    <li><a href="#">Header One</a><span>This is some text, don't move me. Also, let's see how this looks if the line is long and there is more than one. And what if there's more text outside the span tag? </span> And what if there's more text outside the
      span tag? In that case, just make sure that the span and the li tag have the same line height.</li>
    <li><a href="#">Header Two</a><span>This is some text, don't move me</span></li>
    <li><a href="#">Header Three</a><span>This is some text, don't move me</span></li>
    <li><a href="#">Header Four</a><span>This is some text, don't move me</span></li>
  </ol>
</nav>

Answer №3

When floating blocks, their height is essentially ignored when it comes to line numbers. By wrapping the content of the <li> in a <div> and floating it to the left, the line numbers will appear above the div.

To ensure that the floated div takes up space and the items below appear below it, a clearfix can be used.

ol {
  list-style-type: decimal-leading-zero;
  font-size: 11px;
}

a {
  font-size: 80px;
  text-decoration: none;
}

.listContent {
  float: left;
}

/** Clearfix **/
li {
  content: "";
  clear: both;
}
<nav>
  <ol>    
    <li><div class="listContent"><a href="#">Header Two</a><span>This is some text, don't move me</span></div></li>
    <li><div class="listContent"><a href="#">Header Three</a><span>This is some text, don't move me</span></div></li>
    <li><div class="listContent"><a href="#">Header Four</a><span>This is some text, don't move me</span></div></li>
  </ol>
</nav>


In the near future, the ::marker pseudo-element may allow for specific styling of list markers. For example:

li::marker {
    vertical-align: top;
}

However, this feature is still in its early stages of browser support as of now. You can check the support matrix here.

Answer №4

In order to properly align the ::marker within the li element, it is necessary to align the element inside the li.

li {
    width: 350px;
}

div {
    display: inline-block;
    vertical-align: top;
}
<ol>
    <li>
        <div>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit,
            sed do eiusmod tempor incididunt ut labore et dolore magna
            aliqua...
        </div>
    </li>
</ol>

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

How can I show the configuration in Laravel?

After updating my pages to utilize an extended header for consistent content across all pages, I encountered an issue with the footer configuration. Whenever I attempt to retrieve a configuration from Laravel, it appears as normal text instead of being pro ...

Create a CSS path that connects two points by drawing a line

My image is currently animating along a path, but I want to make the path itself visible. Here's the CSS code: div { width:10px; height:10px; background:red; position:relative; -webkit-animation-name:Player1; -webkit-animatio ...

When making a POST request using formData through AJAX, the $_POST variable remains empty

Below is the form: <form id="complaintForm" enctype="multipart/form-data" method="POST"> <input type="number" id="isComplaintForm" value="1" hidden> <label for="fname&q ...

Issue with Bootstrap 4 Multi Select Functionality in Modal Not Functioning

Having trouble getting a bootstrap multi-select dropdown to work inside a Bootstrap modal. When the modal opens, the following CSS is affecting the dropdown: select.bs-select-hidden, .bootstrap-select > select.bs-select-hidden, select.selectpicker { ...

Efficiently formatting text in a div container

How can I ensure that the text within a span is word-wrapped inside a div? https://i.sstatic.net/W691d.png Here is the code snippet: <div class="col-xs-6 choice btn btn-default" > <span class="pull-left">(Kepala) Bagian Purchasing (not lis ...

Looking to retrieve just your Twitter follower count using JavaScript and the Twitter API V1.1?

I am trying to retrieve my Twitter follower count using JavaScript/jQuery in the script.js file and then passing that value to the index.html file for display on a local HTML web page. I will not be hosting these files online. I have spent weeks searching ...

Utilize a dual-color gradient effect on separate words within the <li> element

I am attempting to display the fizz buzz function in an unordered list, with each word being a different color ('fizz'-- green, 'buzz'--blue) as shown here: https://i.sstatic.net/Yvdal.jpg I have successfully displayed "fizz" and "buz ...

Creating a Cancel Button in JSP/HTML/JS/CSS Form: A Step-by-Step Guide

It is necessary to incorporate the functionality of "save" and "cancel" in the JSP code for this particular form. By selecting "save", the data entered into the form will be submitted to its intended destination. Alternatively, choosing "cancel" will dismi ...

Utilize Xpath to transition between different html tags seamlessly

Here is the HTML snippet I am working with: <pre> <span class="cm-string">"userId"</span> ":" <span class="cm-string">"abc"</span> "," </pre> My goal is to extract the text "abc" from the "userId" tag, which changes fr ...

Why is it that the edit or delete button is not functioning when I attempt to click on the next page?

Having an issue with my script. It works fine for editing and deleting on the first page, but when I navigate to the next page, those functionalities stop working. Can anyone help me troubleshoot this problem? <script> $(function(){ $(&ap ...

Combine going to an anchor, performing an AJAX request, and opening a jQuery-UI accordion tab all within a

My goal is to have the user click on the hyperlink below, which will (1) anchor to #suggestions, (2) navigate to the url suggestions.php?appid=70&commentid=25961, and (3) open the jquery-ui accordion #suggestions. However, I am facing an issue where c ...

Embedded HTML code within a table cell

How can I dynamically build an HTML string and display it inside a cell in reactJS' ag grid? Here's my example: function generateHtmlString(props) { let myHtmlString = "<span>a</span>"; myHtmlString += "--"; myHtmlString += "&l ...

Change the position of the specified footer on the one-page website

I am currently in the process of creating a single-page layout website. What I want is to have the elements with the class "footer-bar" positioned absolutely on each page, but on the #Home Page, it should be position relatively. Does anyone have any sugge ...

Utilizing next/image as a backgroundImage in a div container

Currently, I am working with nextjs and I am trying to set a background Image for a specific div using next/image. Most of the sources I found only explain how to implement full screen background images with next/image, not for a single div. I stumbled upo ...

block height has become a constant challenge for me - despite my various attempts, I still cannot seem

Struggling with adjusting the height of my posts. Any help would be appreciated! For more details, please visit: <li> <div class="thumb-img"> <a title="Hiring a Professional Designer for Your Kitchen" href="http://www.mulberrydesignerkitc ...

I am experiencing an issue with my localhost website where the images only display after I open the files in VScode. Is there a way to load the images correctly using app.js?

app.js While working on web development in VScode, I've encountered an issue where the images on my localhost website only appear after opening files like home.pug and contact.pug alongside app.js. Is there a way to make the images load properly witho ...

The fading effects are not functioning as expected in the following code

Hey there, I'm not the most tech-savvy person, but I managed to come up with this code for page redirection. Unfortunately, I couldn't quite get the fade out and fade in effects to work properly when executing it. If anyone out there can help me ...

Showing the URL beyond the search bar: A Guide using PHP, JavaScript, and HTML

How can I display the URL link outside the search box instead of opening a new page with the search result? I want to show the full URL (https://www.php.net.) below the search box, not within the search results. I only want to see the URL, not the contents ...

Adjusting Column Placement in Bootstrap Grid System

I am struggling to achieve a specific bootstrap grid system and could use some guidance. Here is the grid system I am trying to create: https://i.sstatic.net/fhEHU.jpg I have attempted to implement the grid system using the code provided in this JSFiddl ...

Can you explain the distinction between using a period in a class selector and not using one?

Could someone please clarify the meaning of the code snippet below? <div class="cls1 cls2">abcd <div class="cls2"> adfffff </div> </div> .cls1 { background-color: yellow; } /*sample1 .cls1.cls2 { color: red; } */ /* ...