What is the method to establish a fixed horizontal sizing for :before elements?

I am attempting to create a ul menu with a :before pseudo-element on li tags. The text inside the :before's content varies for each row. I am aiming to align this text to the right for each li, like so:

  pseudo_x li
 pseudo_xx li
pseudo_xxx li

li:nth-child(n):before {
  content: "===";
  border: 1px dotted red;
}

li:nth-child(2n):before {
  content: "-----------";
  border: 1px dotted red;
}
<ul>
  <li>First</li>
  <li>Second</li>
  <li>Third</li>
  <li>Fourth</li>
</ul>

*** Update

Thank you for your responses. However, I am unable to use list-style-type as I need to format the :before's content. Additionally, I cannot set one width for all contents. The reason I am detailing this is to utilize content for displaying the count of rows inside li tags, like this:

var rows = document.getElementsByTagName('li');
for(var i=0;i<rows.length;i++)
  rows[i].setAttribute("c", "rows: "+"1".repeat(Math.random()*10|0))
li{
    list-style-type: none;
}
li:before{
    color: white;
    content: attr(c);
}
.red:before{background:red}
.orange:before{background:orange}
.blue:before{background:blue}
.green:before{background:green}
<ul>
  <li class="red">First</li>
  <li class="orange">Second</li>
  <li class="blue">Third</li>
  <li class="green">Fourth</li>
</ul>

Answer №1

To implement this style, one approach is to utilize the CSS @counter-style at-rule:

/* Custom properties for styling the gutter */
:root {
  --counterGutter: 0.5em;
  --counterWidth: 4em;
}

@counter-style lines {
  system: cyclic;
  symbols: "===" "-----";
}

ul {
  counter-reset: liCounter;
  list-style-type: none;
}

li {
  margin-block: 0.5em;
  margin-inline-start: var(--counterWidth);
  position: relative; 
}

li::before {
  border: 1px dotted #f00;
  content: counter(liCounter, lines);
  counter-increment: liCounter;
  inline-size: var(--counterWidth);
  position: absolute;
  text-align: end;
  translate: calc(-1 * (var(--counterGutter) + 100%));
}
<ul>
  <li>First</li>
  <li>Second</li>
  <li>Third</li>
  <li>Fourth</li>
</ul>

JS Fiddle demo.

Further customization options can be explored based on specific needs and browser support.

For more details on this technique and related concepts, refer to the provided references.

Answer №2

If you adjust the list-style-type, it may not be possible to include a border around it

li:nth-child(n) {
  list-style-type: "===";
}

li:nth-child(2n) {
  list-style-type: "-----------";
}
<ul>
  <li>First</li>
  <li>Second</li>
  <li>Third</li>
  <li>Fourth</li>
</ul>

Answer №3

Consider using a table layout for better presentation

var rows = document.getElementsByTagName('li');
for (var i = 0; i < rows.length; i++)
  rows[i].setAttribute("c", "rows: " + "1".repeat(Math.random() * 10 | 0))
ul {
  display: table; /* apply table display */
}

li {
  list-style-type: none;
  display: table-row; /* set as table row */
}

li:before {
  color: white;
  content: attr(c);
  display: table-cell; /* specify as table cell */
  text-align: right;
  transform: translate(-5px); /* adjust the position */
}

.red:before{background:red}
.orange:before{background:orange}
.blue:before{background:blue}
.green:before{background:green}
<ul>
  <li class="red">First</li>
  <li class="orange">Second</li>
  <li class="blue">Third</li>
  <li class="green">Fourth</li>
</ul>

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

Adjustments in margins for printed HTML may vary from page to page in Internet Explorer versions 8 to 11

One challenge I'm facing is with a user-generated report page that is typically printed. There seems to be an unusual bug occurring specifically in Internet Explorer. The layout of the page consists of tables within various divs, all aligned perfectly ...

How can I show/hide a div based on checkbox click on my website? It works in jsFiddle, but not on my actual site. Any suggestions?

Is there a way to show or hide a div based on a checkbox click? I've got it working in jsFiddle, but for some reason, it's not functioning properly on my website. Any thoughts on how to fix this? My goal is to offer multiple payment methods (cre ...

How come the max-width property is not functioning properly on tables in Internet Explorer 7?

Is there a reason why the "max-width" property does not seem to have any effect on tables in Internet Explorer 7? While it works fine on other elements, when applied to a table, the max-width rule is completely ignored. <!DOCTYPE html PUBLIC "-//W3C//D ...

Containerized chatboxes with improved stability

https://i.sstatic.net/ILwsO.jpg Within the div labeled chatbox-container, there are two chatboxes that have been positioned to float: right with a margin-right: 15px. .chatbox-container { position: fixed; z-index: 1; right: 0; left: 0; ...

Editable span Option

One of my skills includes creating a dynamic contenteditable span within text that can expand and contract, as well as wrap around multiple lines without taking up the entire width: div { width: 100px; background: #f00; padding: 5px; } span { bo ...

Multiple callbacks triggered by jQuery CSS transitions

I am experiencing an issue with my menu animation. I am curious as to why the slideDown transition is occurring twice. You can view the JSFiddle here. <ul class=top-menu"> <li>Item 1</li> <li>Item 2</li> <ul cl ...

Creating Eye-Catching Images by Incorporating Image Overlays Using Just One Image

I'm facing a bit of a challenge here. I need to figure out how to overlay an image onto another image using just a single image tag. Specifically, I want to add a resize icon to the bottom right corner of an image to let users know they can resize it ...

What is the process for deleting an animation using JavaScript, and how can the background color be altered?

Two issues are currently troubling me. First off, I am facing a challenge with an animation feature that I need to modify within the "popup" class for a gallery section on a website. Currently, when users load the page, a square image and background start ...

Tips on Creating a Responsive Absolutely Positioned Div for Various Screen Sizes

I'm currently working on a website and I've come across an issue regarding the responsiveness of an absolutely positioned div across various screen sizes. Let me provide some context and share what I have attempted so far: Issue: Within the head ...

Implement the addition of a class upon hovering using AngularJS

My goal is to include a new class when hovering over the li element using Angular in the code snippet below. <li ng-mouseenter="cola-selected=true" class="pull-left" ng-class="{'selected' : cola-selected}"> <a href="interna.html"> ...

How can you align a div next to another using the float property in CSS?

/*Custom Styling for Divs*/ #box1 { border: 1px solid; border-radius: 25px; width: 900px; } #box2 { border: 1px solid; border-radius: 25px; width: 430px; } #box3 { float: right; border: 1px solid; border-radius: ...

The toggle button in the navbar takes its time to vanish completely

Every time I try to close my navbar, it seems to take forever for the navbar to disappear. Below is the code snippet: HTML <!-- logo --> <div class="logo"> <img src="assets/logo.png" alt="logo g's s ...

The CSS method for changing the content URL is experiencing difficulties in Firefox

Css: .woocommerce-placeholder.wp-post-image { content: url("DSC0926-1.jpg"); } /*for firefox*/ .woocommerce-placeholder.wp-post-image::before { content: url("DSC0926-1.jpg"); } HTML <img src="placeholder.png" alt="Placeholder" class="woocomm ...

The appearance of the website varies on the localhost compared to what is depicted

My website appears differently on the web URL than it does on localhost. It displays perfectly on localhost, but when I run it on the actual URL, it is shifted upwards and the carousel overlaps the navigation panel. I have tried numerous solutions but noth ...

Is it possible to style an element that has extended beyond its parent container using CSS?

Is it possible to select items that have a float:right property and appear on the next line due to reasons like excessive content on the left or a window size too small? This is necessary because the parent item has a background image that looks good, but ...

Sass - Retain the original class hierarchy

Apologies for the vague title, I couldn't think of a better one. As I compile my scss, this piece of code: .foo { ... &__bar { ... } } transforms into the expected output below: .foo { ... } .foo__bar { ... } However, I actually need it t ...

What is the best way to utilize a JavaScript variable as a background within an inline style sheet?

I have a fun project for this evening - I am trying to make my website load a different background image every time the page is refreshed. Earlier on in this project, I managed to make the background interact with window size and screen resolution similar ...

React - Styling with Pseudo Element on Input Element not displayed properly

I'm struggling to understand why an ::after element is not displaying on an input element, but is working fine on a div. I'm attempting to create a performance-friendly focus effect for an input element, where it glows when in focus using the af ...

Improving Material UI Responsiveness: A Comprehensive Guide

Could someone please help me resolve the overflow issue in my UI? You can view the overflow here. The second image explains the reason behind it, which you can see here. I am currently working on fixing my UI using React and Material UI. When checking the ...

How to position a centered div with a background image on a webpage

I am trying to achieve a centered layout for a div and its content on a webpage. The div includes a background image. Here is my approach using Flexbox: <head> <style> .flex_container { display: flex; flex-d ...