Is it possible to show and hide text using a link with only CSS -- without using JavaScript at all?
For example: On this page
Note the "More" link. When you click it, it unhides text. This particular example is JavaScript, but I am not sure if it can be done with pure CSS.
Use
"display: none;" attribute.
Yes, you can do that by using HTML and CSS only.
now you can hide/show text using only CSS too! If you are using text input and want to show/hide text based on the state of the input box, you can use the new CSS pseudo-class
:placeholder-shown
for<input>
or<textarea>
. Here is an example/demo of the above-mentioned pseudo-class!:Here is Link to MDN Docs.
There’s the
<details>
element, which isn’t yet built into Edge:I’m not sure how hard it is to style consistently across browsers.
There’s a common checkbox hack (where the checkbox can be hidden and the label can be styled to look like anything):
but it’s not always (maybe ever? hmm) appropriate to use it; you can usually just fall back on showing the content when JavaScript fails to load, and have the “more” link link to it.
There’s also
:target
, but it’s probably even less appropriate, since it's harder to build in the closing mechanism.You could hide a checkbox, but allow it to be checked/unchecked via its associated
<label>
element.Based on whether the checkbox is checked or not, you can hide/show the additional text, and even change the text of the label from "More" to "Less".
I've included some additional details in the CSS so that each definition's intentions can be a bit more clear.
1. With one toggle "More" / "Less" button:
2. With "More" button at the top and "Less" button at the bottom:
Technically speaking, it is possible to toggle the visibility of text based on when you click on a button or link, as shown below:
That being said, I do strongly recommend that you familiarize yourself with JavaScript as the solution using JavaScript for something like this is much simpler and allows for additional flexibility.