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>