I have the following DIVs structure:
<div id="d1">
<div id="d2">
</div>
<div id="d3">
<div id="d4"></div>
<div id="d5"></div>
</div>
</div>
DIV d2 is the left column (float: left), while d3 is on the right (float: right). d4 and d5 all go into right column d3, one below the other. The widths are all fixed.
DIVs d4 has fixed height of 300px.
The content of div d2 has dynamic height but it is at least 300px high.
My goal is to make DIV d5's height to fill the remaining space in the right column (d3) so that the total height of this column is equal to the dynamic height of the left column (d2).
I would like a pure CSS solution, no JS.
This is the CSS I am using:
#d1 {
width: 990px;
border: 1px solid black;
background-color: pink;
}
#d2 {
float: left;
width: 300px;
background-color: yellow;
}
#d3 {
float: right;
width: 690px;
}
#d4 {
background-color: blue;
width: 690px;
height: 300px;
}
#d5 {
background-color: brown;
width: 690px;
}
You can use display: tale; and table-cell; with a heightof 100% on d5.
#d1 {
width: 990px;
border: 1px solid black;
background-color: pink;
padding: 3px;
display: table;
height: 10000px;
}
#d2 {
display: table-cell;
width: 300px;
background-color: yellow;
}
#d3 {
display: table-cell;
width: 690px;
}
#d4 {
background-color: blue;
width: 690px;
height: 300px;
}
#d5 {
background-color: brown;
width: 690px;
height: 100%;
}
http://jsfiddle.net/nnjse/1/
Add to main block #d1 border-right 690px and using negative margin -690px put right column #d3 over it. Border will look like right column's background. If #d2 gets higher #d1's border will make illusion that right column fills whole parent height. The same principle with negative margins but in vertical direction can be used for #d4 and #d5.
#d1{
width:300px;
margin:0 auto;
border-right:690px solid #a52a2a;
background:#ffff00;
}
#d2{
width:300px;
float:left;
}
#d3{
width:690px;
float:right;
margin-right:-690px;
border-top:300px solid #EEE;
background:#a52a2a;
}
#d4{
height:300px;
margin-top:-300px;
background:#0000ff;
}
#d5{
height:100%;
}
Related
Is there a possibility for the div (#contentwrapper) to take all the remaining width while floating side by side for the next example:
#maincontainer {
width:1000px;
height: 100%;
display:inline-block;
}
#leftcolumn {
float:left;
width: 100px;
height:20px;
background: blue;
}
#contentwrapper {
float:right;
width:900px;
height: 20px;
background-color: red;
}
<div id="maincontainer">
<div id="leftcolumn"></div>
<div id="contentwrapper"></div>
</div>
JsFiddle
Try using flexbox. It is better than using tables. Make sure to include the vendor prefixes in it.
https://jsfiddle.net/qa6cds9c/
<div id="maincontainer">
<div id="leftcolumn"></div>
<div id="contentwrapper"></div>
</div>
#maincontainer {
border: 1px solid red;
display: flex;
}
#leftcolumn {
border: 1px solid blue;
height: 400px;
width: 100px;
}
#contentwrapper {
border: 1px solid green;
height: 400px;
flex: 1;
}
It's really simple. Using good ol' CSS:
float-left only the left element
add margin-left to the right column to compensate the left's one width:
#leftcolumn {
float:left;
width: 100px;
background: blue;
}
#contentwrapper {
margin-left: 100px; /* same as #leftcolumn width */
background: red;
}
<div id="maincontainer">
<div id="leftcolumn">left</div>
<div id="contentwrapper">right<br>contentwrapper</div>
</div>
You could use a table instead of The divs. Or make the divs behave as a table using CSS.
When you have two table cells in a row and set the width for one then the other will fill the remaining space.
Not sure if this is considered best practice though.
Suppose I have two nested divs. Something like below:-
<div id="div1">Some name here
<div id="div2">DIV2</div>
</div>
Suppose the height and width of div1 is 100px. And the height and width of div2 is 50px. How do I make them appear concentric i.e div2 must lie inside div1 equidistant from all sides (using CSS).
If the two divs got fixed dimensions, you can simply put a margin on the second div. In your case :
#div2 {
margin: 25px;
}
Or, if the divs got variable dimensions, try :
#div1 {
position: relative;
}
#div2 {
position:absolute;
left:50%;
top:50%;
transform: translate(-50%,-50%);
}
OR :
#div1 {
text-align: center;
}
#div2 {
display: inline-block;
vertical-align: middle;
}
That's all the way I know to achieve that :)
I agree with the comment written by #Praveen but I would do some adjustments:
#div1{
display: table-cell;
vertical-align: middle;
}
#div2{
margin: auto;
}
You can use the automatic left and right margins like the following:
<style>
div#container {
border: 1px solid #000000;
width: 500px;
height: 200px;
}
div#inner {
border: 1px solid #FF0000;
width: 200px;
height: 100px;
margin: 0 auto;
}
</style>
<div id="container">
<div id="inner"></div>
</div>
JSFiddle Demonstration >
First, you might want to use percentages for widths in order to make them responsive.
To center div2 horizontally, insert the following in your css:
#div2 {
margin:0 auto;
}
To center div2 vertically,
#div1 {
display:block;
height: 100%;
}
#div2 {
vertical-align: middle;
display:block;
}
<style>
#div1
{
border: 1px solid blue;
width: 100px;
height:100px;
}
#div2
{
margin-left:20px;
border: 1px dotted red;
width:50px;
height:50px;
}
</style>
<div id="div1">Some name here
<div id="div2">DIV2</div>
</div>
here is the code to show div in another div with centre css
//If you have height of outer box and inner box both in px or em etc not in % or auto. Here just a logic given using a formula
#div1 {
display:block;
height: 500px;
width:50%;
margin: 0 auto;
border: 1px solid red;
}
#div2 {
display:block;
width:50%;
height:60px;
border: 1px solid green;
margin: -moz-calc( (500px - 60px) / 2 ) auto; // (height of outer box - height of inner box )/2
}
//may be you can manage height dynamically using js this should work for height in % also
<script type="text/javascript">
var div1_height = $('#div1').height();
var div2_height = $('#div2').height();
var top_margin = (div1_height-div2_height)/2;
$('#div2').css('margin',top_margin+'px auto');
</script>
#div1 {
display: grid;
place-content: center;
}
---
#div1 {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
---
#div1 {
display: flex;
}
#div2 {
margin: auto;
}
---
#div1 {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
This is in style.css:
#top{
background: white;
text-align: center;
height: 180px;
border-bottom: solid 1px #efeecc;
}
#topLogo{
background: yellow;
width: 25%;
float:left;
}
#topDescription {
background: red;
width: 75%;
text-align: center;
}
#topDepartment {
background: blue;
float:right;
width: 25%;
text-align: right;
}
This is index.html:
<div id="top">
<div id="topLogo">
<img src="img/book.jpg" width="170" height="160" border="0"/>
</div>
<div id="topDescription">
Page title
</div>
<div id="topDepartment">
Category
</div>
</div>
This is the expected outcome:
This is the current outcome:
http://jsfiddle.net/p2T5q/
Here's a demo: http://jsfiddle.net/p2T5q/7/
CSS:
#top{
background: white;
text-align: center;
display:table;
height: 180px;
border-bottom: solid 1px #efeecc;
}
#topLogo{
background: yellow;
width: 25%;
display:table-cell;
}
#topDescription {
background: red;
display:table-cell;
width: 50%;
text-align: center;
}
#topDepartment {
background: blue;
display:table-cell;
width: 25%;
text-align: right;
}
Well, the quick answer is that your div's total width is 125%, so you would need to change that so it equals 100%.
Second, you might want to put a clear fix on that top div so it encompasses all of the floating ones.
Also, don't use inline styles because you can put them in the CSS file. You are giving a fixed width to an image that sits inside a div with a percentage width. That is sure to break if the user reduces the size of his viewport.
Here you go, I fixed the fiddle and copied the answer here:
http://jsfiddle.net/p2T5q/2/
#top{
height: 180px;
border: solid 1px #555555;
position:relative;
}
#top * {
float:left;
}
#topLogo{
background: yellow;
width: 25%;
}
#topDescription {
background: red;
width: 50%;
}
#topDepartment {
background: blue;
width: 25%;
}
Basically make sure the width percentages add up to 100% and float all elements to left so they know in relation to what they should position themselves. You can use the * to select all subelements. I would appreciate you accepting this answer so I can finally get my 50 damn coins and make comments, lol.
Given this css:
#parent {
width: 100px;
height: 100px;
background-color: #090;
}
.childs {
width: 50px;
height: 50px;
background-color: #009;
border: 1px solid #999;
}
and this html:
<div id="parent">
<div class="childs"><p>aaa</p></div>
<div class="childs"></div>
<div class="childs"></div>
</div>
this is demo
http://jsfiddle.net/A3PJu/2/
I want that children divs placing in horizontal and not in vertical (as are they now), how make this?
float: left for children tags, not working in this case
You can use display:inline-block with white-space:nowrap. Write like this:
#parent {
width: 100px;
height: 100px;
background-color: #090;
white-space:nowrap;
font-size:0;
}
.childs {
width: 50px;
height: 50px;
background-color: #008;
border: 1px solid #999;
display:inline-block;
*display:inline;/* For IE7 */
*zoom:1;/* For IE7 */
white-space:normal;
font-size:13px;
vertical-align:top;
}
Check this http://jsfiddle.net/A3PJu/3/
The problem is that the width of the parent element is not big enough for 3 times 50px .childs. If you increase the #parent width to say 200px, float: left will work.
I want to create a two column layout with the right floated column containing a div that becomes scrollable once its content overflows the browser window height.
This is the html code that I am working on:
<div class=container>
<div class=column_A>A</div>
<div class=column_B>B
<div class=content>C<br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br>C
</div>B
</div>
</div>
And this is the css code:
.column_A {
float: left;
border: black solid 2px;
height: 500px;
width: 65%;
background: red;
}
.column_B {
float: right;
border: black solid 2px;
width: 30%;
background: blue;
}
.content {
border: white solid 3px;
overflow: auto;
background: green;
}
The scroll is currently on the browser window, how do I transfer it to the content?
You use overflow: auto like this:
.column_B {
float: right;
border: black solid 2px;
width: 30%;
background: blue;
overflow: auto;
height: 600px; /* ? */
}
You need to specify the height for your right column, though.
EDIT: To answer your comment, the easy way to go about it is if you set your document body's height to 100%, like this:
body {
height: 100%;
}
Then use a custom percentage to set the column's height to your liking.
.column_B {
...
height: 99%; /* or whatever you need */
...
}