i

Learn HTML and CSS in 10 Days

Borders, Individual Border Sides, Border Radius, Box Sizing, Content Box and Padding Box

Borders

Borders are found in between the margin and padding. It gives an outline all around the element. Three values are needed by the border property i.e., style, color, and width. For the 'border' property, the shorthand values are written in the order as width, style, and color. For the longhand one, all 3 of them can be broken up as

'border-width,' 'border-style,' and 'border-color' properties. In CSS, there are different appearances available for the border, but the most commonly used are double, dotted, solid, dashed, and none.

Given below is the code for 10 pixels wide, dotted, grey border.

div {

  border: 10px dotted #949599;

}

Individual Border Sides

Like the padding and the margin property, borders can also be placed on one side of the element. For that, we need new properties like border-top, border-bottom, border-right, and border-left. The values are the same as that required alone for width, style, and color. If you want to apply the border at the top of an element, then follow the below example.

div {

border-bottom: 6px, solid #949599;

}

Borders can be controlled at a much finer level. Let's say if we want to change the width of the border at the bottom, then we can go with the below code.

div {

border-bottom-width: 12px;

}

Border Radius

The main functionality of border-radius is to give round corners to an element. The border-radius accepts length in pixels as well as in percentage. One value to this property will round all the corners of the element. Two values will round the top-right/bottom-left and top-left/bottom-right corners. Four values will round top-right, top-left, bottom-right, and bottom-left corners.

div {

border-radius: 5px;

}

Box Sizing

From the above discussion, we already know that the box model is an additive design. If you have the width of the element as 400 pixels, then you are providing 20-pixel padding and 10 pixels of border on all sides, the actual width of the element sums up to 460 pixels.

To support several calculations, the box model may be changed. CSS3 has provided us with the box-sizing property to help us with that. The property takes in 3 important values i.e., content-box, padding-box, and border-box. Let's see each one of them one by one.

Content Box

The content-box value serves as a default value if we are not using the box-sizing property.

div {

  -webkit-box-sizing: content-box;

     -moz-box-sizing: content-box;

          box-sizing: content-box;

}

Padding Box

The value as padding-box in box-sizing property changes the box model by including any property value of padding within the height and width of an element. Using this, if an element consists of a width of 400 pixels as well as padding of 20 pixels, the net width will be 400 pixels.

div {

box-sizing: padding-box;

}

Border Box

The border-box value helps in changing the box model such that any padding or border property values are included within the height and the width of an element. If the width of an element is 400 pixels, a border of 10 pixels, and a padding of 20 pixels around every side, then the net width will be as it is i.e., 400 pixels.

div {

  box-sizing: border-box;

}