Rewritten title: Center a fixed HTML square or circle element horizontally and vertically when resized
P粉455093123
P粉455093123 2024-04-04 12:31:51
0
1
454

How to center a square or circular fixed html element of varying sizes horizontally and vertically? I think the difficulty arises from the fact that width and height must remain equal.

P粉455093123
P粉455093123

reply all(1)
P粉002572690

I think this problem can be easily solved with flex. Wrap related square or circle elements from a parent element (div). Then use justify-content and align-item properties to the parent tag. Here I use 100vh 100vw as the height and width of the parent container for demonstration. But you can change it according to your purpose.

.container{
  min-width:100vw;
  min-height:100vh;
  display:flex;
  align-items:center;
  justify-content:center;
}
.my-element{
  width:100px;
  height:100px;
  background-color:#000000;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template