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
Release: 2025-01-03 01:26:38
Original
832 people have browsed it

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");
}
Copy after login
  1. Assign the class to the img element:
<img class="imageClass">
Copy after login

Example:

<!DOCTYPE html>

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

<img class="MyClass">
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template