Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMing-Tse Ta
1,115 Pointsimg { width: 100%;} .car { display:block; margin: 40px 0 auto 0; why here is "40px 0 auto 0" why put two zero??
img { width: 100%;} .car { display:block; margin: 40px 0 auto 0; why here is "40px 0 auto 0" why ??
/* Background Color */
html {
background: #99E6FF;
}
/* Body */
body {
margin: 0 auto;
max-width: 800px;
}
/* Images */
img { width: 100%;}
.car { display:block;
margin: 40px 0 auto 0;
padding: 0;}
/* Links */
a {
background: transparent;
border: 0;
display: block;
float: left;
margin: 0;
outline: 0;
padding: 0;
width: 33%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="charset" value="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=0.5, maximum-scale=0.5, minimal-ui">
<title>Car Sounds</title>
<!--Style Sheet link-->
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<!--Car image -->
<img src="images/bike.png" class="car" alt="car">
<!--Button-->
<a href="javascript:bikeBell();"><img src="images/bikeLock.png" alt="key"></a>
<!--Audio Files-->
<audio id="bikeBell" src="sounds/bikeBell.mp3" preload="auto"></audio>
<!--Javascript-->
<script type="text/javascript">
function bikeBell() {
document.getElementById('bikeBell').play();
}
</script>
</body>
</html>
1 Answer
Jason Cook
11,403 PointsI can probably help clear this up for you. As you know, the margin property can accept four values, defining top, right, bottom, and left margins, respectively. Defining all margins using the single margin property (rather than margin-top, margin-right, etc.) is also known as using the margin shorthand property.
So, below, let's compare the two methods for defining margins, to help show how values are interpreted.
1) Margin - Shorthand Property (as in your example)
margin: 40px 0 auto 0;
2) Margin - Individual Sides
margin-top: 40px;
margin-right: 0;
margin-bottom: auto;
margin-left: 0;
Note: if a top margin or bottom margin is set to "auto", then its inherent value will be interpreted as 0, whereas setting left and right margins to "auto" will cause remaining horizontal space in the element's container to be split equally, and therefore horizontally centering an element within it's parent container (e.g. "margin: 0 auto;").
I hope this helps answer your question, but if you're still curious about learning more, I recommend these two references:
Happy Coding! Feel free to reply if you have more questions :-)