Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why my inline-block divs are not aligned when only one of them has text? [duplicate]

Live page here.

Given this HTML page:

section[role=main] {    margin: 1em;    width: 95%;    border: 1px solid #999;  }    section[role=main] article {    width: 40%;    height: 180px;    margin: 1em;    display: inline-block;    border: 1px solid black;  }
<section role="main">    <article>Java</article>    <article></article>  </section>    <section role="main">    <article>Java</article>    <article>JavaScript</article>  </section>

I expect both of my articles to be aligned, but as it can be seen in the screenshot below, only when both of my articles have text the <article> elements are center aligned:

alignment issue

Any ideas what is causing this behavior and how can it be fixed?

like image 805
TheFooProgrammer Avatar asked Nov 25 '12 04:11

TheFooProgrammer


People also ask

Does text align work on inline block?

Even though the property says “text” align, it affects all elements inside the block-level element that are either inline or inline-block elements. The property only affects the content inside the element to which it is applied, and not the element itself.

How do you align inline block elements?

To align things in the inline direction, use the properties which begin with justify- . Use justify-content to distribute space between grid tracks, and justify-items or justify-self to align items inside their grid area in the inline direction.

How do I keep my div inline?

The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.

How many CSS vertical alignment issues with inline/inline-block elements?

CSS vertical alignment of inline/inline-block elements 22 inline-block element with no text renders differently 6 Content of div affects horizontal alignment, inline-block issue

How does the content of a Div affect the alignment?

Content of div affects horizontal alignment, inline-block issue 1 Alignment Issue :- Second Post is not aligned properly 0 elements unintentionally move on content change See more linked questions Related 1 Permanent 2x3 CSS image gallery 16 Creating a two-column-100% layout with Bootstrap 3 Submenu with z-index doesn't want to go over content 5

What are the issues with inline-block element with no text?

inline-block element with no text renders differently 6 Content of div affects horizontal alignment, inline-block issue 1 Alignment Issue :- Second Post is not aligned properly

How do I align the baseline of an inline block?

baseline Align the baseline of the box with the baseline of the parent box. If the box does not have a baseline, align the bottom margin edge with the parent's baseline. (my emphasis) Because the default alignment for the inline-blocks is "baseline", unless it is overridden, this rule applies.


1 Answers

Adding:

vertical-align: bottom; 

To your second rule should make it work. Apparently, inline-blocks with no text are rendered as inline-images or something else, and the vertical-align of these elements are incorrect, so forcing them to be aligned to bottom fixes the issue.

Source: inline-block element with no text renders differently

like image 82
jcolicchio Avatar answered Sep 28 '22 04:09

jcolicchio