


Hide and Show Content Seamlessly with CSS
To achieve a collapsible content list with pure CSS, we encounter a challenge: content can be hidden by simply clicking anywhere on the page. This deviates from the desired behavior of hiding content only when the "Hide" link is clicked.
The CSS Solution
The solution lies in leveraging the :focus and ~ selectors. The revised CSS code:
<code class="css">body { display: block; } .span3:focus ~ .alert { display: none; } .span2:focus ~ .alert { display: block; } .alert { display: none; }</code>
The HTML Markup
<code class="html"><span class="span3">Hide Me</span> <span class="span2">Show Me</span> <p class="alert">Some alarming information here</p></code>
Understanding the Solution
- Focus State: The :focus selector targets the element that has received focus (e.g., when clicked).
- Sibling Selector (~): The ~ selector matches an element that is a sibling of the selected element.
- Conditional Display: The CSS rules set the display property of the .alert element based on the focus state of the .span3 and .span2 elements.
When the "Hide Me" link (.span3) receives focus, its sibling, the .alert element becomes hidden. Conversely, when the "Show Me" link (.span2) receives focus, its sibling, the .alert element becomes visible. This ensures that the content is hidden or shown only when the corresponding link is clicked, as intended.
The above is the detailed content of How to Create a Collapsible Content List with Pure CSS: How do you prevent content from hiding when you click anywhere on the page?. For more information, please follow other related articles on the PHP Chinese website!

In this week's roundup: Firefox gains locksmith-like powers, Samsung's Galaxy Store starts supporting Progressive Web Apps, CSS Subgrid is shipping in Firefox

In this week's roundup: Internet Explorer finds its way into Edge, Google Search Console touts a new speed report, and Firefox gives Facebook's notification

You’re probably already at least a little familiar with CSS variables. If not, here’s a two-second overview: they are really called custom properties, you set

Building websites is programming. Writing HTML and CSS is programming. I am a programmer, and if you're here, reading CSS-Tricks, chances are you're a

Here's what I'd like you to know upfront: this is a hard problem. If you've landed here because you're hoping to be pointed at a tool you can run that tells

Picture-in-Picture made its first appearance on the web in the Safari browser with the release of macOS Sierra in 2016. It made it possible for a user to pop

Gatsby does a great job processing and handling images. For example, it helps you save time with image optimization because you don’t have to manually

I learned something about percentage-based (%) padding today that I had totally wrong in my head! I always thought that percentage padding was based on the


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft