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.
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';
.– Matheus Saraiva
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.?– Matheus Saraiva
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 likeclass="btn-confirm"
and would have the same result.– Matheus Saraiva
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.– Matheus Saraiva
@Matheussaraiva now understood better what you intended! I made the edit in reply and even includes this link that you spoke!
– hugocsl
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.
– Matheus Saraiva
Hugão, have you noticed that Chrome is putting a black "Outline" on everything? Links, form elements.
– Sam
@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
– hugocsl
@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)
– hugocsl