Aligning numbers inside an HTML Ordered List
Asked Answered
B

3

16

I am trying to create an ordered numbered list where the background color alternates. I need the numbers to appear inside the background, as well as line up. If I take out list-style-position:inside; the numbers line up, but move outside the background.

Here's my code, with a link to jsfiddle below.

https://jsfiddle.net/kwgpf/

ol {
  list-style-type: decimal;
  list-style-position: inside;
  margin: 1.5em;
}

.alt {
  background-color: #ccc;
}
<ol>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
</ol>
Bostic answered 27/12, 2012 at 17:59 Comment(0)
I
2

This is the best I can come up with right now. It's a bit hacky, but it gets the job done on modern browsers.

View on JSFiddle

ol {
  list-style-type: decimal;
  margin-left: 50px;
}

.alt {
  background-color: #ccc;
  position: relative;
}

.alt::before {
  position: absolute;
  display: inline-block;
  content: "";
  background-color: rgba(0, 0, 0, .2);
  left: -30px;
  height: 100%;
  width: 30px;
}
<ol>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
</ol>
Illegitimate answered 27/12, 2012 at 18:13 Comment(0)
B
30

You shoud use:

list-style: decimal inside none;
Bookstall answered 25/3, 2013 at 6:57 Comment(2)
the full property (no shorthand) for anyone wondering (for just the positioning) is list-style-position: insideTungstic
You can read more about styling lists here: developer.mozilla.org/en-US/docs/Learn/CSS/Styling_text/… And you can read about the specific CSS here: developer.mozilla.org/en-US/docs/Web/CSS/list-style-typeJeffery
I
2

This is the best I can come up with right now. It's a bit hacky, but it gets the job done on modern browsers.

View on JSFiddle

ol {
  list-style-type: decimal;
  margin-left: 50px;
}

.alt {
  background-color: #ccc;
  position: relative;
}

.alt::before {
  position: absolute;
  display: inline-block;
  content: "";
  background-color: rgba(0, 0, 0, .2);
  left: -30px;
  height: 100%;
  width: 30px;
}
<ol>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
  <li class="alt">ha HA ha HA!</li>
  <li>ha HA ha HA!</li>
</ol>
Illegitimate answered 27/12, 2012 at 18:13 Comment(0)
S
1

You could just add a margin before each bullet below #10, if you're willing to go the fixed widths route.

Sewellyn answered 27/12, 2012 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.