6

My colleague and I worked on a website that contains a FAQ page. After each question, there is an "Answer"(Antwoord) spoiler button. Clicking the button shows the answer below the current question, and above the next question. Clicking the button again hides the answer. This makes it easy to scroll through the list of all questions and find the question you could be interested in.

Now the problem I face is that I want to link to for example the answer of question 5, from the answer of question 1.

I could give the <div> that contains question 5 an id= property to link to that, but ideally, I want to show the answer straight away by making the hyperlink "click" the Answer button.

It turns out that I will need to modify the checkbox hack in order for this to properly work, but I face the problem that I can't have both an Hyperlink and a Label on one piece of text.

Just to be clear: I'm looking for a solution using only HTML/CSS.


My original code with just a spoiler button

.spoilerbutton {
  display: block;
  border: none;
  padding: 0px 0px;
  margin: 10px 0px;
  font-size: 150%;
  font-weight: bold;
  color: #000000;
  background-color: transparent;
  outline: 0;
}

.spoiler {
  overflow: hidden;
}

.spoiler>div {
  -webkit-transition: all 0s ease;
  -moz-transition: margin 0s ease;
  -o-transition: all 0s ease;
  transition: margin 0s ease;
}

.spoilerbutton[value="Antwoord"]+.spoiler>div {
  margin-top: -500%;
}

.spoilerbutton[value="Hide Antwoord"]+.spoiler {
  padding: 5px;
}
<strong>1. Question 1?</strong> <input class="spoilerbutton" onclick="this.value=this.value=='Antwoord'?'Verberg':'Antwoord';" type="button" value="Antwoord" />
<div class="spoiler">
  <div>
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>

Trying to implement solutions from: Can I have an onclick effect in CSS?

I get the following which doesn't work:

#btnControll1 {
    display: none;
}

.spoilerbutton {
  display: block;
  border: none;
  padding: 0px 0px;
  margin: 10px 0px;
  font-size: 150%;
  font-weight: bold;
  color: #000000;
  background-color: transparent;
  outline: 0;
}

.spoiler {
  overflow: hidden;
}

.spoiler>div {
  -webkit-transition: all 0s ease;
  -moz-transition: margin 0s ease;
  -o-transition: all 0s ease;
  transition: margin 0s ease;
}

#btnControl1:checked + label {
margin-top; -500%
}
<strong>1. Question 1?</strong> <input type"chekbox" id="btnControl1" />
<label class="spoilerbutton" for="btnControl1">Antwoord</label>
<div class="spoiler">
  <div>
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>

Using combinations of <label> and <a href> (also not working)

.spoilerbutton {
display: none;
}
.spoiler {
overflow: hidden;
height:2em;
}
.spoiler>div {
-webkit-transition: all 0s ease;
-moz-transition: margin 0s ease;
-o-transition: all 0s ease;
transition: margin 0s ease;
}
.spoilerbutton+.spoiler>div {
margin-top: -500%;
}
.spoilerbutton+.spoiler>label:before {
content: 'Antwoord';
font-size: 150%;
font-weight: bold;
color: #000000;
}
.spoilerbutton:checked+.spoiler>label:before {
content: 'Verberg';
}
.spoilerbutton:checked+.spoiler {
height:auto;
}
.spoilerbutton:checked+.spoiler>div {
margin-top: 0;
}
<strong>1. Question 1?</strong> <input type="checkbox" id="question1" value="checked" class="spoilerbutton">
<div class="spoiler">
<label for="question1"></label>
<div>
<ul>
<li><a href="#question5"><label for="#answer5">Open 5. link first</label></a></li>
<li><label for="question5"><a href="#answer5">Open 5. label first</a></label></li>
<li><a href="#answer5">Open 5. link only</a></li>
</ul>
</div>
</div>
<strong>2. Question 2?</strong> <input type="checkbox" id="question2" value="checked" class="spoilerbutton">
<div class="spoiler">
<label for="question2"></label>
<div>
<ul>
<li>possible answer 1.</li>
<li>possible answer 2.</li>
<li>possible answer 3.</li>
</ul>
</div>
</div>
<strong>2. Question 3?</strong> <input type="checkbox" id="question3" value="checked" class="spoilerbutton">
<div class="spoiler">
<label for="question3"></label>
<div>
<ul>
<li>possible answer 1.</li>
<li>possible answer 2.</li>
<li>possible answer 3.</li>
</ul>
</div>
</div>
<strong>2. Question 4?</strong> <input type="checkbox" id="question4" value="checked" class="spoilerbutton">
<div class="spoiler">
<label for="question4"></label>
<div>
<ul>
<li>possible answer 1.</li>
<li>possible answer 2.</li>
<li>possible answer 3.</li>
</ul>
</div>
</div>
<br>
<br>
<br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br>
<strong>2. Question 5?</strong> <input type="checkbox" id="question5" value="checked" class="spoilerbutton">
<div class="spoiler" id="answer5">
<label for="question5"></label>
<div>
<ul>
<li>possible answer 1.</li>
<li>possible answer 2.</li>
<li>possible answer 3.</li>
</ul>
</div>
</div>

Also related How to toggle a checkbox when a link is clicked?

There are some great suggestions there, but I'm unable to adapt the answer from Tushar to my situation.

Luuklag
  • 3,897
  • 11
  • 38
  • 57
  • 1
    You are using JavaScript already in your `onclick` attribute. Are those one liners in attributes okay? Also, you want to click on a hyperlink and another Answer should open up? – Aaron3219 May 15 '19 at 11:14
  • @Aaron3219, well I guess so then, seeing that this is what we currently use and it works. Yay for doing something in a language you know very little about! – Luuklag May 15 '19 at 11:18
  • What about my second question? Did I understand you correctly? – Aaron3219 May 15 '19 at 11:21
  • If I click on go to answer 5 (in the last snippet in my Q) I want the focus of the screen be on question 5, and the answers to be visible immediately. – Luuklag May 15 '19 at 11:22
  • 1
    @Luuklag i updated my answer and you can find working example for both options `CSS` and `jQuery`. Thanks – Hassan Siddiqui May 15 '19 at 13:40

5 Answers5

8

Option 1 - Using JavaScript

Redirection only works if the element is visible, not set to display: none. In your above code snippet, both the checkbox and the list of answers are set to display: none as their initial state.

Step 1: Add id to each <strong id="question"> element.

<strong id="question1">1. Question 1?</strong> 

Step 2: We cannot check/uncheck a checkbox using CSS, we have to use JavaScript/jQuery to achieve this functionality. So I added the basics to a checkbox checked script, when a user clicks on the link.

<li><a href="#question5" onClick="var str = this.attributes['href'].value + 'Checkbox'; document.getElementById(str.replace('#', '')).checked = true;">Open 5. link first</a></li>

.spoilerbutton {
display: none;
}
.spoiler {
overflow: hidden;
height:2em;
}
.spoiler>div {
-webkit-transition: all 0s ease;
-moz-transition: margin 0s ease;
-o-transition: all 0s ease;
transition: margin 0s ease;
}
.spoilerbutton+.spoiler>div {
margin-top: -500%;
}
.spoilerbutton+.spoiler label:before {
content: 'Antwoord';
font-size: 150%;
font-weight: bold;
color: #000000;
}
.spoilerbutton:checked+.spoiler label:before {
content: 'Verberg';
}
.spoilerbutton:checked+.spoiler {
height:auto;
}
.spoilerbutton:checked+.spoiler>div {
margin-top: 0;
}
<strong id="question1">1. Question 1?</strong> 
<input type="checkbox" id="question1Checkbox" value="checked" class="spoilerbutton">
  <div class="spoiler">
    <label for="question1Checkbox"></label>
    <div>
      <ul>
        <li><a href="#question5" onClick="var str = this.attributes['href'].value + 'Checkbox'; document.getElementById(str.replace('#', '')).checked = true;">Open 5. link first</a></li>
      </ul>
    </div>
</div>
<strong id="question2">2. Question 2?</strong> 
<input type="checkbox" id="question2Checkbox" value="checked" class="spoilerbutton">
<div class="spoiler">
  <label for="question2Checkbox"></label>
  <div>
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>
<strong id="question3">2. Question 3?</strong> 
<input type="checkbox" id="question3Checkbox" value="checked" class="spoilerbutton">
<div class="spoiler">
  <label for="question3Checkbox"></label>
  <div>
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>
<strong id="question4">2. Question 4?</strong> 
<input type="checkbox" id="question4Checkbox" value="checked" class="spoilerbutton">
<div class="spoiler">
  <label for="question4Checkbox"></label>
  <div>
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>
<br>
<br>
<br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br>
<strong id="question5">2. Question 5?</strong> 
<input type="checkbox" id="question5Checkbox" value="checked" class="spoilerbutton">
<div class="spoiler" id="answer5">
  <label for="question5Checkbox"></label>
  <div>
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>

Option 2 - Using CSS

By using the CSS :target selector and little bit of a structure update, we can achieve the functionality by using CSS.

.spoilerbutton {
display: none;
}
.spoiler {
overflow: hidden;
height:2em;
}
.spoiler>div {
-webkit-transition: all 0s ease;
-moz-transition: margin 0s ease;
-o-transition: all 0s ease;
transition: margin 0s ease;
}
.spoilerbutton+.spoiler .listOfAnswer {
margin-top: -500%;
}
.spoilerbutton+.spoiler .labelWrap {
  display: inline-block;
  position: relative;
}
.spoilerbutton+.spoiler .labelWrap label:before {
content: 'Antwoord';
font-size: 150%;
font-weight: bold;
color: #000000;
}
.spoilerbutton:checked+.spoiler .labelWrap label:before {
content: 'Verberg';
}
.spoilerbutton:checked+.spoiler {
height:auto;
}
.spoilerbutton:checked+.spoiler .listOfAnswer {
margin-top: 0;
}

:target + .spoilerbutton + .spoiler {
height:auto;
}
:target + .spoilerbutton + .spoiler .labelWrap label:before {
content: 'Verberg';
}
:target + .spoilerbutton + .spoiler .labelWrap .resetTarget {
cursor: default;
display: block;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
:target + .spoilerbutton + .spoiler .listOfAnswer {
margin-top: 0;
}
.resetTarget {
  display: none;
}
<strong class="qTitle" id="question1">1. Question 1?</strong> 
<input type="checkbox" id="question1Checkbox" value="checked" class="spoilerbutton">
  <div class="spoiler">
    <div class="labelWrap">
      <label for="question1Checkbox"></label>
      <a class="resetTarget" href="#question1Checkbox"></a>
    </div>
    <div class="listOfAnswer">
      <ul>
        <li><a href="#question5">Open 5. link first</a></li>
      </ul>
    </div>
</div>
<strong class="qTitle" id="question2">2. Question 2?</strong> 
<input type="checkbox" id="question2Checkbox" value="checked" class="spoilerbutton">
<div class="spoiler">
  <div class="labelWrap">
    <label for="question2Checkbox"></label>
    <a class="resetTarget" href="#question2Checkbox"></a>
  </div>
  <div class="listOfAnswer">
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>
<strong class="qTitle" id="question3">2. Question 3?</strong> 
<input type="checkbox" id="question3Checkbox" value="checked" class="spoilerbutton">
<div class="spoiler">
  <div class="labelWrap">
    <label for="question3Checkbox"></label>
    <a class="resetTarget" href="#question3Checkbox"></a>
  </div>
  <div class="listOfAnswer">
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>
<strong class="qTitle" id="question4">2. Question 4?</strong> 
<input type="checkbox" id="question4Checkbox" value="checked" class="spoilerbutton">
<div class="spoiler">
  <div class="labelWrap">
    <label for="question4Checkbox"></label>
    <a class="resetTarget" href="#question4Checkbox"></a>
  </div>
  <div class="listOfAnswer">
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>
<br>
<br>
<br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br>
<strong class="qTitle" id="question5">2. Question 5?</strong> 
<input type="checkbox" id="question5Checkbox" value="checked" class="spoilerbutton">
<div class="spoiler" id="answer5">
  <div class="labelWrap">
    <label for="question5Checkbox">
      <a class="resetTarget" href="#question5Checkbox"></a>
    </label>    
  </div>
  <div class="listOfAnswer">
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>
TylerH
  • 20,799
  • 66
  • 75
  • 101
Hassan Siddiqui
  • 2,799
  • 1
  • 13
  • 22
  • 1
    "We cannot check/uncheck a checkbox using CSS" Yes you can, it's a well-known CSS-only interactivity technique often referred to as the checkbox hack – TylerH May 15 '19 at 14:02
3

Short answer

In general, yes, it is possible to accomplish this, but it has huge limitations due to the limitations of CSS (Cascading Style Sheets) so in reality the answer is no.


Long answer

The problem we face is that you won't be able to implement a solution dynamically without any JavaScript. You can see that in the possible answer you linked. In pure CSS it is only possible to target elements specified in the file. How can CSS know which checkbox to check (assuming using the checkbox hack) or which answer to extend if you click on a link? The answer is simple: It can't.

So the only way to do it without JavaScript is by doing it not dynamically. The problem is that you can't target parent elements (Cascading Style Sheets) so there isn't even a pure CSS way to even open up another element if, for example, a hyperlink is in :focus and you also can't check/uncheck checkboxes with CSS.

This brings us to JavaScript as the only possible solution with your structure of code.

.spoilerbutton {
  position: absolute;
  margin-top: 9px 0 0 -5px;
  height: 35px;
  width: 102px;
  opacity: 0;
}

.spoilerbutton:checked {
  width: 86px;
}

.spoilerbutton~.text::after {
  content: "Antwoord";
}

.spoilerbutton:checked~.text::after {
  content: "Verberg";
}

.spoilerbutton~.spoiler {
  display: none;
}

.spoilerbutton:checked~.spoiler {
  padding: 5px;
  display: block;
}
<div>
  <strong>1. Question 1?</strong>
  <p class="text"></p>
  <input class="spoilerbutton" type="checkbox" />

  <h2 class="text"></h2>

  <div class="spoiler">
    <div>
      <ul>
        <li><a onclick="document.querySelector(this.attributes['href'].value + ' .spoilerbutton').checked = true;" href="#answer3">Open answer 3</a></li>
        <li>possible answer 2.</li>
        <li>possible answer 3.</li>
      </ul>
    </div>
  </div>
</div>

<div>
  <strong>1. Question 2?</strong>
  <p class="text"></p>
  <input class="spoilerbutton" type="checkbox" />

  <h2 class="text"></h2>

  <div class="spoiler">
    <div>
      <ul>
        <li>possible answer 1.</li>
        <li>possible answer 2.</li>
        <li>possible answer 3.</li>
      </ul>
    </div>
  </div>
</div>

<div style="margin-top: 500px" id="answer3">
  <strong>1. Question 3?</strong>
  <p class="text"></p>
  <input class="spoilerbutton" type="checkbox" />

  <h2 class="text"></h2>

  <div class="spoiler">
    <div>
      <ul>
        <li>possible answer 1.</li>
        <li>possible answer 2.</li>
        <li>possible answer 3.</li>
      </ul>
    </div>
  </div>
</div>

One line of code is enough:

document.querySelector(this.attributes['href'].value + ' .spoilerbutton').checked = true;

Edit

Actually I forgot about the :target pseudo class. But Hassan Siddiqui already completed his answer with it. However, you will have to restructure your code and if you want to use the JavaScript approach (which I strongly recommend) I prefer my line of code.

Community
  • 1
  • 1
Aaron3219
  • 2,168
  • 4
  • 11
  • 28
  • Good explanation and analysis. I also like the approach of using a `querySelector` instead of `getElementById` like @[Hassan Siddiqui](https://stackoverflow.com/users/3169136/hassan-siddiqui) did it. –  May 15 '19 at 12:22
1

The following demo is pure HTML/CSS -- not a bit of JavaScript. That includes on-event attributes which many overlook and don't realize that they are JavaScript. Here's two examples:

<button onclick="jsFunction(this); return false">X</button>

<a href="#/" onclick="this.value=this.value=='Antwoord'?'Verberg':'Antwoord';">X</a>

Each Q&A is a hidden checkbox, a collapsed fieldset containing a visible legend and label, and a definition list as the hidden content. visibility: collapse/visible was used for that part.

We use :target to open the target fieldset when jumping to it. All user interaction and animation works (even the scrolling by using scroll-behavior: smooth) solely by class-centric CSS and strategic HTML layout. No compromises were made using a ton of <br> or using any JavaScript. The only #id used in CSS was a non-essential ruleset so as long as you stick to the layout pattern and class assignments expanding should be painless.

Update
HassanSiddiqui has brought to my intention of an edge case see comment below. I say edge case because I don't see a user dwelling on a FAQ so completely that they have a need to go back and use that particular link. Nevertheless, for the sake of completeness, I have updated it with the following:

  1. The label.tgr for Question #5 is initially display:none.

  2. In it's place is a.tgr which targets Question #5's checkbox, input#act5.

  3. Now backup to the link in Question #1 and lets recall what happens there: when that link is clicked fieldset#t5c2 is now and forever :target which makes closing it difficult since input#act5 is unchecked.

  4. So #t5c2 was opened by the link and it's now :target. To remove that sate from #t5c2 a.tgr is clicked making input#act5:target.

  5. So at this point a.tgr will be replaced by label.tgr and #t5c2 is closed. I won't go into more details I'm confusing myself, so just review the CSS.

Review demo in full page mode, the iframe of the Snippet in the initial compact mode is terribly disproportional.

html {
  scroll-behavior: smooth;
}

.main {
  height: 300vh;
  padding: 2vh 2vw 30vh;
}

.set {
  overflow: hidden;
  visibility: collapse;
  margin: 2vh 0;
}

.cat {
  max-height: 0;
  font-size: 0;
  opacity: 0;
}

:target .cat,
.act:checked+.set .cat {
  max-height: 100vh;
  font-size: 1rem;
  opacity: 1.0;
  transition: opacity 1s, max-height 0.7s;
}

:target,
.act:checked+.set {
  max-height: 100vh;
  visibility: visible;
  transition: 0.7s
}

.que,
.tgr {
  visibility: visible;
  max-height: 50vh;
  color: #000;
  font-size: 1rem;
  text-decoration: none;
}

.tgr {
  cursor: pointer;
}

.que::before {
  content: attr(data-idx)'. ';
}

.lnx::after {
  content: ' 'attr(title);
}


/* Question 5 Switch Triggers */

#act1:checked~.set .top {
  outline: 3px solid cyan
}

#act5:target+#t5c2 {
  visibility: collapse;
}

#act5:checked+#t5c2 {
  visibility: visible;
}

#t5c2 label.tgr {
  display: none;
}

#act5:target+#t5c2 label {
  display: inline-block;
}

#act5:target+#t5c2 a {
  display: none;
}

#act1:not(:checked)~#act5:not(:checked)+#t5c2:not(:target) a {
  display: none;
}

#act1:not(:checked)~#act5:not(:checked)+#t5c2:not(:target) label {
  display: inline-block;
}
<form class='main'>

  <input id='act1' class="act" type="checkbox" hidden>
  <fieldset class='set'>
    <legend class='que' data-idx='1'>Question</legend>
    <label class='tgr' for='act1'>Answers</label>
    <dl class='cat'>
      <dt>Category 1A</dt>
      <dd>Topic 1A1</dd>
      <dd>Topic 1A2</dd>
      <dt>Category 1B</dt>
      <dd>Topic 1B1
        <a href='#t5c2' class='lnx' title='Topic 5C2'>Also see</a>
      </dd>
      <dd>Topic 1B2</dd>
    </dl>
  </fieldset>

  <input id='act2' class="act" type="checkbox" hidden>
  <fieldset class='set'>
    <legend class='que' data-idx='2'>Question</legend>
    <label class='tgr' for='act2'>Answers</label>
    <dl class='cat'>
      <dt>Category 2A</dt>
      <dd>Topic 2A1</dd>
      <dd>Topic 2A2</dd>
      <dt>Category 2B</dt>
      <dd>Topic 2B1</dd>
    </dl>
  </fieldset>

  <input id='act3' class="act" type="checkbox" hidden>
  <fieldset class='set'>
    <legend class='que' data-idx='3'>Question</legend>
    <label class='tgr' for='act3'>Answers</label>
    <dl class='cat'>
      <dt>Category 3A</dt>
      <dd>Topic 3A1</dd>
      <dt>Category 3B</dt>
      <dd>Topic 3B1</dd>
      <dd>Topic 3B2</dd>
      <dd>Topic 3B3</dd>
    </dl>
  </fieldset>

  <input id='act4' class="act" type="checkbox" hidden>
  <fieldset class='set'>
    <legend class='que' data-idx='4'>Question</legend>
    <label class='tgr' for='act4'>Answers</label>
    <dl class='cat'>
      <dt>Category 4A</dt>
      <dd>Topic 4A1</dd>
      <dd>Topic 4A2</dd>
      <dd>Topic 4A3</dd>
    </dl>
  </fieldset>

  <input id='act5' class="act" type="checkbox" hidden>
  <fieldset id='t5c2' class='set'>
    <legend class='que' data-idx='5'>Question</legend>
    <label class='tgr' for='act5'>Answers</label>
    <a href='#act5' class='tgr'>Answers</a>
    <dl class='cat'>
      <dt>Category 5A</dt>
      <dd>Topic 5A1</dd>
      <dd>Topic 5A2</dd>
      <dt>Category 5B</dt>
      <dd>Topic 5B1</dd>
      <dd>Topic 5B2</dd>
      <dt>Category 5C</dt>
      <dd>Topic 5C1</dd>
      <dd class='top'>Topic 5C2</dd>
    </dl>
  </fieldset>

</form>
Community
  • 1
  • 1
zer00ne
  • 41,936
  • 6
  • 41
  • 68
  • As I find this to be the simplest code in HTML I awarded the bounty. This is of great importance as there are many questions. – Luuklag May 20 '19 at 07:45
  • 1
    Indeed, simplicity and functionality is key to maintaining stable code.Happy coding! – zer00ne May 20 '19 at 18:30
  • @zer00ne - its works only first time when you click `Also see Topic 5C2` it'll scroll down and expand answer 5, but when user click `question 5` answer `title` it's collapse the `question 5` all answer. Then scroll up and click `Also see Topic 5C2` again it'll scroll down to answer 5 but not expanding `question 5` answer. – Hassan Siddiqui May 22 '19 at 23:48
  • @HassanSiddiqui it's the nature of `:target` being in a persistent state indefinitely unless there's refresh or another link has been clicked, see update. – zer00ne May 23 '19 at 02:56
  • I don’t think it’s an edge case because right now we are in demo phase, let assume in future we have multiple questions with multiple answers and all answers connected to each other then what happened? – Hassan Siddiqui May 24 '19 at 01:15
  • In your CSS you write hard coded CSS for questions 5 switch trigger, suppose if we have to expand questions 4 answers then we also have to write CSS for them. It should be dynamic for each question. – Hassan Siddiqui May 24 '19 at 01:21
  • I have done miracles with CSS a small amount of JS would make it really impressive. The OP asked for pure CSS for one link not all of the questions. You are not the OP ask a question on your own post about how stacking `
    `s is great for layout.
    – zer00ne May 24 '19 at 02:39
0

I simplified the code a bit and created a semi-working pure CSS option. I wasn't able to get it working exactly as you're looking for, but I figure someone may still find it useful.

Essentially I used the :focus psuedo selector to display answers when buttons are clicked.

I also attempted to use the :active selector to display an answer from within another answer. This doesn't work very well, because the :active selector goes away as soon as the element isn't active.

Nonetheless, this is a possible solution for simply displaying answers when a button is clicked.

/* some quick styling */

.question {
  font-weight: bold;
  margin: 10px 0;
}

.answer {
  display: none;
  font-size: 14px;
  margin: 10px 0;
}

.show-answer {
  background: #cceedd;
}

.show-answer:focus {
  background: #cdcdcd;
}

a.answer-link {
  color: black;
  text-decoration: none;
}

a.answer-link span {
  color: blue;
  text-decoration: underline;
}

/* displaying the answers */

.show-answer:focus + .answer {
  display: block;
}

/* linking to an answer */

.answer-link--2:active ~ .answer--2 {
  display: block;
}
<div class="question">Question 1?</div>
<button class="show-answer show-answer--1">Show Answer</button>
<a class="answer answer--1 answer-link answer-link--2" href="#">This is the answer to Question 1. <span>Clicking and holding this text will open Question 2 answer, until you let go!.</span>.</a>
<hr/>
<div class="question">Question 2?</div>
<button class="show-answer show-answer--2">Show Answer</button>
<div class="answer answer--2">This is the answer to Question 2.</div>
<hr/>
<div class="question">Question 3?</div>
<button class="show-answer show-answer--3">Show Answer</button>
<div class="answer answer--3">This is the answer to Question 3.</div>
<hr/>
<div class="question">Question 4?</div>
<button class="show-answer show-answer--4">Show Answer</button>
<div class="answer answer--4">This is the answer to Question 4.</div>
<hr/>
<div class="question">Question 5?</div>
<button class="show-answer show-answer--5">Show Answer</button>
<div class="answer answer--5">This is the answer to Question 5.</div>
mmshr
  • 937
  • 1
  • 7
  • 9
0

The other answers have illustrated the drawbacks in solving this without JavaScript.

In terms of semantics, I recommend using the details element.

To link from one question to another, and simultaneously reveal that answer, use a small script to programmatically update the [open] attribute on the details elements.

const section = document.getElementById('section');

section.addEventListener('click', (e) => {
  const target = e.target;
  const isLink = target.classList.contains('link');

  if (!isLink) {
    return;
  }

  const href = target.getAttribute('href');
  const question = target.closest('section').querySelector(href);

  question.setAttribute('open', true);
});
body {
  height: 2000px;
}

summary {
  cursor: pointer;
}
<section id="section">
  <details id="q1">
    <summary>Question 1</summary>
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>see the answer to <a class="link" href="#q4">question 4</a></li>
    </ul>
  </details>
  <details id="q2">
    <summary>Question 2</summary>
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </details>
  <details id="q3">
    <summary>Question 3</summary>
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </details>
  <details id="q4">
    <summary>Question 4</summary>
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </details>
</section>
sol
  • 22,311
  • 6
  • 42
  • 59