Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill whole line of LI using CSS

I have these nested ul and li . When i fill background color, nested li leaves indented portion white. I have a number of li like this that gets filled from database so i cannot give margin left to individual text in li . How can i do this so that background fills whole line along with the indentation?

Right now it looks like this

enter image description here

I want it like this

enter image description here

Any suggestions how can do this? Thanks in advance. I cannot change the html markup as i'll have to change a lot of code. Is there a way to do this using this markup. these li are coming from db query so i dont have exact number of li in this case.

Demo http://jsbin.com/uReBEVe/1/

like image 836
Ace Avatar asked Dec 14 '13 21:12

Ace


People also ask

Can you put Li inside div?

No. This is invalid HTML. Permittend content: zero or more <li> elements, eventually mixed with <ol> and <ul> elements.


3 Answers

By default, <ul> has padding-left to accomodate the bullet point.

If you add this to your CSS:

ul {padding-left:0}
ul>li {padding-left:40px}

You should get the effect you want.

EDIT: Also you need to correct your HTML :p <ul> can ONLY have <li> as children.

like image 142
Niet the Dark Absol Avatar answered Sep 19 '22 08:09

Niet the Dark Absol


Best thing to do is to use a structure which makes it easy for database management , html and styling(CSS) .

HTML:

<body>
<ul class="main">
    <li>1.</li>
    <li><ul>2</ul></li>
    <li><ul><li><ul>3.</ul></li></ul></li>
</ul>
</body>

CSS:

.main{
    position:relative;
    right:40px;
}
li{
    list-style:none;
    background:red;
    margin-top:1px;
}

Fiddle 1.

I dont know if ul not containing li is valid or invalid.If its invalid then you can use:

<body>
<ul class="main">
    <li>1.</li>
    <li><ul><li>2</li></ul></li>
    <li><ul><li><ul><li>3.</li></ul></li></ul></li>
</ul>
</body>

Fiddle 2

like image 38
Zword Avatar answered Sep 21 '22 08:09

Zword


Flexible, Multi-Level Nesting Solution

This is very similar to another question I answered here, and I've composed a similar solution for you below. You will want valid html by having all nested li elements inside their own ul (as others have noted here), and it would be best to control all this by some class on the outermost ul (though that is not required, but makes targeting this list a whole lot easier).

The key here is supplying the background through the :before pseudo-element, which is made to span the whole width of the outermost ul.

Here is my demo jsbin.

HTML

  <ul class="full-width-list">
    <li>A</li>
    <li>
      <ul>
        <li>B</li>
        <li>
          <ul>
            <li>B</li>
          </ul>
        </li>  
      </ul>
    </li>
  </ul>

CSS

.full-width-list {
  position: relative;
  padding: 0 0 0 4px;
}

.full-width-list ul {
  margin: 0;
  padding: 0
}

.full-width-list li {
  list-style:none;
  padding: 0;
  margin: 0;
  height: 1.2em;
  line-height: 1.2em;
}

.full-width-list ul > li {
  margin-top: 4px;
  padding: 0 0 0 36px;  
}

.full-width-list li:first-child:before {
      content: '';
      height: 1.2em;
      position: absolute;
      left: 0;
      right: 0;
      z-index: -1;
      background:red;  
}

.full-width-list li:first-child:hover:before {
  background:green
}

Limitations

This solution has two main limitations:

  1. None of the ul or li elements can have a position other than the default static set on them, as the :before pseudo-element of the li elements needs to have its only positioned parent be the .full-width-list element.
  2. There has to be a set height on the li items. In my example I use 1.2em, but whatever height you set, it means that the li elements cannot go to two or more lines of text (so this solution only works with a single line of text).
like image 20
ScottS Avatar answered Sep 20 '22 08:09

ScottS