0
I recently saw that some programmers are using left
and translate
, instead of left
and margin
.
Example below
.container{
width 100%;
border: 1px solid black;
height:300px;
}
.bloco1{
width:50px;
height:50px;
background-color:red;
position:absolute;
left:50%;
transform:translate(-50%, 50%);
}
.bloco2{
width:50px;
height:50px;
background-color:red;
position:absolute;
top:35%;
left:50%;
margin-left:-5%;
}
<div class="container">
<div class="bloco1"></div>
<div class="bloco2"></div>
</div>
What do you mean? It depends on the scope of how you will use.... talking only so it is difficult to give you an answer. In your block2 for example to center horizontally, simply remove the
position:absolute
and putmargin: auto
that it stays in the center.... Explain better what you need.– hugocsl
want to know the best way to position an element in the center responsibly
– Hudson Holanda
In this case Transform:Translate can be better pq facilitates to center both vertically and horizontally, independent of the width and height of the elements. Using margin vc may need "perfect numbers" to find optimal distances and even then may not be 100% responsive depending on the screen. But every case is a case
– hugocsl