How position divs horizontally inside another div - css

I have a php script that is generating an unspecified number of divs inside another but by default the divs are being vertically aligned as in one of top of the other though their widths can allow for them to be horizontally aligned. How can i go about this?

div > div{
display: inline-block;
}

float them to left
float:left;
or give them inline display mode.
display:inline;

Related

CSS - Vertical alignment of divs as list items

I wanted to evenly distribute a few divs horizontally. So I found this link:
How do I distribute items horizontally with CSS?
which explained how to do it with an unordered list. That takes care of the horizontal distribution.
Now, when I add different text and styles of text to each of these divs, they do not vertically align with one another. It seems the bottoms of the last lines of text within each div align with eachother, instead of the last line of the div.
Here's my fiddle: http://jsfiddle.net/3jxV7/1/
How can I just get the divs to vertically align with one another, no matter what content I include within those divs?
To make it easy, assume all my divs have the same dimensions:
div.foo
{
width:100px;
height:100px;
background:cyan;
}
Add this property in your CSS
li
{
vertical-align:middle;
}
View the demo http://jsfiddle.net/3jxV7/2/
If you want the texts inside vertical aligned too then add this on your CSS
div.foo
{
display:table-cell;
vertical-align:middle;
}
View the demo http://jsfiddle.net/3jxV7/4/

vertical aligning floats

How do I vertical align floating elements?
I currently have 3 elements (an image floated left, another image floated right and a div with a margin:0 centered) in a wrapping div. These are currently aligned at the top but I want them to aling at the bottom of the div.
How can this be achieved without using position absolute on them as this is a responsive layout? I have tried making them display:inline-block and vertical-align: bottom but that does not help anything.
In order to use vertical-align on some element, that element must have display:table-cell; css style. http://jsfiddle.net/StPYR/
Your jsfiddle updated: http://jsfiddle.net/chCjT/16/
Instead of floating the elements you need to give them display:inline-block; css property

How to sandwich a content div between two fixed-width columns

Take a look at this fiddle. I've got divs on either side, with fixed widths of 50px, and display:inline-block.
I want the div on the inside to expand to fill the gap between these two divs, but the problem is if I put lots of text inside this div it pushes itself onto the next line, and the layout breaks down.
Also, I want it so the middle div will fill the space even if there is a small amount of text in it (less than the page width).
How can I ensure the left and right divs are always on the left and right, and that the content div always fills the space in between them?
I tried using CSS3's calc, but it appears that it isn't very well supported.
This is the sort of situation that was handled in the past by using tables for layout. This will be fixed in the future by the new Flexbox implementation https://developer.mozilla.org/en-US/docs/CSS/Flexbox
In the meantime, you can emulate the table effect by using display: table-cell, this will cause each div to stretch the way you would expect a table to. If you want more control over the total width you can wrap it in a div that has a display: table-row and a set width.
.col {
background-color:#333;
color:#EEE;
width:50px;
display:table-cell;
}
.middle {
background-color:#EEE;
color:#333;
display:table-cell;
}
If you are open to using javascript, this will work:
​$(function(){
$('div.middle').width( $('div.container').width() - $('div.right.col').width() - $('div.left.col').width() - 10 );
$(window).resize(function() {
$('div.middle').width( $('div.container').width() - $('div.right.col').width() - $('div.left.col').width() - 10 );
});
});
Working JSFiddle:
http://jsfiddle.net/vh8Zu/

CSS Div side-by-side without set width

I'm trying to get two divs to appear side by side as follows:
First div is displayed on the left float: left and takes up as much space as it needs.
Second div appears on the right float: right and takes the remaining space.
The issue is that I do not have a set width for any of those divs so they just take up as much space as they can: jsFiddle
I have found several solutions but all of them required setting width for one of the divs which is what I'm trying to avoid. Any possible solution?
#left, #right { display: table-cell; }​
#left { white-space: nowrap; }
http://jsfiddle.net/CoryDanielson/LzREv/5/

Centering Horizontal divs inside the outer Div?

I'm creating a horizontal menu bar.
There is an outer div that contains inner divs (each of these divs is the individual menu item).
I use float: left for styling on these inner divs to display them horizontally instead of vertically.
The problem is that the menu bar has uneven space on the left and right, hence the overall menu bar doesn't appear to be centralized... i.e the first menu item on left has lesser space from left border and the space between last menu item on right and the right border is more.
I want equal margin/padding on the left and right to make the menu bar display in center.
I tried setting margin-left: auto; margin-right: auto on the outer div and then on the inner div as well. Both don't seem to help.
Already had a look here: How to horizontally center a <div> in another <div>?
However this particular answer is to center just one div, what I have is a collection of horizontal divs (menu items) that need to be centralized.
Any help is appreciated.
If your menu items aren't complex (like no fixed sizes or sub-elements), try adding the following styles:
#outer {
text-align: center;
}
.menu-item {
display: inline; /* replace 'float:left' with this */
}
DEMO
Otherwise you'll need a wrapper for the inner elements that has a fixed width:
#wrapper {
margin: 0 auto; /* center in outer DIV */
width: /* sum of widths of inner elements */;
}
DEMO
NOTE: Semantically its better to style menu items using a list, as in the DEMOs.
Firstly if all your menu items have specified widths and display:inline which is default, you wouldn't need a float to line them up.
But supposing you do, when you set a float, your elements are removed from the natural flow of the document, and hence margin:auto will not work to center it. What you could do in this case is create another container div of specified width for the menu items, within which you could use float to line them up.
Now what's left is to center this one container div within your outer div, which you have already seen how to solve. For example, you could use the margin:auto technique on the container div to center it. Make sure you have text-align:center for the outer div.

Resources