【问题标题】:how to center a div horizontally, vertically, and maintain 1:1 aspect ratio?如何使 div 水平、垂直居中并保持 1:1 的纵横比?
【发布时间】:2018-02-16 21:17:46
【问题描述】:

我希望在 div 内有一个画布,该画布居中且无论其比例如何,都保持 1:1 的纵横比。

通过创建具有以下样式规则的包含 div,我能够使子元素(画布)垂直和水平居中。

display: "flex",
alignItems: "center",
justifyContent: "center",

但是,经过多次谷歌搜索,我无法找到一种强制纵横比的方法。使用顶部填充有一些奇怪的方法,但在弹性盒子的情况下似乎不起作用。

我可能决定更改画布在其包含的 div 中的相对大小,但我希望它保持居中并保持 1:1 的纵横比。哪些 css 规则可以满足这些约束?

【问题讨论】:

    标签: html css flexbox centering


    【解决方案1】:

    如果您的主div 是全屏的,您可以使用vmin。您需要将canvas flex-grow 和flex-shrink 设置为0,这样大小将不会响应,将min-width 设置为0 以缩小超过canvas (300px) 的隐含宽度),用flex-basis 定义宽度,用height 定义高度(与flex-basis 相同,比例为1:1):

    flex: 0 0 100vmin;
    min-width: 0;
    height: 100vmin;
    

    body {
      margin: 0;
    }
    
    .outerdiv {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    canvas.center {
      flex: 0 0 100vmin;
      min-width: 0;
      height: 100vmin;
      background-color: tomato;
    }
    <div class="outerdiv">
      <canvas class="center"></canvas>
    </div>

    如果你想要一个边距,使用calc() 两倍的边距:

    flex: 0 0 calc(100vmin - 10em);
    min-width: 0;
    height: calc(100vmin - 10em);
    margin: 5em;
    

    body {
      margin: 0;
    }
    
    .outerdiv {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    canvas.center {
      flex: 0 0 calc(100vmin - 10em);
      min-width: 0;
      height: calc(100vmin - 10em);
      background-color: tomato;
      margin: 5em;
    }
    <div class="outerdiv">
      <canvas class="center"></canvas>
    </div>

    【讨论】:

    • 有没有办法实现不依赖于视口相对大小的纵横比?
    【解决方案2】:

    您需要使用max-width 和max-height。

    .container {
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      width: 100%;
      height: 100%;
      background-color:#429bf4;
    }
    
    .image {
      position: absolute;
      max-width: 100%;
      max-height: 100%;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
    
    body {
      width: 100%;
      height: 100%;
      position: absolute;
      margin: 0;
    }
    <div class='container'>
      <img class='image' src='http://via.placeholder.com/300x300'>
    </div>

    Flex 示例

    .main {
      width: 200px;
    }
    .aspect1-1 {
      width:100%;
      padding-top:100%;
      position: relative;
    }
    
    .aspect1-1 div {
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      background-color:#429bf4;
      display:flex;
      justify-content:center;
      align-items:center;
     
    }
    <div class="main">
      <div class="aspect1-1">
        <div>
          <img src="http://placehold.it/100x100">
         </div>
       </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 2017-07-10
      • 2017-04-04
      • 2015-01-03
      相关资源
      最近更新 更多