Ghost space between inline-block elements in Chrome, Safari and Firefox [duplicate] - html

This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 8 years ago.
How can I remove the ghost space between inline-block elements?
Here is a jsfiddle http://jsfiddle.net/hFDcV/ where you can clearly see a horizontal space between the divs.
And the StackOverflow mandated example code:
<div id='row'>
<div class='box'>Something</div>
<div class='box'>Something</div>
<div class='box'>Something</div>
<div class='box'>Something</div>
</div>
#row {
background-color: red;
}
.box {
width: 150px;
height: 150px;
background-color: yellow;
display: inline-block;
margin: 0;
padding:0;
}
​

One solution: http://jsfiddle.net/hFDcV/4/
Set the font-size of the parent container to 0 and reset it on the child elements.
#row {
font-size:0;
}
.box {
font-size:12pt;
}
Another solution: http://jsfiddle.net/hFDcV/10/
You can float the box elements left. Setting overflow:hidden; on the row will prevent it from collapsing to 0 height.
#row {
overflow:hidden;
}
.box {
float:left;
}
There are other solutions in the fantastic article on this problem shared by #RickCalder: http://css-tricks.com/fighting-the-space-between-inline-block-elements/

One easy way to achieve this is to insert commentary between inline-block elements!
like this :
<div id='row'>
<div class='box'>Something</div><!--
--><div class='box'>Something</div><!--
--><div class='box'>Something</div><!--
--><div class='box'>Something</div>
</div>
Hope this helps someone!

Related

div margin level why when we apply margin property to lower div it apply on the upper div [duplicate]

This question already has answers here:
How to disable margin-collapsing?
(12 answers)
Closed 11 months ago.
Here i have three divs i want the div three to have margin from the top but since then I am giving it the margin to the third div but what and expected to move from the top only bit the complete div is moving
*{
margin:0;
padding:0;
}
.divone{
background-color:red;
}
.divtwo{
margin:10px 4px;
}
.divthree{
display:flex;
justify-content:space-between;
}
<div class="divone">
<div class="divtwo">
<div class="divthree">
<div class="ndiv">
<h3>I am</h3>
</div>
<div class="ndiv">
<h3>Hello</h3>
</div>
</div>
</div>
</div>
According to your code you're currently applying it to .divtwo.
What you actually need to do is apply padding to .divthree to get what you want.
Change your CSS to the following to accomplished that:
*{
margin: 0;
padding: 0;
}
.divone{
background-color: red;
}
.divthree{
display: flex;
justify-content: space-between;
padding: 10px 4px;
}

How to prevent text inside a div from affecting the spacing around div [duplicate]

This question already has answers here:
Align inline-block DIVs to top of container element
(5 answers)
Why is this inline-block element pushed downward?
(8 answers)
Closed 1 year ago.
<style>
#main {
width: 110px;
height: 110px;
}
#main div {
width:50px;
height: 50px;
border: 1px solid black;
display: inline-block;
}
</style>
<div id='main'>
<div>x</div>
<div>x</div>
<div>x</div>
<div></div>
</div>
I have a grid of divs set up just a like a checker board. The divs are either empty, or have a single unicode character inside of them.
When a character is removed from or added to the div, the spacing around the div is affected. (see snippet)
How can I stop this behavior? I would like for the content inside of the div to not affect the positioning or spacing around the div.
you can fix your code by adding vertical-align:top to your inner 4 divs
Don't use the display:inline-block, try with display:flex on the outer div.
Basic concepts of flexbox
This issue occurs because of the way that vertical-align is calculated. When no vertical-align is set, the default is baseline. However, when there is no text, baseline is calculated differently. See this answer.
Using FlexBox, as suggested by most of the other answers would obviously avoid this issue.
If you want to avoid FlexBox, probably the best option is to just set vertical-align explicitly, as suggested in DCR's answer.
Another method would be to wrap the inner text in a <span> and add position: absolute. This way, all the boxes effectively have the same size content, and the discrepancy is resolved. Here's an example:
<style>
#main {
width: 110px;
height: 110px;
}
#main div {
position:relative;
width:50px;
height: 50px;
border: 1px solid black;
display: inline-block;
}
#main div span {
position: absolute
}
</style>
<div id='main'>
<div><span>x</span></div>
<div><span>x</span></div>
<div><span>x</span></div>
<div><span></span></div>
</div>
Like Nicola Revelant said in their answer, you can use something like flexbox to make this work. Here's an example:
<style>
#main {
width: 110px;
height: 110px;
display: flex;
}
#main div {
display: flex;
flex-direction: column;
}
#main div div {
width: 50px;
height: 50px;
border: solid 1px black;
}
</style>
<div id='main'>
<div class="row1">
<div>x</div>
<div>x</div>
</div>
<div class="row2">
<div></div>
<div></div>
</div>
</div>

HTML/CSS: Display: block with a float or inline-block [duplicate]

This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 7 years ago.
I have a basic issue.
I want two div's next to each other. I would usually do this with a display:block; and a float:left; on both elements.
What I try to do now is display:inline-block; and a width:60% for the left div and a width:40% for the right div.
The problem is that the div's won't align because one of the is to big. If I reduce one in size it works but then there is a lot of space around the second div.
Here is a Fiddle:
Can anyone see what I do wrong?
M.
Inline elements are sensitive to white space in your code. Remove the white space:
<div class="wrapper">
<div id="header">
Header
</div><div id="container">
Container
</div><div class="sidebar">
Sidebar
</div><div id="footer">
Footer
</div>
</div>
jsFiddle example
with inline-block white space will effect the flow of your document.
Remove the white space on these elements.
.wrapper{
margin: 0 auto;
text-align: left;
background:#000000;
}
#header{
width: 100%;
background:#00FF73;
}
#container{
width: 60%;
display: inline-block;
vertical-align: top;
background:#FF0004;
}
.sidebar{
width: 40%;
display: inline-block;
background:#0037FF;
}
#footer{
width: 100%;
background:#B400F9;
}
<div class="wrapper">
<div id="header">
Header
</div>
<div id="container">
Container
</div><!--
--><div class="sidebar">
Sidebar
</div>
<div id="footer">
Footer
</div>
</div>

Layout. Two inline boxes with content where one is divided into other boxes [duplicate]

This question already has answers here:
Align two inline-block div with content
(2 answers)
Closed 7 years ago.
Desired result: The two divs with class inline should be on the same horizontal level (the second one contains two other divs with some content).
However, as can be seen below, the two divs are not aligned vertically. If I remove the content (the word "text") from both the .inside divs, they line up as expected.
How can I make them line up? What is causing this?
.inline,
.inside {
margin: 0;
padding: 0;
width: 100px;
height: 100px;
}
.inline {
display: inline-block;
background-color: chartreuse;
}
.inside {
height: 48px;
background-color: salmon;
border: solid 1px black;
}
<div class="inline">
</div>
<div class="inline">
<div class="inside">text</div>
<div class="inside">text</div>
</div>
<hr>
<div>Without content (i.e. the word "text"):<div>
<div class="inline">
</div>
<div class="inline">
<div class="inside"></div>
<div class="inside"></div>
</div>
.inline {
vertical-align: top;
}
Thanks everybody.

element following emty inline-block div has strange padding at top [duplicate]

This question already has answers here:
Why is this inline-block element pushed downward?
(8 answers)
Closed 8 years ago.
I'm wondering myself, why an empty div, that has the display:inline-block style behaves differently from a div that cointains text. It somehow seems to add some top-padding to following elements, that cannot be removed.
HTML
<p>
<div class="a">A</div><div class="b">B</div>
</p>
<p>
<div class="a"></div><div class="b">B</div>
</p>
CSS
.a {
width:20px;
height:20px;
display:inline-block;
background-color:red;
}
.b {
background-color:orange;
line-height:20px;
font-size:12px;
display:inline-block;
}
Output
There's also a fiddle
Can someone explain this behavior?
inline elements are vertical-align: baseline by default. The baseline is affected by the text.
Change to vertical-align: top / middle / bottom
From the MDN:
baseline
Aligns the baseline of the element with the baseline of its parent. The baseline of some replaced elements, like <textarea> is not specified by the HTML specification, meaning that their behavior with this keyword may change from one browser to the other.
Your new example
.a {
width: 20px;
height: 20px;
display: inline-block;
background-color: red;
vertical-align: top;
}
.b {
background-color: orange;
line-height: 20px;
font-size: 12px;
display: inline-block;
vertical-align: top;
}
br {
line-height: 2;
}
<div class="a">A</div>
<div class="b">B</div>
<br />
<div class="a"></div>
<div class="b">B</div>