Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use negative right margin on table element?

Tags:

html

css

I'm trying to use table-cell elements in a grid system that utilizes negative margins on rows to eliminate the left and right gutters on nested grid elements. This works fine on floated and inline-block elements, but elements with display: table, while respecting the negative left margin, ignore the negative right margin:

Table with negative right margin

Strange, eh? A simple demonstration: http://jsfiddle.net/57FAN/1/

All browsers seem to have implemented this the same way, unfortunately. Any ideas?

P.S. Let's postpone the debate over table-* elements for layout until flex box is standardized.

like image 813
Parker Ault Avatar asked Oct 18 '12 12:10

Parker Ault


People also ask

Why margin-right is not working on table?

margin-right will not work if you set width to 100%. What you could do is : wrap table in a div tag.

When using the margin property Are you allowed to use negative values?

Syntax. The margin-bottom property is specified as the keyword auto , or a <length> , or a <percentage> . Its value can be positive, zero, or negative.

Can you add negative padding to any element?

No. Padding only takes positive values. Negatives are ignored or treated as 0, which would have the same effect: none.


1 Answers

The table doesn't ignore the negative margin, but is only 100% in width and shifted 5px to the left. You would need a "100% + 2*5px" to fill the grey area entirely.

The row doesn't have a width, thus uses the entire width available.

If you would use

.row {
  margin: 0 -5px;
  width: 100%;
}

you would see that the widths are equal

like image 93
Willem Van Bockstal Avatar answered Sep 17 '22 09:09

Willem Van Bockstal