3 floating divs with center dynamic width - css

Alright so my problem is that I can't get the center div to dynamically set its width based on browser width and still float in between the two divs.
<style type="text/css">
#container{width:100%;}
#left{min-height:70px;width:60px;float:left;}
#middle{min-height:70px;width:100%;float:left;}
#right{min-height:70px;width:60px;float:right;}
</style>
<div id="container">
<div id="left">text</div>
<div id="middle">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur fringilla enim eget augue pretium facilisis. Quisque euismod mauris eu odio tincidunt pharetra interdum enim dignissim. Phasellus eleifend viverra diam, et vestibulum orci feugiat vitae. Fusce rhoncus, dolor sit amet rhoncus pulvinar, elit libero tincidunt tortor, non elementum lacus nisl sit amet lorem.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur fringilla enim eget augue pretium facilisis. Quisque euismod mauris eu odio tincidunt pharetra interdum enim dignissim. Phasellus eleifend viverra diam, et vestibulum orci feugiat vitae. Fusce rhoncus, dolor sit amet rhoncus pulvinar, elit libero tincidunt tortor, non elementum lacus nisl sit amet lorem.</div>
<div id="right">text</div>
</div>

You could try setting your CSS up like this:
#container{width:100%;}
#left{min-height:70px;width:60px;float:left;}
#middle{min-height:70px;padding:0 60px}
#right{min-height:70px;width:60px;float:right;}
jsFiddle

You could set the width of the middle div using jQuery, as such:
$('#middle').width($('#container').width()-120);
Working Demo

Try this with some trivial javascript: http://jsfiddle.net/maniator/SjMqU/
var middle = document.getElementById('middle');
var win_width = document.body.offsetWidth;
middle.style.width = (win_width - 140) + 'px';
And this fiddle works on window resize: http://jsfiddle.net/maniator/SjMqU/4/

The best way to do it would be to put it into a table, but if you don't want that just make all the divs behave like table cells:
<style type="text/css">
div{display:table-cell;}
#container{width:100%;}
#left{min-width:60px;}
#middle{min-width:50%;}
#right{min-width:60px;}
</style>
.....

You could do something like this:
#container{width:100%;
}
#left, #middle, #right{min-height:70px;
display:inline-block;
vertical-align:top;
}
#left{width:19%;
background:red;
}
#middle{width:60%;
background:green;
}
#right{width:19%;
background:blue;
}
http://jsfiddle.net/jasongennaro/8BqP9/

Related

Div under another div that fills the space in the parent

Putting it simple.
Here is what I done:
https://codepen.io/matt1991/pen/abdeZNv
I've tried using flexbox to a extent, tried to mess with a lot of classes and properties, and yet can't do what I need, that is:
I need the div right-down to fill the space under the div right-up until both of them, together, have the same height of left, using only css, on a system built up on materialize.css. I know how to do that with javascript involved, but I want to make it using only css, in a way that, if left or right-up get more or less text, the size stays the same.
ps.: The red div MUST stop at the green one, it cannot go until the top and z-indexed behind the green one.
Any ideas?
If it's okay to remove the materialize.css it's easy:
<div id="main" class="container">
<div class="flex">
<div id="left">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris a aliquam urna, non imperdiet turpis. Nullam id mauris vel neque eleifend sollicitudin sit amet nec justo. Mauris auctor, erat nec bibendum posuere, nibh justo elementum orci, ut convallis tellus risus a orci. Integer sodales viverra erat, quis tincidunt ante ornare sit amet. Donec sed urna urna. Cras in lectus in erat iaculis euismod. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum in lorem eget tristique. Nam at ex commodo, vestibulum nulla quis, convallis dolor. Etiam posuere augue massa. In pulvinar vitae dolor vel feugiat.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris a aliquam urna, non imperdiet turpis. Nullam id mauris vel neque eleifend sollicitudin sit amet nec justo. Mauris auctor, erat nec bibendum posuere, nibh justo elementum orci, ut convallis tellus risus a orci. Integer sodales viverra erat, quis tincidunt ante ornare sit amet. Donec sed urna urna. Cras in lectus in erat iaculis euismod. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam rutrum in lorem eget tristique. Nam at ex commodo, vestibulum nulla quis, convallis dolor. Etiam posuere augue massa. In pulvinar vitae dolor vel feugiat.
</div>
<div id="right">
<div id="right-up">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris a aliquam urna, non imperdiet turpis. Nullam id mauris vel neque eleifend sollicitudin sit amet nec justo. Mauris auctor, erat nec bibendum posuere, nibh justo elementum orci, ut convallis tellus risus a orci.
</div>
<div id="right-down"></div>
</div>
</div>
</div>
body {
height: 100%
}
#main {
display: flex;
flex-flow: column;
height: 100%
}
.flex {
display: flex;
}
#left {
background-color: blue;
flex: 2;
}
#right {
display: flex;
flex-direction: column;
flex: 1;
}
#right-up {
background-color: green;
}
#right-down {
background-color: red;
flex: 1;
}

CSS Display Flex. I cant get it to work

Okay I have 3 divs The outside div is a column. All 3 div heights are dynamically generated by its contents.
<div class="outer" style="display:flex; flex-direction:column; float:left;">
<div class="text 1" style="float:left; flex:1; background:red;"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ipsum ex, gravida vitae erat nec, ultricies sollicitudin magna. Nulla facilisi. Nulla gravida congue viverra. Vivamus maximus lacus dolor, sit amet vestibulum orci maximus tristique. Nam non metus nisl. Mauris gravida magna sed dolor venenatis malesuada. Sed in rutrum erat. Sed dictum est neque, sit amet consequat dolor dictum eget. Fusce sit amet dolor orci. Curabitur tempus vel erat ac dictum. Proin vel congue velit. Nam venenatis erat neque, at convallis mauris eleifend ultrices. In hac habitasse platea dictumst</p></div>
<div class="text 2" style="float:left; flex:1; background:blue;"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ipsum ex, gravida vitae erat nec, ultricies sollicitudin magna.</p></div>
</div>
I have applied inline CSS for display Flex. Im working in Firefox, however I do have the cross browser CSS, but im just focused on getting it working here in Firefox and understanding this first.
The goal of what im trying to accomplish here is to make text 2 the exact same height as text 1.
Im new to this whole flex thing, im fairly certain im doing this completely wrong, ive been reading articles for a few hours and everything I try doesnt work. So im assuming im understanding this all wrong.
Im not sure if it matters, but this is being done within wordpress. Also everything is floating left
I apologize if this is trivial but im at a loss, and Stackoverflow never fails me. Any help would be greatly appreciated.
You are not far from what you wanted to do. Please notice that I removed the white spaces from your class names as white spaces are the separator for multiple classes. .1 and .2 are invalid class names.
You should use flex-direction: row; as you want the layout to be horizontally arranged. I used the flex shorthand property to reach the desired result, which stand for flex-grow, flex-shrink and flex-basis.
.outer
{
display: flex;
flex-wrap: row nowrap;
}
.text1, .text2
{
flex: 1 0;
}
.text1
{
background-color: gray;
}
.text2
{
background-color: darkgray;
}
<div class="outer">
<div class="text1"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ipsum ex, gravida vitae erat nec, ultricies sollicitudin magna. Nulla facilisi. Nulla gravida congue viverra. Vivamus maximus lacus dolor, sit amet vestibulum orci maximus tristique. Nam non metus nisl. Mauris gravida magna sed dolor venenatis malesuada. Sed in rutrum erat. Sed dictum est neque, sit amet consequat dolor dictum eget. Fusce sit amet dolor orci. Curabitur tempus vel erat ac dictum. Proin vel congue velit. Nam venenatis erat neque, at convallis mauris eleifend ultrices. In hac habitasse platea dictumst</p></div>
<div class="text2"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ipsum ex, gravida vitae erat nec, ultricies sollicitudin magna.</p></div>
</div>
Also it is not necessary to use float on flex elements. But it should have no effect what so ever, as of the w3c:
float and clear do not create floating or clearance of flex item, and
do not take it out-of-flow.

How to restyle website to flow content around sidebar?

I am attempting to write a userstyle to reformat another website (no chance to change html).
Currently the website has main content on left with a full height sidebar on RHS.
I have used CSS to remove much of the sidebar content and would now like the main content to expand to fill the width of the area below the sidebar.
If I had control of the HTML source I would place the sidebar first with 'float: right' in the style but I don't have control of the source and the sidebar div is after main content.
Traditionally this couldn't be done in CSS but can it be done now using CSS3? and if so how?
The pages I am actually attempting to style are TripAdvisor Forum pages such as this one but they are overly complicated to attempt use as an example so I have created this very simple web page to play with:
<!DOCTYPE html>
<html>
<head>
<style>
#mainbody {
border: 2px solid blue;
width: 600px;
}
#sidebar {
border: 2px solid green;
position: inherit;
float: right;
width: 250px;
}
#content {
position: inherit;
width: 550px;
}
</style>
</head>
<body>
<div id=mainbody>
<div id=content>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper
porta. Mauris massa. </p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper
porta. Mauris massa. </p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper
porta. Mauris massa.</p>
</div>
<div id=sidebar>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper
porta. Mauris massa. </p>
</div>
</div>
</body>
</html>}
With sidebar above content I get the desired format
Using jQuery you can do this:
$("#content").insertAfter("#sidebar");
http://jsbin.com/juvoc/1/edit

how to float 4 divs within another div

im trying to float 4 divs side by side. they are in a parent div width = 100% and each child div is width:25% there is no margin or padding either... they are not displaying correctly!
heres the code...
<div id="bottomsections">
<div id="1a">
<h1>a</h1>
<p>This is Photoshop's version of Lorem Ipsum. Proin gravida nibh vel velit auctor aliquet. Aenean sollicitudin, lorem quis bibendum auctor, nisi elit consequat ipsum, nec sagittis sem nibh id elit. Duis sed odio sit amet nibh vulputate cursus a sit amet mauris. Morbi accumsan ipsum velit. Nam nec tellus a odio tincidunt auctor a ornare odio. Sed non mauris vitae erat consequat</p>
</div>
<div id="1b">
<h1>b</h1>
<p>This is Photoshop's version of Lorem Ipsum. Proin gravida nibh vel velit auctor aliquet. Aenean sollicitudin, lorem quis bibendum auctor, nisi elit consequat ipsum, nec sagittis sem nibh id elit. Duis sed odio sit amet nibh vulputate cursus a sit amet mauris. Morbi accumsan ipsum velit. Nam nec tellus a odio tincidunt auctor a ornare odio. Sed non mauris vitae erat consequat</p>
</div>
<div id="1c">
<h1>c</h1>
<p>This is Photoshop's version of Lorem Ipsum. Proin gravida nibh vel velit auctor aliquet. Aenean sollicitudin, lorem quis bibendum auctor, nisi elit consequat ipsum, nec sagittis sem nibh id elit. Duis sed odio sit amet nibh vulputate cursus a sit amet mauris. Morbi accumsan ipsum velit. Nam nec tellus a odio tincidunt auctor a ornare odio. Sed non mauris vitae erat consequat</p>
</div>
<div id="1d">
<h1>d</h1>
<p>This is Photoshop's version of Lorem Ipsum. Proin gravida nibh vel velit auctor aliquet. Aenean sollicitudin, lorem quis bibendum auctor, nisi elit consequat ipsum, nec sagittis sem nibh id elit. Duis sed odio sit amet nibh vulputate cursus a sit amet mauris. Morbi accumsan ipsum velit. Nam nec tellus a odio tincidunt auctor a ornare odio. Sed non mauris vitae erat consequat</p>
</div>
and css...
#bottomsections {
width:100%;
}
#1a {
width:25%;
float:left;
margin:0;
padding:0;
}
#1b {
width:25%;
float:left;
margin:0;
padding:0;
}
#1c {
width:25%;
float:left;
margin:0;
padding:0;
}
#1d {
width:25%;
float:left;
margin:0;
padding:0;
}
heres the fiddle..http://jsfiddle.net/aM2UL/1/
thanks!
Id can not start with a number:
/* change #1a to #a1 */
#a1 {
width:25%;
float:left;
margin:0;
padding:0;
}
http://jsfiddle.net/dfsq/aM2UL/3/
Refer this comprehensive answer about what characters are allowed: https://stackoverflow.com/a/449000/949476
Upd: As pointed by Allendar in comments you should also clear your floats. You can insert one more element after your floated divs with clear: both. Personally I use .clearfix class as more semantic:
.clearfix:before,.clearfix:after{content:"";display:table}
.clearfix:after{clear:both}
to be used as <div id="bottomsections" class="clearfix">...</div>
You certainly can use numbers to start IDs, but you have to select them differently.
[id="1a"], [id="1b"], [id="1c"], [id="1d"] {
width:25%;
float:left;
margin:0;
padding:0;
}
http://jsfiddle.net/aM2UL/11/
id's cannot start with numbers in any revision of HTML and/or CSS, also, all 4 of your styles are the same, so you would be better off using a class such as
div.inner {
width:25%;
float:left;
margin:0;
padding:0;
}
and set <div id="1d" class="inner">
this will keep your code smaller and more manageable and mean less changes if and when you need to do anything else to the code later on.
#bottomsections div{ float:left; width:25%}
As others have said, numbers aren't valid beginnings to ids
ids cant start with a number, try to change for #a1, or something. but if you want to apply the same properties in all divs of you main div, why you not do this way?
#bottomsections div{
width:25%;
float:left;
margin:0;
padding:0;
}

Hiding the last item in case of overflow via CSS

I have a .list of .items, like this one:
<div class="list">
<div class="item">...</div>
<div class="item">...</div>
<div class="item">...</div>
</div>
Both .item and .list have a maximal height, say 100px and 280px.
I'd like to hide the last .item when it overflows, possibly with no JS.
What I mean is that if .list has overflow: hidden, and all .items reach the max height, then the last one is partially cut. I'd like to hide it completely, i.e. either it fits, or it disappears.
I can change both the CSS and the HTML (including, e.g., using <ul><li>... instead of divs)
If your items would've had a fixed height, and not a maximum, you could've used nth-child to predict at which item it will start to overflow.
Sample | Code
.item{
height: 75px;
border: 1px solid blue;
overflow: auto;
}
.item:nth-child(1n+4){ /* 4th element and up */
display: none;
}
.list{
max-height: 280px;
border: 1px solid red;
overflow: hidden;
}
However, since they aren't fixed heights, the only solution for such dynamics is using javascript.
On that note, here's a javascript solution.
Sample | Code
Javascript
var eList = document.getElementById("list"),
eItems = eList.getElementsByTagName("div"),
iMaxHeight = parseInt(getStyle(eList, "max-height")),
iSumHeight = 0;
for(i = 0; i < eItems.length; i++){
var iHeight = parseInt(getStyle(eItems[i], "height"));
//Check if next item will overflow, in which case, we're going to hide it
if((iSumHeight + iHeight) >= iMaxHeight){
eItems[i].style.display = "none";
}else{
iSumHeight += iHeight;
}
}
function getStyle(el,styleProp){
if (el.currentStyle)
var y = el.currentStyle[styleProp];
else if (window.getComputedStyle)
var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
return y;
}
HTML
<div id="list">
<div class="item">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc pulvinar aliquet risus, vitae suscipit tortor cursus ac.</div>
<div class="item">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc pulvinar aliquet risus, vitae suscipit tortor cursus ac. Nam venenatis, nunc sit amet elementum semper, sem purus ultrices diam, quis scelerisque elit nulla sed sapien. Vivamus urna metus, tincidunt ac accumsan et, dignissim at lorem.</div>
<div class="item">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc pulvinar aliquet risus, vitae suscipit tortor cursus ac. Nam venenatis, nunc sit amet elementum semper, sem purus ultrices diam, quis scelerisque elit nulla sed sapien. Vivamus urna metus, tincidunt ac accumsan et, dignissim at lorem.</div>
<div class="item">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc pulvinar aliquet risus, vitae suscipit tortor cursus ac. Nam venenatis, nunc sit amet elementum semper, sem purus ultrices diam, quis scelerisque elit nulla sed sapien. Vivamus urna metus, tincidunt ac accumsan et, dignissim at lorem.</div>
<div class="item">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc pulvinar aliquet risus, vitae suscipit tortor cursus ac. Nam venenatis, nunc sit amet elementum semper, sem purus ultrices diam, quis scelerisque elit nulla sed sapien. Vivamus urna metus, tincidunt ac accumsan et, dignissim at lorem.</div>
<div class="item">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc pulvinar aliquet risus, vitae suscipit tortor cursus ac. Nam venenatis, nunc sit amet elementum semper, sem purus ultrices diam, quis scelerisque elit nulla sed sapien. Vivamus urna metus, tincidunt ac accumsan et, dignissim at lorem.</div>
<div class="item">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc pulvinar aliquet risus, vitae suscipit tortor cursus ac. Nam venenatis, nunc sit amet elementum semper, sem purus ultrices diam, quis scelerisque elit nulla sed sapien. Vivamus urna metus, tincidunt ac accumsan et, dignissim at lorem.</div>
<div class="item">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc pulvinar aliquet risus, vitae suscipit tortor cursus ac.</div>
</div>
CSS
.item{
max-height: 110px;
border: 1px solid blue;
overflow: auto;
}
#list{
max-height: 290px;
border: 1px solid red;
overflow: hidden;
}
You need javascript to compute height of items, or if you want to do it just with css and you know that heights of both elements will not change, you can compute how many will fit and hide others using nth-child css selector nth-child

Resources