I'm an HTML/CSS beginner and I have a question about the width of <div>
elements.
Here's my code:
header {
background-image: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), url(IMAGES/hero.jpg);
height: 100vh;
background-size: cover;
background-position: center;
background-attachment: fixed;
}
.hero-text-box {
position: absolute;
width: 1140px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<body>
<header>
<nav>
</nav>
<div class="hero-text-box">
<h1>Goodbye junk food. Hello super healthy meals.</h1>
<a href="#" class="btn btn-full">I'm hungry</a>
<a href="#" class="btn btn-ghost">Show me more</a>
</div>
</header>
</body>
This is what the result looks like:
However, if I comment out the 'width' command in the CSS, like this:
header {
background-image: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), url(IMAGES/hero.jpg);
height: 100vh;
background-size: cover;
background-position: center;
background-attachment: fixed;
}
.hero-text-box {
position: absolute;
/*width: 1140px;*/
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<body>
<header>
<nav>
</nav>
<div class="hero-text-box">
<h1>Goodbye junk food. Hello super healthy meals.</h1>
<a href="#" class="btn btn-full">I'm hungry</a>
<a href="#" class="btn btn-ghost">Show me more</a>
</div>
</header>
</body>
Then the image changes to this:
I'd like to know why the second image looks the way it does? Why, for instance, did the browser choose to put a line break after the word 'Food', and another after 'Healthy'? Why isn't there a line break after every word? Or every letter, for that matter?
Ever since I started learning HTML/CSS I've had terrible trouble understanding the rules around spacing and positioning of elements. They seem arbitrary. Is there a reason why the <div>
my text is in defaults to this specific width when I remove the 'width: 1140px'
property from the CSS? It looks like the <div>
containing the heading and my links has some kind of preset width that I don't know about, but that doesn't seem right.