Bootstrap, Sass and Compass in PrestaShop 1.6
Starting with version 1.6, PrestaShop theme API heavily relies on the Bootstrap framework (version 3), together with Sass and Compass. Simply put, if you want to build a 1.6-compatible theme, you must use these technologies.
Bootstrap
About Bootstrap
Bootstrap is "a sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development." This tools makes it a lot simpler to build responsive web sites – meaning websites which adapt their design to the size of the screen, from mobile phone to giant TV.
A Bootstrap design is made of:
- Conventions for structuring HTML code and naming CSS classes.
- A base CSS files (using the LESS or Sass format) built from a file of variables.
- A JavaScript for the the more usual functions.
Bootstrap uses a whole new way of working on web project that will really benefit all your forthcoming project – even non-PrestaShop ones! We strongly advise to dive into it:
- The official Bootstrap website has many informative section, including a Getting started guide (including code compilation, an overview of how it uses CSS, and an explanation of its use of JavaScript.
- Sitepoint has several free articles/tutorials : Understanding Twitter Bootstrap 3, Building Responsive Websites Using Twitter BootStrap, or Twitter Bootstrap Tutorial – Handling Complex Designs.
General technical information
Bootstrap uses some specific HTML elements which make it a requirement to use HTML5. Be careful to use the proper HTML5 doctype for all your template files!
Likewise, CSS3 is used for many CSS properties.
<!DOCTYPE html> <html lang="fr"> ... </html>
Bootstrap 3 is made to be mobile friendly from the start of your project: instead of adding optional mobile styles to your project, these styles are built into the core of the project, ensure that it displays well on all devices.
In order to ensure that your project renders well and that the touch zoom works as expected, you must add the viewport
meta tag to your template's head
element:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Bootstrap makes it easy to build a responsive design, and you can use a little trick to make your images responsive too: use Bootstrap's img-responsive
class on the image. That class will make your image use max-width: 100%;
and height: auto;
in order to adapt to the parent element.
<img src="http://example.com/img/image.jpg" class="img-responsive" alt="Description of the image">
Fluid grid system
Bootstrap uses a 12-column grid system, which helps you build a fluid layout. You can use media queries to indicate breakpoints to the grid system, so that it can scale up or down its number of columns depending on the screen size. The default size is tailored for very small screen sizes (up to 480px width, for small phones), so no media query is required there. Bigger screen size are managed using the following queries:
/* Small devices (tablets, 768px and up) */ @media (min-width: @screen-sm) { ... } /* Medium devices (desktops, 992px and up) */ @media (min-width: @screen-md) { ... } /* Large devices (large desktops, 1200px and up) */ @media (min-width: @screen-lg) { ... }
Bootstrap uses class prefixes to differentiate devices by their screen sizes:
.col-xs-...
: extra small, for phones (< 768px).col-sm-...
: small, for tablets (≥768 px).col-md-...
: medium, for desktops (≥992 px).col-lg-...
: large, for large desktops and TVs (≥1200 px)
For instance:
col-xs-3
:3
columns on phones.col-md-4
: 4 columns on desktops.
For each device, Bootstrap also provides CSS classes, allowing you to change the column's position:
col-size-push-col
: move column to the left.col-
: move column to the right.size
-pull-col
col-
: position column according to other column.size
-offset-col
For instance:
col-md-push-2
: On desktops, move this column by two columns to the left.
Bootstrap & Sass: Using variables and mixins
Variables
You can configure Bootstrap by editiing the variables in the _variables.scss
file. For instance:
@grid-columns: 12; @grid-gutter-width: 30px; @grid-float-breakpoint: 768px;
Mixins
A mixin lets you make groups of CSS declarations that you want to reuse throughout your site. For instance:
@mixin border-radius($radius) { -webkit-border-radius: $radius; -moz-border-radius: $radius; -ms-border-radius: $radius; border-radius: $radius; } .box { @include border-radius(10px); }
You can use use mixins to define new styles. For instance:
.wrapper { .make-row(); } .content-main { .make-lg-column(8); } .content-secondary { .make-lg-column(3); .make-lg-column-offset(1); }
...for the following HTML code:
<div class="wrapper"> <div class="content-main">...</div> <div class="content-secondary">...</div> </div>
Bootstrap classes
Bootstrap has many default CSS classes, and PrestaShop defines several more, to help you build a consistent design.
Helper classes
These classes are to be used when design an element that uses one of PrestaShop's Helper classes.
pull-left
: For left float.pull-right
: For right float.text-muted
: For gray text.clearfix
: To prevent excessive size of floating blocks.close
: To create a close button (x).caret
: To indicate a dropdown effect.center-block
: To center an element.show
andhidden
: To show/hide an element.
Responsive classes
These classes are useful to show/hide an element depending on the device.
visible-xs
/hidden-xs
visible-sm
/hidden-sm
visible-md
/hidden-md
visible-lg
/hidden-lg
visible-print
/hidden-print
Navigation, tabs and menus
The main classes for these contexts are:
navbar
andnavbar-inner
: classes container de la barre de navigationnavbar-fixed-top: pour accrocher la barre de navigation en haut de la page.
brand: pour le titre du site
nav, nav-tabs and nav-pills: pour les onglets
btn btn-navba: pour les boutons
nav-collapse, collapse, data-toggle, data-target: pour afficher et fermer automatiquement
icon-th-list: pour afficher un icon (uniquement sur les petits écrans ..th..)
FontAwesome