Floating Inner Div to affect height of parent - css

I've created a JSFiddle to describe what I mean:
http://jsfiddle.net/3yGLT/
I want my .top section to be affected by the height of the .floated-div, as you can see. At present, my .floated-div content drops over the .bottom section, which is not what I want. The height of the .floated-div needs to dictate the height of the .top section, effectively pushing .bottom down to make room for it.
I thought Clear divs were the solution I wanted, but it's not giving me the behaviour I'm after. I think this would only apply if the main content of .top was in a similar div to floated-div and not embedded in this way.
I can add things like clears, but I can't adjust the structure of this code as it's something that's generated through the CMS.
HTML
<section class="top">
<h1>test</h1>
<p>some content</p>
<div class="floated-div">
<h2>aside content</h2>
<p>some aside content</p>
<p>some aside content</p>
<div class="clear"></div>
</div>
<div class="clear"></div>
</section>
<section class="bottom">
</section>
CSS
.top{
width: 60%;
height:auto;
background:#f1f1f1;
}
.floated-div{
width:40%;
position:absolute;
right:0;
top:0;
}
.clear{
clear:both;
}
.bottom{
width: 100%;
height:100px;
background:#d1d1d1;
}

The problem is that your .float-div doesn't actually float. Because of the "position: absolute" rule it would never affect the height of the parent container (that's the meaning of absolute positioning). To make it float you need to remove this rule and add "float:right" to the div. In this case clearance will do its work.
floated-div {
float: right;
width: 40%;
}
Here is working example: http://jsfiddle.net/qvgz4/

i added this to your css and it worked :
.floated-div > p{margin:0;} /**added**/
.floated-div > h2{margin-bottom:0;}/**added**/
.top{
width: 60%;
height:auto;
background:#f1f1f1;
margin-bottom:5%; /*** added to avoid div overlap **/
}
basically <p> is taking extra area in floated-div1, cleared them through margin!!

If I understand the question correctly, I think the best option is to go with a display:table, like this CSS:
.top{ display:table; width:100%;}
.top .side { display:table-cell; padding:.5em;}
.top .left { width:60%; background:#f1f1f1;}
.top .right { width:40%;}
http://jsfiddle.net/3yGLT/8/

If you want the height of the top block to adapt to the height of the floated div, then you cannot absolutely position the floated div. Your only option then is to float it to the right.
But that will place it below the H1 and P that come before it. The only way to avoid that is to take the H1 and P out of the flow of the document. We do that with absolute positioning.
This solution works only if the height of the floated div is always going to greater than the H1 and P. You will also need to fiddle with the left and top positions of the H1 and P to get it just right.
http://jsfiddle.net/3yGLT/15/
.cf:before,
.cf:after {
content: " ";
display: table;
}
.cf:after {
clear: both;
}
.top {
height:auto;
background:#f1f1f1;
}
.top > h1,
.top > p {
position: absolute;
width: 60%;
left: 10;
}
.top > p {
top: 40px;
}
.floated-div {
width:30%;
float: right;
}
.bottom{
width: 100%;
height:100px;
background:#d1d1d1;
}

Without touching html , you can do as below
.top{
width:100%;
background:#f1f1f1;
float:left;
}
.top > h1{
width:60%;
float:left;
position:relative;
}
.top > p{
width:60%;
float:left;
position:absolute;
margin-top:50px; // margin-top depend on your h1 height
}
.top .floated-div{
width:40%;
float:right;
}
.clear{
clear:both;
}
.bottom{
width: 100%;
height:100px;
background:#d1d1d1;
float:left;
}

Related

css: matching the width of elements while expanding parent

I have a div of set width (as a percentage of its parent) which contains a number of child rows. I want the width of these rows to always match each other- so the row with the widest content would determine the width of the others. However, this width may also be larger than the containing div, in which case that div would need to expand to accommodate the child rows.
Here is my HTML:
<div class="outer">
<div class="inner">
<div class="block-wrapper">
<h2>First heading</h2>
<h2>Another heading</h2>
<h2>The third heading which could be quite long</h2>
</div>
</div>
</div>
And current CSS:
.outer {
width:1000px;
background-color:#000;
height:1000px;
}
.inner {
width: 80%;
background-color:#FFF000;
height:50%;
}
.block-wrapper {
width: 40%;
background-color:#fff000;
height:100%;
}
h2 {
font-size:18px;
width: 100%;
float: left;
clear: left;
line-height: 40px;
margin-top: 10px;
padding:1%;
background-color:rgba(0,0,0,0.75);
}
Here is a JS Fiddle.
So my problem is the third row there, "The third heading..." Should be all on one line, with the other h2 rows expanding to the same width. The minimum width for the h2 rows should be the width of the parent div.
I hope this makes sense... any ideas?
Update your .block-wrapper with width:auto and display:inline-block.
.block-wrapper {
width: auto;
min-width:40%;
background-color:#fff000;
height:100%;
display:inline-block;
}
DEMO
inline-block and width:auto on your .block-wrapper should help. Plus you'll need to lose that padding 1% on the h2 to avoid confusing it. There are other ways you can add that in.
http://jsfiddle.net/guxkcqyz/
.outer {
min-width:1000px;
background-color:#000;
height:1000px;
}
.inner {
width : 80%;
background-color:#FFF000;
height:50%;
}
.block-wrapper {
background-color: red;
width : auto;
display : inline-block;
height:100%;
}
h2 {
font-size:18px;
line-height: 40px;
margin-top: 10px;
background-color:rgba(0,0,0,0.75);
}

Floated div not expanding to 100%

I know that this is not an uncommon problem, as a bit of Googling threw up quite a few pages with similar problems to my own. But try as I might I can't fix it so here goes:
I am currently building the website to my rugby team. It has a two column layout, with a main area and a sidebar. The relevant HTML is roughly
<div id="wrapper">
<div id="maincolumn"></div>
<div id="sidebar"></div>
<div class='clear'></div>
</div>
From some of the websites, I have gleaned that I need to set body and html to 100% and all the containers, so I have:
html, body, #wrapper, #innerwrapper, #sidebar { height: 100%; min-height: 100%;
#wrapper { max-width:900px; margin:0 auto; width:90%; }
#sidebar { float: right; width: 35%; padding:2%; background-color:#f7f7f7; }
#maincolumn { width:56%; float:left; padding-right:5%; }
.clear { clear:both; }
The problem I am having, is that when #maincolumn has a lot of content, the sidebar does not expand all the way down to the bottom of the page which is the behaviour I would like. I made some progress by setting all the containers to 100% and then adding the clear element, but that still only expands it a short way.
Instead of floating, you can use CSS tables:
#wrapper {
display: table;
}
#sidebar, #maincolumn {
display: table-cell;
}
Demo
Since you want both columns to have the same height regardless of the amount of content within them, first you have to understand that setting height:100% sets the height in relation to the width of the parent div(or containing block).
So if that's the case, here's what you can do:
#wrapper{
height:900px;
}
#sidebar{
height:100%;
}
#maincolumn{
height:100%;
}
DEMO
HTML :
<div id="wrapper">
<div id="maincolumn">vgnhngbbv hbcv nfvfbngbc</div>
<div id="sidebar">dfrtnjnbc ghm gbgfnbvfnythgfbbfg</div>
<div class='clear'></div>
</div>
CSS :
html, body, #wrapper, #maincolumn, #sidebar { height: 100%; min-height: 100%;}
#wrapper { max-width:900px; margin:0 auto; width:90%; }
#sidebar { float: right; width: 35%; padding:2%; background-color:#f7f7f7; }
#maincolumn { width:56%; float:left; padding:2%; background-color:#ff0000; }
.clear { clear:both; }
DEMO

How to auto expand absolute positioned DIVs

I have layout comprising of a 100% width header, 2 column content divs (30-70% width) and a 70% width footer (visible only in the bottom of right div).
My HTML mark up is like:
<section id="mySection" >
<header id="headerTop">
</header>
<div id="wrapperLeft">
</div>
<div id="wrapperRight">
</div>
<footer id="footerRight">
</footer>
</section>
My CSS is
#mySection
{
margin:0 auto;
padding:0;
text-align:center;
vertical-align:middle;
overflow:hidden;
}
#headerTop
{
position:absolute;
top:0;
left:0;
height:40px;
width:100%;
overflow:hidden;
}
#wrapperLeft
{
position:absolute;
top:40px;
left:0;
width:30%;
bottom:0;
overflow:auto;
}
#wrapperRight
{
position:absolute;
top:40px;
left:30%;
width:70%
bottom:30px;
overflow:auto;
}
#footerRight
{
position:absolute;
left:30%;
bottom:0;
width:70%;
overflow:hidden;
}
I would like to know if I can design this better such that if i hide the left or right div, the other div is displayed at 100%. I think i can change the CSS dynamically via javascript and adjust the left and width values for the other div, but it is getting messy and would like to avoid it if possible.
Ideally would love to call show or hide on the div and the other div automatically adjusts itself to 100% width.
I have no control over the height of the content in either div and would want the browser to display scrollbar when the content height exceeds the window.
Thanks in advance for your help.
I would add a wrapper to the divs so you can float then instead of positioning then absolutely. This way you can make at least one div 100% wide. For instance the right div. If you want both divs to be dynamic in size you will have to use jquery. For instance adding classes if you want to keep the jquery to a minimal.
example HTML:
<div id="header"></div>
<div id="main">
<div id="left"></div>
<div id="right"></div>
</div>
<div id="footer"></div>
example CSS :
#main{
position:relative;
overflo:hidden // This will make the container grow with the children
width:960px;
}
#left{
width:200px;
float:left;
height:100%;
}
#right{
float:left;
width:100%;
height:100%;
}
Example of CSS with additional classto toggle divs
#main.only-left #left{
width:100%;
}
#main.only-left #right{display:none;}
I think I know what you're talking about. I've created a little example here. Basically set 30% on the sidecolumn, and display: block; on the main column. Click on the body anywhere to toggle the side column to show how the main column adapts... is this going in the right direction?
Codepen sketch
HTML
<div class='wrapper'>
<header>Header</header>
<section>
<aside>Sidebar</aside>
<article>Main article</article>
</section>
<footer>Footer</footer>
</div>
CSS
body {
margin: 0;
padding: 0;
}
section {
overflow: hidden;
position: relative;
}
header {
background: crimson;
height: 100px;
width: 100%;
}
aside {
background: #efefef;
float: left;
height: 300px;
width: 30%;
}
aside.hide { display: none; } /** For demo purposes **/
article {
background: #ccc;
display: block;
height: 300px;
}
footer {
background: crimson;
float: right;
height: 100px;
width: 70%;
}
jQuery (just for hideToggle example)
$('html').on('click', function(){
$('aside').toggleClass('hide');
});
UPDATE: Here's an example with a little assitance from jQuery for class toggling. Could probably be generalized more... http://codepen.io/kunalbhat/pen/kuAcg

How to set div width to 100%

I'm trying to design a 2 column layout using divs. My left column size is fixed to 150 px. But right column is not fixed size, so I want it to extend to right boundary of the browser. I used width:auto and width:100% values in right column but they didn't work.
CSS :
html {
height:100%; width:100%
}
body {
color: #000040;
text-align: left;
padding:0;
margin:0;
height:100%;
width:100%
}
#header {
position:relative;
float:left;
background-color: #000053;
width: 100%;
height: 76px
}
#wrapper {
position:relative;
overflow:hidden;
width:100%;
height: 100%;
margin:0px auto;
padding:0;
background-color:Aqua
}
#container {
clear:left;
float:left;
overflow:hidden;
width:100%;
height:100%
}
#left_column {
position: relative;
float: left;
background-color:Fuchsia;
width: 150px;
overflow:hidden;
height:100%
}
#right_column {
position: relative;
float:left;
overflow:hidden;
background-color:Blue;
height: 100%;
width:auto }
HTML
<html>
<body>
<div id="wrapper">
<div id="header">
HEADER
</div>
<div id="container">
<div id="left_column">
LEFT COLUMN
</div>
<div id="right_column">
RIGHT COLUMN
</div>
</div>
</div>
</body>
</html>
I would remove all position statements and only put a float:left on the left column, not the right nor the container. Give the right column a margin-left:150px and it should work fine.
Except for the left-floated column, you can also remove the width:100% statements from the rest; when they're not floated, they'll be 100% wide automatically.
The overflow:hidden is only needed on the wrapper; at least if you are using it to have the div grow in height to accommodate the floats inside it.
change for the div right_column the position from relative to fixed, and width from auto to 100%. Also add left:150px;
With these changes you css for right_column will look like the following:
#right_column {
position: fixed;
left:150px;
float:left;
overflow:hidden;
background-color:Blue;
height: 100%;
width:100%; }
you can check it here http://jsbin.com/ejetu3

CSS positioning inside div

I am using a div with 2 elements inside and I want to position my 1st element to be vertically aligned top and 2nd element to the bottom of the div. The div is the right portion of my page and equal to the height of my main content.
#right {
float:right;
width: 19%;
background:#FF3300;
margin-left:2px;
padding-bottom: 100%;
margin-bottom: -100%;
}
#right .top {
display:block;
background-color:#CCCCCC;
}
#right .bottom {
bottom:0px;
display:block;
background-color:#FFCCFF;
height:60px;
}
HTML:
<div id="right">
<span class="top">Top element</span>
<span class="bottom"><img src="images/logo_footer1.gif" width="57" height="57" align="left" class="img"> <img src="images/logo_footer2.gif" width="57" height="57" align="right" class="img"></span>
</div>
I want the right div to be like this:
alt text http://christianruado.comuf.com/element.png
If you specify position: relative for #right, and then position: absolute for the two internal elements, you should be able to use top/left/bottom/right attributes to achieve the effect you want.
Try this.
#right {
position:relative; <-- add this
float:right;
width: 19%;
background:#FF3300;
margin-left:2px;
padding-bottom: 100%;
margin-bottom: -100%;
}
}
#right .top {
position:absolute; <-- add this
top: 0px; <-- and this
display:block;
background-color:#CCCCCC;
}
#right .bottom {
position:absolute: <-- add this.
bottom:0px;
display:block;
background-color:#FFCCFF;
height:60px;
}
Adding position:relative; to the parent and position:absolute; with top and bottom will tell your spans that they're meant to be positioned absolutely within your parent and force them to stick to the top and bottom of your div.
Make #right {position:relative}
Make #right .top {position:absolute, top:0}
Make #right .bottom {position:absolute, bottom:0}

Resources