Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force elements to be horizontally aligned

Let's say I have random children in my div, which has fixed height and width set to 100% to breathe with the layout.

Which CSS must I use to force child elements to align horizontally and when the div's width is smaller then the content, display a scrollbar and not overlap one another?

Fiddle:http://jsfiddle.net/GRBc6/1/

simple css:

.parent{
    width:500px;
    height: 50px;
    background-color: red;
}
.kid{
    width: 150px;
    height: 20px;
     background-color: green;
    float:left;
    margin-left:4px; 
}
like image 623
Jacob Avatar asked Mar 22 '13 09:03

Jacob


People also ask

How do you horizontally align elements?

Elements can be aligned horizontally with the help of CSS float property which aligns multiple elements to either left/right and not in center or using CSS positioning scheme absolute method.

How do you make elements appear horizontally in CSS?

To align div horizontally, one solution is to use css float property. But a better solution is to to use CSS display:inline-block on all divs which needs to be aligned horizontally and place them in some container div.

How do I align a div element horizontally?

To center a div horizontally on a page, simply set the width of the element and the margin property to auto. That way, the div will take up whatever width is specified in the CSS and the browser will ensure the remaining space is split equally between the two margins.

What is horizontally align?

Definition English: Horizontal alignment is the positioning of a roadway, as shown in the plan view, using a series of straight lines called tangents connected by circular curves.


1 Answers

if you make the kid an inline-block element and take off the float:left, you can make the parent have white-space:nowrap and it will achieve what you want:

.parent{
    width:300px;
    height: 50px;
    background-color: red;
    white-space:nowrap;
    overflow-x:scroll;
}
.kid{
    width: 150px;
    height: 20px;
    background-color: green;
    display:inline-block;
    margin-left:4px;

}

http://jsfiddle.net/GRBc6/6/

like image 141
Pete Avatar answered Sep 22 '22 02:09

Pete