HTML & CSSEasy
What is a border?
A border is the line drawn around an element's padding box, defined by three parts: width, style and color (border: 2px solid #0b1020). Borders take up space in the box model, can be set per side, and border-radius rounds the corners.
Properties
- Shorthand:
border: <width> <style> <color>. A style is required; without it nothing is drawn. - Styles:
solid,dashed,dotted,double,groove,ridge,inset,outset,none,hidden. - Per side:
border-top,border-right,border-bottom,border-left, or logicalborder-inline-start,border-block-end. border-radius:8px,50%(circle on a square), or elliptical40px / 20px.border-image: paints the border with an image or gradient.
Border vs outline vs box-shadow
- Border: part of the box model; changing its width shifts layout.
- Outline: drawn outside the border, takes no space, follows
border-radiusin modern browsers. It is used for focus rings, so never remove it without a replacement. - Box-shadow:
box-shadow: 0 0 0 2px redfakes a border without affecting layout and can be layered.
Practical tips
- Use
currentColorso the border matches the text color. - To avoid a layout jump on hover, reserve the space with
border: 2px solid transparent. - For hairlines between list items, use
border-toponli + li. - Tables:
border-collapse: collapsemerges adjacent borders.
Code example
html
1<style>2 body { font-family: system-ui; display: flex; flex-wrap: wrap; gap: 12px; }3 .b { padding: 12px 16px; }4 .solid { border: 3px solid #0b1020; }5 .dashed { border: 3px dashed #2b4bff; border-radius: 12px; }6 .sides { border-left: 6px solid #e11d48; background: #fef2f2; }7 .pill { border: 2px solid currentColor; border-radius: 999px; color: #059669; }8 .circle { width: 60px; height: 60px; border: 4px double #0b1020; border-radius: 50%; padding: 0; }9 .gradient { border: 4px solid; border-image: linear-gradient(90deg, #2b4bff, #e11d48) 1; }10 /* Reserve space so hover does not shift layout */11 .hover { border: 2px solid transparent; }12 .hover:hover { border-color: #0b1020; }13 button:focus-visible { outline: 3px solid #ffd84d; outline-offset: 2px; }14</style>15<div class="b solid">solid</div>16<div class="b dashed">dashed + radius</div>17<div class="b sides">left accent</div>18<div class="b pill">pill</div>19<div class="b circle"></div>20<div class="b gradient">border-image</div>21<div class="b hover">hover me</div>22<button class="b">Tab to me</button>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.