To center something in the center of the page there are several techniques.
Explaining the margin
its function as observed is to define a margin for some element, and when its value is auto
means that the browser will set the margin automatically, follows example:
.quadrado{
background-color: red;
display: block;
height: 100px;
margin: 0 auto;
width: 100px;
}
<div class="quadrado"></div>
Note that I am using margin: 0 auto
zero represents the margin-top
and the margin-bottom
, and the value auto
represents the margin-left
and the margin-right
summarizing is an abbreviation.
In my example the right and left margins are automatic and with this the browser automatically sets its positioning, and when the element is a block with defined width it centralizes horizontally.
To align horizontally and vertically I would use this technique:
body, .container, html{
height: 100%;
}
.container{
display: flex;
align-items: center;
justify-content: center;
}
.quadrado{
background-color: red;
display: inline-block;
height: 100px;
width: 100px;
}
<div class="container">
<div class="quadrado"></div>
</div>
obs. When you set margin: auto
you are assigning an automatic margin to top right bottom left
With this code the div does not even appear on the screen, you tested it where? https://jsfiddle.net/twhxf5k4/ does not work
– Caetano Sincero