Skip to content

Borders and Backgrounds

  • Border properties (width, style, color, radius)
  • Background properties (color, image, repeat, position, size)

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:

  • none: No border
  • solid: A solid line
  • dotted: A series of dots
  • dashed: A series of dashes
  • double: Two solid lines
  • groove: A 3D grooved border
  • ridge: A 3D ridged border
  • inset: A 3D inset border
  • outset: A 3D outset border

Examples:

.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.

Examples:

.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.

Examples:

/* 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.

Example:

.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.

  • repeat: Repeats the image both horizontally and vertically (default).
  • repeat-x: Repeats the image only horizontally.
  • repeat-y: Repeats the image only vertically.
  • 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.

Examples:

/* 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.

Example:

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

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Border Example</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div class="border-example">This is a bordered box</div>
</body>
</html>

CSS:

.border-example {
border-width: 5px;
border-style: solid;
border-color: #ff5733;
border-radius: 10px;
}

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Background Example</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div class="background-example">This is a background box</div>
</body>
</html>

CSS:

.background-example {
width: 300px;
height: 200px;
background-color: lightblue;
background-image: url("path/to/image.jpg");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}

By understanding and utilizing border and background properties, you can enhance the visual presentation and user experience of your web designs, creating more engaging and aesthetically pleasing layouts.