Sub topic 5
Styling the summary, marker and open state
This chapter assumes you know the details and summary structure, the open attribute, and basic CSS selectors. You will style the widget's box, the clickable summary, the disclosure triangle, and the expanded state, without replacing any native behavior.
What you can target
A disclosure widget has three parts you can style separately.
| Part | Selector | What it controls |
|---|---|---|
| The widget | `details` | Border, background, padding, spacing |
| The clickable label | `summary` | Cursor, font, layout |
| The triangle | `summary::marker` | Color and size of the marker |
| The expanded state | `details[open]` | Any rule that applies only while open |
The browser adds and removes the open attribute as the user toggles the widget. That is why details[open] works as a state selector with no JavaScript.
A basic styled widget
Start by giving the widget a visible box and making the summary look clickable.
<details>
<summary>What is your return policy?</summary>
<p>You can return items within 30 days of delivery.</p>
</details>details {
border: 1px solid #ccc;
border-radius: 4px;
padding: 0.5rem 1rem;
}
summary {
cursor: pointer;
font-weight: 600;
}
details[open] summary {
margin-bottom: 0.5rem;
}
summary::marker {
color: royalblue;
}Each rule has a clear job:
detailsdraws one box around both the summary and the body, because the body is a child of the same element.summaryusescursor: pointerto signal that it is interactive. Browsers do not always do this by default.details[open] summaryadds space under the summary only when the body is visible. When the widget is closed, the extra margin would make the box look unbalanced.summary::markerrecolors the triangle without replacing it.
Styling the marker
The marker is a pseudo-element, a part of the page that CSS can style but that is not in the HTML. The summary element has display: list-item, which is why it gets a marker the way a list item does. The ::marker pseudo-element accepts only a small set of properties, mainly color, font-size, and content.
summary::marker {
color: royalblue;
font-size: 1.2em;
}You can also swap the symbol with content:
summary::marker {
content: "+ ";
}
details[open] summary::marker {
content: "− ";
}This gives a plus sign when closed and a minus sign when open. The marker is still generated by the browser, so keyboard behavior and screen reader announcements do not change.
Replacing the marker with your own icon
To use a custom icon, remove the native marker and add a pseudo-element on the summary. Two details matter here. Browsers based on WebKit use ::-webkit-details-marker, and other browsers use the list-style property.
summary {
list-style: none;
display: flex;
justify-content: space-between;
align-items: center;
cursor: pointer;
}
summary::-webkit-details-marker {
display: none;
}
summary::after {
content: "▸";
transition: transform 0.2s;
}
details[open] summary::after {
transform: rotate(90deg);
}The ::after arrow sits on the right edge and rotates a quarter turn when the widget opens. Because the state comes from details[open], the rotation stays in sync even when JavaScript changes open or when an exclusive group closes the widget.
Common errors
- Styling
details > pfor the body. The body can contain any elements. Select it withdetails > :not(summary)if you need to style all of it. - Hiding the body with
display: none. The browser already hides the content when the widget is closed. Overriding its display can break the toggle behavior. Style the body for the open state only. - Removing the focus outline. The summary receives keyboard focus. If you set
outline: none, give it a replacement such assummary:focus-visible { outline: 2px solid royalblue; }. - Relying on
::markerin a flex summary. Settingdisplay: flexon the summary removes its list-item display, so the marker no longer appears. That is the reason the custom icon above draws its own arrow.
Adding an open-state highlight
A small amount of state styling helps users see which items are expanded, especially in an accordion.
details[open] {
background: #f5f8ff;
border-color: royalblue;
}
summary:focus-visible {
outline: 2px solid royalblue;
outline-offset: 2px;
}The first rule colors the whole box while it is open. The second keeps a clear focus ring for keyboard users.
Exercise
A summary has display: flex and the triangle has vanished. Why did this happen, and how do you give the user a visible indicator again?
Show answer
The native marker only appears on elements with display: list-item. Setting display: flex replaces that, so the marker is no longer generated. Add your own indicator with summary::after (for example content: "▸") and rotate it with details[open] summary::after { transform: rotate(90deg); }.
Exercise
Write one CSS rule that gives the expanded widget a light blue background, without using JavaScript.
Show answer
details[open] { background: #f5f8ff; }. The browser sets the open attribute when the user expands the widget, so the attribute selector matches only the expanded state.