Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Ordered List Styling ::before Margins

I'm using the code from here to style my ordered list http://codepen.io/sawmac/pen/txBhK

HTML

<ol class="custom-counter">
<li>This is the first item</li>
<li>This is the second item</li>
<li>This is the third item</li>
<li>This is the fourth item</li>
<li>This is the fifth item is the fifth item is the fifth item is the fifth item is the fifth item is the fifth item is the fifth item is the fifth item</li>
<li>This is the sixth item is the sixth item is the sixth item is the sixth item is the sixth item is the sixth item is the sixth item is the sixth item is the sixth item is the sixth item is the sixth item </li>
</ol>

CSS

body {
  font-size: 1.2em;
  font-family: "Helvetica Neue", Helvetica, sans-serif;
  margin: 50px;
}

.custom-counter {
  margin: 0;
  padding: 0;
  list-style-type: none;
}

.custom-counter li {
  counter-increment: step-counter;
  margin-bottom: 10px;
}

.custom-counter li::before {
  content: counter(step-counter);
  margin-right: 5px;
  font-size: 80%;
  background-color: rgb(0,200,200);
  color: white;
  font-weight: bold;
  padding: 3px 8px;
  border-radius: 3px;
}

When the text for a list item goes onto more than one line, the text goes to the left edge of the page. I want it to be in line with the text above it. Hopefully, the image below can explain this better.

Image example

I've tried adding a left margin onto the li CSS, but that moves the numbers as well.

Any help appreciated!

Thanks

like image 300
joker91 Avatar asked Jul 13 '26 07:07

joker91


1 Answers

Use positioning on li and li:before. Like:

li {
  position: relative;
  padding-left: 35px;
}

li:before {
  position: absolute;
  left: -5px;
}

Have a look at the snippet below:

body {
  font-size: 1.2em;
  font-family: "Helvetica Neue", Helvetica, sans-serif;
  margin: 50px;
}

.custom-counter {
  margin: 0;
  padding: 0;
  list-style-type: none;
}

.custom-counter li {
  counter-increment: step-counter;
  margin-bottom: 10px;
  position: relative;
  padding-left: 35px;
}

.custom-counter li::before {
  position: absolute;
  left: -5px;
  content: counter(step-counter);
  margin-right: 5px;
  font-size: 80%;
  background-color: rgb(0,200,200);
  color: white;
  font-weight: bold;
  padding: 3px 8px;
  border-radius: 3px;
}
<ol class="custom-counter">
  <li>This is the first item</li>
  <li>This is the second item</li>
  <li>This is the third item Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure rem quia et quibusdam dolore impedit porro, velit voluptatibus odit? Rem doloremque quos, officia aut nulla distinctio itaque quisquam excepturi rerum.</li>
  <li>This is the fourth item</li>
  <li>This is the fifth item</li>
  <li>This is the sixth item</li>
  <li>This is the sixth item</li>
  <li>This is the sixth item</li>
  <li>This is the sixth item</li>
</ol

Hope this helps!

like image 85
Saurav Rastogi Avatar answered Jul 17 '26 08:07

Saurav Rastogi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!