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>

3 Answers 3

30

You shoud use:

list-style: decimal inside none; 
Sign up to request clarification or add additional context in comments.

2 Comments

the full property (no shorthand) for anyone wondering (for just the positioning) is list-style-position: inside
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-type
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>

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.