I have 3 div's like on this image:
div1 has fixed width but variable height, so what I would like is that if div1 height is bigger that the div2 height, the div3 stays under div2 and on the right of the div1, like here:
Any idea on how to do this? For the moment, I have created a container:
<div class="colcontainer">
<div class="col-left">
{% block news %}{% endblock %}
</div>
<div id="main_content">
<div class="col-right">
{% block rightcol %}{% endblock %}
</div>
<div id="content">
<div class="contentwrap">
<div class="itemwrap">
{% block content %}{% endblock %}
</div>
</div>
</div>
<div class="content-bottom"><div class="content-bottom-left"></div></div>
</div>
</div>
My CSS is like this:
.col-left {
float:left;
padding:0;
margin:0;
width: 300px;
min-height:198px;
}
.col-right {
text-align:left;
padding:0;
margin:0 0 0 200px;
}
#content {
background: none repeat scroll 0 0 #FFFFFF;
float: left;
margin-top: 10px;
padding: 10px;
width: 980px;
}
Here is the JSFiddle demo
If you've faced the situation when you need to wrap words in a <div>, you can use the white-space property with the "pre-wrap" value to preserve whitespace by the browser and wrap the text when necessary and on line breaks. Also, you'll need the word-wrap property.
HTML <pre> tag defines preformatted text. It is used to display code blocks since it preserves spaces and line breaks. If the line is large, then the <pre> tag won't wrap it by default. To wrap it, we need to use CSS.
Atlast, I got it :) Just wrap all those three elements in a parent element as shown below.
HTML
<div class="main">
<div class="div1"></div> <!-- Change height to see result -->
<div class="div2"></div>
<div class="div3"></div>
</div>
CSS
.main{width:200px;height:200px;border:1px solid black;overflow:hidden;}
.div1{width:100px;min-height:100px;float:left;background-color:green;}
.div2{width:100px;display:inline-block;height:100px;background-color:blue;}
.div3{width:100px;display:inline-block;height:100px;background-color:red;margin-top:-4px;}
Working fiddle
If you want to have the width of the third div to be wide from before itself then I highly recommend you to go with jQuery.
.div3{width:200px;} /* Keeping width wide enough with the container */
jQuery
$(function(){
if($('.div1').height() > 100)
$('.div3').width(100)
});
Working Fiddle
As CSS doesn't provide anything for that job yet, you will have to use Javascript.
I would firstly create a page like on your first picture. Then I would use
$("#div").height()
, which returns the height of your div and compare this to your second div's height:
if($("#div1").height() > $("div2").height())
//change website...
In the if body put the required css changes... That should do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With