Use Sass bootstrap semantically and selectively

Asked

Viewed 232 times

1

When it comes to Sass and Bootstrap the recommendation/advantage that is most around is, "import only what you will use, so your final css will be smaller and will contain only the bootstrap parts you are using and not the whole bootstrap."

Wow that’s great but the "million dollar question" is how to do that?

Here in my case I am using npm and have boostrap and Sass installed via npm (bootstrap in node_modules).

In the example below I have the html of a navbar, in the example bootstrap classes are applied directly to html:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#conteudoNavbarSuportado" aria-controls="conteudoNavbarSuportado" aria-expanded="false" aria-label="Alterna navegação">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="conteudoNavbarSuportado">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(página atual)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
          <a class="dropdown-item" href="#">Ação</a>
          <a class="dropdown-item" href="#">Outra ação</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Algo mais aqui</a>
        </div>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Desativado</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search" placeholder="Pesquisar" aria-label="Pesquisar">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Pesquisar</button>
    </form>
  </div>
</nav>

I also have a file home.Sass and home ts. which import the bootstrap and generate the final file concatenated via webpack.

home.Sass:

@import '~bootstrap';
//Importação para gerar o css

home ts.

import 'bootstrap/dist/js/bootstrap';
//importação do bootstrap.js para gerar o .js final

The example works as expected, but in this case I am importing the entire bootstrap. Another criticism they make is to use the bootstrap classes directly in html, thus losing the semantics of the class name and end up having a very polluted html code, this code is a simple example of a navbar taken from the bootstrap documentation but I have much more polluted application code than that, full of py-x, mr-auto, p-0, align-items-x among other classes. Unfortunately while searching I only found the recommendations, but no clear example of how to implement them.

1 answer

1


Sorry for the "move" answer, but in this Bootstrap file you can delete the @imports that you don’t want.

After you’ve erased the @imports that you don’t want you need to recompile the .SCSS to generate your new .CSS just what you need. A tip is you rename your file .SCSS, because if you run an update or reinstall with NPM you may lose your changes as the new file will overwrite your.

But be careful, some of these @imports are fundamental to the proper functioning of the Framework, for example if you delete the import of transitions your navbar can be without the animation effects etc...

inserir a descrição da imagem aqui

Other details you can refer to in this reply how to use for example only the Bootstrap Grid etc I’d like to just use the bootstrap grid to make it responsive


About the classes "soiling" the HTML

I recommend you read this answer How not to repeat CSS code

But this image already answers your question, the more class in HTML, the less code in CSS, and the less class in HTML, the more code in CSS...

inserir a descrição da imagem aqui

And if you want to "extend" the properties of one class to another you can use the @extent

In the example below, using SCSS you can put the class property .p-0 (padding: 0) in elements with class .nav and .nav-item

.nav,
.nav-item{
  @extend .p-0;
}

More details you can see in this link that the questioner commented https://www.sitepoint.com/sass-semantically-extend-bootstrap/

  • I got it, I started commenting some of these Imports there to test, and really when recompiling the final file was, so far, 400 lines less, as I do not know the usefulness of some of these files, I will have to do in trial and error. Can I do the same with bootstrap . js? pq in my file home ts. I am importing everything too import 'bootstrap/dist/js/bootstrap';.

  • To finish you could show how I would "wipe" this html to get these html classes out of bootstrap and do this via Sass, that is, note that the code has nonsemantic classes like class="form-inline my-2 my-lg-0", class="btn btn-outline-success my-2 my-sm-0", etc. for purists this is bad practice. How can I be taking it out of html and putting it into my home scss.?

  • Just explaining that last comment. I wanted to find a way to "extend" (something similar to @extend from Sass) the bootstrap classes to a custom class, so instead of doing class="btn btn-outline-success my-2 my-sm-0" I could do something like class="btn-confirm" and would have the same result.

  • I found this contents that shows how to use Sass for this /. It seems to me that it is exactly as I imagined, it is through the @extend Sass. You can add this example in your reply which I mark as completed. I will also edit the question title.

  • @Matheussaraiva now understood better what you intended! I made the edit in reply and even includes this link that you spoke!

  • 1

    In order to leave a secure link (that will not go offline in the future), about using semantic classes with bootstrap and Sass, I am leaving a question done here in the stack network.

  • Hugão, have you noticed that Chrome is putting a black "Outline" on everything? Links, form elements.

  • @Sam Great Uncle, I’m with Chrome on iOS and here’s a so https://prnt.sc/snr7zb check if there’s some accessibility config that you’ve activated... what I had noticed 'and that Chrome has changed the color of inputs with autocomplete, but it has a time

  • @Sam olha ai https://developers.google.com/web/updates/2020/05/nic83#Forms I think that was the problem... but it seems that with a simple override by CSS you change this Outline (I haven’t tested it yet, but I think you can solve it)

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.