Div does not center

Asked

Viewed 37 times

-1

.container-form {
    width: 300px;
    height: 300px;
    background-color: rgb(22, 20, 20);
    position: relative;
 }

.filha {
    width: 100px;
    height: 100px;
    background-color: azure;
    position: absolute;
    top: 50%;
    left: 50%;
}

*{   
    margin: 0px;
    padding: 0px;
}
<div class="container-form">    
    <div class="filha">
    </div>
</div>

inserir a descrição da imagem aqui

I have tried everything and I cannot center any div inside another or any element in relation to the body... It’s like I put 50% in one direction, but in practice it’s like 70%... Someone can help me solve this problem?

1 answer

-2

You must add transform: translate(-50%,-50%);. This property moves an element from its current position (according to the parameters given for the X axis and the Y axis). See resolved:

.container-form {
    width: 300px;
    height: 300px;
    background-color: rgb(22, 20, 20);
    position: relative;
 }

.filha {
    width: 100px;
    height: 100px;
    background-color: azure;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%)
}

*{   
    margin: 0px;
    padding: 0px;
}
<div class="container-form">    
    <div class="filha">
    </div>
</div>

  • 1

    can explain how the transform: translate(-50%,-50%) centralizes the div? this will improve the answer and the understanding of who reads the question, is better than "use it there" :)

  • our downvote in a correct answer, this stack is getting worse and worse. These point hunters are out of control

  • 1

    I can not speak for others but, I did not give negative but I believe it is because of the answer "no explanation" as I had commented, because it was edited a short time ago, or maybe pq already has an answer with the same solution

Browser other questions tagged

You are not signed in. Login or sign up in order to post.