Borders in CSS
CSS Borders
CSS
Border Properties
The
CSS
border
properties allow us to specify the style,
width, and color of an element's border.
CSS Border Style
The
border-style
property specifies what kind of border to display.
The following values are allowed:
dotted
- Defines a dotted borderdashed
- Defines a dashed bordersolid
- Defines a solid borderdouble
- Defines a double bordergroove
- Defines a 3D grooved border. The effect depends on the border-color valueridge
- Defines a 3D ridged border. The effect depends on the border-color valueinset
- Defines a 3D inset border. The effect depends on the border-color valueoutset
- Defines a 3D outset border. The effect depends on the border-color valuenone
- Defines no borderhidden
- Defines a hidden border
The
border-style
property can have from one to four values (for the top
border, right border, bottom border, and the left border).
Example
Demonstration of the different border styles:
p.dotted {border-style: dotted;}p.dashed {border-style: dashed;}p.solid {border-style: solid;}p.double {border-style: double;}p.groove {border-style: groove;}p.ridge {border-style: ridge;}p.inset {border-style: inset;}p.outset {border-style: outset;}p.none {border-style: none;}p.hidden {border-style: hidden;}p.mix {border-style: dotted dashed
solid double;}
Note: None of the
OTHER CSS border properties have ANY effect unless the
border-style
property is set!
CSS Border Width
The
border-width
property specifies the width of the four borders.
The width can be set as a specific size (in px, pt,
cm, em, etc) or by using one of the three pre-defined values: thin, medium, or
thick:
Example
Demonstration of the different border widths:
p.one {
border-style: solid;
border-width: 5px;
}
p.two {
border-style: solid;
border-width: medium;
}
p.three {
border-style: dotted;
border-width: 2px;
}
p.four {
border-style: dotted;
border-width: thick;
}
border-style: solid;
border-width: 5px;
}
p.two {
border-style: solid;
border-width: medium;
}
p.three {
border-style: dotted;
border-width: 2px;
}
p.four {
border-style: dotted;
border-width: thick;
}
Specific Side Widths
The
border-width
property can have from one to four values (for the top
border, right border, bottom border, and the left border):
Example
p.one {
border-style: solid;
border-width: 5px 20px; /* 5px top and bottom, 20px on the sides */
}
p.two {
border-style: solid;
border-width: 20px 5px; /* 20px top and bottom, 5px on the sides */
}
p.three {
border-style: solid;
border-width: 25px 10px 4px 35px; /* 25px top, 10px right, 4px bottom and 35px left
}
border-style: solid;
border-width: 5px 20px; /* 5px top and bottom, 20px on the sides */
}
p.two {
border-style: solid;
border-width: 20px 5px; /* 20px top and bottom, 5px on the sides */
}
p.three {
border-style: solid;
border-width: 25px 10px 4px 35px; /* 25px top, 10px right, 4px bottom and 35px left
}
CSS Border Color
The
border-color
property is used to set the color of the four borders.
The color can be set by:
- name - specify a color name, like
"red"
- HEX - specify a HEX value, like
"#ff0000"
- RGB - specify a RGB value, like
"rgb(255,0,0)"
- HSL - specify a HSL value, like
"hsl(0, 100%, 50%)"
- transparent
Note: If
border-color
is not set, it inherits the color of the element.
Example
Demonstration of the different border colors:
p.one {
border-style: solid;
border-color: red;
}
p.two {
border-style: solid;
border-color: green;
}
p.three {
border-style: dotted;
border-color: blue;
}
border-style: solid;
border-color: red;
}
p.two {
border-style: solid;
border-color: green;
}
p.three {
border-style: dotted;
border-color: blue;
}
CSS Border Color
The
border-color
property is used to set the color of the four borders.
The color can be set by:
- name - specify a color name, like "red"
- HEX - specify a HEX value, like
"#ff0000"
- RGB - specify a RGB value, like
"rgb(255,0,0)"
- HSL - specify a HSL value, like
"hsl(0, 100%, 50%)"
- transparent
Note: If
border-color
is not set, it inherits the color of the element.
Example
Demonstration of the different border colors:
p.one {
border-style: solid;
border-color: red;
}
p.two {
border-style: solid;
border-color: green;
}
p.three {
border-style: dotted;
border-color: blue;
}
border-style: solid;
border-color: red;
}
p.two {
border-style: solid;
border-color: green;
}
p.three {
border-style: dotted;
border-color: blue;
}
CSS Shorthand Border Property
To shorten the code, it is also possible to specify
all the individual border properties in one property.
The
border
property is a shorthand property for the following
individual border properties:border-width
border-style
(required)border-color
Example
p {
border: 5px solid red;
}
border: 5px solid red;
}
You can also specify all the individual border
properties for just one side:
p {
border-left: 6px solid red;
background-color: lightgrey;
}
border-left: 6px solid red;
background-color: lightgrey;
}
p {
border-bottom: 6px solid red;
background-color: lightgrey;
}
border-bottom: 6px solid red;
background-color: lightgrey;
}
CSS Rounded Borders
The
border-radius
property is used to add rounded borders to an element:
p {
border: 2px solid red;
border-radius: 5px;
}
border: 2px solid red;
border-radius: 5px;
}
Comments
Post a Comment