- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个内联 <SVG>
定义它是 viewBox
但不是 height
或 width
.我曾经设置width/height
与 viewBox
相同的值在创建 SVG 时;好处是最终图像被缩放到我想要的 SVG 自然单位;问题是固定的 width/height
可能会在最后一页溢出父容器。删除 width/height
来自 <SVG>
导致图像呈现为父容器允许的最大大小,这几乎总是大于 SVG 的自然单位。
有没有办法缩放 <SVG>
到它的自然单位,但如果父容器太小,则按比例缩小(!)?
最佳答案
如果您不使用内联 svg(并且不使用 IE 缩放...),<img>
中的 svg是你的 friend 。
When an SVG file has a
viewBox
, and it is embedded within an<img>
, browsers will (nearly always) scale the image to match the aspect ratio defined in theviewBox
.
HTML
<figure>
<figcaption><code>img</code>, auto size</figcaption>
<div>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/91525/potofgold.svg">
</div>
</figure>
内联 SVG 缩放支持远没有... Read from this amazing article to learn more about how this works有关广泛支持的解决方案,请参见第 4 点。它建议使用 padding-bottom
hack,这在 iframe 嵌入中也很常见。
关于css - 将 SVG 缩放到它的 viewBox 或更小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51468513/
我有一个具有可变数量子元素的固定大小的 div。我不知道 children 的大小。目标是缩小它们以适合父级。 例子: .parent { width: 100px; height: 100p
我是一名优秀的程序员,十分优秀!