1
Let’s say I’ll position a div
100px
of height
and width
, (a square) then I position this square in margin-left 40%
and margin-top 20%
, the square is more or less centered, right? Now I will put a text written "hi" inside it with the tag <p>
, now if I use the magnifying glass, the following happens, only the right side and the bottom part of the square will expand while its other two sides do not expand and the text remains intact, let’s say I want everything to expand without the sides moving as it would do this ?
About the code I mentioned above:
.caracteristicas{
position: absolute;
background-color: green;
height: 100px;
width: 100px;
margin-left: 40%;
margin-top: 20%;
}
<html>
<head>
<title>Aprendendo a centralizar</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="caracteristicas">
<p>oi</p>
</div>
</body>
</html>
Just a tip, stop while it’s time to use the
position: absolute
. Try to center things using theposition: relative
that will have better results. But, if you combine theabsolute
at themargin
, in one way or another is not collaborating to structure work. Whenever useabsolute
match the attributeleft
,top
,right
,bottom
. Something liketop: 20%
for example.– William Novak
In your example, to make the element 100% centered, resizing the browser or applying the magnifying glass, remove the
position
, changemargin-left: 40%, margin-top: 20%
formargin: 20% auto 0 auto
. This ensures 100% centering of the object.– William Novak