What is a nice way to do leading dots in a table of contents with CSS?
Example:
Link.............Chapter 1 Link.............Chapter 2 Link.............Chapter 3
We create the dots using a radial-gradient background. We space repeat the background along the x-axis ( background-repeat: space no-repeat ) to prevent any dot from being clipped at the middle and positioned on the bottom. The background size is three times as wide as the dot to create adequate space between them.
Unicode includes ⠇ which is the Braille symbol for the letter "U". To use, just use the HTML entity ⠇ in your code.
To type the dot symbol on your keyboard, turn on the numeric keypad by pressing NumLk , hold Alt and press the 0 , 1 , 4 , and 9 keys in succession. If you don't type the numbers with the numeric keypad, the dot symbol will not show.
This is the best CSS-only solution I have found for this issue of dot leaders:
http://www.w3.org/Style/Examples/007/leaders.en.html
HTML
<ul class="leaders"> <li><span>Salmon Ravioli</span> <span>7.95</span></li> <li><span>Fried Calamari</span> <span>8.95</span></li> <li><span>Almond Prawn Cocktail</span> <span>7.95</span></li> <li><span>Bruschetta</span> <span>5.25</span></li> <li><span>Margherita Pizza</span> <span>10.95</span></li> </ul>
CSS2/CSS3
ul.leaders { max-width: 40em; padding: 0; overflow-x: hidden; list-style: none } ul.leaders li:before { float: left; width: 0; white-space: nowrap; content: ". . . . . . . . . . . . . . . . . . . . " ". . . . . . . . . . . . . . . . . . . . " ". . . . . . . . . . . . . . . . . . . . " ". . . . . . . . . . . . . . . . . . . . " } ul.leaders span:first-child { padding-right: 0.33em; background: white } ul.leaders span + span { float: right; padding-left: 0.33em; background: white }
We create the dot leaders with a ‘:before’ pseudo-element attached to the LI elements. The pseudo-element fills the whole width of the list item with dots and the SPANs are put on top. A white background on the SPANs hides the dots behind them and an ‘overflow: hidden’ on the UL ensures the dots do not extend outside the list.
I used an arbitrary 80 dots, which is enough to fill about 38em, hence the maximum width on the list.
Building up on @Nico O‘s answer, there is no need for the un-semantic .dots
element.
.toc li { display: flex; } .toc li .title { order: 1; } .toc li .chapter { order: 3; } .toc li::after { background-image: radial-gradient(circle, currentcolor 1px, transparent 1.5px); background-position: bottom; background-size: 1ex 4.5px; background-repeat: space no-repeat; content: ""; flex-grow: 1; height: 1em; order: 2; }
<ul class="toc"> <li> <span class="title">Foo</span> <span class="chapter">Chapter 1</span> </li> <li> <span class="title">Bar</span> <span class="chapter">Chapter 2</span> </li> </ul>
We take advantage of the fact that we can order the children of our flex container however we want, and the fact that a pseudo element behaves like a child of where it was defined. The key is the flex-grow
rule. a flex-grow
of 1
while all other siblings have the default 0
will grow to the remaining space even though it has no content.
This will work until the .title
and .chapter
elements together fill all the space. Then the ::after
pseudo element will have a width
of 0
and the dotted border won’t be displayed, even though the .title
and .chapter
will wrap their content. So if you’re sure that won’t happen, and your viewers use modern browsers this might be the optimal solution.
We create the dots using a radial-gradient background. We space repeat the background along the x-axis (background-repeat: space no-repeat
) to prevent any dot from being clipped at the middle and positioned on the bottom. The background size is three times as wide as the dot to create adequate space between them. Also note that we add some blur around the edge of each dot (0.5px) for anti-aliasing.
Kudos to @Denis Savenko for coming up with using radial gradient to create the dots in a different answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With