I was re-structuring my web project so that it would be a little cleaner and tidier than before, and a question arose.
When there is a part in which a class is already assigned to several things, but you don't want them all to be the same, would it be necessary to use both a class (the one that defines what it is) and an id to style it?
I give an example:
/* ESTRUCTURA */
.flex-container {
display: flex;
align-items: stretch;
background-color: none;
}
.flex-container > div {
background-color: none;
color: black;
width: 100px;
margin: 5px;
text-align: center;
line-height: 30px;
font-size: 15px;
}
#contenido {
background-color: none;
float: left;
width: 100%;
padding: 15px;
margin-top: 7px;
text-align: center;
}
#contenido2{
background-color: FloralWhite;
float: left;
width: 100%;
padding: 15px;
margin: 5px;
margin-top: 7px;
text-align: center;
}
/* ESTRUCTURA IMÁGENES */
*,
*::before,
*::after {
box-sizing: border-box;
}
.grid {
display: grid;
margin: 0 auto;
grid-template-columns: repeat(var(--columnas), 1fr);
width: calc(100% - 20px);
max-width: 1200px;
gap: 10px;
}
.grid__item {
padding-top: 85%;
position: relative;
border: 1px solid;
}
.grid__img {
--object-fit: cover;
--object-position: center center;
display: block;
top: 0;
bottom: 0;
object-fit: var(--object-fit);
object-position: var(--object-position);
height: 100%;
width: 100%;
position: absolute;
}
@media screen and (min-width: 600px) {
.grid {
--columnas: 3;
}
}
@media screen and (min-width: 400px) and (max-width:599px) {
.grid {
--columnas: 2;
}
}
@media screen and (max-width: 399px) {
.grid {
--columnas: 1;
}
}
.limpiar {
clear: both;
}
<div class="flex-container" id="contenido"> <div style="flex-grow: 1">
<p class="lateral">
Gatos, gatos, muchos gatos<br>
¡Los gatos son adorables!<br>
</p>
</div>
<div style="flex-grow: 9">
<div class="grid">
<div class="grid__item">
<img class="grid__img" src="https://ichef.bbci.co.uk/news/410/cpsprodpb/8536/production/_103520143_gettyimages-908714708.jpg">
<div class="caption">
Texto a colocar
</div>
<div class="limpiar"></div>
</div>
</div>
</div>
<div class="flex-container" id="contenido2">
<div style="flex-grow: 10">
<p>Hola, Hola, Hola, Hola, Hola, Hola,<br>
Hola, Hola, Hola, Hola, Hola, Hola, Hola, <br>
</p>
</div>
</div>
In this case, it would be with the class flex-container
and the id
respective.
Recently, in a comment they told me that it was not recommended to use class
e id
in the same object, nor to mix both. Then the doubt arose:
How are you supposed to style two different objects with the same class?
I give the example of flex-container
being a class that gives shape, so if it has more elements, it has to have the same class.
Not that I have much of an idea either, but if you want to modify something with the same class already defined but you don't want it to be the same, in my opinion, the use of an id becomes necessary. (To avoid using style=""
).
If not, why? What would be a good way to style it then?