Div width equal to gap between two other divs - html

I have several divs in a container, organized in a row.
$(document).ready(function() {
$('#swap').click(function() {
$('#container').find('div.blue').each(function() {
$(this).removeClass('blue');
$(this).addClass('green');
});
});
});
#container {
border: 1px solid black;
border-right: 0px;
width: 500px;
min-height: 40px;
}
#left {
float: left;
}
#right {
float: right;
}
.purple {
background-color: #9471AB;
width: 70px;
}
.red {
background-color: #D48A8A;
width: 40px;
}
.green {
background-color: #A4B995;
width: 50px;
}
.blue {
background-color: #95AEB9;
width: 75px;
}
.red,.green,.blue,.purple {
height: 40px;
float: left;
box-sizing: border-box;
border-right: 1px black solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="container">
<div id="left">
<div class="purple">
</div>
<div class="purple">
</div>
<div class="purple">
</div>
</div>
<div id="middle">
<div class="red">
</div>
</div>
<div id="right">
<div class="blue">
</div>
<div class="blue">
</div>
<div class="green">
</div>
</div>
</div>
<br/>
<button id="swap">Swap</button>
.purple, .blue, .green are defined widths, but not .red. For this example, I've given it a width.
I'd like .red to have a width equal to the gap between #left and #right. I could put it underneath all of them and make the width equal to container width, but I'm looking for something that's friendly with text.
I've put a button that changes all .blue to .green. .red should stretch its width accordingly so there's no gap. Some scenarios might have two .green and one .blue on the right, some might be three .blue or three .green., etc. It should be dynamic and not calculated against the width of other classes.

flexbox solution
$(document).ready(function() {
$('#swap').click(function() {
$('#container').find('div.blue').each(function() {
$(this).removeClass('blue');
$(this).addClass('green');
});
});
});
* {
box-sizing: border-box;
}
#container {
border: 1px solid black;
border-right: 0px;
width: 500px;
margin: 10p auto;
display: flex;
justify-content: space-between;
}
#left {
display: flex;
}
#middle {
display: flex;
flex:1;
}
#right {
display: flex;
}
.purple {
background-color: #9471AB;
width: 70px;
}
.red {
background-color: #D48A8A;
flex:1;
}
.green {
background-color: #A4B995;
width: 50px;
}
.blue {
background-color: #95AEB9;
width: 75px;
}
.red,.green,.blue,.purple {
height: 40px;
border-right: 1px black solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div id="left">
<div class="purple">
</div>
<div class="purple">
</div>
<div class="purple">
</div>
</div>
<div id="middle">
<div class="red">
</div>
</div>
<div id="right">
<div class="blue">
</div>
<div class="blue">
</div>
<div class="green">
</div>
</div>
</div>
<br/>
<button id="swap">Swap</button>

Update
http://jsfiddle.net/w4rxg7v3/1/
This approach lets you have dynamic widths of left and right, and middle fills in the rest.
<div class="middle">
<div class="left"></div>
<div class="right"></div>
<p>Content for Middle: cupiditate blanditiis dolorum natus!</p>
</div>
============================================================
http://jsfiddle.net/w4rxg7v3/
You can accomplish this using float and calc().
Make 1st div float:left, right div to float:right, then make middle one float either left or right. Then set its width to calc( 100% - sumofwidthothertwodivs)
body {
min-width: 500px;
}
.left,.middle,.right {
height: 80px;
}
.left {
background-color: blue;
width: 200px;
float: left;
}
.middle {
width: calc( 100% - 400px ) ;
float: right;
background-color: red;
}
.right {
background-color: green;
float: right;
width: 200px;
}

Related

Divs don't float how I would

I've 5 divs in a single div, that I would make it floats 2 at left and 3 at right:
.container {
height: 400px;
border: 1px solid black;
}
.first, .second, .third, .fourth, .fifth {
width: 50%;
}
.first {
height: 300px;
background-color: red;
float: left;
}
.second {
height: 100px;
background-color: blue;
float: left;
}
.third {
height: 100px;
background-color: green;
float: right;
}
.fourth {
height: 200px;
background-color: yellow;
float: right;
}
.fifth {
height: 100px;
background-color: aquamarine;
float: right;
}
<div class="container">
<div class="first"> FIRST </div>
<div class="second"> SECOND </div>
<div class="third"> THIRD </div>
<div class="fourth"> FOURTH</div>
<div class="fifth"> FIFTH </div>
</div>
I would like that they were placed:
FIRST and SECOND at left
THIRD, FORTH and FIFTH at right.
Instead, they are placed like:
FIRST and FIFTH at left
SECOND, THIRD and FOURTH at right.
Do you know how fix it? Here there is my code: https://jsfiddle.net/82bkdbpn/
Since you have defined fixed height on container element you can use Flexbox to do this and define flex-direction: column.
.container {
height: 400px;
border: 1px solid black;
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
.first, .second, .third, .fourth, .fifth {
width: 50%;
height: 100px;
}
.first {
height: 300px;
background-color: red;
}
.fourth {
height: 200px;
background-color: yellow;
}
.second {background-color: blue;}
.third {background-color: green;}
.fifth {background-color: aquamarine;}
<div class="container">
<div class="first"> FIRST </div>
<div class="second"> SECOND </div>
<div class="third"> THIRD </div>
<div class="fourth"> FOURTH</div>
<div class="fifth"> FIFTH </div>
</div>
You can wrap the divs in Columns, and float these two. Or you can use flexbox, there is a very good answer from Nenad Vracar about this.
Here is an example with two column wrapper div elements:
.container {
height: 400px;
border: 1px solid black;
}
.col {
width: 50%;
float: left;
}
.first {
height: 300px;
background-color: red;
}
.second {
height: 100px;
background-color: blue;
}
.third {
height: 100px;
background-color: green;
}
.fourth {
height: 200px;
background-color: yellow;
}
.fifth {
height: 100px;
background-color: aquamarine;
}
<div class="container">
<div class="col">
<div class="first"> FIRST </div>
<div class="second"> SECOND </div>
</div>
<div class="col">
<div class="third"> THIRD </div>
<div class="fourth"> FOURTH</div>
<div class="fifth"> FIFTH </div>
</div>
</div>

Make inner div width dynamic

I want to give remaining width to .inner div. At the Same time it's siblings (a & span tags) can be of dynamic width.
Any idea?
Code below & at https://jsfiddle.net/pge8rqw0/
.top {} .inner {
border-bottom: 1px solid black;
background-color: green;
width: auto;
float: left;
width: 50px;
}
a {
float: left;
}
span {
float: right;
background-color: red;
}
<div class="top">
Visit W3Schools.com!
<div class="inner"></div>
<span>Html is good</span>
</div>
Thanks
Solution using display: flex.
.top {
display: flex;
}
.inner {
border-bottom: 1px solid yellow;
background-color: green;
min-width: 50px;
flex: 1;
}
a {
background-color: #f5f5f5;
}
span {
background-color: red;
}
<div class="top">
Visit W3Schools.com!
<div class="inner"></div>
<span>Html is good</span>
</div>
If you can change the order of elements in your code then you can achieve it without flex as follows:
.top {overflow: hidden;}
a {
float: left;
}
span {
float: right;
background-color: red;
}
.inner {
border-bottom: 1px solid black;
background-color: green;
overflow: hidden;
}
<div class="top">
Visit W3Schools.com!
<span>Html is good</span>
<div class="inner">Inner Content</div>
</div>

HTML table-cell how to set width

How can I force width to elements with display: table-cell?
<div id="wraper">
<div id="left">
<p>Left</p>
</div>
<div id="right">
<p>Right and Hide</p>
</div>
</div>
My CSS doesn't do anything, width of #left and #righ elements isnt affected.
#wraper {
display: table-wrap;
}
#left {
border: 1px solid red;
display: table-cell;
width: 30%;
}
#right {
border: 1px solid blue;
display: table-cell;
width 70%;
}
it just works fine for me
JS Fiddle: https://jsfiddle.net/qq0t46pt/1/
HTML:
<div id="wraper">
<div id="left">
<p>Left</p>
</div>
<div id="right">
<p>Right and Hide</p>
</div>
</div>
CSS:
#wraper {
display: table-wrap;
}
#left {
border: 1px solid red;
display: table-cell;
width: 30%;
}
#right {
border: 1px solid blue;
display: table-cell;
width 70%;
}
table-wrap is not a valid display value. See MDN
Use display:table.
Then you need to give the table a width so the child elements can calculate their own width accordingly.
#wraper {
display: table;
width: 70%; /* or your chosen value */
margin: auto;
}
#left {
border: 1px solid red;
display: table-cell;
width: 30%;
}
#right {
border: 1px solid blue;
display: table-cell;
width 70%;
}
<div id="wraper">
<div id="left">
<p>Left</p>
</div>
<div id="right">
<p>Right and Hide</p>
</div>
</div>

How do I make a div take the remaining height of a parent?

JsFiddle Demo
I have 2 divs in a container (actually I tagged the first as h1) and I'd like the 2nd div to take the remaining space of it's parent div. Doing height:100% makes it use 100% of its parent height causing it to be larger then the parent because of the other div. In the demo you can see the blue pass the grey.
How do I tell it to use the remaining height? The HTML may change but try not to go crazy
HTML:
<div class="outer_box">
<div class="container">
<h1>Title</h1>
<div class="box">Box</div>
</div>
<div class="container">
<h1>Title</h1>
<div class="box2">Box</div>
</div>
</div>
CSS:
.outer_box {
height: 500px;
}
.container {
width: 30%;
height: 100%;
background-color: grey;
float:left
}
.box {
background-color: blue;
height: 100%;
}
.box2 {
background-color: green;
}
You could do the CSS table layout, and set the box to height:100% to push the title to its minimal height.
http://jsfiddle.net/0w7pqeo6/3/
.outer_box {
height: 300px;
}
.container {
width: 30%;
height: 100%;
background-color: grey;
float: left;
display: table;
}
.container h1, .container > div {
display: table-row;
}
.box {
background-color: blue;
height: 100%;
}
.box2 {
background-color: green;
height: 100%;
}
<div class="outer_box">
<div class="container">
<h1>Title</h1>
<div class="box">Box</div>
</div>
<div class="container">
<h1>Title</h1>
<div class="box2">Box</div>
</div>
</div>
http://jsfiddle.net/utsavoza/9v0dfv39/
HTML part
Title
<div class="box">Box</div>
</div>
<div class="container">
<h1>Title</h1>
<div class="box2">Box</div>
</div>
CSS
.outer_box {
height: 500px;
}
.container {
width: 30%;
height: 100%;
background-color: grey;
float:left
}
.box {
background-color: blue;
height: 84%;
}
.box2 {
background-color: green;
height: 84%;
}
Use height 84% instead of 100%. you can see it in the above link..

Simple HTML/CSS - positioning div's

I've recently gotten into the world of HTML/CSS and I am struggling with something.
I want to position the div's like this:
But the I only manage to it like this:
As you see, I want div 5 to be below Div 4, and next to div 3. But I only
Of course, I can wrap div 4 and div 5 in a new div, but I'd rather learn a better way of doing this.
body {
background-color: yellow;
}
#banner {
background-color: green;
}
#topp-meny {
background-color: pink;
}
#side-meny {
background-color: violet;
float: left;
}
#innhold {
background-color: grey;
float: left;
}
#footer {
background-color: blue;
clear: both;
}
<div id="banner">Webutvikling</div>
<div id="topp-meny">Meny</div>
<div id="side-meny">
<p>sidemeny</p>
</div>
<div id="innhold">
<p>innhold</p>
</div>
<div id="footer">
<p>footer</p>
</div>
I don't think that there is a better way to do so without wrapping in a DIV your Div 4 and 5. But in place of using float, you can specify a width to each div and display them as display : inline-block; (a bit better).
* {
margin: 0;
padding: 0;
}
body {
background-color: yellow;
}
#banner {
background-color: green;
}
#topp-meny {
background-color: pink;
}
#side-meny {
background-color: violet;
display: inline-block;
vertical-align: top;
}
#innhold {
background-color: grey;
}
#footer {
background-color: blue;
}
#wrapping {
display: inline-block;
}
<div id="banner">Webutvikling</div>
<div id="topp-meny">Meny</div>
<div id="side-meny">
<p>sidemeny</p>
</div>
<div id="wrapping">
<div id="innhold">
<p>innhold</p>
</div>
<div id="footer">
<p>footer</p>
</div>
</div>
Without wrapping, you can add a height (in PX, not in %) to your Div 3 so that Div 5 remain of the right. I don't really like this option...
* {
margin: 0;
padding: 0;
}
body {
background-color: yellow;
}
#banner {
background-color: green;
}
#topp-meny {
background-color: pink;
}
#side-meny {
background-color: violet;
float: left;
height: 50px;
}
#innhold {
background-color: grey;
float: left;
width: 90%
}
#footer {
background-color: blue;
float: left;
width: 90%
}
<div id="banner">Webutvikling</div>
<div id="topp-meny">Meny</div>
<div id="side-meny">
<p>sidemeny</p>
</div>
<div id="innhold">
<p>innhold</p>
</div>
<div id="footer">
<p>footer</p>
</div>
The problem is that you need to specify a static height for your sub-menu...
Going by the diagram you supplied:
#side-meny {
float:left
}
Make a new div containing #innhold and #footer, let's call it #right-container so you have
<div id="right-container">
<div id="innhold">
<p>innhold</p>
</div>
<div id="footer">
<p>footer</p>
</div>
</div>
Then
#right-container {
float:right
}
See if that works.
Please use the following code which I have modified.
body {
background-color: yellow;
}
#banner {
background-color: green;
}
#topp-meny {
background-color: pink;
}
#side-meny {
background-color: violet;
display: inline-block;
vertical-align: top;
width: 25%;
}
#block {
display: inline-block;
vertical-align: top;
width: 74%;
}
#block p {
margin: 0;
}
#innhold {
background-color: grey;
}
#footer {
background-color: blue;
clear: both;
}
<div id="banner">Webutvikling</div>
<div id="topp-meny">Meny</div>
<div id="side-meny">
<p>sidemeny</p>
</div>
<div id="block">
<div id="innhold">
<p>innhold</p>
</div>
<div id="footer">
<p>footer</p>
</div>
</div>
in your css file you can set
#footer{
...
position:absolute;
left: (div3's width);
top: (div1+div2+div4 height)
}