forked from PoulNichols/PoulNichols.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
HTML and CSS Tips
Sid edited this page Feb 28, 2025
·
3 revisions
A link (<a> tag) normally looks and behaves like this: the clickable area is the link text, or any child element you may have inside the <a>.
However, there are a lot of situations where we want the whole parent element to be clickable, like in a typical nav: your list items are styled to look like buttons, so they should behave like buttons too. You've probably encountered websites with buttons that wouldn't do anything unless you clicked the text inside the button - that's annoying, especially on mobile devices!
This is easy to fix with CSS. Here's a great guide covering a number of common scenarios.
For example, to fix this in a nav item:
nav li > a {
display: block;
padding: 1rem;
/* or whatever padding value you want */
}- Apply padding to the
ainside anav li- Padding, not margin - padding adds space inside an element, making it bigger. Margin adds space around it.
- Apply padding to the
a, not theli, because theadefines the clickable area.
-
display: block;helps us two ways here:- It lets us add padding - an
ais normally inline, and inline elements are unaffected by padding and margin. - Without this rule, our
ais only as wide as its content. A block element defaults to the width of its parent, which makes the full width of the button clickable.
- It lets us add padding - an
See the linked article for more examples.