背景SVG不会在Bootstrap div列中缩放

时间:2016-12-01 00:40:34

标签: html css twitter-bootstrap svg responsive-design

我希望能够在Bootstrap的<div>中嵌入的<div class="col-xs-N">上扩展大型SVG。

代码是这样的:

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">    
    <style type="text/css">
      .my-img {
      background-size: cover;    
      padding-bottom: 100%;
      background: url(https://upload.wikimedia.org/wikipedia/commons/e/ec/Arctic_big.svg) no-repeat center center;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="col-xs-offset-5 col-xs-2">
    <div class="my-img"> </div>
      </div>
      <div class="col-xs-5">
      </div>
    </div>
  </body>
</html>

任何想法都非常感激。

2 个答案:

答案 0 :(得分:3)

您需要将background-size: cover;放在background: url(https://upload.wikimedia.org/wikipedia/commons/e/ec/Arctic_big.svg) no-repeat center center;之后。在您加载图像之前调用background-size,因此它没有生效。

变化:

.my-img {
  background-size: cover;
  padding-bottom: 100%;
  background: url(https://upload.wikimedia.org/wikipedia/commons/e/ec/Arctic_big.svg) no-repeat center center;
  }

为:

.my-img {
      padding-bottom: 100%;
      background: url(https://upload.wikimedia.org/wikipedia/commons/e/ec/Arctic_big.svg) no-repeat center center;
      background-size: cover;
      }

示例jsFiddle

答案 1 :(得分:1)

只需将 min-height 添加到您的css。

&#13;
&#13;
 .my-img {     
        padding-bottom: 100%;
        min-height:100vh;         background: url(https://upload.wikimedia.org/wikipedia/commons/e/ec/Arctic_big.svg) no-repeat center center;
        background-size: cover;       
      }
&#13;
<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">    
    
  </head>
  <body>
    <div class="container">
      <div class="col-xs-offset-5 col-xs-2">
    <div class="my-img"> </div>
      </div>
      <div class="col-xs-5">
      </div>
    </div>
  </body>
</html>
&#13;
&#13;
&#13;