Display more text without JS

The details element looks good for toggling block content but I see no way to make it work with inline fragments such as truncating a paragraph which is a shame.

1 Like

Ah nice. I was hoping something like that was possible.

Actually, I had someone complain about just that recently (hence my curosity). I ended up hiding the element by default and using JS to show it (which isn’t overly accessible).

Would it make sense to use CSS here to avoid the reliance on JS?

https://www.sitepoint.com/community/t/hiding-a-div-once-user-has-closed-it/247005/5

For accessibility you could add a js class to the html element which can be done straight away before the rest of the document needs to load loads and then use that class to hide the element in the css. I use that method all the time.

e.g.
js .myElement {display:none}

I would have done the same for the above demo except its not possible as we are not hiding specific blocks as such but using a character limit to truncate the text and therefore there was nothing for the css to hook into.

If instead of counting characters we used a span around the raw html we wanted to hide then the content could be hidden initially using the js class easily.

1 Like

I’m probably being dense, but that would still mean the element is hidden by default and only visible to those with JS enabled (or did I miss the point entirely)?

The class would be added to the HTML element by JS, so it will only take effect if JS is operating. The script would be added early on in the head, before the CSS.

4 Likes

Ah, gotchya. Right. Thanks, Ralph.

Yes as Ralph said you just add the js snippet as the first rule in the head of the document followed by the css which means html has loaded but the body hasn’t loaded yet and therefore there is no flash of content because the css hiding rule takes effect straight away. If js in’t available the element is displayed as normal.

2 Likes

Cheers, Paul. That’s a much better way. I updated my answer in the other thread.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.