Skip to content

Borders and Backgrounds

Borders in CSS are used to define the edges of an element’s box. Borders can be customized in terms of width, style, color, and radius.

The border-width property specifies the width of the border. It can be set using specific values (in pixels, ems, etc.) or predefined keywords (thin, medium, thick).

Examples:

/* Specific values */
.border-example {
border-width: 2px;
}
/* Predefined keywords */
.border-example {
border-width: thin;
}

The border-style property defines the style of the border. Common values include:

  1. none - No border
  2. solid - A solid line
  3. dotted - A series of dots
  4. dashed - A series of dashes
  5. double - Two solid lines
  6. groove - A 3D grooved border
  7. ridge - A 3D ridged border
  8. inset - A 3D inset border
  9. outset - A 3D outset border
.border-example {
border-style: solid;
}
.border-example {
border-style: dashed;
}

The border-color property sets the color of the border. It can accept color names, hexadecimal values, RGB, RGBA, HSL, or HSLA values.

.border-example {
border-color: #ff5733; /* Hexadecimal */
}
.border-example {
border-color: rgb(255, 87, 51); /* RGB */
}

The border-radius property allows you to round the corners of an element’s border. It can be set for all corners or individually for each corner.

/* All corners */
.border-example {
border-radius: 10px;
}
/* Individual corners */
.border-example {
border-top-left-radius: 10px;
border-top-right-radius: 20px;
border-bottom-right-radius: 30px;
border-bottom-left-radius: 40px;
}

The border shorthand property can be used to set the width, style, and color of the border in a single declaration.

{
border: width style color;
}
.border-example {
border: 2px solid #ff5733;
}

Background properties in CSS are used to control the background color, image, repeat behavior, position, and size of an element.

The background-color property sets the background color of an element.

Example:

.background-example {
background-color: lightblue;
}

The background-image property sets an image as the background of an element. It uses the url() function to specify the image path.

Example:

.background-example {
background-image: url("path/to/image.jpg");
}

The background-repeat property specifies how a background image is repeated.

  1. repeat - Repeats the image both horizontally and vertically (default).
  2. repeat-x - Repeats the image only horizontally.
  3. repeat-y - Repeats the image only vertically.
  4. no-repeat - Does not repeat the image.

Example:

.background-example {
background-image: url("path/to/image.jpg");
background-repeat: no-repeat;
}

The background-position property specifies the initial position of the background image. It can be set using keywords (e.g., top, right, bottom, left, center) or specific values (e.g., px, %).

Examples:

/* Keywords */
.background-example {
background-image: url("path/to/image.jpg");
background-position: center;
}
/* Specific values */
.background-example {
background-image: url("path/to/image.jpg");
background-position: 50% 50%;
}

The background-size property specifies the size of the background image.

  • auto: Default. The background image is displayed in its original size.
  • cover: Scales the background image to cover the entire element.
  • contain: Scales the background image to be contained within the element.
  • Specific values (e.g., px, %): Sets the width and height of the background image.
/* Cover */
.background-example {
background-image: url("path/to/image.jpg");
background-size: cover;
}
/* Contain */
.background-example {
background-image: url("path/to/image.jpg");
background-size: contain;
}
/* Specific values */
.background-example {
background-image: url("path/to/image.jpg");
background-size: 100px 50px;
}

The background shorthand property allows you to set multiple background properties in a single declaration.

{
background: <background-image> <background-repeat> <background-position> <background-size>;
}
.background-example {
background: url("path/to/image.jpg") no-repeat center/cover;
}
/*
Expanded values of
background-image: url("path/to/image.jpg");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
*/

If we want all the below values as shorthand

.element {
background-image: url("path/to/image.jpg");
background-position: center;
background-size: cover;
background-repeat: no-repeat;
background-origin: padding-box;
background-clip: border-box;
background-attachment: scroll;
background-color: transparent;
}

it can be written like this.

.element {
background: url("path/to/image.jpg") center/cover no-repeat padding-box
border-box scroll transparent;
}