html - 将图像放入 Bootstrap SVG
问题描述
所以我正在用 Bootstrap 5 构建一个网页,我正在使用的元素之一(来自 BS5 模板)是这个
<div class="col" data-aos="fade-up" data-aos-delay="200">
<div class="card shadow-sm">
<svg class="bd-placeholder-img card-img-top" width="100%" height="225" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: Thumbnail" preserveAspectRatio="xMidYMid slice" focusable="false"><title>Placeholder</title><rect width="100%" height="100%" fill="#55595c"/><text x="50%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text></svg>
<div class="card-body">
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-outline-secondary">Learn More</button>
</div>
</div>
</div>
</div>
</div>
如您所见,上面有一个svg
带有rect
内部的标签。我想显示一个图像来代替rect
(或在其中,只需要它与 的尺寸相同rect
),同时保留当前显示的文本rect
。
现在我知道为了在 an 中显示图像,svg
您需要clip path
使用所需的形状定义 a ,但这看起来像是为更好的实现而编写的。
谁能指出我如何在rect
不改变尺寸的情况下将图像放置在原处或原处,并且仍然保留显示的文本?
解决方案
您可以简单地使用<image>
标签,通过设置width
和/或height
as 100%
- image 将填充整个容器区域。
<svg class="bd-placeholder-img card-img-top" width="100%" height="225" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: Thumbnail" preserveAspectRatio="xMidYMid slice" focusable="false">
<title>Placeholder</title>
<rect width="100%" height="100%" fill="#55595c" />
<image href="https://www.wpbeginner.com/wp-content/uploads/2018/12/svgnoqualityloss.png" width="100%"/>
<text x="50%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text>
</svg>
JSFiddle:https ://jsfiddle.net/rcuq7sLb/6/
您还可以使用图像的宽度和高度,例如height: 100%
JSFiddle:https ://jsfiddle.net/rcuq7sLb/7/
推荐阅读
- java - 如何在android中动态获取java字符串常量?
- javascript - DOM 事件加载挑战与 D3 图
- c# - Unity-使对象跟随但不撞到另一个对象?
- java - 在将音库作为嵌入式资源加载时,我应该将缓冲流链接到输入流还是使用 URL 类?
- mysql - 当每笔交易的数量存储在单独的字段中时,使用 GROUP BY 计算购买次数
- ruby-on-rails - 具有扩展名 .woff 的 Rails 5 字体文件不能在 AWS EB 中进行预编译
- javascript - 使用回调或承诺使异步代码像 nodejs 中的同步一样工作
- java - JavaFX 登录访问被拒绝
- java - HashMap 为 String 返回 null 但为 Integer 返回值
- arrays - 使用 BASH 在一行上输出满足条件的数组的所有元素