Let’s see...
I’m going to expose a alternative to the @Uilherme-Henriques response using the concept of flexbox CSS 3. This alternative also aligns more with your centering mode.
Before CSS 3, it was the "default" mode. Today it is not mainstream or cool. Fashion stuff..
Centering a <div>
A <div>
, to be centralized, must have a defined size from horizontal (width). Their banks must be configured to be automatic. Example:
#fundo {
width: 100%;
height: 100px;
background-color: #c33;
}
#exemplo {
width: 200px;
height: 50px;
margin: auto;
background-color: #fff;
text-align: center;
}
<div id="fundo">
<br />
<div id="exemplo"><div> centralizado horizontalmente</div>
</div>
Note that this does not centralize the internal elements of <div>
. However, this will still be reference for the internal elements to it - including other elements <div>
. Thus:
- The internal elements of a
<div>
parent are not centralized when centralizing the <div>
father. They need to be centralized independently following the rules already exposed: defined size and automatic margin.
- For text and images, you should use text alignment (
text-align: center
).
The size of a <div>
father must be larger than the internal elements so that the phenomenon of horizontal centralization can be observed. If they are of equal size (e. g width: 100%;
in parent and child elements), centralization occurs however without visual observation. They will be superimposed.
See the following example. Note that I differentiated the heights so that the centralization and overlap can be observed.
#fundo {
height: 100px;
background-color: #c33;
}
#exemplo {
height: 50px;
margin: auto;
background-color: #fff;
text-align: center;
}
<div id="fundo">
<br />
<div id="exemplo"><div> centralizado horizontalmente</div>
</div>
In most real-life cases, a programmer with no experience (or even a strong theoretical basis) could assume that nothing happened. Identical backgrounds as to color and identical heights corroborate for wrong premise. It is a common mistake.
Therefore, consider using margin: auto;
in all elements <div>
that you want to center inside the parent element. Can be individually:
.desligado {
...
margin: auto;
}
.ligado {
...
margin: auto;
}
... Or general:
.center > div { // ">" indica elementos-filho (descendente direto)
margin: auto;
}
Functional example
Follows your code functionally. Note that .center
was used only for flowering and visual observance of the phenomenon.
.ligado {
background: yellow;
width: 150px;
height: 150px;
border-radius: 50%;
}
.desligado {
background: gray;
width: 150px;
height: 150px;
border-radius: 50%;
}
.center {
text-align: center; /* centraliza o texto */
background-color: #eee; /* fornece cor ao fundo para melhor visualizar */
padding: 2em; /* 'desgrudado' das bordas do div-pai */
}
.center > div { /* centraliza os elementos 'div' dentro do 'div.center' */
margin: auto;
}
<div class="center">
<div id="1" class="ligado"></div>
<p>Quarto</p>
<div id="2" class="desligado"></div>
<p>Cozinha</p>
<div id="3" class="ligado"></div>
<p>Sala de estar</p>
</div>
Solved my problem
– Kelvin Mariano