This HTML:
<div style="border:1px solid blue; margin: auto; height:250px; width:600px;">
<div style="border:1px solid red; height:50px; width:80px;"></div>
<div style="border:1px solid red; height:50px; width:80px;"></div>
<div style="border:1px solid red; height:50px; width:80px;"></div>
<div style="border:1px solid red; height:50px; width:80px;"></div>
</div>
Why are the red divs not in the same horizontal row and how do I get them in the same row?
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. Here is are some examples. Default vertical-align value is baseline.
An HTML <div> (division) is a block-level elementdesigned to not display any HTML elements next to it unless its default behavior is changed. Below are all the different methods of preventing a div from breaking to the next line. Tip Depending on why you want to break a div, also consider a <span> tag.
An HTML <div> (division) is a block-level elementdesigned to not display any HTML elements next to it unless its default behavior is changed. Below are all the different methods of preventing a div from breaking to the next line.
Below is an example of the default div behavior of the block element Div oneoccupying the first line of its containing element followed by the second Div two. Div one Div two HTML code
Here's what you looking for:
<style type="text/css">
div.littleDiv {
display: inline-block;
border:1px
solid red;
height:50px;
width:80px;
}
</style>
<div style="border:1px solid blue; margin: auto; height:250px; width:600px;">
<div class="littleDiv"></div>
<div class="littleDiv"></div>
<div class="littleDiv"></div>
<div class="littleDiv"></div>
</div>
This has been possible for a long time using float, but now with inline-block it's even easier. inline-block elements are like inline elements but they can have a width and height.
However you might want to use float: lefts
instead of display: inline-block;
From MDN
The float CSS property specifies that an element should be taken from the normal flow and placed along the left or right side of its container, where text and inline elements will wrap around it. A floating element is one where the computed value of float is not none.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With