Home >Web Front-end >CSS Tutorial >Can CSS Change an Image's Source Attribute Directly?

Can CSS Change an Image's Source Attribute Directly?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-03 01:26:38833browse

Can CSS Change an Image's Source Attribute Directly?

Can CSS Alter Image Source Attributes?

Question: Is it possible to modify the src attribute of an img tag using CSS, without employing background or background-image properties?

Solution:

You can achieve this with the content:url() property.

Usage:

  1. Create a CSS class with the desired image source:
.imageClass {
    content: url("path/to/image.jpg");
}
  1. Assign the class to the img element:
<img class="imageClass">

Example:

<!DOCTYPE html>

<style>
.MyClass {
    content: url("http://imgur.com/SZ8Cm.jpg");
}
</style>

<img class="MyClass">

The above is the detailed content of Can CSS Change an Image's Source Attribute Directly?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn