Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML List element : Sharing the parent width into equal parts

Tags:

I have a parent <ol> and couple of <li> items in that.

<ol style='width=800px;display :block;float:left;'>    <li style='display :block;float:left;'> Item 1  </li>    <li style='display :block;float:left;'>  Item 2 </li>    <li style='display :block;float:left;'>  Item 3 </li>    <li style='display :block;float:left;'>  Item 4 </li>  </ol> 

Is there any way my list item can be arranged in a way where it will equally divide the parent width (800px), and each item will have the same amount of width? I.e. each <li> will take 200px width.

I don’t want to hardcode the value. Is there any style attribute which will do that?

I dont want to hardocode the width like 20 % or something because the list items are dynamically added.it may be 4 or 5 or 6 sometimes

like image 985
Shyju Avatar asked Jun 10 '11 18:06

Shyju


People also ask

What does width inherit mean?

17. Bookmark this question. Show activity on this post. My understanding is that width: 100% lets the element's width be the same as its parent's, whereas width: inherit does that only when the parent's width is explicitly specified.

What should be the value of the width attribute so that the width of the element adjust itself to the current width of its parent element?

What should be the table width, so that the width of a table adjust to the current width of the browser window? The table width should be 100% so that the width of a table adjusts to the current width of the browser window.


1 Answers

Try this: http://jsfiddle.net/QzYAr/

  • For details on display: table-cell: Is there a disadvantage of using `display:table-cell`on divs?
  • table-layout: fixed ensures equal width li elements.

CSS:

ol {     width: 400px;     /*width: 800px;*/      display: table;     table-layout: fixed; /* the magic dust that ensures equal width */     background: #ccc } ol > li {     display: table-cell;     border: 1px dashed red;     text-align: center } 

HTML:

<ol>     <li>Item 1</li>     <li>Item 2</li>     <li>Item 3</li>     <li>Item 4</li> </ol> 
like image 66
thirtydot Avatar answered Sep 28 '22 05:09

thirtydot