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
Related
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.
I have something like this:
<div class="top">top</div>
<div class="container">
<div class="left">left</div>
<div class="right">right</div>
</div>
<div class="bottom">bottom</div>
Relevant code in jsFiddle
As you can see, between top and bottom divs, there is a div container. I want this div container to move bottom dive as much as is needed (and i don't want it to be a fixed value - that means if, lets say left container will get much higher - the bottom div will be pushed down as well.
How can i do that?
This is a simple seeming problem that ends up being kind of tricky. The above suggestion about position:relative vs. position:absolute are a good first step. After that you need to make some room for the set width right div:
.left {
height: 100%;
min-height: 50px;
border:1px dashed red;
padding-right: 50px; <---
}
Then float your right div in the space you made:
.right {
float:right; <---
width: 50px; (This needs to match the padding-right value above.)
text-align: right;
min-height: 50px;
height: 100%;
border:1px dashed blue;
}
Finally, put the right div before the left div in the html:
<div class="top">top</div>
<div class="container">
<div class="right">right</div>
<div class="left">left</div>
</div>
<div class="bottom">bottom</div>
(Tested in Chrome and IE.)
See: Right div fix width, left div extend to max width?
You can check out a fiddle here: http://jsfiddle.net/x3QfG/1/
Will that work for you?
Right now you're using absolute positions for the left/right div's, so you will always need to know the height in order to position the bottom div correctly. What you want to do is float these instead, then clear the floats in the bottom div. That way the left/right can be as high as their contents, and the bottom div will always appear below.
.bottom {
clear: both;
}
.left {
float: left;
width: 50%;
min-height: 50px;
}
.right {
float: right;
width: 50%;
min-height: 150px;
}
I've modified your jsFiddle accordingly, and made the right div higher to show how the bottom always appears below.
Use floats rather than positioning them absolutely. That will make your architecture very much fluid and flexible.
After you apply necessary float values to your .left and .right, use a clearfix hack to contain your floated elements within the container. Now whenever any of the .left or .right divs increase in height, the bottom div will be pushed down.
Make Container Relative and left and right absolute,and for positioning set width rather than using float.
I want to make a web page that uses 100% of screen space. I have two divs:
1st - menu with fixed width (~250px)
2nd - whats left
The misleading part for me is that the menu div is not in the 2nd div. They both are in a wrapper div (100% width). The problem is that if I write 100% width for the 2nd div, it goes below the menu. If I write less %, I cannot be sure how it will be displayed in smaller resolutions.
Is there is some negative sizing or something? ATM. 1st div floats left and 2nd div float right.
UDPATE: here is some code:
div.main {
width: 100%;
}
div.1st {
width: 250px;
float: left;
}
div.2nd {
width: 100%; #here should be the space that is left in the main div#
float: right;
}
<div class="main">
<div class="1st">menu</div>
<div class="2nd">content</div>
</div>
Problem: content could be as wide as it needs to so if string or objects in it is big enough 2nd div goes below 1st. Menu width is fixed.
UPDATE #2: if i leave content width empty then it will also goes below menu since content is wide enough
Take a look at this Post, there you have the correct solution:
http://www.alistapart.com/articles/holygrail
You could do something like this : http://jsfiddle.net/steweb/78x8y/
markup:
<div id="container">
<div id="left">Width=> 250px, float left</div>
<!-- following div takes automatically the remaining width, no need to declare further css rules -->
<div id="remaining">Width => the remaining space</div>
</div>
css:
#container{
width: 100%;
float:left;
overflow:hidden; /* instead of clearfix div */
}
#left{
float:left;
width:250px;
background:red;
}
#remaining{
overflow: hidden;
background:#DEDEDE;
}
Yes, you can determine the width of absolutely positioned elements by setting left and right. This makes the browser solve the equation in the standard for width. See this demo for an example.
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
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/