Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying CSS to ordered list - background colour under numbers?

Tags:

css

html-lists

I have an ordered list which I have styled in two ways:

  1. Coloured the numbers differently to the list text
  2. Applied a background colour and border to each list item

See the list on the right hand side of this page

The numbers are made orange by first applying the orange style to the <li>, then applying the black style to the list text only using jQuery:

jQuery('#secondary ol li').wrapInner('<span class="notes"> </span>');

I would prefer to use CSS only, but hey.

So the remaining issue is how to extend the background colour/border under the numbers too.

Thanks for checking out my question!

like image 701
Caroline Elisa Avatar asked Jun 14 '12 14:06

Caroline Elisa


People also ask

How do I change the background color of an ordered list in HTML?

To add background color in HTML, use the CSS background-color property. Set it to the color name or code you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a table, heading, div, or span tag.

Can you style ordered list numbers?

If you want to number items in order, you can use the <ol> (ordered list) tag. But it is kind of hard to style those list numbers in CSS. There is an easier way to create a number styled list of item using the :before pseudo element along with counter properties.

How do you change the color of text in a list in CSS?

Simply add the appropriate CSS selector and define the color property with the value you want. For example, say you want to change the color of all paragraphs on your site to navy. Then you'd add p {color: #000080; } to the head section of your HTML file.


2 Answers

Revised Answer: jsFiddle Pure CSS Solution

Here is a revised method that does not use the OP's original jQuery framework, a request that was not fulfilled in my previous answer. That method used existing jQuery to apply a span wrapper for the li element since direct HTML modification was not possible.

This new method uses CSS Pseudo selectors :before and :after along with CSS 2.1 counter function that can have it's number list stylized so that no jQuery wrapper is needed. In fact, the jsFiddle revision shows the numbers using Google @font-face font.

enter image description here

Using the CSS counter function is done by declaring a variable name to use in the ul element, then incrementing the counter in the :before element as well as using it as actual content.

Simple Example: jsFiddle CSS Counter

enter image description here

HTML:

<ul>
    <li>A numbered list that's stylized!</li>
    <li>A numbered list that's stylized!</li>    
    <li>A numbered list that's stylized!</li>    
    <li>A numbered list that's stylized!</li>
    <li>A numbered list that's stylized!</li>
    <li>A numbered list that's stylized!</li>
    <li>A numbered list that's stylized!</li>
    <li>A numbered list that's stylized!</li>
    <li>A numbered list that's stylized!</li>
    <li>A numbered list that's stylized!</li>
    <li>A numbered list that's stylized!</li>
    <li>A numbered list that's stylized!</li>
    <li>A numbered list that's stylized!</li>
    <li>A numbered list that's stylized!</li>
    <li>A numbered list that's stylized!</li>
    <li>A numbered list that's stylized!</li>
    <li>A numbered list that's stylized!</li>
    <li>A numbered list that's stylized!</li>
    <li>A numbered list that's stylized!</li>
    <li>A numbered list that's stylized!</li>    
</ul>

CSS:

ul{
    list-style-type: none;
    white-space: nowrap;
    margin: 0;
    padding: 0;
    width: 210px;
    height: 200px;
    overflow: auto;
    border: 3px solid royalblue;
    /* Setup the counter. */
    counter-reset: yourVariableName;
}
li:before{
    /* Advance the number. */
    counter-increment: yourVariableName;
    /* Use the counter number as content. */
    content: 'Item ' counter(yourVariableName) ': ';
    color: royalBlue;
    font-family: 'Impact';
}

More about CSS counters HERE.


My original dated answer that uses existing framework in OP's question is shown below.


I looked at your Question carefully and then looked at your webpage.

Here is the solution you need: jsFiddle

To summarize, this are the replacement CSS Rules that make it work:

#secondary ol {
    color:#F60;
    margin-bottom:0;
    margin-left: 0px;   /* Since 'ul,ol{}' setting in line 108 affects this selector, 'margin-left' is redefined. */
    list-style-position: inside;  /* This will place the number on top and inside of the current color of li */
}

.notes{
    color:#333;
    width: 230px;
    heigh: 50px;
    display: inline-block;
    vertical-align:text-top;
}

Result:
enter image description here


Extra: This variant example emphasizes the numbers: jsFiddle

like image 141
arttronics Avatar answered Nov 15 '22 15:11

arttronics


For your second question, one possibility would be to change the list-style-position to inside for your li tags:

li { list-style-position: inside; }

This will move the number inline with the rest of the text and allow you to style it all together. This does however stop the text aligning separately.

For your first question, it is possible to style the numbers separately in theory using the pseudo-element marker like so:

li::marker { color: orange; }

However, I think this is currently not supported in any browser. You can use the mozilla specific ::-moz-list-number but that will obviously not work in other browsers.

You can see all these bits in my example here: http://jsfiddle.net/XmtxL/

like image 31
John Lawrence Avatar answered Nov 15 '22 17:11

John Lawrence