CSS Calc Viewport Units Workaround?

前端 未结 4 974
慢半拍i
慢半拍i 2020-12-02 15:22

From what I\'ve seen in other answers, CSS viewport units can\'t be used in calc() statements yet. What I would like to achieve is the following statement:

4条回答
  •  情深已故
    2020-12-02 15:38

    Doing this with a CSS Grid is pretty easy. The trick is to set the grid's height to 100vw, then assign one of the rows to 75vw, and the remaining one (optional) to 1fr. This gives you, from what I assume is what you're after, a ratio-locked resizing container.

    Example here: https://codesandbox.io/s/21r4z95p7j

    You can even utilize the bottom gutter space if you so choose, simply by adding another "item".

    Edit: StackOverflow's built-in code runner has some side effects. Pop over to the codesandbox link and you'll see the ratio in action.

    body {
      margin: 0;
      padding: 0;
      background-color: #334;
      color: #eee;
    }
    
    .main {
      min-height: 100vh;
      min-width: 100vw;
      display: grid;
      grid-template-columns: 100%;
      grid-template-rows: 75vw 1fr;
    }
    
    .item {
      background-color: #558;
      padding: 2px;
      margin: 1px;
    }
    
    .item.dead {
      background-color: transparent;
    }
    
      
        Parcel Sandbox
        
        
      
    
      
        
    Item 1

提交回复
热议问题