


How to Prevent Unintended Content Hiding in CSS: A Solution for the \':focus\' Problem
Hide/Show Content-List with CSS: Resolving Unintended Behavior
In pursuit of a solution to hide and show content using only CSS, a developer encountered an issue: the content could be hidden by clicking anywhere on the page, rather than just by clicking the designated "hide" link.
The developer's initial code used the following CSS:
<code class="css">#cont {display: none; } .show:focus + .hide {display: inline; } .show:focus + .hide + #cont {display: block;}</code>
This code successfully hides the content when the "hide" link is clicked. However, it also enables hiding by clicking any part of the page, because the CSS rule targets the ":focus" pseudo-class.
To resolve this issue, we can use the following updated CSS:
<code class="css">body { display: block; } .span3:focus ~ .alert { display: none; } .span2:focus ~ .alert { display: block; } .alert{display:none;}</code>
In this updated code, we use the "~" combinator to target elements that are siblings of the focused element. This means that the "hide" element (~ .alert) is only hidden when the "show" element is in focus.
The HTML remains the same:
<code class="html"><span class="span3">Hide Me</span> <span class="span2">Show Me</span> <p class="alert" >Some alarming information here</p></code>
With this revised approach, the content is now only hidden when the "Hide Me" element is clicked, as the developer intended.
The above is the detailed content of How to Prevent Unintended Content Hiding in CSS: A Solution for the \':focus\' Problem. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

One thing that caught my eye on the list of features for Lea Verou's conic-gradient() polyfill was the last item:
