Float vs display: inline-block
The reason your code doesn't work is because you are using display: inline-block;.
If you use float: left; it will make the elements that have this property float against eachother without bumping other elements down as long as these other elements have float aswell (except for when the width exceeds the width of the screen).
CSS Float
Bad coding conduct
You could ofcourse change the font-size or comment out the whitespace or even remove the closing tags (which is supported by HTML5) but these methods are only a plaster on the wound (and are very bad coding conduct, please don't ever do this). If you use float to make the elements fit you won't have to do any "fix" to make it work.
examples of good coding practices
Look at 1, 3 and 4 specifically, but the entire page is a very good read.
CSS
On the topic of assigning css:
You should always use class-based styling instead of inline css for the following reasons:
- It's easier to read (for both you and others) and looks nicer.
- It allows the use of external CSS files.
- It allows easier use of JQuery or Javascript with class/id selectors.
- It's a more professional method of coding.
- You can assign multiple classes to one element, removing the need of writing the same code twice.
- Easier to debug code in consoles (FireBug/Chrome console).
- All the cool kids are doing it.
Different methods of assigning style to elements
Try the following:
Working Fiddle
<style>
#main {
width: 100%;
height: 50px;
}
#green {
width: 50%;
height: 50px;
background-color: green;
float: left;
}
#red {
width: 50%;
height: 50px;
background-color: red;
float: left;
}
</style>
<div id="main">
<div id="green"></div>
<div id="red"></div>
</div>