A guide to updating the image src with a click using CSS exclusively, without JavaScript intervention

I am looking to update the image source when clicking on it using only CSS, something along the lines of:

img:active {
}

Answer №1

If you want to achieve this effect, you can utilize the element replacement feature of the content property.

However, keep in mind that using this technique may result in certain accessibility features not being set properly, potentially leading to information being lost for some users. Depending on the context and audience, this could have legal implications.

To ensure better accessibility, consider setting a tabindex or enclosing the image in a button element.

It's important to note that the :active pseudo-class represents "While being clicked on," not "After being clicked on." CSS does not offer an "after being clicked on" pseudo-class, though :focus may serve as a workaround in certain scenarios.

img:active {
    content: url(http://placekitten.com/200/200);
}
<img src="http://placekitten.com/g/200/200" alt="">

Answer №2

Modifying the source of an image using only CSS is not possible. However, one workaround is to set the image as a background for its parent element and then change the background using CSS like this:

.image-container { background-image: url('myurl...')} ;
.image-container:active {background-image: url('anotherurl...')}

I hope this solution works for you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is preventing the information from being shown in oj when using oj[0], oj[2], and oj[x]?

This webpage was built using HTML. I incorporated javascript into it. I assigned the content of the HTML element h1 to a variable named oj using the method getElementById. In an online tutorial, it mentioned that oj should behave like an array. So, why ...

What is the best way to position two form input boxes so that they line up perfectly?

Having a modal with a form (using Bootstrap-Vue and Vue2) where each row contains two fields. If the user does not input a valid value into a field, the state of that field turns false, causing the box to be highlighted in red and displaying an error messa ...

The CSS property for setting the background color on the body element is not functioning properly

I am attempting to make changes to my app.scss file in my Symfony 4 project. Unfortunately, the background-color property is not being applied as expected. body { background-color: #ccccff; } In my base.html.twig file, I have the following structure: ...

Use jQuery to set the onclick attribute for all elements rather than relying on inline JavaScript

I am currently facing a challenge with converting inline JS to jQuery. My goal is to eliminate all inline onclick events and instead target them by class. HTML - checkbox <td class="center"> <?php if ($product['selected']) { ?> ...

Incorporating descriptions below the images within this JavaScript carousel

I'm struggling to add text under the images in this slider. The functionality is simple, but I can't figure it out. I might have to rewrite it. Here's the HTML and JS: <div id="look-book-scroll"> <a href="javascript:;" id="lookbo ...

Initial loading issue with HTML5 Canvas in Android WebView

As I work on developing a HTML5 canvas-based game within a WebView of an existing application, I encounter a puzzling issue. Upon the initial run of the game, everything seems to be in order - logs indicate that it's ready and running, yet nothing is ...

Different text in AMP

Is there a way to make a line of text quickly fade in, stay visible for 5 seconds, fade out quickly, then have a different line of text quickly fade in, stay for about 5 seconds, fade out quickly, and repeat this process infinitely in AMP? I tried using ...

Get the value of the button that has been clicked

I have a query regarding building a website that can be started via PowerShell. The PowerShell HTML code I am using is: $proxys = "" foreach ($email in $ADObj.proxyAddresses){ $proxys += "<button id='$email' name='alias&apo ...

If the width is below 767, the previous code will not be effective in jQuery

Challenge: How can I ensure that the code within the if statement only executes when the screen width exceeds 767px, and does not work if it is less than 767px? Using jQuery: $(document).ready(function() { $(window).resize(function() { if ($( ...

What strategies can be implemented to avoid using .stop() in specific scenarios?

Currently, I am working on a jQuery animation where clicking on a tile should display its information by hiding all other tiles and showing its details. While I have implemented .stop() to prevent queuing issues, I encountered a problem with the transition ...

I can't understand why this question continues to linger, I just want to remove it

Is there a valid reason for this question to persist? I'm considering removing it. ...

A combination of fixed width column and dynamic content area in Bootstrap design

Is there a way to achieve the combination of fixed and fluid width columns using Twitter Bootstrap alone, similar to this example? The HTML and CSS in the example make it seem simple. But can this be done solely with Bootstrap? ...

Submitting the form does not result in the textbox being cleared

I have been attempting to clear the txtSubTotal text box upon clicking the PROCEED button, but it seems that my efforts have been in vain despite trying various code examples, including those from SO. btnProceed/HTML <input type="submit" name="btnProc ...

Tips for generating multiple URL destinations within a single hyperlink

I am looking to create a unique feature in HTML where multiple tabs can be opened with different URLs using one link. While I know how to open one link in a new tab, I am unsure of how to simultaneously add another URL that will open up in a separate tab ...

Encountering a 404 error while attempting to showcase the SQL table data on an HTML webpage via Python Flask

I have recently developed a BMI calculation web application where users can input their details and calculate their BMI. However, I am facing an issue with displaying the records on the webpage. Below is the code snippet for the application: app.py f ...

Tips on managing scrollbar behavior in Angular UI Grid

In my current project, I am utilizing Angular UI Grid. The grid automatically resizes itself so that all columns fit within a specific div horizontally. While this feature works well initially, it becomes problematic when there are more rows than can fit ...

Combining multiple text areas into a single form submission button in PHP/HTML

I have a task to create a table for 10 different projects (Jupiter, Uranus, Saturn, etc..) with an individual comment section for users to add updates. Each row has its own "add record" button, but I want to implement a single "submit" button at the bottom ...

html table layout disrupted due to immovable column

I am facing a challenge in creating a table where the first column is fixed while the rest of the columns are scrollable horizontally. Although I was able to implement this successfully, it does not seem to work when integrated into my website. Whenever ...

Expanding circle with CSS borders on all edges

My goal is to create a background reveal effect using JavaScript by increasing the percentage. The effect should start from the center and expand outwards in all directions. Issue: The percentage increase currently affects only the bottom and not the top ...

Proper utilization of hyperlinks

Imagine a page showcasing links to detailed pages in the form of cards. Each card features an image, a heading, a view button, and a save button. Here's a visual representation: +-----------------+ | | | IMAGE | | ...