Sub topic 5
Styling the summary, marker and open state
This chapter assumes you can write basic HTML and CSS selectors. It also assumes you have built a details element with a summary child and know that the browser adds an open attribute when the widget expands. Here you will style three parts of the widget: the summary row, the marker (the disclosure triangle), and the open state. You will also keep the widget usable for keyboard and screen reader users while you do it.
What the browser gives you by default
The browser's built-in style sheet decides how a bare details looks. These defaults explain most styling surprises.
detailsis a block box with no border, padding or background.summaryis displayed as alist-item. That display type is why it can have a marker, the same way anlican.- The marker is a triangle that points right when closed and down when open. In Chromium-based browsers, the default
list-styleforsummaryisdisclosure-closedanddisclosure-open. Firefox and Safari draw an equivalent triangle. - The marker sits inside the summary box, on the same line as the text. It is not placed in the margin the way a bullet in a list is.
- The body has no wrapper element. Every child of
detailsother than the firstsummaryis the hidden content, and it receives no special class or tag.
The last point matters for styling. You cannot write details .body unless you add a wrapper yourself. The selectors in this chapter use details > :not(summary) or the ::details-content pseudo-element instead.
Styling the container and the summary row
Start with the outer box and the clickable row. Where you put padding decides how big the click target is.
details {
border: 1px solid #c8ccd0;
border-radius: 6px;
background: #fff;
}
summary {
padding: 0.75rem 1rem;
cursor: pointer;
font-weight: 600;
}
details > :not(summary) {
margin: 0;
padding: 0.75rem 1rem;
border-top: 1px solid #e3e5e8;
}Here the padding is on summary, not on details. If you pad details instead, the padded edge is outside the summary. A user who clicks there hits the container and nothing toggles. A summary that fills the full width of the box has a larger target, which helps touch users.
The rule details > :not(summary) styles every child that is part of the body. The child combinator > keeps it from reaching into nested details elements deeper in the tree. This matters if your body contains another disclosure.
Hover, focus and active states
summary accepts the usual state pseudo-classes. Give each one a visible style.
summary:hover {
background: #f4f6f8;
}
summary:focus-visible {
outline: 3px solid #1a5fd0;
outline-offset: -3px;
}
summary:active {
background: #e9edf1;
}The negative outline-offset draws the focus ring inside the box. This keeps it from being clipped when the details has overflow: hidden or sits inside a tight container.
Styling the open state
When the widget expands, the browser sets the open attribute on details. An attribute selector matches it.
details[open] {
border-color: #1a5fd0;
box-shadow: 0 2px 8px rgb(0 0 0 / 0.08);
}
details[open] > summary {
background: #eef3fc;
}Use the child combinator in details[open] > summary. A bare details[open] summary also matches the summary of any nested closed details inside an open one, and that style is usually unwanted.
CSS also defines an :open pseudo-class that matches open details elements. It reads better (details:open), but it is newer than the attribute selector. Check current browser support before you rely on it. The attribute selector works everywhere, so this chapter uses it.
Why the state selector is reliable
The browser adds and removes open itself when the user toggles the widget, and it does the same when script sets details.open. Your CSS reacts to the attribute, so it needs no class toggling and no JavaScript. The attribute is the single source of truth for the styles.
Styling the marker
The marker is the pseudo-element ::marker. It is available on any list item, and summary is a list item by default.
summary::marker {
color: royalblue;
font-size: 1.2em;
}The ::marker pseudo-element accepts only a short list of properties.
| Property | Works on `::marker`? |
|---|---|
| `color` | Yes |
| `font-size`, `font-weight`, `font-family` | Yes |
| `content` | Yes, in browsers that support it on `summary` |
| `white-space`, `direction`, `unicode-bidi` | Yes |
| `animation`, `transition` | Yes |
| `background`, `border`, `padding`, `margin` | No |
| `transform` | No |
Because transform is not allowed, you cannot rotate the native marker. Because margin and padding are not allowed, you cannot adjust the gap between marker and text. For either of those, you need a custom marker.
Swapping the marker's character
Where content is supported on ::marker, you can replace the triangle with text. Pair the closed and open rules.
summary::marker {
content: "+ ";
}
details[open] > summary::marker {
content: "\2212 ";
}The escape \2212 is the true minus sign, which has the same width as the plus sign in most fonts. The same swap through list-style-type takes a string, such as list-style-type: "+ ".
Browsers differ in how much of ::marker they honor on summary. Safari has historically limited the pseudo-element on this element. Test in every browser you support. If a swap fails, use the portable approach in the next section.
Building a custom marker with ::before
This approach works in all current browsers and gives you full control, including animation. You hide the native marker, then draw your own with ::before.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Custom marker</title>
<style>
details {
max-width: 32rem;
border: 1px solid #c8ccd0;
border-radius: 6px;
font-family: system-ui, sans-serif;
}
summary {
display: flex;
align-items: center;
gap: 0.6rem;
padding: 0.75rem 1rem;
cursor: pointer;
font-weight: 600;
list-style: none;
}
summary::-webkit-details-marker {
display: none;
}
summary::before {
content: "";
flex: none;
width: 0.5em;
height: 0.5em;
border-right: 2px solid currentColor;
border-bottom: 2px solid currentColor;
transform: rotate(-45deg);
transition: transform 0.2s ease;
}
details[open] > summary::before {
transform: rotate(45deg);
}
summary:focus-visible {
outline: 3px solid #1a5fd0;
outline-offset: -3px;
}
details > :not(summary) {
margin: 0;
padding: 0.75rem 1rem;
border-top: 1px solid #e3e5e8;
}
@media (prefers-reduced-motion: reduce) {
summary::before {
transition: none;
}
}
</style>
</head>
<body>
<details>
<summary>What is your return policy?</summary>
<p>You can return items within 30 days of delivery.</p>
</details>
</body>
</html>Each part of the summary rule has a job.
list-style: noneremoves the marker in browsers that follow the standard model.summary::-webkit-details-marker { display: none; }removes it in Safari, which used its own vendor pseudo-element for years.display: flexlets the chevron and the text align and take agap. Changingdisplayaway fromlist-itemalso removes the marker in most browsers, but do not rely on that alone.- The chevron is a box with two borders. Rotating it by -45 degrees points it right, and 45 degrees points it down.
transition: transformanimates the turn. Theprefers-reduced-motionquery turns the animation off for users who asked their system to limit motion.
The chevron is only decoration. The state is still carried by the open attribute, which assistive technology reads. See the accessibility notes below.
Placing the marker on the right
Many designs put the indicator at the end of the row. Add order or use justify-content.
summary {
display: flex;
justify-content: space-between;
align-items: center;
}
summary::before {
order: 1;
}Since flex items sort by order, the pseudo-element moves after the text, and space-between pushes the two apart. The ::before name stays the same even though it now appears last.
Animating the body
The body of a closed details is not rendered, so a plain transition on height has nothing to animate. Newer browsers expose the hidden content as the ::details-content pseudo-element, which you can target. Combined with interpolate-size, it allows a height animation.
@supports selector(details::details-content) {
:root {
interpolate-size: allow-keywords;
}
details::details-content {
block-size: 0;
overflow: clip;
transition:
block-size 0.25s ease,
content-visibility 0.25s allow-discrete;
}
details[open]::details-content {
block-size: auto;
}
}The @supports block makes this a progressive enhancement. A browser without these features ignores the whole block, and the widget still opens and closes instantly. Do not make the widget's function depend on this animation.
Styling a group of items
An accordion is several details elements in a row. Without extra rules, each one has its own full border, so adjacent borders double up. Collapse them with the adjacent sibling combinator.
details + details {
border-top: 0;
border-top-left-radius: 0;
border-top-right-radius: 0;
}
details:has(+ details) {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
}The first rule removes the top border from every item that follows another item. The second rule squares the bottom corners of every item that has a following sibling. The result is one joined block with rounded outer corners. Grouping behavior itself (the name attribute) is covered in another chapter. These rules only change how the items look.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Padding on `details` instead of `summary` | Clicks on the padding do nothing | Put padding on `summary` and on the body children |
| `summary { display: none }` | The widget has no visible control, and the browser may create its own default label | Style the summary, never hide it |
| `list-style: none` with no replacement indicator | Users cannot tell the row is expandable | Add a `::before` chevron or a text cue |
| Only `list-style: none`, no `::-webkit-details-marker` rule | The marker still shows in older Safari | Add the vendor rule as shown above |
| Removing the focus outline | Keyboard users lose their position | Style `:focus-visible` instead |
| `details[open] summary` with nested widgets | Styles leak onto closed inner summaries | Use `details[open] > summary` |
| Trying to rotate `::marker` | `transform` is ignored | Use a custom `::before` marker |
| Setting `display: flex` on `details` | The hidden body may render or layout may break in some browsers | Apply layout rules to `summary` and to body children |
Keeping styled widgets accessible
Styling must not remove what the native element already provides.
- Keep the summary focusable and keep the visible focus ring. Restyle it if you like, but a clear ring must remain.
- Keep enough color contrast between summary text and its background, and between the marker and its background. A pale gray chevron on white can fail.
- Do not rely on color alone to show the open state. The marker's direction or shape change is a second cue.
- Do not set
pointer-events: noneon the summary or place an overlay over it. - Do not replace the summary with a styled
divand a script to match the look. The native element already exposes its expanded or collapsed state to assistive technology.
Putting the pieces together
A typical styled disclosure follows this order of work.
- Write the plain
detailsandsummarymarkup and confirm it toggles with no CSS. - Style the container and the summary row, with padding on the summary.
- Add hover and
:focus-visiblestates. - Add
details[open] > summarystyles for the expanded look. - Adjust the native marker with
::marker. If you need rotation, position or animation, switch to a hidden marker and a::beforeindicator. - Add motion last, behind
prefers-reduced-motionand@supports. - Test with the keyboard and in each browser you support.
Exercise
A teammate writes summary { list-style: none; } and the triangle disappears in Chrome and Firefox but still shows in Safari. They also notice that users now can't tell the rows are expandable. What two changes fix both problems?
Show answer
Add summary::-webkit-details-marker { display: none; } so Safari also hides its native marker. Then add a replacement indicator, such as a summary::before chevron that rotates when details[open] > summary::before matches, so the row still shows that it can expand.
Exercise
Why does details[open] > summary work better than details[open] summary in a page where one open widget contains another closed widget?
Show answer
The descendant selector details[open] summary matches every summary inside an open details, including the summary of the closed inner widget, so the inner one gets the "open" look by mistake. The child combinator matches only the summary that is a direct child of the open details.
Exercise
You set summary::marker { transform: rotate(90deg); transition: transform 0.2s; } to animate the triangle. Nothing rotates. Why, and what is the alternative?
Show answer
::marker accepts only a small set of properties (such as color, font-*, content, white-space, animation and transition), and transform is not one of them, so the declaration is ignored. Hide the native marker with list-style: none and the -webkit-details-marker rule, draw an indicator with summary::before, and rotate that element in a details[open] > summary::before rule.