首页 > 解决方案 > 如何通过 src 设置图像标签的样式?

问题描述

我有一个图像标签,例如,

<img src="image/path/goes/here/random_id">

并且 random_id 随机变化。如何在不使用 id、class 或任何其他属性的情况下从外部 css 文件中捕获此标记。

标签: csshtml

解决方案


您可以根据此在 src 属性上使用路径包含选择器

https://www.w3schools.com/cssref/sel_attr_contain.asp

img[src*="image/path/goes/here/"]{
  border:1px solid red;
  min-width:50px;
  min-height:50px;
}
<img src="image/path/goes/here/123">
<img src="image/path/goes/here/321">

<img src="this/is/another/path/321">


推荐阅读