Why Is The Top Border of This Inline Element Not Displaying and Why Does Using Float Correct This

Why is the top border of this inline element not displaying and why does using float correct this?

Technical explanation of how outline, border and padding are rendered in this example? ? :)

  1. Because <span> is an inline element, and the positioning of inline elements starts from the top-left of the padding (not counting the border and margin).

  2. float: left applies display: inline-block, which means that it's no longer inline. The positioning for inline-block elements starts from the top-left of the margin.

  3. You can use margin on a <span> but it won't do anything useful :P

Can't see the border-top in this inline element

Let me quote this answer:

display: inline means that the element is displayed inline, inside the current block on the same line. Only when it's between two blocks does the element form an 'anonymous block', that however has the smallest possible width.

But if top border would be taken into account, it would make your div vertically misaligned with the other elements on the same line, even though in your case there is only a single element on the line. However, top border is ignored, therefore it is "sticking out" of the body and you cannot see it.

As a "proof", try to modify your HTML code in the provided fiddle as:

<div style="line-height: 50px"><div class="content">test test test</div></div>

Then you'll be able to see the top border, as the height of parent element has enough space for it not to stick out.

Why is this inline-block element pushed downward?

Basically you have added more clutter in your code which is creating more confusion so first I try to remove clutter which hinders understanding the real issue.

First of all we have to establish that what's the real question?
Its that why "inline-block" element is pushed downward.

Now we start to understand it and remove the clutter first.

1 -
Why not give all three divs same border width?
Let's give it.

2 - Does floating element has any connection with inline-block element being pushed downward?
No, it has nothing to do with it.

So, we have removed that div altogether. And you are witnessing same behavior of inline-block element being pushed downward.

Here comes the turn of some literature to grasp the idea of line boxes and how they are lined in the same line esp read last paragraph carefully because there lies the answer of your question.

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.

If you are not sure about baseline then here is brief explanation in simple words.

All characters except 'gjpqy' are written on the baseline you can think of baseline as if you draw a simple horizontal line same as underlining right below these "random characters" then it will be the baseline but now if you write any of 'gjpqy' character(s) on the same line then lower part of these characters would fall below the line.

So, we can say that all characters except 'gjpqy' are written completely above the baseline while some part of these characters are written below the baseline.

3 - Why not check where is the baseline of our line?
I have added few characters which show the baseline of our line.

4 - Why not add some characters in our divs too to find their baselines in the div?
Here, some characters added in divs to clarify baseline.

Now when you understand about baseline, read the following simplified version about baseline of inline-blocks.

i) If inline-block in question has its overflow property set to visible (which is by default so no need to set though).
Then its baseline would be the baseline of the containing block of the line.

ii) If inline-block in question has its overflow property set to OTHER THAN visible.
Then its bottom margin would be on the baseline of the line of containing box.

  • First point in detail

Now look at this again to clarify your concept that what's happening with green div.
If yet any confusion then here is added more characters close to green div to establish the baseline of the containing block and green div baseline is aligned.

Well, I am now claiming that they have same baseline? RIGHT?

5 - Then why not overlap them and see if they are fit right one on another?
So, I bring third div -left: 35px; to check if they have same baseline now?

Now, we have got our first point proved.

  • Second point in detail

Well, after explanation of first point second point is easily digestible and you see that first div which has overflow property set to other than visible (hidden) has its bottom margin on the base line of the line.

Now, you can do couple of experiments to further illustrate it.

  1. Set first div overflow:visible (or remove it altogether).
  2. Set second div overflow: other than visible.
  3. Set both divs overflow: other than visible.

Now bring back your clutter and see if everything is looking to fine to you.

  1. Bring back your floated div (of course there is need to

    increase some width of body)
    You see it has no effect.
  2. Bring back same odd margins.
  3. Set green div to overflow: visible as you set in your question (that misalignment is due to increase of border width from 1px to 5px so if adjust negative left you'll see there is no issue)
  4. Now remove additional characters I added to aid in
    understanding. (and of course remove negative left)
  5. Finally reduce body width because we no longer need wider one.

And now we are back to where we started from.

Hopefully I have answered your question.

My inline-block elements are not lining up properly

10.8 Line height calculations: the 'line-height' and 'vertical-align' properties

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.

This is a common issue involving inline-block elements. In this case, the default value of the vertical-align property is baseline. If you change the value to top, it will behave as expected.

Updated Example

.position-data {
vertical-align: top;
}

Why Inline-block doesn't work properly in this CSS?

This is actually expected behavior in HTML. Because you are using inline-block, any newline character or whitespace you have after the element and before another inline element, will be counted as a space. If you want the blocks to stack side by side like in your picture, your HTML would need to be like this.

<div class="rex">
<div class="ex"></div><div class="ex2"></div><div class="ex3"></div>
</div>

working demo

It's not very pretty, but then again, I would recommend using another approach, possibly floating the elements instead.

Refer to here for a more in depth explanation of why this occurs.

How to remove the space between inline-block elements?

Why top margin of html element is ignored after floated element?

You've correctly identified the problem. The floated <div> is no longer used to calculate the top margin, and therefor the 2 <div>'s just butt against each other. A simple way to solve this is just to wrap the 2nd <div>. This will allow the wrapper to (invisibly) butt up against the first <div>, and allow you to specify white space for it.

The trick to getting the wrapper to work right is to have the white space be internal white space. In other words, the wrapper uses padding instead of margin. This means whatever is going on outside the wrapper (like other floating elements) won't really matter.

<div style="float: left; border: solid red 1px">foo</div><div class="wrapper" style="clear: both; padding-top: 90px;">    <div style="border: solid red 1px">This div should not touch the other div.</div></div>

CSS: clear on inline elements

It's simple: clear only applies to block-level elements.

'clear'

Applies to: block-level elements

Block-level elements are defined as

Block-level elements are those elements of the source document that
are formatted visually as blocks (e.g., paragraphs). The following
values of the display property make an element block-level: block,
list-item, and table.

Why does displaying my divs as inline cause them to overlap my h1?

Only block elements respect width and height rules. Inline elements just flow with their text content. The overlap is caused by the padding and the fact that the divs are further down in the DOM, so they're drawn after and on top of previous elements.

Try changing your divs to display: inline-block instead.

Align inline-block DIVs to top of container element

Because the vertical-align is set at baseline as default.

Use vertical-align:top instead:

.small{
display: inline-block;
width: 40%;
height: 30%;
border: 1px black solid;
background: aliceblue;
vertical-align:top; /* <---- this */
}

http://jsfiddle.net/Lighty_46/RHM5L/9/

Or as @f00644 said you could apply float to the child elements as well.



Related Topics



Leave a reply



Submit