To make it clear, I have two squares they both have position: absolute
and are inside another div that has a relative position.
This is related more to a UI design and not about centering two divs into each other and use if for web, it's more a UI thing or an animation thing for popups or similar.
That means it could be that I have a circle that is bigger than box1 somewhere. Then I use an invisible box that has position relative that contains box2 as a child
Both of the squares have a different size.
New example of issue here: https://jsfiddle.net/mgznef98/2/
The red box is not allowed to change position The border somehow has to be centered. And I put it inside a box2-container to support it, because box1 could be another shape and not a simple box if I would edit it
"some-container" is just the "thing" that would move around the website and carry all of the "boxes", so it can't be used with table-cell.
If you would look at this Rotate objects around circle using CSS? On this page they rotate elements around the center of another element, but they're not inside the center. And I'm trying to put something in a center. That would be as example a square with a border that would rotate around.
I've managed to center the blue square inside the red square, somehow. I'm not even sure if it is exactly centered. https://jsfiddle.net/mgznef98 on this one before, but for the border one, I don't really know.
My question is, I think there is some math behind it. I think there has to be some formula to find out what to put inside "top" and "left" so I can center the blue "border" box exactly inside the red box when the red box changes the width and height.
What I know is that it changes depending on the size of box2 and its container and box1.
Example:
Let's say again I have two boxes but one of them is invisible, in this case the container of box2. And box2 is not a box instead it is "something" that rotates. And box1 is not a box, instead it is an element a shape or something that has a center. And box2 has a border instead of a background color, and that border has to rotate around the center of the element. The thing is that box2 is bigger than box1.
.some-container {
top: 30px;
left: 30px;
position: relative;
}
.box1 {
width: 20px;
height: 20px;
background: red;
position: absolute;
}
.box2-container {
position: relative;
height: 20px;
width: 20px;
}
.box2 {
width: 40px;
height: 40px;
border: 2px solid blue;
position: absolute;
top: -50%; /* These values have to be changed when I want to recenter a new width and height from box1 */
left: -50%;
border-radius: 50%;
border-color: blue blue blue transparent;
border-width: 2px;
animation: box2-rotation 3s linear infinite;
}
@keyframes box2-rotation {
0% {transform: rotate(0deg);}
100% {transform: rotate(360deg);}
}
<div class=some-container>
<div class=box1>
</div>
<div class=box2-container>
<div class=box2>
</div>
</div>
</div>