DEV Community

Cover image for CSS Border
Suprabha
Suprabha

Posted on

CSS Border

CSS border property is used to set the border of an HTML element.

The border property is shorthand for three sub-properties that define the style, color, and width of a border.

Example:

border: 1px solid red;
Enter fullscreen mode Exit fullscreen mode

Syntax:

border: border-width border-style color;
Enter fullscreen mode Exit fullscreen mode

border-width ๐Ÿ‘ป

Sets the thickness of the border. Defaults to medium if absent.

border-width: thin | medium | thick;
Enter fullscreen mode Exit fullscreen mode

OR

border-width: border-top-width border-right-width border-bottom-width border-right-width;
border-width: 0 4px 8px 12px;
Enter fullscreen mode Exit fullscreen mode

border-style ๐Ÿ™…โ€โ™€๏ธ

Sets the style of the border. Defaults to none if absent.

border-style: none | hidden | dotted | dashed | solid | double | groove | ridge | inset | outset
Enter fullscreen mode Exit fullscreen mode

color ๐ŸŽจ

Sets the color of the border. Defaults to currentcolor if absent.

border-color: red;
border-color: border-top-color border-right-color border-bottom-color border-right-color
border-color: red yellow green pink;
Enter fullscreen mode Exit fullscreen mode

CSS Individual Borders

The border sub-properties and property can also be applied to an individual side of a web element.

Syntax:

border-left: green;
border-top: pink;
border-right: blue;
border-bottom: skyblue;
Enter fullscreen mode Exit fullscreen mode

border-inline โœฃ

This maps to a physical border style depending on the element's writing mode, directionality, and text orientation

border-inline-start: 8px solid red;
Enter fullscreen mode Exit fullscreen mode

Output:
border-inline-start

border-inline-end: 8px solid red;
Enter fullscreen mode Exit fullscreen mode

Output:
border-inline-end

border-inline-start-style/border-inline-end-style โœฃ

The border-inline-start-style CSS property defines the style of the logical inline start/end border of an element, which maps to a physical border style depending on the element's writing mode, directionality, and text orientation.

Syntax:

border-inline-start-style: dotted | dashed | groove; 
Enter fullscreen mode Exit fullscreen mode

Example:

border-inline-start-style: dashed;
writing-mode: horizontal-tb;
border-inline-end-style: dotted;
Enter fullscreen mode Exit fullscreen mode

Output:
border-inline-style

border-block โ–€

The border-block-end CSS property is a shorthand property for setting the individual logical block-end border property values in a single place in the style sheet.

Syntax:

border-block-end: border-block-end-width border-block-end-style border-block-end-color;
Enter fullscreen mode Exit fullscreen mode

Example:

border-block-start: 1px solid red;
Enter fullscreen mode Exit fullscreen mode

Output:
border block start

border-block-end: 1px solid red;
Enter fullscreen mode Exit fullscreen mode

Output:
border block end

border-image ๐Ÿ™

The border-image CSS property draws an image around a given element. It replaces the element's regular border.

Syntax:

border-image: source || slice / width / outset || repeat;
Enter fullscreen mode Exit fullscreen mode

border image

  1. border-image-outset
  2. border-image-repeat
  3. border-image-slice
  4. border-image-source
  5. border-image-width

1๏ธโƒฃ border-image-outset

Theย border-image-outsetย CSSย property sets the distance by which an element'sย border imageย is set out from its border box.

The distance of the border image from the element's outside edge. Up to four values may be specified.

The parts of the border image that are rendered outside the element's border box withย border-image-outsetย do not trigger overflow scrollbars and don't capture mouse events.

// this will be inside
border-image-outset: 0;

// this will be outside of div 30px
border-image-outset: 30px;
Enter fullscreen mode Exit fullscreen mode

The border-image-outset property may be specified as one, two, three, or four values.

Each value is a length/number. Negative values are invalid and will cause the border-image-outset declaration to be ignored.

border-image-outset: 1px 3px 4px 10px;
Enter fullscreen mode Exit fullscreen mode

2๏ธโƒฃ border-image-repeat

The border-image-repeat CSS property defines how the edge regions of a source image are adjusted to fit the dimensions of an element's border image.

border-image-repeat: stretch | round | repeat | space;

/* vertical | horizontal */
border-image-repeat: round stretch;
Enter fullscreen mode Exit fullscreen mode

stretch

The source image's edge regions are stretched to fill the gap between each border.

repeat

The source image's edge regions are tiled (repeated) to fill the gap between each border. Tiles may be clipped to achieve the proper fit.

round

The source image's edge regions are tiled (repeated) to fill the gap between each border. Tiles may be stretched to achieve the proper fit.

space

The source image's edge regions are tiled (repeated) to fill the gap between each border. Extra space will be distributed in between tiles to achieve the proper fit.

3๏ธโƒฃ border-image-slice

The border-image-slice CSS property divides the image specified by border-image-source into regions. These regions form the components of an element's border image.

/* All sides */
border-image-slice: 30;

/* vertical | horizontal */
border-image-slice: 10% 30%;

/* top | right | bottom | left */
border-image-slice: 7 12 14 5;
Enter fullscreen mode Exit fullscreen mode

The slicing process creates nine regions in total: four corners, four edges, and a middle region. Four slice lines, set a given distance from their respective sides, control the size of the regions.

  • Zones 1-4 areย corner regions. Each one is used a single time to form the corners of the final border image.
  • Zones 5-8 areย edge regions. These areย repeated, scaled, or otherwise modifiedย in the final border image to match the dimensions of the element.
  • Zone 9 is theย middle region. It is discarded by default, but is used like a background image if the keywordย fillย is set.

The border-image-repeat, border-image-width, and border-image-outset properties determine how these regions are used to form the final border image.

4๏ธโƒฃ border-image-source

The border-image-source CSS property sets the source image used to create an element's border image.

border-image-source: url('/media/examples/border-diamonds.png');

border-image-source: linear-gradient(to top, red, yellow);

border-image-source: repeating-linear-gradient(45deg, transparent, #4d9f0c 20px);
Enter fullscreen mode Exit fullscreen mode

5๏ธโƒฃ border-image-width

The border-image-width CSS property sets the width of an element's border image.

/* top | right | bottom | left */
border-image-width: 5% 2em 10% auto;
Enter fullscreen mode Exit fullscreen mode

Created few border & border-image example, Checkout the link in @codepen:

Reference ๐Ÿง

๐ŸŒŸ Twitter ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป Suprabha.me ๐ŸŒŸ Instagram

Top comments (3)

Collapse
 
adam_cyclones profile image
Adam Crockett ๐ŸŒ€ • Edited

What color is this border?

.classname {
    color: red;
    border: 1px solid;
}
Collapse
 
suprabhasupi profile image
Suprabha

Here, the border color will be red.

Collapse
 
adam_cyclones profile image
Adam Crockett ๐ŸŒ€

It's my favorite border strange thing. Great post!