内联 SVG 仅缩放到 Google Chrome 中的父容器

Sen*_*pah 5 html css svg css-grid


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

边缘 svg 元素溢出其容器


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

通过将元素max-height属性设置为100%. 然而,当在其他浏览器中查看时,SVG 似乎忽略了这个规则。

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

  • 谷歌浏览器:版本 80.0.3987.87(官方版本)(64 位)——这个有效
  • Microsoft Edge:44.18362.449.0
  • Firefox 开发者版:73.0b12(64 位)
  • 火狐: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%;
    }
Run Code Online (Sandbox Code Playgroud)
 <!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>
Run Code Online (Sandbox Code Playgroud)

Tem*_*fif 3

添加min-height:0以修复大多数浏览器中的问题(相关:防止内容扩展网格项),对于 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%;
}
Run Code Online (Sandbox Code Playgroud)
<!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>
Run Code Online (Sandbox Code Playgroud)