Html

Why is a div with display table-cell not affected by margin

27 September 2026 · 8 min read

Why is a div with display table-cell not affected by margin

Understanding CSS layout properties can sometimes feel like navigating a maze, especially when elements behave unexpectedly. One common point of confusion arises when developers discover that margins don’t seem to work on div elements styled with display: table-cell;. This behavior isn’t arbitrary; it’s rooted in how the CSS table model is designed to function. When you set display: table-cell on a div, you’re essentially telling the browser to treat that div like a table cell, similar to a

| element in a traditional HTML table. This means the box-model properties like margins are handled differently. This article will delve into the reasons behind this behavior, explore alternative styling techniques, and provide a comprehensive understanding of how to achieve the desired spacing effects when using display: table-cell; in your web layouts. Let’s unravel the intricacies of CSS table layout and learn how to effectively control spacing around table cells. Why Margins Don’t Work with display: table-cell; ———————————————— The core reason margins fail to affect elements with display: table-cell; lies in the CSS table model specification. Unlike block-level elements, table cells are designed to fit within the rigid structure of a table. According to the W3C specifications, margins are not applicable to table cells. The browser calculates the cell dimensions based on the table’s overall layout and the content within the cells, prioritizing the integrity of the table structure over individual cell margins. This design ensures that the table remains consistent and predictable across different browsers and screen sizes. This also helps manage cell content without creating unexpected layout breaks. The CSS table model aims to provide a consistent rendering of tabular data, and the inclusion of margins on cells could disrupt this consistency. Consider a scenario where multiple cells in a row have margins applied. These margins would potentially collapse or overlap, leading to unpredictable spacing and layout inconsistencies. To avoid these issues, the CSS specification dictates that margins are ignored on table cells. The browser instead relies on properties like border-spacing on the | or display: table-cell element to control the spacing between cells and their content. It’s important to note that this behavior is not a bug, but rather a deliberate design choice. The CSS table model is optimized for displaying tabular data, and the omission of margins on cells is a key aspect of this optimization. This approach ensures that tables render predictably and consistently, regardless of the content within the cells or the browser being used. Understanding this fundamental principle is crucial for effectively working with CSS table layouts and achieving the desired visual effects. Alternatives for Adding Space Around Table Cells ———————————————— While margins are ineffective for creating space around table cells, several alternative CSS properties can achieve the desired spacing. The most common and effective alternatives are padding and border-spacing. Padding adds space between the content of the cell and its border, effectively creating an internal margin. Border-spacing, on the other hand, is a property applied to the Using padding on the div with display: table-cell; allows you to control the inner spacing. For example, padding: 10px; will add a 10-pixel space around all sides of the content within the cell. This approach is ideal when you want to create space between the content and the cell’s border. Remember that padding affects the overall size of the cell, potentially influencing the table layout. If you want to maintain the original size, consider using box-sizing: border-box; to include padding in the cell’s total width and height. According to a recent study by CSS-Tricks, using box-sizing: border-box; can significantly improve layout consistency across different browsers [1]. Alternatively, border-spacing is applied to the Understanding the CSS Table Model ——————————— To fully grasp why margins don’t work with display: table-cell;, it’s essential to understand the underlying CSS table model. The CSS table model defines a set of display values that allow elements to behave like HTML table elements. These values include table, table-row, table-cell, table-column, table-row-group, table-header-group, table-footer-group, and table-column-group. By using these display values, you can create complex layouts that mimic the structure and behavior of traditional HTML tables without actually using When you apply display: table; to an element, you’re essentially creating a table container. Elements with display: table-row; then define the rows within the table, and elements with display: table-cell; represent the individual cells within those rows. The browser then lays out these elements according to the rules of the CSS table model. The table model prioritizes the overall structure and consistency of the table, which is why properties like margins are ignored on table cells. Instead, the browser relies on properties like padding and border-spacing to control the spacing between cells and their content. Understanding the table model helps in debugging layout issues. The CSS table model offers a powerful way to create structured layouts, but it also comes with its own set of limitations. While it can be useful for certain types of layouts, it’s often more flexible and efficient to use other layout techniques, such as Flexbox or Grid, for more complex or responsive designs. Flexbox and Grid offer more granular control over element positioning and spacing, making them better suited for modern web development practices. However, understanding the CSS table model remains valuable for maintaining legacy code and troubleshooting layout issues in older projects. CSS Grid is now supported by the vast majority of browsers [2]. Best Practices and Common Pitfalls ———————————- When working with display: table-cell;, several best practices can help you avoid common pitfalls and achieve the desired layout results. One of the most important practices is to always remember that margins don’t work on table cells. Instead, rely on padding and border-spacing to control the spacing. Additionally, make sure to set the border-collapse property on the Another common pitfall is forgetting to set display: table; on the parent element. If you only set display: table-cell; on the child elements, the layout will not behave as expected. The parent element needs to be explicitly defined as a table container for the child elements to be rendered as table cells. Also, remember that the CSS table model has certain limitations compared to other layout techniques like Flexbox and Grid. While it can be useful for creating structured layouts, it’s often more flexible and efficient to use Flexbox or Grid for more complex or responsive designs. Here are some key takeaways to keep in mind: - Margins do not affect elements with display: table-cell;. - Use padding and border-spacing as alternatives for creating spacing. - Ensure the parent element has display: table; set. By following these best practices and avoiding common pitfalls, you can effectively use display: table-cell; to create structured layouts and achieve the desired visual effects. Always test your layouts across different browsers and screen sizes to ensure consistency and responsiveness. Remember to consider the specific requirements of your project and choose the most appropriate layout technique based on those requirements. For more in-depth information, refer to the Mozilla Developer Network documentation [3]. 1. Set display: table; on the parent element. 2. Set display: table-row; on the row elements. 3. Set display: table-cell; on the cell elements. 4. Use padding on the cell elements for internal spacing. 5. Use border-spacing on the table element for spacing between cells. FAQ: Common Questions About display: table-cell; and Margins ————————————————————

Why are margins ignored on elements with display: table-cell;?
Margins are ignored to maintain the integrity and consistency of the CSS table model. The table layout prioritizes the overall structure over individual cell margins.
What are the alternatives to using margins for spacing around table cells?
The primary alternatives are padding (to add space within the cell) and border-spacing (to add space between cells on the table element).
Does border-collapse affect the use of border-spacing?
Yes, border-spacing only works when border-collapse is set to separate. If border-collapse is set to collapse, border-spacing is ignored.
Can I use negative margins as a workaround?
While technically possible, using negative margins is generally not recommended due to potential layout inconsistencies and maintenance issues. It’s better to stick to padding and border-spacing.
Infographic here: Visual comparison of padding, margin, and border-spacing on table cells.
Working with CSS layouts requires a nuanced understanding of how different display properties interact. While display: table-cell; offers a way to structure content like a table, it’s essential to remember that margins won’t behave as they do with block-level elements. By leveraging properties like padding and border-spacing, you can achieve precise control over spacing and create visually appealing layouts. This knowledge empowers you to choose the right tools for the job, leading to cleaner, more maintainable code. So, experiment with these techniques, explore the possibilities, and continue honing your CSS skills to master the art of web layout. Question & Answer : I have div elements next to each other with display: table-cell;. I want to set margin between them, but margin: 5px has no effect. Why? My code: <div style="display: table-cell; margin: 5px; background-color: red;">1</div> <div style="display: table-cell; margin: 5px; background-color: green;">1</div> Cause —– From the MDN documentation: > [The margin property] applies to all elements except elements with table display types other than table-caption, table and inline-table In other words, the margin property is not applicable to display:table-cell elements. Solution ——– Consider using the border-spacing property instead. Note it should be applied to a parent element with a display:table layout and border-collapse:separate. For example: HTML <div class="table"> <div class="row"> <div class="cell">123</div> <div class="cell">456</div> <div class="cell">879</div> </div> </div> CSS .table {display:table;border-collapse:separate;border-spacing:5px;} .row {display:table-row;} .cell {display:table-cell;padding:5px;border:1px solid black;} See jsFiddle demo —————————————————– — Different margin horizontally and vertically As mentioned by Diego Quirós, the border-spacing property also accepts two values to set a different margin for the horizontal and vertical axes. For example .table {/*...*/border-spacing:3px 5px;} /* 3px horizontally, 5px vertically */