Activate a CSS button upon clicking and update the content within the div

In order to achieve the desired result of having menu CSS buttons change the content of a div when clicked, I have managed to do that successfully. However, a problem arises when clicking on the 2nd or 3rd button/link - it changes the div content, but as soon as you click anywhere else on the page, it reverts back to its default content. The goal is to keep both the button/link and the div content active even when clicking anywhere on the page, all using Pure CSS without any additional coding expertise. Can anyone assist in solving this issue?

Sample HTML Code:

<div id="mynavtabcontent">

<a id="video" tabindex="1">Video</a>
<a id="apps" tabindex="2">Apps</a>
<a id="music" tabindex="3">Music</a>

<div id="content">
<div id="def">Video Content here</div>
<div id="videocontent">Video Content here</div>
<div id="appscontent">Apps Content here</div>
<div id="musiccontent">Music Content here</div>
</div>
</div>

Sample CSS code:

The #def id represents the default content, while #videocontent mirrors the default for viewing the video div content again.

#mynavtabcontent{
font-family: Verdana;
font-size: 18px;
}
#mynavtabcontent a{
text-decoration: none;
padding: 7 7 7 7px;
background-color: yellow;
color: #222;
}
#mynavtabcontent a:hover{
background-color: #333;
color: white;
cursor: pointer;
}
#content{
border:1px dashed black;
background-color: pink;
height: 400px;
width: 200px;
margin-top: 50px;
}
#videocontent, #appscontent, #musiccontent{
display: none;
}
#video1:focus~#content div:nth-child(1),
#video:focus~#content div:nth-child(2),
#apps:focus~#content div:nth-child(3),
#music:focus~#content div:nth-child(4){
display: block;

}
#video1:focus~#content #def,
#video:focus~#content #def,
#apps:focus~#content #def,
#music:focus~#content #def{
display:none;
}

Test Result:

Run code snippet, click on the 2nd or 3rd button, then click anywhere within the content area to observe the behavior described.

#mynavtabcontent{
font-family: Verdana;
font-size: 18px;
}
#mynavtabcontent a{
text-decoration: none;
padding: 7 7 7 7px;
background-color: yellow;
color: #222;
}
#mynavtabcontent a:hover{
background-color: #333;
color: white;
cursor: pointer;
}
#content{
border:1px dashed black;
background-color: pink;
height: 400px;
width: 200px;
margin-top: 50px;
}
#videocontent, #appscontent, #musiccontent{
display: none;
}
#video1:focus~#content div:nth-child(1),
#video:focus~#content div:nth-child(2),
#apps:focus~#content div:nth-child(3),
#music:focus~#content div:nth-child(4){
display: block;

}
#video1:focus~#content #def,
#video:focus~#content #def,
#apps:focus~#content #def,
#music:focus~#content #def{
display:none;
}
<div id="mynavtabcontent">

<a id="video" tabindex="1">Video</a>
<a id="apps" tabindex="2">Apps</a>
<a id="music" tabindex="3">Music</a>

<div id="content">
<div id="def">Video Content here</div>
<div id="videocontent">Video Content here</div>
<div id="appscontent">Apps Content here</div>
<div id="musiccontent">Music Content here</div>
</div>
</div>
If a pure CSS solution is not possible, kindly provide a JavaScript code that could fix this issue. Your help would be greatly appreciated. Thank you! -edited.

Answer №1

The links lose focus once you click away. To maintain state, consider using the :checked pseudo element. You can hide inputs and manage their states with labels. Check out the modified code below:

<div id="mynavtabcontent">

  <input type="radio" name="content-toggle" id="video"> <label for="video" tabindex="1">Video</label>
  <input type="radio" name="content-toggle" id="apps"> <label for="apps" tabindex="2">Apps</label>
  <input type="radio" name="content-toggle" id="music"> <label for="music" tabindex="3">Music</label>

  <div id="content">
    <div id="def">Video Content here</div>
    <div id="videocontent">Video Content here</div>
    <div id="appscontent">Apps Content here</div>
    <div id="musiccontent">Music Content here</div>
  </div>

</div>

Here is the accompanying CSS:

#mynavtabcontent input {
  display: none;
}

#mynavtabcontent{
  font-family: Verdana;
  font-size: 18px;
}
#mynavtabcontent label{
  text-decoration: none;
  padding: 7 7 7 7px;
  background-color: yellow;
  color: #222;
}
#mynavtabcontent label:hover{
  background-color: #333;
  color: white;
  cursor: pointer;
}
#content{
  border:1px dashed black;
  background-color: pink;
  height: 400px;
  width: 200px;
  margin-top: 50px;
}
#videocontent, #appscontent, #musiccontent{
  display: none;
}
#video1:checked~#content div:nth-child(1),
#video:checked~#content div:nth-child(2),
#apps:checked~#content div:nth-child(3),
#music:checked~#content div:nth-child(4){
  display: block;

}
#video1:checked~#content #def,
#video:checked~#content #def,
#apps:checked~#content #def,
#music:checked~#content #def{
  display:none;
}

Link to view the code in action: http://codepen.io/MakiBM/pen/KgXzma

Answer №2

When it comes to CSS, its main focus is on styling rather than actions.

If you want to incorporate actions into your website, JavaScript will be necessary.

JavaScript works with the Document Object Model (DOM) to implement changes in the browser.

Although CSS primarily handles styling, it does offer some control over animations through properties like transition, transform, :hover, :active, :focus, and @keyframes.

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

The Font Awesome icons do not display offline

I'm having trouble using Font Awesome icons offline on my webpage. I've included the necessary code, but for some reason it's not working. Here is what I have: <html> <head> <title>font-awesome example</title> ...

Oops! Looks like the module "@google-cloud/vision" hasn't been loaded in this context yet. Make sure to use require([]) to load it before proceeding

Encountering an issue with the error message Module name "@google-cloud/vision" has not been loaded yet for context: _. Use require([]) while running my project. I have included require.js in my project and added the script tag in my HTML file <script d ...

Errors persist with PHP form submission despite all fields being filled out

Snippet of PHP code for form validation: <html> <head> </head> <body bgcolor="000033" text="navy"> <?php $nume=""; $email=""; $subiect=""; if (!$nume || !$email || !$subiect) { ?> <h4 style="color:gray" align="center"> ...

The Vue Swiper does not support inline containers in my code

I am facing an issue with the layout of my simple swiper in my vue app. The containers are not inline as expected, causing the second one to appear under the first. Additionally, I am struggling with making the slider element visible only within the viewpo ...

What symbol represents the share function in Unicode?

I've combed through Google, Stack Overflow, and various HTML character sites multiple times but have been unable to find the specific icon I'm looking for. Can someone please assist me with obtaining the HTML Unicode sequence for an icon that rep ...

Creating tailored output data from a single table

Can you please assist me with a problem I am facing in generating output data using PHP and MySQL? Below is an image of my table: https://i.stack.imgur.com/zaieZ.png I would like the output to be displayed to the user in the following format: 1. Account ...

A step-by-step guide on utilizing jQuery to cyclically change the background color of elements upon clicking, and resetting the colors upon clicking another element

So I've been working on a solution to change the background color for accessibility purposes on a client's website. To achieve this, I have a set of div elements with different background colors and I'm using jQuery to update the body backgr ...

Discovering the process of extracting a date from the Date Picker component and displaying it in a table using Material-UI in ReactJS

I am using the Date Picker component from Material-UI for React JS to display selected dates in a table. However, I am encountering an error when trying to show the date object in a table row. Can anyone provide guidance on how to achieve this? import ...

The Div elements are not displaying properly in the correct size on the responsive webpage

Looking to create a responsive design for a follow page. Although I've managed to make it work, adding a line at the top of each row is causing layout issues and the Box container is not displaying consistently in size. I attempted to set up a fiddl ...

Collapse the accordion item when a different one is selected

Check out this code snippet that's working on jsfiddle I'm attempting to add a function to close any open accordion items when another one is clicked. I've added a class "open", but I'm having trouble removing the class when a differen ...

Can React components receive props or data when they are inserted into regular HTML code?

I have a project where I need to make updates to an old-fashioned website. The current layout is table-based and coded by hand. My idea to streamline the process is to use React to minimize repetitive coding tasks. Specifically, I want to loop through an a ...

Lacking HTML5 support, your current location cannot be accessed on Google Maps

I have implemented google maps on my basic HTML website. Currently, I am able to center the map based on the user's current location using HTML5, however this requires obtaining permission from the user. Interestingly, when I visit maps.google.com, th ...

Trouble with the query waypoints extension in a simple demonstration

Can anyone help me figure out why the basic example from the waypoints plugin isn't working for me? Here's a link to the jsfiddle I created: http://jsfiddle.net/ZA8bd/2/ CSS .block1 { margin-top:30px; width: 400px; background: red; ...

How can the values be preserved with two action forms and two submit buttons?

On my webpage, I have 2 action forms and 2 submit buttons. The first form is for adjusting the zoom level. The second form is for setting the refresh rate in seconds. Whenever I submit the zoom form, the refresh rate resets. Similarly, submitting the re ...

Is there a way to simplify this "stopwatch" even more?

Looking for advice on simplifying my JS stopwatch timer that currently only activates once and keeps running indefinitely. As a newcomer to JS, this is the best solution I could come up with: let time = 0 let activated = 0 function changePic() { if(a ...

Changing the image source using Javascript and extracting part of the URL

i'm attempting to extract the image url from a series of urls in a loop, removing the hash portion () without the hash (?sqp=-oaymwEjCNACELwBSFryq4qpAxUIARUAAAAAGAElAADIQj0AgKJDeAE=&rs=AOn4CLDi79vN15idfFETvntyC9yat7FvZQ). I've managed to mak ...

Responsive columns and fluid Flex row implemented in Bootstrap 4

I am striving to optimize this section for responsiveness. It looks fantastic on larger viewports, but as we start shrinking down, for instance, on a viewport around 930px, I'm facing difficulty in keeping the image perfectly aligned as shown in the i ...

What measures can be taken to restrict users from inputting decimal values?

My website includes an order page where users can input quantities for various items. While some items allow for decimal quantities, others do not. What is the most effective method to restrict users from entering decimal quantities? (Besides using an ale ...

Navigating through Ruby on Rails' index routes

I have set up an index for the object "request". Each request has an address and a body. I configured the index to display the address as a link, with the intention of directing users to the show page of that specific object. However, when I click on the l ...

Using the video-js feature to play multiple videos simultaneously

Is it possible to play multiple videos using video-js functionality simultaneously? The answer is yes! Check out Fiddle 1 However, an issue arises when wrapping a trigger, such as a button, around the function that invokes playVideo(). This causes the v ...