【问题标题】:Inline SVG only scaling to parent container in Google Chrome内联 SVG 仅缩放到 Google Chrome 中的父容器
【发布时间】:2020-02-08 19:02:35
【问题描述】:

编辑:将min-height: 0; 应用于.svg-container 会修复容器大小,但在 Edge 中,svg 元素会溢出其容器。


我正在尝试使内联 SVG 元素缩放到其父容器的高度。父容器的大小使用 CSS 网格(我也尝试过弹性框)。

通过将元素 max-height 属性设置为 100%,在 Google Chrome 中查看时,我已经能够按照需要进行此操作。但是,在其他浏览器中查看时,SVG 似乎忽略了此规则。

这些是我尝试过的浏览器:

  • Google Chrome:版本 80.0.3987.87(官方版本)(64 位)-这个工作
  • Microsoft Edge:44.18362.449.0
  • Firefox 开发者版:73.0b12(64 位)
  • Firefox:72.0.2(64 位)

这是它在 Google Chrome 中的样子(期望的结果):

这是 FFDE 中的样子(也在常规 FF 和 Edge 中):

bodyhtml 元素都具有 height: 100vh; 规则,所有其他子大小都派生自 %fr

我非常感谢任何有关如何确保在容器使用 css grid 或 flex 时元素不超过其父容器的 100% 的任何指导,因为到目前为止我在其他线程中找到的解决方案都没有奏效对我来说。

这是一个jsfiddle,下面我将包含代码。感谢您的宝贵时间。

    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    html,
    body {
      width: 100vw;
      height: 100vh;
      font-family: Arial, Helvetica, sans-serif;
      display: flex;
      justify-content: center;
    }
    
    .container {
      display: grid;
      grid-template-rows: 1fr 2fr 3fr;
      width: 100%;
      max-height: 100%;
      max-width: 540px;
    }
    
    .svg-container {
      border: 1px solid black;
    }
    
    svg {
      width: 100%;
      height: 100%;
      max-height: 100%;
    }
 <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <link rel="stylesheet" href="style.css">
      <title>Document</title>
    </head>
    
    <body>
      <div class="container">
        <div class="svg-container">
          <svg viewBox='0 0 3.904897 29.412678' xmlns='http://www.w3.org/2000/svg' preserveAspectRatio='none'>
            <path
              d='M 0,25.550255 V 3.8624222 H 2.01753 V 25.550255 H 0 m 2.850575,3.862422 H 2.551199 C 1.142257,29.412677 0,27.979719 0,26.211588 v -0.661333 h 2.01753 v 0.981485 c 0.0024,0.884983 0.574154,1.599169 1.2756,1.593463 h 0.611767 v 1.287474 z M 2.850575,0 H 2.5512 C 1.142257,0 0,1.4329586 0,3.2010897 V 3.8624222 H 2.01753 V 2.8809378 C 2.01993,1.9959549 2.591684,1.2817684 3.29313,1.2874741 H 3.904897 V 0 Z' />
          </svg>
    
        </div>
        <div class="svg-container">
          <svg viewBox='0 0 3.904897 29.412678' xmlns='http://www.w3.org/2000/svg' preserveAspectRatio='none'>
            <path
              d='M 0,25.550255 V 3.8624222 H 2.01753 V 25.550255 H 0 m 2.850575,3.862422 H 2.551199 C 1.142257,29.412677 0,27.979719 0,26.211588 v -0.661333 h 2.01753 v 0.981485 c 0.0024,0.884983 0.574154,1.599169 1.2756,1.593463 h 0.611767 v 1.287474 z M 2.850575,0 H 2.5512 C 1.142257,0 0,1.4329586 0,3.2010897 V 3.8624222 H 2.01753 V 2.8809378 C 2.01993,1.9959549 2.591684,1.2817684 3.29313,1.2874741 H 3.904897 V 0 Z' />
          </svg>
        </div>
        <div class="svg-container">
          <svg viewBox='0 0 3.904897 29.412678' xmlns='http://www.w3.org/2000/svg' preserveAspectRatio='none'>
            <path
              d='M 0,25.550255 V 3.8624222 H 2.01753 V 25.550255 H 0 m 2.850575,3.862422 H 2.551199 C 1.142257,29.412677 0,27.979719 0,26.211588 v -0.661333 h 2.01753 v 0.981485 c 0.0024,0.884983 0.574154,1.599169 1.2756,1.593463 h 0.611767 v 1.287474 z M 2.850575,0 H 2.5512 C 1.142257,0 0,1.4329586 0,3.2010897 V 3.8624222 H 2.01753 V 2.8809378 C 2.01993,1.9959549 2.591684,1.2817684 3.29313,1.2874741 H 3.904897 V 0 Z' />
          </svg>
        </div>
      </div>
    </body>
    
    </html>

【问题讨论】:

  • 您需要 min-height: 0; 到 SVG 容器。这是以下内容的副本:stackoverflow.com/q/43311943/8620333
  • @TemaniAfif 这解决了 FF 上的问题,但没有解决 Edge 上的问题。

标签: html css svg css-grid


【解决方案1】:

添加 min-height:0 以解决大多数浏览器中的问题(相关:Prevent content from expanding grid items),对于 Edge,您需要将 height:0;min-height:100% 添加到 SVG。最后一个修复将删除高度百分比值的使用,这会导致 Edge 出现问题。

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  width: 100vw;
  height: 100vh;
  font-family: Arial, Helvetica, sans-serif;
  display: flex;
  justify-content: center;
}

.container {
  display: grid;
  grid-template-rows: 1fr 2fr 3fr;
  width: 100%;
  max-height: 100%;
  max-width: 540px;
}

.svg-container {
  border: 1px solid black;
  min-height: 0;
}

svg {
  width: 100%;
  height: 0;
  min-height: 100%;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="style.css">
  <title>Document</title>
</head>

<body>
  <div class="container">
    <div class="svg-container">
      <svg viewBox='0 0 3.904897 29.412678' xmlns='http://www.w3.org/2000/svg' preserveAspectRatio='none'>
            <path
              d='M 0,25.550255 V 3.8624222 H 2.01753 V 25.550255 H 0 m 2.850575,3.862422 H 2.551199 C 1.142257,29.412677 0,27.979719 0,26.211588 v -0.661333 h 2.01753 v 0.981485 c 0.0024,0.884983 0.574154,1.599169 1.2756,1.593463 h 0.611767 v 1.287474 z M 2.850575,0 H 2.5512 C 1.142257,0 0,1.4329586 0,3.2010897 V 3.8624222 H 2.01753 V 2.8809378 C 2.01993,1.9959549 2.591684,1.2817684 3.29313,1.2874741 H 3.904897 V 0 Z' />
          </svg>

    </div>
    <div class="svg-container">
      <svg viewBox='0 0 3.904897 29.412678' xmlns='http://www.w3.org/2000/svg' preserveAspectRatio='none'>
            <path
              d='M 0,25.550255 V 3.8624222 H 2.01753 V 25.550255 H 0 m 2.850575,3.862422 H 2.551199 C 1.142257,29.412677 0,27.979719 0,26.211588 v -0.661333 h 2.01753 v 0.981485 c 0.0024,0.884983 0.574154,1.599169 1.2756,1.593463 h 0.611767 v 1.287474 z M 2.850575,0 H 2.5512 C 1.142257,0 0,1.4329586 0,3.2010897 V 3.8624222 H 2.01753 V 2.8809378 C 2.01993,1.9959549 2.591684,1.2817684 3.29313,1.2874741 H 3.904897 V 0 Z' />
          </svg>
    </div>
    <div class="svg-container">
      <svg viewBox='0 0 3.904897 29.412678' xmlns='http://www.w3.org/2000/svg' preserveAspectRatio='none'>
            <path
              d='M 0,25.550255 V 3.8624222 H 2.01753 V 25.550255 H 0 m 2.850575,3.862422 H 2.551199 C 1.142257,29.412677 0,27.979719 0,26.211588 v -0.661333 h 2.01753 v 0.981485 c 0.0024,0.884983 0.574154,1.599169 1.2756,1.593463 h 0.611767 v 1.287474 z M 2.850575,0 H 2.5512 C 1.142257,0 0,1.4329586 0,3.2010897 V 3.8624222 H 2.01753 V 2.8809378 C 2.01993,1.9959549 2.591684,1.2817684 3.29313,1.2874741 H 3.904897 V 0 Z' />
          </svg>
    </div>
  </div>
</body>

</html>

【讨论】:

    【解决方案2】:

    如果您确定 svg 容器的数量,那么以下内容适用于 Firefox 和 Chrome

    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    html,
    body {
      width: 100vw;
      height: 100vh;
      font-family: Arial, Helvetica, sans-serif;
    }
    
    .container {
      width: 100%;
      height: 100%;
    }
    
    .svg-container:nth-child(1) {
      height: calc(100% / 6);
      border: 1px solid black;
    }
    
    .svg-container:nth-child(2) {
      height: calc(100% / 3);
      border: 1px solid black;
    }
    
    .svg-container:nth-child(3) {
      height: calc(100% / 2);
      border: 1px solid black;
    }
    svg {
      width: 100%;
      height: 100%;
      max-height: 100%;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <link rel="stylesheet" href="style.css">
      <title>Document</title>
    </head>
    
    <body>
      <div class="container">
        <div class="svg-container">
          <svg viewBox='0 0 3.904897 29.412678' xmlns='http://www.w3.org/2000/svg' preserveAspectRatio='none'>
            <path
              d='M 0,25.550255 V 3.8624222 H 2.01753 V 25.550255 H 0 m 2.850575,3.862422 H 2.551199 C 1.142257,29.412677 0,27.979719 0,26.211588 v -0.661333 h 2.01753 v 0.981485 c 0.0024,0.884983 0.574154,1.599169 1.2756,1.593463 h 0.611767 v 1.287474 z M 2.850575,0 H 2.5512 C 1.142257,0 0,1.4329586 0,3.2010897 V 3.8624222 H 2.01753 V 2.8809378 C 2.01993,1.9959549 2.591684,1.2817684 3.29313,1.2874741 H 3.904897 V 0 Z' />
          </svg>
    
        </div>
        <div class="svg-container">
          <svg viewBox='0 0 3.904897 29.412678' xmlns='http://www.w3.org/2000/svg' preserveAspectRatio='none'>
            <path
              d='M 0,25.550255 V 3.8624222 H 2.01753 V 25.550255 H 0 m 2.850575,3.862422 H 2.551199 C 1.142257,29.412677 0,27.979719 0,26.211588 v -0.661333 h 2.01753 v 0.981485 c 0.0024,0.884983 0.574154,1.599169 1.2756,1.593463 h 0.611767 v 1.287474 z M 2.850575,0 H 2.5512 C 1.142257,0 0,1.4329586 0,3.2010897 V 3.8624222 H 2.01753 V 2.8809378 C 2.01993,1.9959549 2.591684,1.2817684 3.29313,1.2874741 H 3.904897 V 0 Z' />
          </svg>
        </div>
        <div class="svg-container">
          <svg viewBox='0 0 3.904897 29.412678' xmlns='http://www.w3.org/2000/svg' preserveAspectRatio='none'>
            <path
              d='M 0,25.550255 V 3.8624222 H 2.01753 V 25.550255 H 0 m 2.850575,3.862422 H 2.551199 C 1.142257,29.412677 0,27.979719 0,26.211588 v -0.661333 h 2.01753 v 0.981485 c 0.0024,0.884983 0.574154,1.599169 1.2756,1.593463 h 0.611767 v 1.287474 z M 2.850575,0 H 2.5512 C 1.142257,0 0,1.4329586 0,3.2010897 V 3.8624222 H 2.01753 V 2.8809378 C 2.01993,1.9959549 2.591684,1.2817684 3.29313,1.2874741 H 3.904897 V 0 Z' />
          </svg>
        </div>
      </div>
    
    
    
    </body>
    
    </html>

    【讨论】:

    • 本例中的 SVG 现在有效地缩放到具有固定高度的父级。我的问题是当父容器高度由 % 设置时缩放
    • 那你想要多高? @Senipah
    • 在原始示例中,第一个孩子是 1/6 第二个 2/6,第三个是 100vh 的 3/6。您的计算将使 100vh 的每 1/3。 Calc 正在有效地计算固定大小。如果没有固定大小,是否无法使 SVG 响应父容器?
    • @Senipah 更新了我的答案。
    • 欣赏它,但我希望能够使用 grid 或 flex 解决这个问题,而不是本质上计算比率,因为这最终会被一些未知数量的组件使用。
    【解决方案3】:

    我不确定这正是你想要达到的效果,但我已经着手减少标记和样式,看看我是否可以通过单亲@实现你想要的效果987654321@ 容器和 3 个&lt;svg&gt; 孩子。

    工作示例:

    .svg-container {
    display: flex;
    flex-direction: column;
    width: 60vw;
    height: 100vh;
    border: 1px solid rgb(0, 0, 0);
    }
        
    svg {
    width: 100%;
    border: 1px solid rgb(0, 0, 0);
    }
        
    svg:nth-of-type(1) {
    height: calc(100% / 6 * 1);
    }
    
    svg:nth-of-type(2) {
    height: calc(100% / 6 * 2);
    }
    
    svg:nth-of-type(3) {
    height: calc(100% / 6 * 3);
    }
    <div class="svg-container">
    <svg viewBox='0 0 3.904897 29.412678' xmlns='http://www.w3.org/2000/svg' preserveAspectRatio='none'>
    <path d='M 0,25.550255 V 3.8624222 H 2.01753 V 25.550255 H 0 m 2.850575,3.862422 H 2.551199 C 1.142257,29.412677 0,27.979719 0,26.211588 v -0.661333 h 2.01753 v 0.981485 c 0.0024,0.884983 0.574154,1.599169 1.2756,1.593463 h 0.611767 v 1.287474 z M 2.850575,0 H 2.5512 C 1.142257,0 0,1.4329586 0,3.2010897 V 3.8624222 H 2.01753 V 2.8809378 C 2.01993,1.9959549 2.591684,1.2817684 3.29313,1.2874741 H 3.904897 V 0 Z' />
    </svg>
    
    <svg viewBox='0 0 3.904897 29.412678' xmlns='http://www.w3.org/2000/svg' preserveAspectRatio='none'>
    <path d='M 0,25.550255 V 3.8624222 H 2.01753 V 25.550255 H 0 m 2.850575,3.862422 H 2.551199 C 1.142257,29.412677 0,27.979719 0,26.211588 v -0.661333 h 2.01753 v 0.981485 c 0.0024,0.884983 0.574154,1.599169 1.2756,1.593463 h 0.611767 v 1.287474 z M 2.850575,0 H 2.5512 C 1.142257,0 0,1.4329586 0,3.2010897 V 3.8624222 H 2.01753 V 2.8809378 C 2.01993,1.9959549 2.591684,1.2817684 3.29313,1.2874741 H 3.904897 V 0 Z' />
    </svg>
    
    <svg viewBox='0 0 3.904897 29.412678' xmlns='http://www.w3.org/2000/svg' preserveAspectRatio='none'>
    <path d='M 0,25.550255 V 3.8624222 H 2.01753 V 25.550255 H 0 m 2.850575,3.862422 H 2.551199 C 1.142257,29.412677 0,27.979719 0,26.211588 v -0.661333 h 2.01753 v 0.981485 c 0.0024,0.884983 0.574154,1.599169 1.2756,1.593463 h 0.611767 v 1.287474 z M 2.850575,0 H 2.5512 C 1.142257,0 0,1.4329586 0,3.2010897 V 3.8624222 H 2.01753 V 2.8809378 C 2.01993,1.9959549 2.591684,1.2817684 3.29313,1.2874741 H 3.904897 V 0 Z' />
    </svg>
    
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多