Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left align both list numbers and text

I'd like to left align both the numbers and the text in my <ol>. This is what I'm trying to accomplish:

enter image description here

For now, each <li> contains an <a>, but that can change. So far I tried putting left padding and then a text-indent on the <a>.

Here is my code as of now.

HTML:

<ol class="dinosaurs">
    <li><a>Dinosaur</a></li>
    <li><a>Tyrannosaurus</a></li>
    <li><a>Camptosaurus</a></li>
</ol>

CSS:

.dinosaurs{
    list-style-position: inside;
}

NOTE: I'm viewing the webpage in Chrome 23 on Windows 7.

like image 305
dmr Avatar asked Jan 15 '13 21:01

dmr


People also ask

How do I align text and numbers in Word?

Click anywhere inside the list. From the Numbering dropdown (in the Paragraph group), choose Define New Number Format (at the bottom). In the resulting dialog, change the Alignment setting from Left to Right, as shown in Figure G. Click OK to see the newly aligned list shown in Figure H.

How do you left align an ordered list in HTML?

If you use list-style-position: inside; the number is placed inside the block of text. To adjust the position of the number and keep ident of the text use this css.

How do I align the numbers of an ordered list?

You can use list-style: inside; to align the numbers. the initial value is set to outside, but i have the feeling firefox set it to inside by default. Anyway, this property gives you control of position the bullets/numers from your list.


5 Answers

This seems to work:

.dinosaurs { counter-reset: item }
.dinosaurs li { display: block }
.dinosaurs li:before { 
  content: counter(item) ". ";
  counter-increment: item;
  width: 2em;
  display: inline-block;
}
like image 153
ford Avatar answered Oct 06 '22 04:10

ford


You could position the list elements like so:

    .dinosaurs {
  list-style-position: inside;
}

.dinosaurs li{
  position: relative;
}
.dinosaurs li a {
  position: absolute;
  left: 30px;
}

which would yield http://jsfiddle.net/wBjud/2/

like image 40
gotohales Avatar answered Oct 06 '22 05:10

gotohales


None of the existing answers worked for me, but by combining and building on them I found similar method that does, including for multiline entries, without requiring any tags inside list items:

ol {
    counter-reset: item;
}
li {
    display: block;
    margin-left: 1.7em;
}
li:before {
    content: counter(item) ". ";
    counter-increment: item;
    position: absolute;
    margin-left: -1.7em;
}

Which looks like this: https://jsfiddle.net/b3dayLzo/

I think it works by putting the li:before in the left margin of the li.

You may want a different margin-left.

There's no class on the ol because in my case this appears within the style of the container.

like image 25
ShadSterling Avatar answered Oct 06 '22 03:10

ShadSterling


Try adding padding-left: 0; to your style, and changing list-style-position: to outside if necessary.

like image 28
alanmoo Avatar answered Oct 06 '22 03:10

alanmoo


You can fake it by using positioning, padding and margins.

jsFiddle example

.dinosaurs {
  list-style-position: inside;
  position:relative;
  margin-left: -20px;
}
a {
  position:absolute;
  left:70px;
}
like image 30
j08691 Avatar answered Oct 06 '22 05:10

j08691