CSS position a div based on dynamic height of another div - html

In my site layout I'm using a negative margin to move my left column up next to my banner so it overlaps. The problem is I don't know what the banner's height will be in the final version. At first I used position:absolute on the left column, but that won't work because it needs to be part of the layout and push down the footer if necessary. I'd like to know how to position the left column to the top of the page, because then I could set a top margin the same height as the header since that won't change height. I could figure this out with javascript but I'd like to avoid that and use pure css.
https://jsfiddle.net/z77fwaj7/1/
#Header
{
background-color: gray;
height: 50px;
}
#Banner
{
background-color: orange;
height: 50px;
}
#Content
{
background-color:white;
border:1px solid red;
max-width:500px;
margin:0px auto;
}
#LeftColumn
{
float:left;
height:200px;
width:25%;
background-color: blue;
margin-top:-51px;/*this needs to be dynamic*/
}
#MiddleColumn
{
float:left;
height:200px;
width:45%;
background-color: yellow;
}
#RightColumn
{
float:left;
height:250px;
width:30%;
background-color: green;
}
#Footer
{
background-color: gray;
height: 50px;
}
<div id="Header">header</div>
<div id="Banner">banner</div>
<div id="Content">
<div id="LeftColumn">left</div>
<div id="MiddleColumn">middle</div>
<div id="RightColumn">right</div>
<div style="clear:both;"></div>
</div>
<div id="Footer">footer</div>

Is this Ok.
<style type="text/css">
#Header
{
background-color: gray;
height: 50px;
}
#Banner
{
background-color: orange;
height: 50px;
}
#Content
{
background-color:white;
border:1px solid red;
max-width:500px;
margin:0px auto;
}
#LeftColumn
{
float:left;
height:200px;
width:25%;
background-color: blue;
margin-top:0px;
}
#MiddleColumn
{
float:left;
height:200px;
width:45%;
background-color: yellow;
}
#RightColumn
{
float:left;
height:250px;
width:30%;
background-color: green;
}
#Footer
{
background-color: gray;
height: 50px;
}
</style>
<div>
<div id="Header">header</div>
<div id="Banner">banner</div>
<div id="Content">
<div id="LeftColumn">left</div>
<div id="MiddleColumn">middle</div>
<div id="RightColumn">right</div>
<div ></div>
</div>
<div id="Footer" style="clear:both;">footer</div>
</div>

If anyone is curious I had to change my layout in order to get it working without javascript. BackgroundBanner won't change height when Banner shrinks, but in my case that doesn't matter since it will be out of view anyway.
https://jsfiddle.net/z77fwaj7/4/
css:
#Header
{
background-color: gray;
height: 50px;
}
#Background
{
position:absolute;
width:100%;
z-index:-1;
}
#BackgroundBanner
{
height: 50px;
background-color:orange;
}
#Banner
{
float:left;
background-color: orange;
height: 50px;
width:75%;
}
#Content
{
background-color:white;
border:1px solid red;
max-width:500px;
margin:0px auto;
}
#LeftColumn
{
float:left;
height:200px;
width:25%;
background-color: blue;
}
#MiddleColumn
{
float:left;
height:200px;
width:45%;
background-color: yellow;
}
#RightColumn
{
float:left;
height:250px;
width:30%;
background-color: green;
}
#Footer
{
background-color: gray;
height: 50px;
}
html:
<div id="Header">header</div>
<div id="Background">
<div id="BackgroundBanner"></div>
</div>
<div id="Content">
<div id="LeftColumn">left</div>
<div id="Banner">banner</div>
<div id="MiddleColumn">middle</div>
<div id="RightColumn">right</div>
<div style="clear:both;"></div>
</div>
<div id="Footer">footer</div>

As a conclusion:
Using a value of an element on another element in css is not possible. (as far as i know)
So there are two solutions:
Change the layout.
Use javascript.
I would prefer the second. Don't know why it's such a shame to do so.
A short simple javascript is better then mess up the layout. (In my opinion)

Related

What is the little white gap for my CSS?

There is a little white gap on the downside of mainBox, what is it and which cause it?
body {
margin: 0px;
padding: 0px;
}
div {
border: 1px solid black;
box-sizing: border-box;
font-size: 30px;
}
#wrapper {
width: 900px;
margin: 0px auto;
}
#header {
width: 100%;
height: 100px;
background: red;
}
#container {
width: 100%;
height: 100px;
background: black;
}
#mainBox {
width: 75%;
height: 150px;
background: blue;
float: left;
}
#sideBox {
width: 25%;
height: 100px;
background: yellow;
float: left;
}
#footer {
clear: both;
width: 100%;
height: 50px;
background: white;
}
<div id="wrapper">
<div id="header">this is the header
</div>
<div id="container">
<div id="mainBox">main box
</div>
<div id="sideBox">side box
</div>
</div>
<div id="footer">this is the footer
</div>
</div>
Add border: none to #container
body{
margin:0px;
padding:0px;}
div{
border:1px solid black;
box-sizing:border-box;
font-size:30px;}
#wrapper{
width:900px;
margin:0px auto;}
#header{
width:100%;
height:100px;
background:red;}
#container{
border: none; /**** Add this extra line ****/
width:100%;
height:100px;
background:black;}
#mainBox{
width:75%;
height:150px;
background:blue;
float:left;
}
#sideBox{
width:25%;
height:100px;
background:yellow;
float:left;
}
#footer{
clear:both;
width:100%;
height:50px;
background:white;
}
<body>
<div id="wrapper">
<div id="header">this is the header
</div>
<div id="container">
<div id="mainBox">main box
</div>
<div id="sideBox">side box
</div>
</div>
<div id="footer">this is the footer
</div>
</div>
You have a white gap here because your blue div (#mainBox) is 150px, which is taller than its container div (#container), set at 100px. This has caused the #mainBox div to extend outside the container with a slight offset at the leftmost side due to the black border applied to the div.
There are a number of ways to correct this gap, such as removing the border or the hardcoded height values.
It is because #sideBox have 100px height and #mainBox have 150px height. Make both same solve your issue.
Here i set #sideBox to height:150px;
And you have given parent container #container to height:100px and child to height:150px.
Edit:
I think i misunderstood your issue before. But as i say above. Your parent height is small then your child. Make it proper solve your issue.
body{
margin:0px;
padding:0px;}
div{
border:1px solid black;
box-sizing:border-box;
font-size:30px;}
#wrapper{
width:900px;
margin:0px auto;}
#header{
width:100%;
height:100px;
background:red;}
#container{
width:100%;
height:150px;
background:black;}
#mainBox{
width:75%;
height:150px;
background:blue;
float:left;
}
#sideBox{
width:25%;
height:100px;
background:yellow;
float:left;
}
#footer{
clear:both;
width:100%;
height:50px;
background:white;
}
<body>
<div id="wrapper">
<div id="header">this is the header
</div>
<div id="container">
<div id="mainBox">main box
</div>
<div id="sideBox">side box
</div>
</div>
<div id="footer">this is the footer
</div>
</div>

How to format a Header, Footer, and 2 Body Elements properly?

<style type="text/css">
.container{
position:center;
margin-left:500px;
margin-right:500px;
}
header {
height:200px;
background-color:green;
}
footer {
height:100px;
background-color:red;
}
.body {
display:flex;
flex-direction:row;
height:650px;
}
.side {
background-color:yellow;
flex:0.5;
}
.middle {
background-color:teal;
flex:2;
}
</style>
</head>
<body>
<div class="container">
<header> I'm a header idiot</header>
<div class="body">
<div class="side"> Stuff</div>
<div class="middle">More Stuff</div>
</div>
<footer>SHEEEEEEEEEET</footer>
</div>
</body>
I'm trying to make a webpage that is formatted similarly to this:
The code I have currently is a very botchy, way of doing it but doesn't scale with window size and only looks good full screen relative to my screen size. What am I missing? It is supposed to be centered on the page and not have its width spread across the whole screen.
You have to remove just:
.container {
margin-left: 500px;
margin-right: 500px;
}
from your code will solved your issue.
Check Fiddle
And for getting output same as image(like center div). Give parent text-align:center;. Here body is parent so.
body{
text-align:center;
}
Check Updated Fiddle
<!DOCTYPE html>
<style>
#main{
height:500px;
width:100%;
}
#div1{
width:99%;
height:25%;
border: 2px solid;
margin-bottom:2%;
top: 30px;
}
#div2{
width:20%;
height:60%;
border: 2px solid;
margin-bottom:2%;
float:left;
}
#div3{
width:78%;
height:60%;
border: 2px solid;
margin-bottom:2%;
float:right;
}
#div4{
width:100%;
height:10%;
border: 2px solid;
float:left;
}
</style>
<body>
<div id="main">
<div id="div1">11
</div>
<div id="div2">33
</div>
<div id="div3">222
</div>
<div id="div4">
test
</div>
</div>
</body>
</html>
Description:- You jsut nedd to give margin for adjusting all layouts and need to remove (.container) Class .Below is code you can check that.
Code:-
<style type="text/css">
.body {
display:flex;
flex-direction:row;
height:650px;
}
header {
height:200px;
background-color:green;
margin:20px 20px 0px 20px;
}
footer {
height:100px;
background-color:red;
margin:15px 20px 0px 20px;
}
.side {
background-color:yellow;
flex:0.5;
margin:15px 10px 0px 20px;
}
.middle {
background-color:teal;
margin:15px 20px 0px 10px;
flex:2;
}
</style>
</head>
<body>
<div >
<header> I'm a header bitch</header>
<div class="body">
<div class="side"> Stuff</div>
<div class="middle">More Stuff</div>
</div>
<footer>SHEEEEEEEEEET</footer>
</div>
</body>
Change your .container to this. You can use any width you like.
.container {
/* position:center; */
margin-left:auto;
margin-right:auto;
width:100%
}
.container {
/* position:center; */
margin-left: auto;
margin-right: auto;
width: 100%
}
header {
height: 200px;
background-color: green;
}
footer {
height: 100px;
background-color: red;
}
.body {
display: flex;
flex-direction: row;
height: 650px;
}
.side {
background-color: yellow;
flex: 0.5;
}
.middle {
background-color: teal;
flex: 2;
}
<div class="container">
<header>I'm a header</header>
<div class="body">
<div class="side">Stuff</div>
<div class="middle">More Stuff</div>
</div>
<footer>footer</footer>
</div>

how align div one beside other, and on other div

i tried some codes but, no works anything.
would like make this with css, thanks =)
this code i tried, but doesn't work.
#left{
float:left;
width:65%;
overflow:hidden;
}
#right{
overflow:hidden;
}
<div id="wrapper">
<div id="left">Left side div</div>
<div id="right">Right side div</div>
</div>
i don{t know why this doesnt work.
A simple solution with no floats:
#main {
width: 200px; /* adjust as needed */
font-size: 0;
}
div div {
display: inline-block;
height: 60px; /* adjust as needed */
width: 100%;
box-shadow: inset 0 0 4px #000; /* cosmetics only */
background: #eee; /* cosmetics only */
}
div.h {
width: 50%;
}
<div id="main">
<div class="h"></div>
<div class="h"></div>
<div></div>
</div>
Note: using font-size: 0 for the container div to avoid the actual whitespace in the markup - can be avoided by removing spaces between elements, of course: <div>content here...</div><div>other one...</div>
Add float:left; to #right, then it should work. Note that you could also use float:right; to #right, then #right would be on the right side. Using float: left; for both displays both divs next to each other without any gap.
For reference: https://developer.mozilla.org/en-US/docs/Web/CSS/float
Try this script, I wrote it on JSfiddle:
http://jsfiddle.net/xb5vvpzn/1/
HTML
<div class="main">
<div class="top"> </div>
<div class="bottom1"> </div>
<div class="bottom2"> </div>
</div>
CSS
html, body {
padding:0;
margin:0;
}
.main {
width:400px;
border:1px solid #000;
height:400px;
padding:10px;
}
.main div {
display:inline-block;
}
.top {
width:396px;
border: 1px solid #cc0000;
height:100px;
}
.bottom1, .bottom2 {
margin-top:10px;
border: 1px solid #cc0000;
width:195px;
height:100px;
}
Here's a jsFiddle that I've quickly created for you. The layout is same as what you had requested and it's responsive as well.
HTML:
<div id="container">
<div id="onetwo">
<div id="one"></div>
<div id="two"></div>
</div>
<div id="three"></div>
</div>
CSS:
#container {
width: 100%;
border: 3px solid blue;
padding: 1% 1%;
text-align: center;
}
#onetwo {
display: block;
width: 100%;
}
#one, #two {
width: 49%;
border: 3px solid red;
height: 50px;
display: inline-block;
}
#three {
width: 100%;
border: 3px solid red;
height: 50px;
}
#media (max-width: 820px) {
#one, #two {
width: 46%;
}
}
#media (max-width: 240px) {
#one, #two {
width: 40%;
}
}

child not stretching to its parent

I am not sure why contactBox is overlapping the mainInfo box.The contact box is also not stretching to its parent container which is 960px.
CSS
.mainInfo {
position:relative;
height:500px;
background-color: pink;
padding:30px 0 0 30px;
}
.col-6 .imagePlaceholder {
width:300px;
height:420px;
background-color: red;
}
.col-6 .about {
position: absolute;
top:30px;
left:414px;
padding:1em;
}
.contactBox {
height:450px;
background-color:green;
}
Here is a JS fiddle:
http://jsfiddle.net/2zm47/
I would probably code this css differently, but checkout the
.mainInfo {
position:relative;
height:500px;
background-color: pink;
padding:30px 0 0 30px;
}
.col-6 .imagePlaceholder {
width:300px;
height:420px;
background-color: red;
}
.col-6 .about {
position: absolute;
top:30px;
left:414px;
padding:1em;
}
.contactBox {
height:450px;
background-color:green;
}
I hope this helps. I put a note in the JS section of the fiddle.
http://jsfiddle.net/emersive/84WeT/1/
Your HTML is messed up. Should be like:
<div class="container">
<div class="mainInfo">
<div class="col-6">
<div class="imagePlaceholder"></div>
</div>
<div class="col-6">
<div class="about">
<p>...</p>
<p>...</p>
</div>
</div>
</div>
<div class="contactBox"></div>
</div>

chrome: "ghost" width of floated elements inside inline block

test case http://codepen.io/anon/pen/hFumw.
Works fine in all browsers except chrome. In chrome width of .container is calculated like .child.one elements are not floated. Is there any way to fix this behaviour?
HTML:
<div class="container">
<div class="header">
header
</div>
<div class="child one">
</div>
<div class="child one">
</div>
<div class="child one">
</div>
</div>
CSS:
.container {
background:red;
padding:10px;
display: inline-block;
overflow:hidden;
.child {
width:100px;
height: 100px;
background: green;
float:left;
clear:left;
border: 1px solid blue;
}
.one {
float: left;
clear:left;
background:yellow;
}
.header {
background:blue;
}
}
UPD:
.header {
float: left;
width: 100%;
}
is not acceptable in my particular case.
Following CSS seems to work fine in Chrome and FF. See header declaration.
body {
text-align: center;
}
.container {
margin-left:auto;
margin-right:auto;
background:red;
padding:10px;
display: inline-block;
overflow:hidden;
.child {
width:100px;
height: 100px;
background: green;
float:left;
clear:left;
border: 1px solid blue;
}
.one {
float: left;
clear:left;
background:yellow;
}
.two {
float: right;
margin-left:0px;
clear: right;
}
.header {
background:blue;
display:block;
float:left;
width:100%;
}
}
Try adding this css to .header
clear:both;
float:left;
width:100%