Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Content Preparation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Harnessing Div Dimensions

You've probably experienced this:

A div fits nicely in your page design.

You change its padding or its border and suddenly the div is no longer the same size.

Changing the padding or border of a div generally won't change the dimensions of the content area. Instead, the div dimensions are adjusted according to the space required for the padding and the borders.

But there is a fix to keep the div from resizing itself. It's a CSS property called box-sizing.

With the box-sizing CSS, the dimensions of the div are maintained and the content area dimensions are adjusted — instead of the other way around. Adjusting padding and borders will maintain the div's dimensions so the div continues to fit nicely in your page design.

Let me demonstrate with a few divs. (The demonstrations have background colors so borderless div edges can be seen.)

Two divs, both 200 pixels wide and 50 pixels high, no box-sizing CSS applied:

No padding or border
Padding and border

The padding in the second div is 10 pixels and the border is 3 pixels wide. Thus, both the horizontal and vertical dimensions of the div are increased by 26 pixels (10x2+3x2). The dimensions are now 226 pixels wide and 66 pixels high.

Here are the same divs except the second div has the box-sizing CSS:

No padding or border
Padding and border

The dimensions of the div remain 200 pixels by 50 pixels even with the 10px padding and 3px border added. The content area became smaller instead of the div becoming larger.

Here's the box-sizing CSS (example follows):

-moz-box-sizing:border-box; 
-webkit-box-sizing:border-box; 
box-sizing:border-box;

Here's the code for an example of using the CSS. The div the example renders is below the code.

<style type="text/css">
.example {
   -moz-box-sizing:border-box; 
   -webkit-box-sizing:border-box; 
   box-sizing:border-box;
   width:300px; 
   height:100px; 
   padding:10px; 
   border:1px solid #666;
   }
</style>

<div class="example">
Content in div.
</div>

Content in div.

The -moz-box-sizing property is for Firefox (2.0-29.0) and the -webkit-box-sizing property is for both Chrome (4.0-10.0) and Safari (3.2-5.1). The latest versions of those browsers recognize box-sizing without the "-moz-" or "-webkit-" prefix, as do IE (8.0+) and Opera (9.5+).

Browser versions that don't recognize the box-sizing CSS generally will resize the div when padding or borders are added or changed.

To be more specific about the box-sizing property, it specifies what area of the div (the box) is included with sizing specifications.

box-sizing:content-box; means to size the content. (This is the default.) Any padding and borders (being outside the content) will be put outside the size specified for the content within the div — resulting in the div dimensions being adjusted and the content dimensions remaining the same.

box-sizing:border-box; means to size the border. Any padding and content (being inside the border) will be put within the size specified for the border of the div — resulting in the div dimensions remaining the same and the content dimensions being adjusted.

With the box-sizing CSS, you now have a choice. You can tell the browser which part of the div the sizing information shall apply to, the content or the border (or the outside edge of the div when no border is specified).

(This article first appeared in Possibilities ezine.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC