- Background image
- Border color
- Border radius
- Border width
- Borders
- Class attribute
- Color
- Comment
- First-child selector
- Font family
- Font size
- Font style
- Font weight
- Height
- Linking a style sheet
- Margin
- N-th-child selector
- Overflow property
- Padding
- Pixels
- Position property
- Rounding an image
- Selectors
- Text align
- Transition property
- Width
CSS
CSS border: The Shorthand for Borders in CSS
The CSS border
shorthand property specifies the borders of an HTML element. As a shorthand, border
can set the values of the CSS properties border-width
, border-style
, and border-color
at once.
How to Use CSS Border
You can use the border
shorthand property to specify width, style, and color with a single property. Also, you can set these properties individually using border-width
, border-style
, and border-color
.
/* Shorthand for setting all border properties */
.element { border: 2px solid black; }
/* Individually setting border properties */
.element {
border-width: 2px;
border-style: solid;
border-color: black;
}
/* Applying a border to a specific side */
.element { border-bottom: 2px dotted red; }
When to Use CSS Borders
CSS borders are essential for defining the outlines of elements, enhancing the design and layout of web pages.
Visual Separation
You can use borders to visually separate different sections of content, making layouts clearer and more digestible.
.section { border: 1px solid #ccc; }
Emphasis on Elements
Borders can highlight buttons, quotes, or significant blocks within content, drawing attention to these areas.
.button { border: 2px solid blue; padding: 5px 10px; }
.quote { border: 3px solid green; margin: 20px; padding-left: 15px; }
Styling Forms
Borders enhance form inputs and text areas, improving usability and aesthetics, making forms more interactive and appealing.
input[type="text"], textarea {
border: 1px solid #888;
padding: 8px;
}
input[type="text"]:focus, textarea:focus {
border-color: blue;
}
Examples of Borders Using CSS
Navigation Menu
A navigation menu of a website might use distinctive borders to separate it from other page content effectively.
.nav { border-bottom: 4px solid #333; }
.nav-item { border: 1px solid #666; padding: 10px; }
.nav-item:last-child { border-right: none; }
Card Components
E-commerce websites often create card components with borders for product listings, user profiles, or information snippets.
.card {
border: 1px solid #ddd;
border-radius: 8px;
box-shadow: 2px 2px 5px #ccc;
padding: 20px;
margin: 10px;
}
Decorative Borders
Borders can also serve decorative purposes, such as photo frames or as separators with aesthetic appeal.
.photo {
border: 5px solid #222;
padding: 5px;
display: inline-block;
}
.decorative-separator {
border-top: 1px dashed #999;
margin: 20px 0;
}
Learn More About CSS Borders
Individual CSS Border Properties: Top, Right, Bottom, and Left
You can also set borders for each side of an element using border-top
, border-right
, border-bottom
, and border-left
. Using individual border properties, you create elements with asymmetric borders.
/* Individual border properties */
.box {
border-top: 2px solid red;
border-right: 3px dotted green;
border-bottom: 4px dashed blue;
border-left: 5px double yellow;
}
Border Styles in CSS
You can use a variety of advanced CSS border styles including dashed
, dotted
, double
, groove
, ridge
, inset
, and outset
. Styling Borders in CSS helps you create unique visual effects and enhance the design of your elements.
For example, dashed borders create a broken line effect, which can be useful for highlighting sections without making them too bold. Dotted borders use small dots to form a border, offering a subtle way to outline elements. Double borders consist of two solid lines, creating a more prominent and decorative look.
.dashed-border {
border: 2px dashed #000;
}
.dotted-border {
border: 2px dotted #000;
}
.double-border {
border: 3px double #000;
}
CSS Border Color
You can use the border-color
property to specify the border color of an HTML element. The border-color
property works with color names, hex codes, RGB, RGBA, HSL, and HSLA. You can specify a single color to apply for all sides or different colors for each side.
.element {
border-color: blue; /* Single color for all sides */
/* Individual colors for each side */
border-color: red green blue purple;
}
CSS Border Radius for Rounded Borders
The border-radius
property allows you to create rounded borders in CSS, giving elements an approachable and friendly look. You can define border-radius
using pixels (px
), percentages (%
), ems (em
), and rems (rem
).
.rounded-corners { border-radius: 15px; }
border-radius
in CSS allows you to specify different radii for each corner of an element, providing precise control over the shape. A single value applies the same radius to all four corners:
.card {
border-radius: 10px;
}
Two values set the radius for top-left/bottom-right and top-right/bottom-left corners:
.container {
border-radius: 15px 30px;
}
Three values set the radius for top-left, top-right/bottom-right, and bottom-left corners:
.modal {
border-radius: 10px 20px 30px;
}
Four values specify each corner in clockwise order:
.profile {
border-radius: 5px 15px 25px 10px;
}
CSS Border Images
With the border-image
shorthand property, you can use images as borders. The border-image
property allows you to define the source image, the slice values, and the repeat behavior at once.
.image-border {
border: 8px solid transparent;
border-image: url(border.png) 30 round;
}
However, you can also set these properties individually using border-image-slice
, border-image-source
, and border-image-repeat
.
The border-image-slice
property controls how to slice the image into regions. You can specify up to four values (for top, right, bottom, and left), or use a single value for all sides.
.image-border {
border-width: 10px;
border-image-slice: 10;
border-image-source: url(border.png);
border-image-repeat: round;
}
The border-image-repeat
property controls how the image is repeats along the border. It accepts values like stretch
, repeat
, and round
. Using repeat
, you can ensure that the image fills the border area, while stretch
stretches the image to fit.
.image-border {
border-width: 10px;
border-image-slice: 10;
border-image-source: url(border.png);
border-image-repeat: repeat;
}
Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.