Reputation: 1038
I have following html
<div class="main-conteiner">
<div class="block">First</div>
<div class="block">Second</div>
</div>
And this css
.main-conteiner {
width: 600px;
margin: 0 auto;
}
.block {
width: 50%;
display: inline-block;
background: red;
-webkit-box-sizing: border-box;
box-sizing: border-box;
-moz-box-sizing: border-box;
}
div {
margin: 0;
padding: 0;
}
I need to arrange both div
s in a same line. But instead of that it brakes down into two lines.
I cant figure out where I've done a mistake.
Upvotes: 2
Views: 1408
Reputation: 419
You have to change a line in .block
class
.block {
width: 50%;
display: inline-block;
background: red;
-webkit-box-sizing: border-box;
box-sizing: border-box;
-moz-box-sizing: border-box;
}
You can have a look here: FIDDLE
Upvotes: 0
Reputation: 2404
<div class="main-conteiner">
<div class="block">this is just a div</div><!--
--><div class="block">this is another div</div>
</div>
comment is for removing the spaces between the DIVs.
Upvotes: -1
Reputation: 11496
Here is a nice explanation and various other ways to to get rid of it
Some of them are as others,
<div class="main-conteiner">
<div class="block">this is just a div</div><!--
--><div class="block">this is another div</div>
</div>
or
<div class="main-conteiner">
<div class="block">
this is just a div</div><div class="block">this is another div</div>
</div>
or
<div class="main-conteiner">
<div class="block">this is just a div</div
><div class="block">this is another div</div>
</div>
Upvotes: 0
Reputation: 32202
define your parent font-size:0;
and child define font-size:12px
as like your design
as like this
.main-conteiner{font-size:0;}
.block{font-size:12px;}
2nd option is this
now write your html code as like this
<div class="main-conteiner">
<div class="block">this is just a div</div><!-- --><div class="block">this is another div</div>
</div>
Upvotes: 0
Reputation: 1
.main-conteiner {
width: 600px;
margin: 0 auto;
}
.block {
width: 50%;
display: inline-block;
background: red;
-webkit-box-sizing: border-box;
box-sizing: border-box;
-moz-box-sizing: border-box;
float:left;
}
div {
margin: 0;
padding: 0;
}
Upvotes: 0
Reputation: 292
Use this CSS :
.block {
width: 50%;
display: block;
background: red;
float:left;
-webkit-box-sizing: border-box;
box-sizing: border-box;
-moz-box-sizing: border-box;
}
Upvotes: 0
Reputation: 1196
If you want both the div
s which have a class of .block
to be on same line then just use a float
.block {
float:left;
width: 50%;
display: inline-block;
background: red;
-webkit-box-sizing: border-box;
box-sizing: border-box;
-moz-box-sizing: border-box;
}
Upvotes: 0
Reputation: 106058
When you use display:inline-block, you should know and remenber that any space in HTML in between tags will be rendered as white-space. like in between two words.
One easy way to avoid yhis is to bring together closing and opening brackets of tags. DEMO
<div class="main-conteiner">
<div class="block">this is just a div</div><div class="block">this is another div</div>
</div>
or insert a comment in between
<div class="main-conteiner">
<div class="block">this is just a div</div><!--
--><div class="block">this is another div</div>
</div>
Upvotes: 1
Reputation: 71230
Its because inline-block
takes into account white space in your HTML, simply remove the line break between the two elements.
Revised HTML
<div class="main-conteiner">
<div class="block">this is just a div</div><div class="block">this is another div</div>
</div>
The reason being that inline elements are taken into account effectively in the same way as text, a new line/line break separation will always be rendered as a white space- which in addition to 2*50% will bring the width to greater than 100%.
To stop the linebreak in your HTML being rendered as an additional space, simply set font-size
on the container to 0, then set font-size for the child div
, this will mean the rendered space has a size of zero, thus no width, so will not compromise the width calculations.
.main-conteiner {
width: 600px;
margin: 0 auto;
font-size:0;
}
.block {
width: 50%;
display: inline-block;
background: red;
-webkit-box-sizing: border-box;
box-sizing: border-box;
-moz-box-sizing: border-box;
font-size:15px;
}
div {
margin: 0;
padding: 0;
}
Upvotes: 2