Using My CSS Methodology and Architecture with Bootstrap
Here I propose modifications to the style guide if aiming for maximum leverage of Bootstrap classes for styling and layout.
Goal: Use Bootstrap classes as much as possible
- Abstract and distinct components combined into just "components", and prefixed with a
c-
, to distinguish themselves from Bootstrap classes - Since we'll be relying on the Bootstrap grid, the
layouts/
directory has been removed and any custom layout classes (which are now often unneeded) added toutilities/
We'll still be using the .block-element--modifier
convention to name classes.
Bootstrap follows a similar convention, but uses only a single dash for modifiers as well.
All classes will have a namespace prefix. Thus, classes without a prefix are assumed to be Bootstrap classes.
l-
for global layout utilitiesu-
for specific purpose utilitiest-
for typography related stylingis-
andhas-
to reflect temporary statesjs-
for javascript hooksc-
for all components
- Prefix all components with
c-
- Components can contain other components and objects
- Components start a context for sub-elements
Examples: c-media
, c-footer
, basically any of your custom elements!
Now let's summarize in an example. Classes without a prefix are from Bootstrap.
<div class="row">
<div class="col-md-8">
<div class="card border-dark t-bold js-main-nav">
<div class="card-header"></div>
<div class="card-body u-center t-sm"></div>
<div class="card-footer">
<div class="button button-primary js-close"></div>
</div>
</div>
</div>
</div>
We'll now separate our scss partials (or css files) into three directories, which import into a master style.scss
file to be compiled for distribution:
base/
contains resets/normalize, and HTML element modifying code
utilities/
contains a partial for types of utilities, including typography and custom layouts
components/
contains a partial for each custom component, with relevant state classes, sub-elements, and modifiers
Here's an example directory structure:
scss/
├─ base/
│ ├─ _root.scss
├─ components/
│ ├─ _menu.scss
│ ├─ _sidebar.scss
│ └─ _footer.scss
├─ utilities/
│ ├─ _spacing.scss
│ └─ _typography.scss
└─ style.scss
style.scss
is composed entirely of imports in the following order:
base/
, utilities/
, components/