簡體   English   中英

按比例調整SVG背景圖像的大小

[英]resize an SVG background image proportionally

我想將SVG設置為高度為100px的背景圖像,並根據圖像的原始比例設置寬度(以避免變形)。 SVG的XML是

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   viewBox="0 0 600.09332 291.44"
   height="291.44"
   width="600.09332"
   xml:space="preserve"
   id="svg2"
   version="1.1">
  <!-- other markup omitted -->
</svg>

我正在使用的CSS是

.image {
  display: block;
  height: 100px;
  width: auto;
  background-image: url(../images/header_logo.svg);
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  background-color: transparent;
}

當我使用width: auto時,圖像完全不顯示。 如果我將其更改為width: 100px ,則顯示圖像,但寬度/高度比例變形。

請使用嵌入式圖像作為背景,以便正確維護比例: https : //jsfiddle.net/5cxr18jf/

<div class="wrapper">
   <h1>Other content</h1>
   <img class="background" src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/compass.svg">
</div>

.wrapper {
  width: 500px;
  height: 500px;
  background: #ddd;
  position: relative;
}

.background {
  position: absolute;
  height: 100%;
  top: 0;
  left: 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM