Jari Flederick Jari Flederick - 2 months ago 7
CSS Question

HTML Text over image

I'm trying to put 4 headers over an image, but with the code I give something weird happens: The 2 headers on the left get thrown out of the div.

Picture for refrence:

As you can see the div has a red border but the text jumps out of it for some reason.

The code:



@font-face {
font-family: Head;
src: url('HeadFont.ttf');
}
@font-face {
font-family: SpecifyLight;
src: url('SpecifyLight.ttf');
}
@font-face {
font-family: SpecifyMedium;
src: url('SpecifyMedium.ttf');
}

@font-face {
font-family: Android;
src: url('Android.ttf');
}

BODY {
background-image: url("bg.jpg");
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
width: 75%;
margin:auto;
}

/*Navigatie bar styling*/
ul {
list-style-type: none;
margin: auto;
margin-top: 0;
padding: 0;
overflow: hidden;
background-color: black;
}

.ulMain {
background-image: url("menu.png");
background-position: center;
background-position: top;
background-size: 100% auto;
border-radius: 0px 0px 20px 20px;
}

li {
float: left;
border-right: 1px solid #bbb;
}

li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

li a:hover:not(.active) {
color: #B8371A;
background-color: white;
}

li:last-child {
border-right: none;
}

.active {
background-color: darkred;
}

h1 {
width: 100%;
font-family: Head;
font-size: 80px;
margin-top: 0;
text-align: center;
color: black;
padding: 0;
}

h2 {
width:100%;
font-family: Head;
font-size: 50px;
text-align: center;
padding: 0;
}

h3 {
width:100%;
font-family: Head;
font-size: 30px;
text-align: left;
padding: 0;
}

h4 {
width:100%;
font-family: Head;
font-size: 40px;
margin: 10px;
text-align: left;
color: red;
}

h5 {
width:100%;
font-family: Head;
font-size: 40px;
text-align: right;
color: red;
}

header {
width: 100%;
margin: auto;
height: auto;

}

.menu {
width: 100%;
margin: auto;
height: auto;
}

footer {
width: 100%;
margin: auto;
height: 200px;
background-color: #B8371A;
border-radius: 0px 0px 20px 20px;
}

.content {
font-family:SpecifyLight;
font-size: 20px;
width: 100%;
margin: auto;
}

.bioFoto {
float: right;
width: 20%;
height: auto;
margin-right: 100px;
border: solid;
border-color: black;
border-radius: 20px;
}

.website {
width: 100%;
margin:auto;
background-color: lightgray;
}

.headerFoto {
display:block;
width: 100%;
margin: auto;
height: auto;
}

.mainFoto {
width: 100%;
margin: auto;
height: auto;
}

.homePage {
width:inherit;
margin:auto;
border:solid;
border-color:red;
}

<!DOCTYPE html>

<html>
<head>
<title>Atlas</title>
<link rel="stylesheet" type="text/css" href="siteStyle.css">
<link rel="shortcut icon" type="image/png" href="Logo.png" />
</head>
<body>
<div class="website" style="background-color:black">
<div class="menu">
<ul>
<li class="active"><a href="index.html">Home</a></li>
<li><a href="bio.html">Biografie</a></li>
<li><a href="projecten.html">Projecten</a></li>
<li><a href="galerij.html">Galerij</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</div>
<h2 style="color:white">Klik op de opties hier beneden</h2>
<div class="homePage">
<img src="grootLogo.png" class="mainFoto">
<h4 style="position: absolute; top:50%;">Biografie</h4>
<h5 style="position: absolute; top:50%;">Projecten</h5>
<h4 style="position: absolute; top:75%;">Galerij</h4>
<h5 style="position: absolute; top:75%;">Contact</h5>
<br />
</div>
</div>
<footer></footer>
</body>
</html>




Answer

Short answer

Set position: relative to homePage.

Less short answer

From MDN:

The absolutely positioned element is positioned relative to nearest positioned ancestor (non-static). If a positioned ancestor doesn't exist, the initial container is used.

In your case, since all elements are position: static (this is the default behavior), this means your 4 absolute elements are relative to the <html> tag (e.g. your initial container).
Setting position: relative (or any value not static) on their parent fixes their positioning by making any top|right|bottom|left relative to it.