search
HomeWeb Front-endCSS TutorialTwo Issues Styling the Details Element and How to Solve Them

Two Issues Styling the Details Element and How to Solve Them

Previously, creating simple expandable content blocks required JavaScript or complex CSS workarounds. Modifying the HTML could also become cumbersome. Now, the <details></details> and <summary></summary> elements (forming a "disclosure widget") simplify this significantly. We use them extensively at work for FAQs, for example.

Addressing Common Styling Challenges

While <details></details> and <summary></summary> inherently provide expand/collapse functionality, you might still need CSS for optimal presentation. Without styling, two key issues arise:

Issue 1: <summary></summary> Cursor

The <summary></summary> element, while interactive, defaults to a text selection cursor instead of the expected pointer.

Issue 2: Nested Block Elements in <summary></summary>

Nesting block-level elements (like headings) within <summary></summary> causes them to appear below the arrow, not inline.

The CSS Solution

To resolve these, add these styles to your CSS reset:

details summary {
  cursor: pointer;
}

details summary > * {
  display: inline;
}

Let's examine each issue and its solution in detail.

Customizing the <summary></summary> Cursor

A cursor should visually reflect its intended interaction. The default text cursor on <summary></summary> elements, while technically correct (the text is selectable), is less intuitive than a pointer.

The solution is simple:

details summary {
  cursor: pointer;
}

Many prominent websites, including MDN Web Docs and GitHub, already employ this style for their disclosure widgets. The default cursor: text likely reflects the selectability of the summary text, but a pointer is generally preferable for interactive elements. Note that changing the cursor only affects the visual appearance; selectability remains unchanged.

Displaying Nested <summary></summary> Content Inline

For FAQs, I often wrap questions in headings (e.g., <h3></h3>) within <summary></summary>:

<details><summary><h3 id="Will-my-child-s-Plan-be-implemented">Will my child's 504 Plan be implemented?</h3></summary><p>Yes. Similar to the Spring, case managers will reach out to students.</p></details>

This offers several advantages:

  • Consistent Styling: Maintains visual consistency with other headings.
  • IE/EdgeHTML Compatibility: Provides fallback for older browsers that don't support <details></details>.
  • Accessibility: Aids assistive technology navigation (though interpretation by screen readers can vary, as discussed below).

Headings vs. Buttons

The <summary></summary> element behaves like a button (it implicitly has role=button), yet unlike buttons, it allows nested headings. This creates a conflict:

  • Headings aid navigation.
  • Buttons typically strip semantics from nested elements.

Screen reader compatibility is inconsistent here. NVDA and VoiceOver recognize headings inside <summary></summary>, but JAWS does not. Therefore, while styling headings within <summary></summary> is possible, their semantic interpretation is not guaranteed.

Inline Styling

To prevent the arrow from appearing above the heading, use inline styling for elements nested directly within <summary></summary>:

details summary > * {
  display: inline;
}

Use inline, not inline-block, to avoid wrapping issues. While tempting to use display: flex on <summary></summary>, this hides the arrow.

Bonus: Excluding Internet Explorer Styles

Since IE and older Edge versions don't support <details></details>, avoid applying custom styles to them using a feature query:

@supports not (-ms-ime-align: auto) {
  details summary {
    cursor: pointer;
  }
  details summary > * {
    display: inline;
  }
  /* Other <details>/<summary> styles */
}</summary></details>

IE ignores this block entirely. EdgeHTML also ignores it due to the -ms-ime-align check. Note that very old Chrome and Safari versions (with negligible market share) also lack feature query support. A @supports (details) block would be ideal but has even less browser support.

Conclusion

With the correct HTML structure and these CSS styles, you can easily customize your disclosure widgets. Remember that while styling <summary></summary> elements is straightforward, screen reader compatibility for nested headings requires consideration.

The above is the detailed content of Two Issues Styling the Details Element and How to Solve Them. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Lost CSS Tricks of Cohost.orgThe Lost CSS Tricks of Cohost.orgApr 25, 2025 am 09:51 AM

In this post, Blackle Mori shows you a few of the hacks found while trying to push the limits of Cohost’s HTML support. Use these if you dare, lest you too get labelled a CSS criminal.

Next Level CSS Styling for CursorsNext Level CSS Styling for CursorsApr 23, 2025 am 11:04 AM

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Worlds Collide: Keyframe Collision Detection Using Style QueriesWorlds Collide: Keyframe Collision Detection Using Style QueriesApr 23, 2025 am 10:42 AM

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Using CSS backdrop-filter for UI EffectsUsing CSS backdrop-filter for UI EffectsApr 23, 2025 am 10:20 AM

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

SMIL on?SMIL on?Apr 23, 2025 am 09:57 AM

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

'Pretty' is in the eye of the beholder'Pretty' is in the eye of the beholderApr 23, 2025 am 09:40 AM

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

CSS-Tricks Chronicles XLIIICSS-Tricks Chronicles XLIIIApr 23, 2025 am 09:35 AM

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Tailwind's @apply Feature is Better Than it SoundsTailwind's @apply Feature is Better Than it SoundsApr 23, 2025 am 09:23 AM

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft