Borders and Backgrounds
- Border properties (width, style, color, radius)
- Background properties (color, image, repeat, position, size)
Borders and Backgrounds
Section titled “Borders and Backgrounds”Border Properties
Section titled “Border Properties”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.
Border Width
Section titled “Border Width”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;}
Border Style
Section titled “Border Style”The border-style
property defines the style of the border. Common values include:
none
: No bordersolid
: A solid linedotted
: A series of dotsdashed
: A series of dashesdouble
: Two solid linesgroove
: A 3D grooved borderridge
: A 3D ridged borderinset
: A 3D inset borderoutset
: A 3D outset border
Examples:
.border-example { border-style: solid;}
.border-example { border-style: dashed;}
Border Color
Section titled “Border Color”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 */}
Border Radius
Section titled “Border Radius”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;}
Border Shorthand
Section titled “Border Shorthand”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
Section titled “Background Properties”Background properties in CSS are used to control the background color, image, repeat behavior, position, and size of an element.
Background Color
Section titled “Background Color”The background-color
property sets the background color of an element.
Example:
.background-example { background-color: lightblue;}
Background Image
Section titled “Background Image”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");}
Background Repeat
Section titled “Background Repeat”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;}
Background Position
Section titled “Background Position”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%;}
Background Size
Section titled “Background Size”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;}
Background Shorthand
Section titled “Background Shorthand”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;}
Examples
Section titled “Examples”Example: Border Properties
Section titled “Example: Border Properties”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;}
Example: Background Properties
Section titled “Example: Background Properties”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.