<!doctype html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="/assets/reset.css" rel="stylesheet"> <link href="setup.css" rel="stylesheet"> <link href="style.css" rel="stylesheet"> </head> <body> <section> <h2>Line/<wbr>bounding box</h2> </section> <section> <h2 class="precise">Much better!</h2> </section> </body> </html>
body { --base: 20px; display: grid; font-family: 'Helvetica', sans-serif; gap: var(--base); padding: var(--base); } section { background-color: gold; } h2 { font-size: calc(var(--base) * 3); }
h2 { /* Just to see things better, a transparent blue. */ background-color: hsla(200, 100%, 50%, 33%); } .precise::before, .precise::after { content: ''; /* Gives us an “empty” one to work with. */ display: block; visibility: hidden; /* We don’t need to see it or click it. */ } .precise::before { margin-top: var(--inset--top); } .precise::after { margin-bottom: var(--inset--bottom); } .precise { --inset--top: -0.2em; /* Font/line-height-dependent! */ --inset--bottom: -0.23em; --inset--left: -0.07em; --inset--right: -0.06em; display: flow-root; /* Cinch the height. */ margin-left: var(--inset--left); margin-right: var(--inset--right); width: fit-content; /* Cinch the width. */ }