0

I am using a grid system for my web page layout, much like the Grid 960 layout. I have a bunch of predefined classes for all the grid widths. When I want a div to be a certain width, I simply do:

<div class="grid_4">
  ...
</div>

However, for some of my divs I add borders so I created a custom border class that I add to those divs for which I want a border.

.right-border {
  border-right: 1px solid #e1e1e1;
}

<div class="grid_4 right-border">
  ....
</div>

The problem is that when I add a border, the grid system fails since there isn't enough room across the page to fit all the divs (since one of them has a border). I can manually adjust every div I add a border to, but I want a cleaner solution since every width of every grid is different (depending on which grid class I use).

Is there a way to get the current divs width, and simply subtract 1 for those divs that have a class of right-border? I'm thinking this is likely only doable with jQuery, but perhaps theres some CSS magic I don't know about that will accomplish this.

Any ideas?

1
  • @AtalShrivastava That feels too imprecise to me. 99% width is variable depending on the div width. Commented Mar 22, 2014 at 18:19

1 Answer 1

6

You can try border-boxes (if your grid systems doesn't already) so that the width of the element will include the border:

[class*="grid_"]{box-sizing:border-box}

So that adding a border (or padding) won't affect the computed width of the element -- instead the content area shrinks appropriately.

Sign up to request clarification or add additional context in comments.

3 Comments

Don't forget to add vendor prefixes (-moz, -ms, -o etc).
Perfect! I knew someone would have some magic that I had not yet learned. Thx.
You should remove the leading period from .grid_ within your attribute selector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.