I have div a container with two elements inside, el and an absolute positioned element pp. el shares the same width and height as the container and their width can be dynamically changed.
pp should be centered to el and the container. I have a red marker in the middle of the pp div to mark it's center.
With a 30px width to the container and el, to center pp I used, translateX(-50%). From my understanding this should center pp to el. But actual result is not that. -50% moves the div a little further to the left. When tweaking -43% centers the red marker. But this -43% is not applicable to another el div with a different width.
How do I always center the pp container using translateX to another div of any size.
.container {
width: 30px;
margin: 0 auto;
}
.el {
width: 30px;
height: 30px;
background-color: pink;
border-radius: 100%;
}
.pp {
position: absolute;
max-width: 300px;
background-color: yellow;
}
.pp1 {
transform: translateX(-50%);
}
.pp2 {
transform: translateX(-43%);
}
Not centered when translateX is -50%
<br />
<br />
<div class="container">
<div class="el">
elem
</div>
<div class="pp pp1">
<div style="width:10px; height: 10px; margin: 0 auto; background-color: red;"></div>
popoverasdasd asjdjasd sdfsdfsdfsdf
</div>
</div>
<br />
<br />
<br />
<br />
Expected result comes when translateX is -43%
<br />
<br />
<div class="container">
<div class="el">
elem
</div>
<div class="pp pp2">
<div style="width:10px; height: 10px; margin: 0 auto; background-color: red;"></div>
popoverasdasd asjdjasd sdfsdfsdfsdf
</div>
</div>
Fiddle: https://jsfiddle.net/v6o5z8a0/
` tag does not use or need a closing slash and never has. – Rob Feb 01 '18 at 13:06