【发布时间】:2016-08-31 07:30:50
【问题描述】:
来自 phrogz.net 的 example 展示了如何在纵横比接近 1:2 或 2:1 的页面上呈现 SVG 元素。无论哪种方式,SVG 都以 1:1 的比例显示。
如何同时满足一个 img 元素的长宽比保持 + centering-in-div 的要求?
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Sizing SVG & IMG to Fill a Container</title>
<style type="text/css" media="screen">
html, body { background:#eee; margin:0; padding:0; height:100%; }
#foo { position:absolute; left:2%; width:46%; top:2%; height:96%; background:red; }
#bar { position:absolute; left:52%; width:46%; top:2%; height:96%; background:grey; }
svg { position:absolute; top:0; left:0; width:100%; height:100%; background:green; }
img { position:absolute; top:0; left:0; width:100%; height:100%; background:green; }
.face { stroke:#000; stroke-width:20px; stroke-linecap:round }
</style>
</head><body>
<div id="foo">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" baseProfile="full" viewBox="-350 -250 700 500">
<circle r="200" class="face" fill="yellow"/>
<path fill="none" class="face"
transform="translate(-396,-230)"
d="M487.41,282.411c-15.07,36.137-50.735,61.537-92.333,61.537 c-41.421,0-76.961-25.185-92.142-61.076" />
<circle cx="-60" cy="-50" r="20" fill="#000"/>
<circle cx="60" cy="-50" r="20" fill="#000"/>
</svg>
</div>
<div id="bar">
<img src="https://openclipart.org/image/800px/svg_to_png/196201/Model-T-Ford.png&disposition=attachment" />
</div>
</body></html>
【问题讨论】: