Dynamically added inline-block divs have wrong vertical spacing [duplicate] - html

This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 3 years ago.
I have an HTML template with a series of blocks, which are just "inline-block". Initially, a number of blocks are rendered as part of the template, but users may add additional blocks, which then get appended dynamically.
My problem is that the dynamically added blocks have a different spacing compared to the pre-rendered ones.
Check out this fiddle: https://jsfiddle.net/7w3hu5gk/
It is clear that the blocks, added dynamically by the Javascript code, don't line up vertically.
HTML:
<div id="blocks">
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>
CSS:
#blocks {
width:140px;
}
#blocks div.block {
display:inline-block;
*display:inline; // Legacy IE love
zoom:1;
vertical-align:top;
width:20px;
height:20px;
margin:5px;
border:1px solid red;
background:1px solid #777;
}
It seems that the culprit is the inherent (and invisible) character spacing, since inline-block makes elements behave sort-of-like text. Setting font-size: 0 on the #blocks element will magically fix the problem. But then the font size of text contained within the div.blocks elements have to be resized.
Does anyone have a nice fix for this?
Floating elements (e.g. float: left;) are not a desirable alternative.

This is due to there being a new line between each div in your HTML. This makes the browser think it needs to add a space in between each element.
Either remove the spacing or add font-size:0; to your parent div.
Fiddle for option 1:
https://jsfiddle.net/Lu0xw1b6/
Fiddle for option 2:
https://jsfiddle.net/fkcb5mrw/

Use a flexbox on the blocks div and there you go!
#blocks {
display: flex;
flex-wrap: wrap;
}
see fiddle here.

Hi try this code.
#blocks {
width:140px;
letter-spacing: -0.31em;
*letter-spacing: normal;
*word-spacing: -0.43em;
}
#blocks .block {
display: inline-block;
*display: inline;
zoom: 1;
letter-spacing: normal;
word-spacing: normal;
vertical-align: top;
width:20px;
height:20px;
margin: 0 5px 5px;
border:1px solid red;
background:1px solid #777;
}
Regards :)

Related

Span style float:left is breaking div block [duplicate]

This question already has answers here:
Floating elements within a div, floats outside of div. Why?
(11 answers)
Closed 7 years ago.
I'm working on maintaining a bit of code that's out of whack at the moment. Basically, we have a <div> tag with it's own style settings, and we have multiple logic tags that will display different <span> tags, which will hold different bits of data.
What I'm seeing is that when I'm using a <span> tag with a style setting float: left; this is causing the <div> tag's color box to not wrap around the <span>.
Here's a sample of the code:
<div id="testData" style="padding:4px; width: 100%; border: 1px solid #999; background: #d1d1d1; text-align:right;">
<span style="padding: 3px 1 1 1; float:left;">
TestData: Float Left
</span>
</div>
I need this span tag to go left, due to requirements. Was wondering what my options are for this to work?
Original jsFiddle
Add overflow:auto to the parent div:
#testData {
overflow:auto;
}
jsFiddle example
Other way is to make use of clear: both
#testData:after {
clear: both;
display: block;
content: "";
}
Fiddle
Other solutions:
Using overflow: hidden
#testData {
overflow: hidden;
}
Or making a dummy element <div class="clearBoth"></div>
HTML
<div id="testData" style="padding:4px; width: 100%; border: 1px solid #999; background: #d1d1d1; text-align:right;">
<span style="padding: 3px 1 1 1; float:left;">
TestData: Float Left
</span>
<div class="clearBoth"></div>
</div>
CSS
.clearBoth {
clear: both;
}
http://jsfiddle.net/gLfw5wc7/3/
#testData {
padding:4px;
width: 100%;
border: 1px solid #999;
background: #d1d1d1;
text-align:right;
}
#testData:after {
content:"";
clear: both;
display: table;
}
#testData > span {
padding: 3px 1px 1px;
float:left;
}
This is known as a clearfix. When floating an element, it gets out "the flow" of the document. This also means that its width and height aren't taken into account by the parent. That's why #testData seems to collapse: it thinks it doesn't have content. To fix this there are some options. The easiest is to use overflow, however, that's bad practice imo. In this particular case it works, but in some other cases you won't be able to use it because content that overflows the parent will either be hidden (overflow: hidden) or a scrollbar will appear (overflow: auto).
The most common and proper solution is to use a pseudo element to fix this. :after is such a pseudo element (see this question for :after vs ::after). Basically, a pseudo element can create an element in CSS that is not visible in HTML.
Every time you use float, you'll be needing a clearfix. Therefore it's useful to create a .clear class which you can apply to every element that needs to clear floats. It would look like this.
HTML
<div id="testData" class="clear">
<span>
TestData: Float Left
</span>
</div>
CSS
.clear:after {
content:"";
clear: both;
display: table;
}
Now you can add class="clear" to every element that needs to be cleared. If you are into SASS, you might find this answer helpful but considering you are new to HTML, I'd suggest sticking to HTML and CSS first.

Why inline-block have different rendering than inline in firefox?

Friends,
My question is why Firefox adds additional pixel (padding) below the box if I use display: inline-block?
Let's see what we have here: http://jsfiddle.net/xbU5s/9/
HTML - Two perfectly same elements.
<div class="wrap">
<section class="ib">Hello world</section>
<section class="il">Hello world</section>
</div>
CSS - Everything is the same, but our first section is inline-block and second one is inline.
.wrap { font-size: 0; }
.ib { display: inline-block; }
.il { display: inline; margin-left: 10px; }
section {
background: #000; border-radius: 3px; color: #fff; font-size: 11px; font-family: Sans-serif;
padding: 3px 5px;
}
And here's our 1px padding:
display: inline-block; vs display: inline;
Is is just rendering glitch (cause it's only happens in firefox) or I'm misinformed about inline-block's behavior?
Perhaps the answer is already explained here in old post
I will like to clear the difference..
If the element is with style display:inline the style restricts the object in line-height.
But, when block comes with inline the behavior of the same changes.
It is inline but with block it will expand to the possible height or width available.
For a change. select the text in both the box, you will see the second box is selecting out of the box. that is overflow of line-height which is restricted by inline but with inline-block it will grow with overflow caused by padding + line-height
I think this will clear most of the doubts, please refer the old post for more details.

Display: inline-block issues

i am using display:inner-block in my code. But inline-block automatically assigns padding between divs's which is causing browser incompatibility in my site. Can any one point out a solution.
Here a fiddle for basic reference.Here you can clearly see the padding assigned by inner block property
http://jsfiddle.net/damsarabi/vbhnF/#&togetherjs=4aiQ9gSCpq
This is the basic code for reference in fiddle
<div class="LabelColumn">label column</div>
<div class="DataColumn">data column</div>
div{
border:1px solid #000
}
div.LabelColumn
{
display: inline-block;
vertical-align: top;
}
Regards,
inline-block doesn't automatically add margin, it is inline, which means it takes notice of your white space between each element. Even though this might look like a 4px or so margin, it's not, it's a single space. One way to get round it would be to remove the whitespace:
<div class = "LabelColumn">asdfasdf</div><div class = "LabelColumn">asdfasdf</div><div class = "DataColumn">data</div><div class = "LabelColumn">asdfasdf</div><div class = "DataColumn">data</div><div class = "LabelColumn">asdfasdf</div><div class = "DataColumn">data</div>
Another, to comment out the whitespace:
<div class = "LabelColumn">asdfasdf</div><!--
--><div class = "LabelColumn">asdfasdf</div>
JSFiddle
Or lastly, but by no means least, float:left instead of changing the display type:
JSFiddle
By default, there is an extra margin-right of 4px (according to the font-size parent).
You can fix this issue with a css tweak. It's the solution I use more often, and it's easy way to adjust this alignment.
div.LabelColumn
{
display: inline-block;
vertical-align: top;
margin-right: -4px;
}
You can see more tweaks on this link : http://css-tricks.com/fighting-the-space-between-inline-block-elements/
Also you can set font-size of container to 0 and set necessary font-size for floating elements. This will eliminate spaces.
This is useful for cases when you can't avoid white-spaces between your elements (for example, some IDEs can be configured to automatically reformat markup).
div.LabelColumn
{
font-size:16px;
display: inline-block;
vertical-align: top;
text-align:left;
border:1px solid #000;
}
.full_width {
width:100%;
font-size:0;
}
Demo:
http://jsfiddle.net/keaukraine/htAR6/2/
Try this:
<div class="LabelColumn">label column</div>
<div class="DataColumn">data column</div>
div.LabelColumn
{
font-size:16px;
display: inline-block;
vertical-align: top;
text-align:left;
border:1px solid #000;
}
.full_width {
width:100%;
font-size:0;
}

Elements won't stay inline in Google Chrome next to Twitter Widget

I'm trying to keep three squares vertically aligned. The third element is a twitter widget. I'm running Ubuntu 12.04 and the two squares seem to shift downwards in google chrome. All other browsers seem to render this correctly (with them all inline).
I've tried removing white space, looking deeper into my source, etc., but even this simple jsfiddle seems to have the same issue.
What's up?
http://jsfiddle.net/bwzGC/
HTML
<div id="info-block">
<div id="twaewsit-content">
<span class="header">This Week # EWSIT</span>
</div>
<div id="ue-content">
<span class="header">Upcoming Events</span>
</div>
<div id="twit-content" style="border:none !important;">
<a class="twitter-timeline" href="https://twitter.com/EWSITGOGREEN" data-widget-id="362066477261680640">Tweets by #EWSITGOGREEN</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
</div>
</div>
CSS
#info-block {
padding:20px 0 20px 0;
width:100%;
text-align:center;
}
#info-block > div {
height:250px;
width:30%;
max-height:250px;
overflow-y:hidden;
display:inline-block !important;
border:1px solid #e8e8e8;
-webkit-border-top-left-radius: 5px;
-webkit-border-top-right-radius: 5px;
-moz-border-radius-topleft: 5px;
-moz-border-radius-topright: 5px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
font:normal normal normal 12px/16px "Helvetica Neue",Arial,sans-serif;
}
#info-block > div > .header {
display:block;
padding:8px;
font-weight:bold;
font-size:14px;
text-align:left;
border-bottom-style:solid;
border-bottom-color:#e8e8e8;
border-bottom-width:1px;
}
One option is to add vertical-align: bottom; to #info-block > div
#info-block > div {
height: 250px;
width:30%;
max-height:250px;
overflow-y:hidden;
vertical-align: bottom;
}
http://jsfiddle.net/bwzGC/2/
I came across something similar the other day and looked into the causes a little.
As Adrift pointed out this is due to the vertical-align of the inline-block elements. Specifically it seems to be related to the baseline value which inline-block elements use by default and the fact you also have overflow-y:hidden set.
There seems to be a rendering difference between Webkit browsers (I see the boxes pushed down in Chrome, Safari and Opera 15) and others (Firefox and Opera 12 have the boxes aligned to the top) when vertical-align:baseline and overflow:hidden are applied. Removing the overflow-y css gives me the same results (boxes pushed down) in both Firefox & Chrome.
Per the W3C CSS 2.1 Specification:
The baseline of an 'inline-block' is the baseline of its last line box in the normal flow, unless it has either no in-flow line boxes or if its 'overflow' property has a computed value other than 'visible', in which case the baseline is the bottom margin edge.
So it seems like Gecko is rendering according to the specification in this case whereas Webkit is not.
Float the 3 divs left and you will be fine
EXAMPLE
#twaewsit-content,#ue-content,#twit-content{
float:left !important;
}

Why is content of the inline-block affects its position in the container [duplicate]

This question already has answers here:
Why does this inline-block element have content that is not vertically aligned
(4 answers)
Closed 8 years ago.
Here's a fiddle that shows my code in action
The result seems crazy to me: in Chrome second button is slightly above the first.
In Firefox it is slightly below.
<div id="accounts">
<button class="account">
<h1>VISA Card</h1>
<span class="balance">-433.18</span>
</button>
<button class="account">
<h1 class="plus"><i class="icon icon-plus-sign"></i></h1>
<span class="plus-text">Add Account</span>
</button>
</div>
What is even more confusing is that padding on the h1.plus affects the position of the whole div.
What is going on here? I want two buttons to show up on the same line and simply don't undestand why they aren't. Is this a bug in the rendering engine?
UPDATE:
Narendra suggested an easy fix - float:left the buttons. I want to figure out why this misalignment happening in the first place.
You are using display:inline-block, so the buttons are aligned by their vertical-align property, which defaults to baseline.
This is a diagram from the specs which illustrates exactly that:
You can see in the first two boxes how padding and the font size of the content influence the positioning.
As a fix, use vertical-align: top or bottom, or even middle.
Edit: The image is from the table section and the situation is slighty different for inline-blocks.
Add this to your button.account: vertical-align: middle; .
And you can lose the display: inline-block; property, as it is not needed.
Check below code
button.account {
display: block;
float: left;
height: 80px;
margin: 10px 10px;
padding: 10px 5px;
width: 170px;
}
.account h1 {
font-size: 16px;
height: 16px;
margin: 0 0 5px;
padding: 4px 0 2px;
}
.account .balance {
display: block;
font-size: 24px;
}
.account h1.plus {
font-size: 24px;
padding-top: 0px;
}
Here is the fiddle http://jsfiddle.net/Gq3U8/13/
If you are using inline-block, the main concern is about the whitespace (you will see the default margin around the element). To fix this just add vertical-align:top, instead of using float:left. It will align the element to the top.
.account {
display: inline-block;
vertical-align: top; /*add this one*/
margin: 10px 10px; /*remove this one then can see whitespace*/
}