Reputation: 3924
I want to make a console overlaid on a background image but cannot get positioning to work. There is a container div with two overlapping child divs, one for a semi-transparent shade and one for text.
What is the proper CSS to fit the children inside the container's content box?
EDIT: the desired result is for the yellow word "content" to be placed inside the content area highlighted in blue in the image, and for the content area to be resized within the browser window with 50px margins (150px on top). If you run the code, you won't see a blue box - that desired content location is illustrated by hovering over the content box (labeled 384 x 384) in Chrome's debugging window.
The desired result is:
Fiddle at: https://jsfiddle.net/dave2/381rnsy5/
body {
background-color: gray;
}
#container {
position: absolute;
padding: 50px;
padding-top: 150px;
height: 100%;
width: 100%;
box-sizing: border-box;
}
#bg {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-color: red;
opacity: 0.25;
border: 1px solid blue;
box-sizing: border-box;
}
#text {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
padding: 20px;
overflow-y: hidden;
color: greenyellow;
border: 1px dashed;
box-sizing: border-box;
}
<div id="container">
<div id="bg">
background
</div>
<div id="text">
content
</div>
</div>
Upvotes: 0
Views: 1461
Reputation: 881
Is this what you're going for?
https://jsfiddle.net/qjLbdtmv/2/
HTML
<!DOCTYPE html>
<html lang="en">
<body>
<div id="container">
<div id="bg">
background
</div>
<div id="text">
content
</div>
</div>
</body>
</html>
CSS
body {
background-color: gray;
}
#container {
width: 100%;
height: 100vh;
position: relative;
box-sizing: border-box;
}
#bg,
#text {
position: absolute;
top: 150px;
right: 50px;
bottom: 50px;
left: 50px;
}
#bg {
background-color: red;
opacity: 0.25;
border: 1px solid blue;
box-sizing: border-box;
}
#text {
padding: 20px;
overflow-y: hidden;
color: greenyellow;
border: 1px dashed;
box-sizing: border-box;
}
The child divs can only be positioned based on their parent, but you need a position
set on the parent for that to happen.
Upvotes: 1