I have an unknown number of thumbs to display, here is an example of the HTML rendered:
<div class="row-fluid">
<ul class="thumbnails">
<li class="span3">
<a href="#" class="thumbnail">
<img data-src="holder.js/260x180" alt="260x180" style="width: 260px; height: 180px;" src="mySrc">
</a>
</li>
<li class="span3">
<a href="#" class="thumbnail">
<img data-src="holder.js/260x180" alt="260x180" style="width: 260px; height: 180px;" src="mySrc">
</a>
</li><li class="span3">
<a href="#" class="thumbnail">
<img data-src="holder.js/260x180" alt="260x180" style="width: 260px; height: 180px;" src="mySrc">
</a>
</li><li class="span3">
<a href="#" class="thumbnail">
<img data-src="holder.js/260x180" alt="260x180" style="width: 260px; height: 180px;" src="mySrc">
</a>
</li><li class="span3">
<a href="#" class="thumbnail">
<img data-src="holder.js/260x180" alt="260x180" style="width: 260px; height: 180px;" src="mySrc">
</a>
</li>
<li class="span3">
<a href="#" class="thumbnail">
<img data-src="holder.js/260x180" alt="260x180" style="width: 260px; height: 180px;" src="mySrc">
</a>
</li>
<li class="span3">
<a href="#" class="thumbnail">
<img data-src="holder.js/260x180" alt="260x180" style="width: 260px; height: 180px;" src="mySrc">
</a>
</li>
</ul>
</div>
Here is the result:
Question : Since I build the UI dynamically, how can I avoid the margin on the second row without creating another <div class="row-fluid">
Update IE8 solution is required
I have an unknown number of thumbs to display, here is an example of the HTML rendered:
<div class="row-fluid">
<ul class="thumbnails">
<li class="span3">
<a href="#" class="thumbnail">
<img data-src="holder.js/260x180" alt="260x180" style="width: 260px; height: 180px;" src="mySrc">
</a>
</li>
<li class="span3">
<a href="#" class="thumbnail">
<img data-src="holder.js/260x180" alt="260x180" style="width: 260px; height: 180px;" src="mySrc">
</a>
</li><li class="span3">
<a href="#" class="thumbnail">
<img data-src="holder.js/260x180" alt="260x180" style="width: 260px; height: 180px;" src="mySrc">
</a>
</li><li class="span3">
<a href="#" class="thumbnail">
<img data-src="holder.js/260x180" alt="260x180" style="width: 260px; height: 180px;" src="mySrc">
</a>
</li><li class="span3">
<a href="#" class="thumbnail">
<img data-src="holder.js/260x180" alt="260x180" style="width: 260px; height: 180px;" src="mySrc">
</a>
</li>
<li class="span3">
<a href="#" class="thumbnail">
<img data-src="holder.js/260x180" alt="260x180" style="width: 260px; height: 180px;" src="mySrc">
</a>
</li>
<li class="span3">
<a href="#" class="thumbnail">
<img data-src="holder.js/260x180" alt="260x180" style="width: 260px; height: 180px;" src="mySrc">
</a>
</li>
</ul>
</div>
Here is the result:
Question : Since I build the UI dynamically, how can I avoid the margin on the second row without creating another <div class="row-fluid">
Update IE8 solution is required
Share Improve this question edited Mar 4, 2013 at 16:30 Shlomi Schwartz asked Mar 4, 2013 at 16:10 Shlomi SchwartzShlomi Schwartz 8,91330 gold badges119 silver badges198 bronze badges 4- Will the number of items per line change? – Jamie Hutber Commented Mar 4, 2013 at 16:16
- could change, basically it is one line with an unknown number of thumbs <div class="row-fluid"><ul class="thumbnails"><li>....<li> – Shlomi Schwartz Commented Mar 4, 2013 at 16:18
- Ye but my thinking is, assuming the width of its parent won't change you will always have the 5th li at the start? – Jamie Hutber Commented Mar 4, 2013 at 16:19
- This is way behind you now but I would just change the location of the margin to the other side i.e. on the right rather than the left. It make sense to think that when a thumbnail gets on the page it should say "whoever else es next stay this far away from me" this would always be fine even if no one else came along rather than say "when I get here, I stay this far away from whoever is here" which as your question shows might not apply if it was the first one around. You would also get consistent column alignment that way (sorry I think in analogies hence the story) – Obi Commented Jun 13, 2014 at 10:15
3 Answers
Reset to default 8Assuming the width won't change of the LI's parent using :nth-child(4n) should work to target the x
element.
.row-fluid li:nth-child(4n) {
margin: 10px;
padding: 0;
}
See the spec for details on how to write formulas for :nth-child().
A very very basic Fiddle displaying it working.
Update
To work with IE8 just use jQuery (assuming you're using it)
$('.row-fluid li:nth-child(4n)').css({'margin':'10px'});
I do believe that should do the trick.
I had a case where the margin was ok to have, but it had to be consistent on all rows.
You could achieve this by adding in a blank <li style="display:none"></li>
to the start of the list. That way Bootstrap targets the margin removal on a <li>
that isnt shown.
Having a margin may not be acceptable, but I feel this is an elegant solution without the need for mixing style into js.
Had same issue, not clean, but a fast workaround was the following:
.row {
margin-left: 0px !important;
margin-right: 0px !important;
}