Justin Mathew Justin Mathew - 2 months ago 10
CSS Question

<nav> element doesn't show top padding

I am designing a website using HTML5 elements. With the following HTML5 and CSS code, there is some abnormality with the top padding for the menus.



/* CSS Resets */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin:0;
padding:0;
border:0;
font-size:100%;
font:inherit;
vertical-align:baseline;
}
/* CSS Resets end */


/* CSS document starts */

body {
background-color: #e4deaf;
margin: 0;
padding: 0;
}

nav {
display: block;
}

a {
text-decoration: none;
color: #808080;
border: 1px dashed black;
border-radius: 7px;
padding: 1em 3em;
}
a:hover {
background-color: #ffbc6a;
}
a:active {
background-color: #e4deaf;
}

<nav>
<a href="">Menu 1</a>
<a href="">Menu 2</a>
<a href="">Menu 3</a>
</nav>





This is the output screenshot:

What is wrong with the padding of the menus?

Answer

It's because of the inline default value of display property on a elements. Try to change with inline-block and it works.

/* CSS Resets */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin:0;
	padding:0;
	border:0;
	font-size:100%;
	font:inherit;
	vertical-align:baseline;
}
/* CSS Resets end */


/* CSS document starts */

body {
	background-color: #e4deaf;
	margin: 0;
	padding: 0;
}

nav {
	display: block;
}

a {
	text-decoration: none;
	color: #808080;
	border: 1px dashed black;
	border-radius: 7px;
	padding: 1em 3em;
    display: inline-block;
}
a:hover {
	background-color: #ffbc6a;
}
a:active {
	background-color: #e4deaf;
}
<nav>
  <a href="">Menu 1</a>
  <a href="">Menu 2</a>
  <a href="">Menu 3</a>
</nav>