A guide to implementing code that, when one element is activated, causes another element to change using HTML and CSS

Is it feasible to click on one element and modify another element using only HTML and CSS? If so, how can this be accomplished?

For example, does the following code illustrate this concept (although it doesn't work)?

a:active div{
    background-color: blue;
}

Answer №1

If Javascript is not an option for you, your choices become quite limited. You will need to come up with a way to toggle the state through a click and represent those states using CSS.

Here are some possible options:

  • Utilizing (hidden?) radio buttons or checkboxes and leveraging their :checked pseudo-class in CSS
  • Using anchors and their pseudo-classes (similar to what you have already tried)

However, one issue with these approaches is that all your dynamic content (the elements you show/hide on click) must be placed inside or alongside these toggling elements, which can be inconvenient.

Personally, I prefer using the :target pseudo-class. When you visit a URL like "https://something.com#foobar", the element with the id "foobar" becomes the current target (if it exists). The drawback here is that only one target is supported per document. You can manipulate the target by clicking on anchors like this:

.dynamic-content {
  display: none;
}

#first:target, #second:target {
  display: block;
}
<div>
  <div id="first" class="dynamic-content">
    First dynamic content
    <a href="#">Hide</a>
  </div>
  <div id="second" class="dynamic-content">
    Second dynamic content
    <a href="#">Hide</a>
  </div>
</div>

<a href="#first">Show first</a>
<a href="#second">Show second</a>

Answer №2

My preferred method is utilizing the :focus pseudo class. div:focus a {}

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

At the header of my webpage, there is a subtle grey border

As a beginner in the world of HTML website building, I am currently working on creating a fast food website. My goal is to have a "Home", "Menu", and "Contact" links at the top of my webpage, but I am struggling with adjusting the margin and padding. Here ...

I would like for my element to increase and decrease in size while rotating when the button is clicked

I am trying to create an element that changes its size while spinning when a button is clicked. It seems to be working fine on hover, but not onclick. Here is the code I have: <div class="rec">Rec</div> <button id="button&quo ...

Overlay text on an image using a CSS grid

I'm working on a CSS grid layout where I want to overlay text on an image. Below you can see the code I currently have without the text overlay. .sbp-item12 { grid-row: 6 / 7; grid-column: 9/13; height: 250px; } <div ...

Upgrade your input button style using jQuery to swap background images

I have an input button with an initial background image. When a certain condition changes, I want to update its image using jQuery without overriding the button's global CSS in the stylesheet. Is there a way to only change the background attribute wit ...

CSS can always surprise with its unexpected animations

Currently developing a static website with CSS animations: * { -webkit-transition: all 1s ease-in-out; -moz-transition: all 1s ease-in-out; -o-transition: all 1s ease-in-out; -ms-transition: all 1s ease-in-out; transition: all 1s ease- ...

Attempting to utilize a custom element, the message "You attempted to use polymer without loading it first" confirms that it has indeed been imported

I've been experimenting with a custom element for a paper-dialog popup that I want to incorporate. Surprisingly, when I create a test page within the same file where the custom element is defined, using all the identical imports as my main index, it w ...

JavaScript is failing to execute the DELETE SQL transaction

I've been attempting to clear out my html5 local sql database, but for some reason, it's not working as expected. Below is the code I'm using, and no matter what I try, the alert always shows that the length is greater than 0. Any ideas why ...

What is the method to integrate HTML tags as native elements within my iOS app?

I am working on creating a custom control that will allow me to convert HTML tags into native tags. For example, when I input the following: label.text = @"&lt;b&gt;&lt;i&gt;Hello World&lt;/i&gt;&lt;/b&gt; The output shoul ...

Tips for preserving scroll location on Angular components (not the window) when navigating

My current layout setup is like this: https://i.sstatic.net/hOTbe.png In essence <navbar/> <router-outlet/> The issue I'm facing is that the router-outlet has overflow: scroll, making it scrollable (just the outlet section, not the ent ...

PHP, HTML, Email - Special characters in emails are being altered into other symbols

Here's the Situation... I am currently utilizing PHP to send out emails in a conventional manner... Snippet 0 mail('', $subject, $message, $headers); ... with the following content setup: Snippet 1 $boundary = uniqid('np&a ...

Vue.js enhances user input interactions

CSS <span :style="{ display : displayTitle }" @click="toggleInput()"> {{ text }} </span> <input v-if="isEditing" type="text" v-model="text" @blur="hideInput" @keydown.enter="saveChanges" @keydown.esc="cancelE ...

Tips for preventing Razor from interpreting special characters as HTML code

I'm encountering an issue with special characters displaying as HTML codes on the page I'm working on. The content is retrieved from a database and shown in readonly input boxes. For example, & is displayed as &. How can I ensure that the ...

Tips for determining whether the current request is an AJAX request in MVC3 view using JavaScript

I have a div with a maximum height and overflow set to auto. When the size of the div overflows, a scroll bar is automatically added. However, I have also set $("#itemsdiv").scrollTop(10000); so that the scroll bar is always at the bottom. Now, I want t ...

Floating labels not displaying properly in Bootstrap

Having some trouble with a template where the Floating labels are not displaying. <section class="text-center"> <div class="card mx-auto" style="max-width: 450px;"> <div class="c ...

Formatting text and images in CSS

Looking to align images at the bottom of each column in a div with 3 columns, but struggling due to varying text lengths. Any tips on how to achieve this? https://i.sstatic.net/fuPgl.png Thank you, Carolin #content_row{ margin-top:150px; marg ...

Looking for a personalized grid layout with 3 columns for your thumbnails?

I am trying to make this work and I stumbled upon this website. Instead of having 4 columns, I would like to have 3 columns with the same height. Is there a way to achieve this? This is how it appears on my webpage: x http://imagizer.imageshack.us/v2/15 ...

Is there a way to retrieve all div elements within a specific div using webdriver and selenium?

As a newcomer to the world of web scraping, I am currently working on scraping a website in order to extract all elements with the data-feature-id=homepage/story. My ultimate goal is to access a subelement within each of the divs contained in the parent el ...

Fade In Effect in Angular 2 Using SwitchCase

Hi everyone, I'm facing an issue with making my switch cases fade in after one is called. Here's what I have so far. When the correct switch case is entered in the input field, I want the current one to fade out and the new one to fade in. How ...

Continue to alter elements by stacking them on top of each other

Currently, I am in the process of familiarizing myself with CSS3 3D transforms. However, I am encountering difficulties in keeping elements that are undergoing transformation on top of other elements. If you'd like to take a look at what I have accom ...

How to align text to the left and image to the right using CSS

I'm fairly new to CSS and struggling with a design issue. I want to display a series of stacked divs on my page, each containing text and one or more images. My goal is to have the text left-aligned and vertically centered, while the images are right ...