Home > Web Front-end > CSS Tutorial > Why Doesn't Margin Work on Divs with `display: table-cell;` and How Can I Add Spacing?

Why Doesn't Margin Work on Divs with `display: table-cell;` and How Can I Add Spacing?

Linda Hamilton
Release: 2024-12-06 22:35:12
Original
528 people have browsed it

Why Doesn't Margin Work on Divs with `display: table-cell;` and How Can I Add Spacing?

Margin Property Not Affecting Div with "display: table-cell;"

In HTML, we can encounter a scenario where div elements with "display: table-cell;" lack the expected spacing introduced by the margin property.

Cause

The margin property is incompatible with elements exhibiting display types other than table-caption, table, or inline-table. Regrettably, display: table-cell falls outside of these exceptions.

Solution

To circumvent this limitation, consider utilizing the border-spacing property as an alternative. However, it's crucial to apply border-spacing to a parent element featuring a display: table layout, accompanied by border-collapse: separate.

HTML

<div><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><div>
Copy after login


CSS

.row {display:table-row;}<br>.cell {display:table-cell;padding:5px;border:1px solid black;}<br>

With this setup, you can add spacing between the div elements.

Horizontal and Vertical Margin Variations

Additionally, the border-spacing property allows for independent margin values along the horizontal and vertical axes by specifying two distinct values.

.../border-spacing:3px 5px;} /<em> 3px horizontally, 5px vertically </em>/<br>

The above is the detailed content of Why Doesn't Margin Work on Divs with `display: table-cell;` and How Can I Add Spacing?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Previous article:How Can I Make Header Heights Equal in a Flexbox Layout? Next article:Inline Styles vs. External CSS: Is Inline Styling Really Faster?
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template