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:
Any ideas what is causing this behavior and how can it be fixed?
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.
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.
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.
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
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
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With