I would like to have "Hello" and the black box on the right centered exactly on the image. I dont want to use position: absolute;
and top: %;
. Please help me 🙁
How it looks now: https://imgur.com/a/vY4Qcpm
How I want it to look: https://imgur.com/a/ywkqrO2
HTML
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/style.css">
</head>
<body style="margin: 0">
<img id="background" src="img/background.jpeg">
<div class="middle-part">
<p id="text">Hello</span></p>
<div id="field">
<div id="field-content"></div>
</div>
</div>
</body>
</html>
CSS
html {
scroll-behavior: smooth;
}
#background {
width: 100%;
height: 80vh;
object-fit: cover;
pointer-events: none;
}
.middle-part {
margin: 0 auto;
max-width: 1300px;
}
#text {
position: absolute;
margin: 0;
margin-left: 6px;
font-size: 50px;
}
#field {
display: flex;
justify-content: flex-end;
}
#field-content {
position: absolute;
margin-right: 10px;
width: 380px;
height: 275px;
background-color: black;
}
With position: absolute;
and top: %;
it seems to work but I am searching for a better solution.
I also tried to put the img
into the middle-part
below while using justify-content: center;
but it won’t work either.
2
Answers
To place another element above another, you basically would want to use
position
having the parent element have a value ofrelative
and the childabsolute
as so:Find your solution in this codepen and adjust it to suit your needs
Add a
<section>
element with a height of80vh
, use flexbox to position the contents, and reference the image in CSS as thebackground-image
.