Two Inline-Block Elements with 50% Width Fail to Fit Side by Side in a Row: Why?
When utilizing inline-block elements, there's an inherent issue with whitespace between them (approximately 4px wide). This means that two divs with 50% width each, plus this whitespace, exceed 100% width, causing them to break out of their intended single row.
Example:
body { margin: 0; } div { display: inline-block; width: 50%; } .left { background-color: aqua; } .right { background-color: gold; }
<div class="left">foo</div> <div class="right">bar</div>
Reasons for the Whitespace Issue:
- Margin Collapse: Margins of inline elements don't merge or collapse; instead, they create small spaces between the elements.
- Spacing between Inline Elements: Browsers typically add a 4px gap between inline elements if no padding or border is defined.
Alternative Solutions:
While setting one div to 49% width can resolve the gap issue, it's not an ideal practice. Alternatives include:
- Float: Floating elements removes the inline gap issue, but affects vertical alignment.
- Flexbox: Using flexbox allows for more precise layout control and avoids whitespace issues.
- CSS Grid: Modern CSS Grid layout provides a grid-based approach for controlling element positioning, eliminating the need for inline-block elements and their spacing concerns.
The above is the detailed content of Why Do Two 50% Width Inline-Block Elements Not Fit Side-by-Side?. For more information, please follow other related articles on the PHP Chinese website!

A fascinating new site called The Markup just launched. Tagline: Big Tech Is Watching You. We’re Watching Big Tech. Great work from Upstatement. The

I posted about parsing an RSS feed in JavaScript the other day. I also posted about my RSS setup talking about how Feedbin is at the heart of it.

Learn how to create a custom CodePen block with a preview for Sanity Studio, inspired by Chris Coyier’s implementation for Wordpress’ Gutenberg editor.

Line, bar, and pie charts are the bread and butter of dashboards and are the basic components of any data visualization toolkit. Sure, you can use SVG

We are always looking to make the web more accessible. Color contrast is just math, so Sass can help cover edge cases that designers might have missed.

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

Not long ago, I posted about PHP templating in just PHP (which is basically HEREDOC syntax). I'm literally using that technique for some super basic

Have you ever clicked on an image on a webpage that opens up a larger version of the image with navigation to view other photos?


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment