Fill div with divs with auto margins (CSS) - css

I bet this question is asked several times before, but I can't find an answer. Not here or google. I guess that says alot of my front end- (and searching?-) skills.
So apologies for an expected already answered post.
My problem is how ever that I have a div on 980 pixels that I want to fill with other smaller divs. Lets say that the smaller divs are 180 pixels wide, and that i have 7 of them. Then it should be 2 rows of small divs with 20 pixels beween them and it should be 5 on the first row and 2 on the next row.
One way is to set them to float: left and have a margin-left: 20px. This would how ever end up in a total row with of 1000px (20 to wide) wich will result in 4 divs on the first row and 3 on the second.
One try was to set a margin-right:-20px on the container, but it did (not that unexpected) not work.
Any help or links to answers will be welcome!
EDIT:
I realized i was not really clear in my question. The container will also be rezisable, so the smaller divs should just fill the space in the container with the first on the row 0px from left and the last on the row will be 0px from right. The divs in between should just autofit. I have only found stuff like this for one row problems. I would then also prefer if it broke the row 'til next line if the margins between the smaller divs are < 1 pixel.
EDIT 2:
I made a jsfiddle to my problem. The green boxes should autofill the gray area (horizontally, the vertical margin can be a given value, like 15/20 px).
http://jsfiddle.net/yfnpv/

It's difficult to tell exactly want you want.
If I understand you right, it's this.
demo
I have added some helper elements :
....
<div class="obj">7</div>
<div class="obj push"></div>
<div class="obj push"></div>
<div class="pushend"></div>
</div>
and the CSS is :
#container {
max-width: 980px;
background-color: gray;
display: inline-block;
text-align: justify;
}
.obj {
width: 180px;
height: 180px;
background-color: green;
display: inline-block;
}
.obj.push {
height: 0px
}
.pushend {
width: 100%;
height: 0px;
display: inline-block;
}
The idea is not mine, but I can't just find the place where i saw it.
I believe that the original author also used some clever trick with pseudo-elements, just to not change the HTML, but I can't exactly remember.

<div id="container">
<div class="1column first">one column</div>
<div class="1column">one column</div>
<div class="1column">one column</div>
<div class="1column last">one column</div>
<div class="clear"></div>
<div class="1column first">one column</div>
<div class="1column">one column</div>
<div class="1column">one column</div>
<div class="1column last">one column</div>
</div> <!-- container -->
The CSS
#container {width: 630px;}
.1column {width: 150px; float: left; margin-left: 5px; margin-right: 5px;}
.first {margin-left: 0px;}
.last {margin-right: 0px;}
.clear {clear:both;}
In order to make sure everything fits properly, you have to add the width of the smaller div + its margin values to get a total div value and then add that to the others.
So for the first div, its total width should be 155px (150px + 5px), the next should be 160px (150px + 5px + 5px). So if you have the first and last div's 155px and two other divs 160px, your total width should now be 630px.
To make a break to form another row, you would add a clear:both div in between. This clears all floats, left or right, and moves to the next line. Then you can repeat.
The 5px margins would make a 10px margin between each smaller div (5px + 5px).
This is just a broad example. You can have different size divs (you should Google css grid frameworks), you will learn a lot about how these systems are set up. They're essentially the same as what I did but go further than this.
What ever you do, just make sure that the inner divs don't go beyond the "container" divs width or you will run into problems. Remember, padding + margin + width + borders = total width. So even adding a simple 1px border to any div will automatically increase it by 2px (1px on left 1 px on the right).
Edit: then if you wanted to center align the whole thing, you would add to the container div in the CSS
#container {width: 630px; margin-left: auto; margin-right: auto;}
I hope this helps.

Related

Left/Center/Right DIVs with Vertical Alignment

Trying to set up a outer DIV that is 114px high and 100% wide (page width). Inside that I want three DIVs left, center and right. The height of these inner 3 divs vary. I'd like all three inner DIVs to be centered top to bottom (vertically aligned).
I have gotten close floating left and right or left:0 and right:0 but stuck on the vertically centered part. Left and right DIVs contain images center text only. Having a hard time getting images to show on jsfiddle so I can't provide much of an example, sorry.
I have found examples with top and bottom alignment but no luck centered.
Thanks!
EDIT:
TyBlitz, you pointed me in the right direction. Thanks! However, on your container div height - inner div height you then need to divide by 2. For your example:
114-50=64 then divide by 2 = 32 for top.
Also needed to play around with text-align (left/center/right) Not thrilled making all inner DIVs 33% but it works. Seems like I should be able to have them scalable width and still position them correctly.
Here is my fiddle with example images imbedded: My Fiddle
Like so?
Simply float the 3 divs, give each of them a height, position them relatively (important!) and give them a top: container.height - innerdiv.height; E.g. container.height of 114 & innerdiv.height of 50 => 114-50 = 64=> top: 64px;
EDIT: Well if you don't want to make your divs all 33% , with some simple math you can approximately position your divs with the float and margin properties.
Like in this fiddle of yours I updated.
The math behind it is:
Container.length - combined innerdiv.length = unoccupied.length
margin-left: unoccupied.length / x (eg 50px / 5 = margin of 20% = 10px)
I think this can solve your problem, although you'll have to create 3 wrapper divs.
HTML
<div class="outer">
<div class="leftDiv">
<div class="leftInnerDiv">Left Div</div>
</div>
<div class="rightDiv">
<div class="rightInnerDiv">Right Div</div>
</div>
<div class="centerDiv">
<div class="centerInnerDiv">Center Div</div>
</div>
</div>
CSS
.outer,
.leftDiv,
.rightDiv,
.centerDiv{
height: 114px;
}
.leftDiv{
float: left;
}
.rightDiv{
float: right;
}
.centerDiv{
overflow: hidden;
}
.leftDiv:before,
.rightDiv:before,
.centerDiv:before{
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
.leftInnerDiv,
.rightInnerDiv,
.centerInnerDiv{
vertical-align: middle;
display: inline-block;
}
.leftInnerDiv{
background-color: red;
}
.rightInnerDiv{
background-color: green;
}
.centerInnerDiv{
background-color: blue;
}
jsfiddle link: http://jsfiddle.net/pv6yJ/1/
Please note though:
The right div is declared before the middle div in the html.
My solution (css vertical align) doesn't work on IE7 or lower.
I've created this example to show how to vertically align the container and use column count (css3) which will automatically layout the 3 divs into 3 equal columns.
-moz-column-count: 1;
-webkit-column-count: 3;
column-count: 3;
and for non supporting browsers (IE) you can just set the width to 33% and float left.
http://codepen.io/tom-maton/pen/oqsEJ
Hope this helps

Getting three divs to auto resize when the content in the middle one changes

What I'm trying to do is have three divs wrapped in a fixed width container that will auto resize when the content in the middle div expands. As the middle gets larger the two beside it get smaller if that makes sense.
<div id="container">
<div class="one"/>
<div class="middle">...</div>
<div class="two"/>
</div>
Not sure if I should be using div or span for this.
Any advice on the CSS?
#thirtydot The two divs at the side of
the middle div will contain nothing,
just a border-top, the middle div will
contain two links. :)
In that case, I'm answering with something simpler that you might be able to use.
See: http://jsfiddle.net/uZ5dn/
<div class="container">
<span class="middle">content con tent the tent of cons content content</span>
</div>
.container {
border-top: 5px solid #f0f;
text-align: center;
}
.middle {
background: #fff;
display: inline-block;
position: relative;
top: -5px; /* same as border-top-width */
}
It's not awesome, but it might be good enough.
At the very least, I'll get a better idea of what to suggest next.
If I'm reading your question correctly, I suspect that you'll have to do this with JavaScript, and DIVS.
You can get and set the actual size of the DIVs in pixels using the .height() function.
So, you could do something like:
if ($('#div2').height() > 200) {
$('#div1').height(100);
$('#div3').height(100);
} else {
$('#div1').height(200);
$('#div3').height(200);
}

css Suggestion for 3 columns

I'm looking to create a header like:
<div id="header>
<span class="left></span>
<span class="center"></span>
<span class="right></span>
</div>
So have a header that's all inline. the left to all the way to the left, the right is all the way to the right, and the center is in the center of the header.
The challenge is #header is fluid. Any CSS suggestions. right now the best way I can think to get this done is with a table with 3 rows.
Thanks
Try this:
http://jsfiddle.net/z7k3J/
If you adjust the spacer bar in the middle of the page, you will see that all of your "columns" stay appropriately aligned.
The key is that all of the columns' widths add up to 100% and you float them all to the left (or right, it doesn't really matter). When your widths are percentage-based, they will adjust appropriately as the parent changes size.
If you only care about the text being right/center/left (and not images, etc), you could also make all of the columns 100% width and absolutely positioned, and then just use text-alignment:
http://jsfiddle.net/h7qB8/
Is there a reason that floating the left and right spans won't work for you?
Can you re-order the HTML to be left/right/center (or right/left/center)? If so, float the columns and use margins or borders on the center to hold it off the side bars.
This would be a good start:
<div id="header" style="position:relative;width:100%;">
<div class="left" style="position:relative;width:200px;float:left;"></div>
<div class="center" style="position:relative;float:left;text-align:center;"></div>
<div class="right" style="position:relative;width:100px;float:right;"></div>
</div>
This will center everything in the middle div and keep the other 2 divs to the side. Make sure the element containing the header div is set to position:relative;width:100%; as well.
You can also use display: inline-block on the inner elements, on the outer container do a text align center, and then on .left float: left; .right float: right. This would allow you to set a width on the spans, but keep them evenly spaced from the center. See:
#header {
width: 100%;
text-align: center;
}
.left, .center, .right {
display: inline-block;
width: 100px;
border: 1px solid green;
}
.center {
text-align: center;
}
.left {
float: left;
}
.right {
float: right;
}
You don't mention it in your question but have it in your tag - if you're able to use CSS3 then the possibilities open up more. You can use the flex box layout: http://www.the-haystack.com/2010/01/23/css3-flexbox-part-1/ or css3 columns: https://developer.mozilla.org/en/CSS3_Columns

CSS: 3 divs - Resizing 2 outer divs automatically based on width of inner div/text

My problem is best outlined with this schematic/image which outlines how I want it to look:
!
I have a background image and 2 divs for text over the top of it (headline, and intro-text). I also have 2 divs on either side of the headline - these are for the white horizontal stripes.
My issue is that the headline is changeable in a CMS, and I want the horizontal white stripes to automatically fill up the space to the left and to the right of it, regardless of the headline's width.
I can't figure out how to make those 2 horizontal white stripes resize automatically.
Here's my HTML:
<div id="masthead">
<div id="headline-container">
<div id="left-stripe"> </div><div id="headline">{headline}</div><div id="right-stripe"> </div>
</div>
<div class="clear-both"> </div>
<div id="intro-text">{intro_text}</div>
</div>
And here's my CSS - ignore the widths specified for the left-stripe and right-stripe - they're just placeholders:
#masthead {
height: 260px;
}
div#headline-container {
width:960px;
padding:none;
}
div#left-stripe{
float: left;
background-color:#fff;
height: 3px;
width:500px;
display: inline;
}
div#right-stripe{
float: right;
background-color:#fff;
height: 3px;
width:100px;
display: inline;
}
div#headline {
text-align:right;
color: #fff;
font-size: 200%;
float: left;
display: inline;
}
div#intro-text {
text-align: left;
float: right;
width: 300px;
color: #fff;
}
Ideas? Please let me know if I can provide more detail.
I'm a bit too busy to actually test this, but this might give you some direction. i'm not sure the exact effect you're trying to achieve (see comment about finding a live demo someone made).
Regardless, this kind of fluid layout is a bit difficult to achieve reliably with straight CSS. To make it easier I would suggest making the right-stripe a static width.
This CSS solution MIGHT work... no promises.
markup
<div class="container">
<div class="headline-container">
<div class="left-stripe"></div>
<div class="headline">Headline goes here</div>
<div class="right-stripe></div>
</div>
<div class="content"></div>
</div>
CSS
//static width for right stripe
.right-stripe { width: 20px; }
.headline { width: auto; }
.left-stripe { width: auto; }
Using javascript would make it really easy though... here's how i would do it with jQuery. Again, I would make the right-stripe a static width to achieve this effect.
(same markup...)
..
js
var totalWidth = $("#container").width();
var leftWidth = totalWidth - ($("headline").width() + $("right-stripe").width());
$("left-stripe").width(leftWidth);
You can do this dynamically, with jQuery, for example. You take the full width of the 3 div's, drop the size of the inner div and assign dynamically the widths of the 2 outer div's in which the bar should repeat horizontally.
Basically, you will need:
$("#whole-div").width() - $("#inner-div").width() for the outer div's total width. Then, depending on your positioning of the inner-div, you assign values for the outer div's.
For example: whole div has 1000px, inner div has 200px and inner div is positioned 600px left. You will then assign 600px to the left div ($("#whole-div").width() - $("#inner-div").css('left')) and 200px for the right div ($("#whole-div").width() - $("#inner-div").css('left') - $("#inner-div").width()). Of course, you will then set a background-repeat property on the outer div so that the image repeats.
Hope that helps!
UPDATE CSS only fluid solution: http://jsfiddle.net/SebastianPataneMasuelli/XnvYw/1/
it uses the same background image twice, on #masthead and on #headline-container. except ton headline container the background is offset to match its left position relative to its parent element. then we only need one div.line behind it, which gets covered by the background image under the headline and copy, giving the illusion of a seamless image.
do you mean like this?: http://jsfiddle.net/SebastianPataneMasuelli/XnvYw/

2 column CSS div with stretchable height

Related (possibly duplicate) questions:
How do I achieve equal height divs with HTML / CSS ?
Make Two Floated CSS Elements the Same Height
Hello, every one,
I tried for hours to create a stretchable 2 columns div but without any luck. here is my html code and my css code below it
<div class="two_cols_container">
<div class="two_cols">
<div class="left-col">
test
</div>
<div class="right-col">
test
</div>
</div>
</div>
my css code is
.two_cols_container {
width: 100%;
height: 100%;
}
.two_cols {
position: relative;
margin: 0 auto;
border: 1px solid black;
height: 100%;
height: auto !important;
min-height: 100%;
}
.two_cols .left-col {
/*position: absolute;
left: 0;*/
float: left;
}
.two_cols .right-col {
/*position: absolute;
right: 0;*/
float: right;
}
any idea?
A: either use float OR absolute positioning to make your columns. not both. You can just float both the columns to the left and it should be ok with no absolute positioning.
B: you're big problem is the columns can't be next to each other if both of their' widths are 100%. There's no way they can sit side by side in their containing element when they both take up the whole width. Set the width to at most 50%, but I'd go with a little lower to account for some browser bugs.
EDIT: I agree with Sneakiness, wet the width to something lower than 50%, because the margins and padding have to fit too.
There's
Tables ( you probably wouldn't want to rely on this )
Faux Columns ( the most practical way, faking columns going down using images - see http://www.alistapart.com/articles/fauxcolumns/ )
Border Trick ( a little complex but this only works for solid colors )
Padding / Margin / Clipping ( another complex one I wouldn't recommend )
I'd go with #2. If you need colors that are backgrounds of those columns to go all the way down, set a background on the container of those columns and make sure it repeats vertically, e.g,
div#wrapper { background:url(/images/faux.gif) repeat-y; }
If the columns are floated make sure to have overflow:hidden and a hasLayout trigger for IE like a width.
By the way since you have floats, apply overflow:hidden to .two_cols selector and add this rule:
html, body { height:100%; }
I found this method to be the simplest and most effective of all equal-height two-column layouts. You don't have to fake anything, and it Just Works.
If you mean that you want a fluid two-column layout, you need to set margins for both columns separately to position them both on the page.
You can use div style property to create as many columns you need, with what ever CSS effect you need :
<div style=”width: 100%;”>
<div id=”left” style=”float: left;">
<--! your text here -->
</div>
<div id=”right” style=”float: right;">
<--! your text here -->
</div>
</div>
Source and example : WordPress Tutorial Series - Basics about HTML and CSS

Resources