Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add background color to items in a list

Tags:

html

css

I have an ordered list

<ol>
<li class="odd">Lorem ipsum dolor sit amet, consectetur ...</li>
<li class="even">Some more text</li>
</ol>

To look something like this:

  1. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
  2. Some more text

I want the list to have list-position: outside so the numbers overhang (as they do on this page) but have the background of each list item (which alternate) cover the numbers as well.

like image 725
Gazzer Avatar asked Jul 27 '10 10:07

Gazzer


People also ask

How to add color to drop-down list items?

1) Check the scale you want to add color to in the Apply to section. If you check Row of data range in the Apply to section, you need to select the row range. 2) Add colors to the items of drop-down list one by one. Click at one item in the List Items, then click one color you need to add the color to the selected item.

How to set background color with HTML and CSS?

How to Set Background Color with HTML and CSS Add the style attribute to the <body> element ¶. You can set a background color for an HTML document by adding... Add the CSS background-color property to the <body> element ¶. The background-color property is used to change the... Create a background ...

How do I change the color of my background in illustrator?

Click Home > the arrow next to Fill Color, or press Alt+H, H. Under Theme Colors or Standard Colors, pick the color you want. To use a custom color, click More Colors, and then in the Colors dialog box select the color you want. Tip: To apply the most recently selected color, you can just click Fill Color.

How do I change the background color of a worksheet?

To use a different background color for the whole worksheet, click the Select All button. This will hide the gridlines, but you can improve worksheet readability by displaying cell borders around all cells. Click Home > the arrow next to Fill Color, or press Alt+H, H. Under Theme Colors or Standard Colors, pick the color you want.


3 Answers

As the "outside" name suggests, the numbers are placed outside the element so you cannot affect them with li's background color. A workaround for this may be using an additional div inside the li:

<ol>
<li><div>Lorem ipsum dolor sit amet, consectetur ...</div></li>
<li><div>Some more text ...</div></li>
</ol>

Then add the following CSS for the div:

ol li div  {
  width: 100%;
  height: 100%;
  background-color: #FFAAAA;
  margin-left: -20px;
  padding-left: 20px;
}

This will move the div to appear under the number (that's the -20px value) and the text in it back to the right place (that 20px value).

like image 150
Karel Petranek Avatar answered Oct 03 '22 11:10

Karel Petranek


instead of using outside, could you use list-position: inside, set the background, and then use a negative left margin to push it out?

like image 36
second Avatar answered Oct 03 '22 11:10

second


One option to try is text-indent, e.g.

li {
    list-style-position: inside;
    text-indent: -1em;
    padding: 10px 2em;
    background-color: lime;
}
li.odd {
    background-color: aqua;
}

I've used a negative text-indent to pull the first line of text out, and then left padding to pull everything back into alignment. You might need to play with the text indent and padding values a bit. I've only tested this with list items with single-digit numbers.

like image 21
Olly Hodgson Avatar answered Oct 03 '22 12:10

Olly Hodgson