-5

I have a list in which I need to had a break for every two elements:

Original:

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

Need:

<ul>
<li></li>
<li></li>
</br>
<li></li>
<li></li>
</ul>

Fiddle: http://jsfiddle.net/9uksT/

Is this possible with Jquery?

EDIT: Oh wow sorry for the question. I am also open to using CSS. I just need to have a space between every 2 pairs of list items

Batman
  • 5,563
  • 18
  • 79
  • 155

3 Answers3

3

Don't abuse <br> tags this way. Give every second element a wider bottom margin using CSS:

.root ul li:nth-child(2n) {
    margin-bottom: 1em;
}

http://jsfiddle.net/mblase75/9uksT/3/

That said, I would replace your <ul> sub-list with a definition list, which is more semantically correct in this case:

.root dl {
    margin: 0 0 0 2em;
}
.root dl dd {
    margin-bottom: 1em;
}

http://jsfiddle.net/mblase75/E7gAA/

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
  • 1
    [But only use this in CSS if you're targeting IE >= 9.](http://caniuse.com/#search=nth-child) – rid Jan 24 '14 at 15:50
  • 1
    @rid [There are solutions for that](http://stackoverflow.com/questions/10577674/how-to-make-internet-explorer-8-to-support-nth-child-css-element). – Blazemonger Jan 24 '14 at 15:54
  • Thanks. I don't know what was so unclear about my question but thanks for your help. – Batman Jan 24 '14 at 20:44
0

As described that would be invalid HTML, but you can add <br/>&nbsp; inside the end of every second <li> element

http://jsfiddle.net/TrueBlueAussie/9uksT/2/

$('ul:not(.root) li:odd').append("<br/>&nbsp;");

Update:

As the question has now changed, to include CSS, I would not do it this way, but would use a selector to set a style on the LIs (as Arun P Johny shows). e.g.

$('ul:not(.root) li:odd').addClass('second')

This works on all browsers.

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
0

You can use a class and jQuery to assign the class to the right elements

.menu-vertical li.even{
    margin-bottom: 10px;
}

then

$('.menu-vertical > ul li:nth-child(2n)').addClass('even')

Demo: Fiddle

Note: As I shown in the comments it can be done using nth-child selector, but it is not supported by IE < 9

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531