How to use Bootstrap whilst maintaining semantic HTML markup?
Bootstrap provides mixins which you can use. So instead of adding classes like col-xs-12
in the HTML, you'd use @include make-xs-column(12)
inside the selector block for the element. To add a row, there's the @mixin make-row
.
Referring to an answer by Alexey Morashko, instead of this unsemantic markup:
<div class="items-list row">
<div class="item col-xs-12 col-sm-6 col-md-4">
<div class="item-name">Product first</div>
<div class="item-description">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</div>
</div>
<div class="item col-xs-12 col-sm-6 col-md-4">
<div class="item-name">Product second</div>
<div class="item-description">Ratione, ullam, reprehenderit aliquid saepe ab harum.</div>
</div>
<div class="item col-xs-12 col-sm-6 col-md-4">
<div class="item-name">Product third</div>
<div class="item-description"> totam repudiandae velit eum accusantium veritatis.</div>
</div>
</div>
You can instead use:
HTML
<div class="items-list">
<div class="item">
<div class="item-name">Product first</div>
<div class="item-description">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</div>
</div>
<div class="item">
<div class="item-name">Product second</div>
<div class="item-description">Ratione, ullam, reprehenderit aliquid saepe ab harum.</div>
</div>
<div class="item">
<div class="item-name">Product third</div>
<div class="item-description"> totam repudiandae velit eum accusantium veritatis.</div>
</div>
</div>
CSS
.items-list {
@include make-row();
.item
@include make-xs-column(12);
@include make-sm-column(6);
@include make-md-column(4);
}
}
You can find more examples of Bootstrap mixins in the Sitepoint article - 5 Useful Sass Mixins in Bootstrap.