You can achieve this using CSS with the ::before or ::after pseudo-elements, or by using a decorative <hr> element. Here are a few approaches:
Method 1: Styled <hr> with CSS
<hr class="decorative-line" aria-hidden="true">
.decorative-line {
border: none;
height: 1em;
overflow: hidden;
text-align: center;
}
.decorative-line::before {
content: "==================================================";
display: block;
letter-spacing: 0.2em;
}
Method 2: Using a div with aria-hidden
<div class="separator" aria-hidden="true"></div>
.separator::before {
content: "==================================================";
display: block;
text-align: center;
letter-spacing: 0.1em;
}
Method 3: Using CSS repeating-linear-gradient
<div class="line-separator" role="separator" aria-hidden="true"></div>
.line-separator {
height: 20px;
background-image: repeating-linear-gradient(
to right,
transparent,
transparent 10px,
currentColor 10px,
currentColor 20px
);
background-position: center;
background-size: 20px 2px;
background-repeat: repeat-x;
}
The key accessibility considerations:
- Use
aria-hidden="true" to hide decorative elements from screen readers
- If the line has semantic meaning (like separating sections), use
role="separator" on the element
- For meaningful separators, you might want to include visually hidden text:
<div class="separator" role="separator">
<span class="visually-hidden">Section break</span>
<span aria-hidden="true">====================</span>
</div>
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
This way, screen reader users get meaningful information while visual users see the decorative line.