![]() |
In this article, we are going to see if it is possible to set the equivalent of an src attribute of a <img> tag in CSS. The main objective is to find an alternative that will replace the src attribute in the <img> with something which will do the equivalent work but with CSS. There are two approaches that are prevalent, one is using the content and then the url() function. The other is the background/background-image property followed by the URL of the image. In the first property, there is one drawback, it might not work in some older versions of Firefox and Internet Explorer but the second approach is versatile and will work in every browser almost seamlessly. content property: It is used to generate the content dynamically (during run time) i.e. it replaces the element with generated content value. It is used to generate content ::before & ::after pseudo-element. Syntax: img { content: url(); ... } background/background-image property: The CSS background property can use to define the background effects for elements. The background-image property is used to set one or more background images for an element Syntax: img { background: url(); background-image: url(); } Properties used:
Example 1: The code demonstrates the implementation of the first approach which is using the content property to add the URL of the source to the image that will be used. HTML
Output: ![]()
Example 2: The example demonstrates the implementation of the second approach which uses the background/background-image property to add the URL of the source to the image that will be used: HTML
Output: ![]()
|
Reffered: https://www.geeksforgeeks.org
CSS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |