html - 在 CSS 中显示来自十个图标的图像中的单个图标,它们也必须缩放?
问题描述
在 CSS 中,如果我有一个包含十个 100x100 像素的图标的单个文件。它是icons.png
。
我想要我的 html 中的图像
<img alt="The third image in the file, but scaled to 30x30"> Menu
但是,我想要文件中的第三个图像(位置 [200,0] 和宽度 100x100) ,但是将完整的 100x100 图标缩放到 30x30 像素。
这可以在没有任何 JavaScript 的 CSS/HTML 中完成吗?
解决方案
您可以使用 :nth-child() 选择器来选择父 div 的第三个孩子。
HTML
<div>
<img src="https://via.placeholder.com/100x100"> Menu
<img src="https://via.placeholder.com/100x100"> Menu
<img src="https://via.placeholder.com/100x100"> Menu
<img src="https://via.placeholder.com/100x100"> Menu
<img src="https://via.placeholder.com/100x100"> Menu
<img src="https://via.placeholder.com/100x100"> Menu
<img src="https://via.placeholder.com/100x100"> Menu
<img src="https://via.placeholder.com/100x100"> Menu
<img src="https://via.placeholder.com/100x100"> Menu
<img src="https://via.placeholder.com/100x100"> Menu
</div>
CSS
div img {
width: 100px;
height: 100px;
}
div img:nth-child(3) {
width: 30px;
height: 30px;
}
工作演示。
推荐阅读
- sql-server - 如何根据另一个表中不存在的数据更新一个表中的行?
- javascript - 如何将我的(已经工作的)javascript 代码添加到我的 Wordpress 页面?
- javascript - 从本地文件向本地主机 8080 发出请求时出错
- javascript - ComponentWillUnmount() 不清除间隔
- javascript - 使用 Next 按钮生成和循环遍历随机数数组
- java - 如何在 Paypal for java SDK 中获取付款人 ID?
- android - 为什么视图模型不应该引用视图?仅仅是设计[如 SOC(关注点分离)] 还是其他?
- azure - 是否可以让 Azure AD B2C 在对自定义声明 REST API 的调用中包含 aud?
- python - 为什么使用相对路径包导入此导入不起作用?
- xml - 条件 Bean 配置