search

Home  >  Q&A  >  body text

Rewritten title: Center a fixed HTML square or circle element horizontally and vertically when resized

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粉455093123P粉455093123265 days ago460

reply all(1)I'll reply

  • P粉002572690

    P粉0025726902024-04-05 11:00:03

    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;
    }

    reply
    0
  • Cancelreply