0

Is it possible to place a delimiter between each inline <li> but so that it appears only between each item in the line and never in the beginning or at the end of a line? If it can't be done with CSS then maybe can it be solved with JavaScript/jQuery?

http://codepen.io/anon/pen/Pbxddo

<ul>
  <li>Something</li>
  <li>Something</li>
  <li>Something</li>
  <li>Something</li>
</ul>

ul {
  list-style-type: none;
  padding: 10px;
  li {
    display: inline;
    &:after {
    padding-left: 5px;
      content: '|';
    }
  }
}

EDIT: I don't think anyone understood... what I want is to remove EVERY delimiter that is in the beginning or at the end of each line, I don't know how to write it more clearly.

In this image this delimiters should be removed enter image description here

So that the result will look like this

enter image description here

Ivan Topić
  • 2,524
  • 5
  • 21
  • 42

4 Answers4

1

Use li:last-child selector, like:

li:last-child:after {
  content: '';
}

Have a look at the snippet below, or Codepen (with SCSS).

div {
  border: 3px solid #333;
  width: 300px;
  margin: auto;
}

ul {
  list-style-type: none;
  padding: 10px;
}
ul li {
  display: inline;
  font-size: 20px;
}
ul li:after {
  padding-left: 5px;
  content: '|';
}
ul li:last-child:after {
  content: '';
}
<div>
<ul>
  <li>Something</li>
  <li>Something else</li>
  <li>Bananas, apples </li>
  <li>Tea and cookies</li>
  <li>Football or soccer</li>
  <li>sky, earth, trees, nature</li>
  <li>Furniture, painting, closet, bed, carpet</li>
  <li>window, clothes, light, door</li>
  <li>Something else in the room</li>
  </ul>
</div>

Hope this helps!

Saurav Rastogi
  • 8,959
  • 3
  • 25
  • 34
1

You can use not() pseudo class and select all li elements except last one.

ul {
  display: flex;
  padding: 0;
  list-style-type: none;
}
li:not(:last-child):after {
  content: '|';
  padding: 0 10px;
}
<ul>
  <li>Something</li>
  <li>Something</li>
  <li>Something</li>
  <li>Something</li>
</ul>
Nenad Vracar
  • 102,378
  • 14
  • 116
  • 136
1

This is a different approach than all of the (correct) :last-child answers.

Use the adjacent sibling selector: +. This selects the second li that is immediately preceded by an li. In your context, this will select all but the first li elements:

ul {
  list-style-type: none;
  padding: 10px;
  li {
    display: inline;
  }
  li + li {
    &:before {
    padding-right: 5px;
      content: '|';
    }
  }
}

Here's a working snippet demonstrating your example:

div {
  border: 3px solid #333;
  width: 300px;
  margin: auto;
}

ul {
  list-style-type: none;
  padding: 10px;
}
ul li {
  display: inline;
  font-size: 20px;
}
ul li + li:before {
  padding-right: 5px;
  content: '|';
}
<div>
<ul>
  <li>Something</li>
  <li>Something else</li>
  <li>Bananas, apples </li>
  <li>Tea and cookies</li>
  <li>Football or soccer</li>
  <li>sky, earth, trees, nature</li>
  <li>Furniture, painting, closet, bed, carpet</li>
  <li>window, clothes, light, door</li>
  <li>Something else in the room</li>
  </ul>
</div>

Personally, I prefer this method because it's less verbose, and doesn't require "overrides".

random_user_name
  • 23,924
  • 7
  • 69
  • 103
0

You can change your css to this:

ul {
  list-style-type: none;
  padding: 10px;
  li {
    display: inline;
    font-size: 20px;
    &:after {
    padding-left: 5px;
      content: '|';
    }
  }
  li:last-child {
    display: inline;
    font-size: 20px;
    &:after {
    padding-left: 5px;
      content: '';
    }
  }
}
George
  • 6,116
  • 2
  • 24
  • 36