Skip to content

Instantly share code, notes, and snippets.

@dennyferd
Created January 23, 2022 15:44
Show Gist options
  • Save dennyferd/111de6831512f67fcf4f5d84ca618fe3 to your computer and use it in GitHub Desktop.
Save dennyferd/111de6831512f67fcf4f5d84ca618fe3 to your computer and use it in GitHub Desktop.
Native HTML details element styled via CSS – customized
<div class="card">
<h1>Coronavirus - Facts, advice and measures</h1>
<details class="warning">
<summary>Facts and knowledge about COVID-19</summary>
<p>Some text to be hidden.</p>
<p>Some text to be hidden.</p>
<p>Some text to be hidden.</p>
<p>Some text to be hidden.</p>
</details>
<details class="info" open>
<summary>For the public</summary>
<p>Social distance, quarantine and isolation</p>
<p>Hand hygiene, cough etiquette, cleaning and laundry</p>
<p>When children have acute respiratory tract infections</p>
<p>Risk groups and their relatives</p>
</details>
<details class="alert">
<summary>Facts and knowledge about COVID-19</summary>
<p>Some text to be hidden.</p>
<p>Some text to be hidden.</p>
<p>Some text to be hidden.</p>
<p>Some text to be hidden.</p>
</details>
</div>

Native HTML details element styled via CSS – customized

Rebound shot from Dribbble to implement an accordion-like element using only the details/summary native HTML element and CSS.

Check the Dribbble shot over at: https://dribbble.com/shots/14907895-Native-HTML-details-element-styled-via-CSS.

Best results can be seen in Chrome or Blink-based browsers. Firefox and others have decent results as well, but they could be improved creating alternative open/close icons.

A Pen by dennyferd on CodePen.

License.

/*
Native <details> element styling
AUTHOR: https://codepen.io/nicolasjengler
+ replaced list-item styling with flex and pseudo-element content on summary => vertically aligned sign with summary content
+ added selection and focus outline matching detail style
+ addede custom list-style-type
- corona-warning
- corona-info
- corona-alert
+ fix body hehight –> min-height:
allow scroll on all details opened
*/
@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@300;400;500;600;700&display=swap');
:root {
--color-bg: #EEEDEB;
--color-title: #0E1C4E;
--color-summary-1: #FFF6EE;
--color-summary-1-highlight: #FFC48B;
--color-summary-2: #FAFAFF;
--color-summary-2-highlight: #B4B3FF;
--color-summary-3: #FFF0F3;
--color-summary-3-highlight: #FFB3C0;
--font-ibm-plex-sans: 'IBM Plex Sans', sans-serif;
}
html,
body {
overflow: auto;
min-height: 100vh;
width: 100%;
background: var(--color-bg);
display: flex;
align-items: center;
justify-content: center;
}
.card {
background: white;
padding: 38px 36px;
margin-top: 40px;
margin-bottom: 40px;
border-radius: 4px;
box-shadow: 0 8px 10px rgba(0, 0, 0, .1);
max-width: 30em;
width: 100%;
h1 {
font-family: var(--font-ibm-plex-sans);
font-style: normal;
font-weight: bold;
font-size: 20px;
line-height: 1.2;
color: var(--color-title);
margin-bottom: 20px;
}
details {
display: flex;
border-radius: 5px;
overflow: hidden;
background: rgba(0, 0, 0, .05);
border-left: 15px solid gray;
padding:15px;
& {
margin-top: 15px;
}
&.warning {
--highlight: var(--color-summary-1-highlight) ;
background: var(--color-summary-1);
border-left-color: var(--color-summary-1-highlight);
p {
list-style-type: corona-warning;
}
}
&.info {
--highlight: var(--color-summary-2-highlight) ;
background: var(--color-summary-2);
border-left-color: var(--color-summary-2-highlight);
p {
list-style-type: corona-info;
}
}
&.alert {
--highlight: var(--color-summary-3-highlight) ;
background: var(--color-summary-3);
border-left-color: var(--highlight);
p {
list-style-type: corona-alert;
}
}
summary,p {
position:relative;
display:flex;
flex-direction:row;
align-content: center;
justify-content: flex-start;
font-family: var(--font-ibm-plex-sans);
font-style: normal;
font-weight: normal;
font-size: 18px;
color: var(--color-title);
padding: 20px;
cursor:pointer;
&::-webkit-details-marker {
display:none;
}
&:focus {
outline: solid 3px var(--highlight);
}
&::selection {
background-color:var(--highlight);
}
}
p {
display: list-item;
cursor:default;
margin-left:3rem;
list-style-type: corona;
}
summary::before {
cursor: pointer;
position:absolute;
display:inline-flex;
width:1rem;
height:1rem;
left: 0rem;
margin-right:.5rem;
content: url("data:image/svg+xml,%3Csvg width='100%' height='100%' viewBox='0 0 24 24' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M22.6066 12H1.3934' stroke='%23202842' stroke-width='1.875' stroke-linecap='round' stroke-linejoin='round'/%3E%3Cpath d='M12 1.39343V22.6066' stroke='%23202842' stroke-width='1.875' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E%0A");
}
&[open] {
summary {
font-weight: 700;
&::before {
transform: rotate(45deg);
content: url("data:image/svg+xml,%3Csvg width='100%' height='100%' viewBox='0 0 24 24' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M22.6066 12H1.3934' stroke='%23202842' stroke-width='3.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3Cpath d='M12 1.39343V22.6066' stroke='%23202842' stroke-width='3.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E%0A");
}
}
}
}
}
@counter-style corona-warning {
system: cyclic;
symbols: 🧼 🩺 👩🏻‍⚕️ 🚑;
suffix: " ";
}
@counter-style corona-info {
system: cyclic;
symbols: 🧬;
suffix: " ";
}
@counter-style corona-alert {
system: fixed;
symbols: 💉 🩸 😷 🦠 🧫;
suffix: " ";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment