Place square svg to the center of the screen, scale it to fit the screen's width and height with some constraints

后端 未结 6 1420
臣服心动
臣服心动 2021-02-14 14:03

It seem to be a silly question and possibly a dupe, but I couldn\'t find a good solution for a while, so finally dare to ask.

I want to place an

6条回答
  •  再見小時候
    2021-02-14 14:49

    The flexbox solution would be my first choice. Since someone else has already answered with that idea I came up with this different approach.

    Basically it is the absolute centering technique and rely on absolute position and auto margin to center the element.

    body {
      height: 100vh;
      margin: 0;
    }
    
    svg#picture {
      max-height: 15cm;
      max-width: 15cm;
      height:100%;
      width:100%;
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      margin:auto;
    }
    
      
    

提交回复
热议问题