I’ve stumbled across an issue that I’m not entirely sure how to resolve.
I have a page with a number of divs, one of which contains a table but has a margin of 20px. I need this table to ‘butt’ against the right-hand side of another div, which I have accomplished by using a margin of -20px – works as I’d hoped. As this div (which covers the entire right-hand side of the page) is fluid, the table has a width of 100%.
Whilst the left-hand side of the table is where I want it, the right-hand side is now 20px short of everything else.
Is there a way I can keep the negative margin on the right, without it also moving the table 20px from the right? I’ve tried a few things without success. My table CSS is pasted below.
.pricetable {
width:100%;
margin-left: -20px;
padding: 5px;
}
You could absolutely position your table, so that it’s aligned at the right.
There’s no way to add a left margin without moving the table, because the table offset is calculated in this way: margin + padding + width = offset width.
When you set the width to be
100%, the margin and padding cause the element to expand.over 100%over 100%+ negative width (X+y) =100%.The first definition adds some padding at each side of the table. The second definition shifts the table to the left, because it’s a negative margin.