|
1 | | -<ComingSoon /> |
| 1 | +--- |
| 2 | +title: "The border Property" |
| 3 | +description: "Learn how the CSS border property defines the style, width, and color of the line separating an element's padding and margin." |
| 4 | +keywords: [CSS border, border property, border-style, border-width, border-color, box model, CodeHarborHub] |
| 5 | +tags: [CSS border, border property, border-style, border-width, border-color, box model] |
| 6 | +sidebar_label: border |
| 7 | +--- |
| 8 | + |
| 9 | +The **`border`** property is the structural line that surrounds an element, situated between the `padding` and the `margin`. It is the visual frame of the element. |
| 10 | + |
| 11 | +A border requires three components to be visible: a **width**, a **style**, and a **color**. |
| 12 | + |
| 13 | +<AdsComponent /> |
| 14 | +<br /> |
| 15 | + |
| 16 | +## The `border` Shorthand |
| 17 | + |
| 18 | +The most common and efficient way to define a border is using the `border` shorthand property, which accepts the width, style, and color in any order. |
| 19 | + |
| 20 | +```css title="styles.css" |
| 21 | +selector { |
| 22 | + border: <border-width> <border-style> <border-color>; |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +### Example |
| 27 | + |
| 28 | +```css title="styles.css" |
| 29 | +.highlight-box { |
| 30 | + /* 2px wide, solid line, indigo color */ |
| 31 | + border: 2px solid #5C6BC0; |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +## Longhand Properties |
| 36 | + |
| 37 | +For more granular control, especially when dealing with different styles or colors on specific sides, you can use the longhand properties. |
| 38 | + |
| 39 | +### 1. `border-width` |
| 40 | + |
| 41 | +Sets the thickness of the border. Values can be length units (`px`, `em`, `rem`) or keywords (`thin`, `medium`, `thick`). |
| 42 | + |
| 43 | +| Syntax | Description | |
| 44 | +| :--- | :--- | |
| 45 | +| `border-width: 5px;` | All four sides are $5\text{px}$. | |
| 46 | +| `border-width: 1px 3px;` | Top/Bottom $1\text{px}$, Left/Right $3\text{px}$. | |
| 47 | +| `border-top-width: 10px;` | Only the top border is $10\text{px}$. | |
| 48 | + |
| 49 | +### 2. `border-style` (Mandatory) |
| 50 | + |
| 51 | +This is the most critical property—if the style is omitted or set to `none`, the border will not be displayed, regardless of its width or color. |
| 52 | + |
| 53 | +| Style | Description | |
| 54 | +| :--- | :--- | |
| 55 | +| **`solid`** | A single, continuous line (most common). | |
| 56 | +| **`dotted`** | A series of dots. | |
| 57 | +| **`dashed`** | A series of short lines. | |
| 58 | +| **`double`** | Two parallel solid lines. | |
| 59 | +| **`groove`** | Creates a 3D grooved effect. | |
| 60 | +| **`ridge`** | Creates a 3D raised effect (opposite of groove). | |
| 61 | +| **`none`** | No border (default). | |
| 62 | + |
| 63 | +<AdsComponent /> |
| 64 | +<br /> |
| 65 | + |
| 66 | +### 3. `border-color` |
| 67 | + |
| 68 | +Sets the color of the border, using color names, hex codes, or RGB/HSL values. |
| 69 | + |
| 70 | +```css title="styles.css" |
| 71 | +.special-border { |
| 72 | + border-style: double; |
| 73 | + border-width: 4px; |
| 74 | + border-color: #FFB300; /* Amber */ |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +## Side-Specific Borders |
| 79 | + |
| 80 | +You can define all three properties (width, style, and color) for a single side: |
| 81 | + |
| 82 | +| Property | Description | |
| 83 | +| :--- | :--- | |
| 84 | +| `border-top` | Shorthand for top width, style, and color. | |
| 85 | +| `border-right` | Shorthand for right width, style, and color. | |
| 86 | +| `border-bottom` | Shorthand for bottom width, style, and color. | |
| 87 | +| `border-left` | Shorthand for left width, style, and color. | |
| 88 | + |
| 89 | +```css tiutle="styles.css" |
| 90 | +/* Example: Border only on the left side */ |
| 91 | +.sidebar-note { |
| 92 | + border-left: 6px solid #4CAF50; /* Thick green line */ |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## Shaping Corners with `border-radius` |
| 97 | + |
| 98 | +While technically not part of the Box Model layers (width, padding, margin), the **`border-radius`** property is essential for styling the border and corner shapes. It allows you to round the corners of the element's border. |
| 99 | + |
| 100 | +| Syntax | Description | |
| 101 | +| :--- | :--- | |
| 102 | +| `border-radius: 10px;` | Rounds all four corners equally. | |
| 103 | +| `border-radius: 10px 0;` | Top-Left/Bottom-Right $10\text{px}$, Top-Right/Bottom-Left $0\text{px}$. | |
| 104 | + |
| 105 | +```css title="styles.css" |
| 106 | +.rounded-button { |
| 107 | + border: 1px solid #7986CB; |
| 108 | + border-radius: 8px; /* Smooth corners */ |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Interactive `border` Demo |
| 113 | + |
| 114 | +Use the live editor to experiment with different border styles and colors. Note how the border sits between the inner content/padding and the outer margin. |
| 115 | + |
| 116 | +<CodePenEmbed |
| 117 | + title="Interactive border Demo" |
| 118 | + penId="raeYoyr" |
| 119 | +/> |
0 commit comments