CSS uses color values to specify a color.
You can specify your color values in various formats. The following table lists all the possible formats −
Format | Syntax | Example |
Hex Code | #RRGGBB | p{color:#FF0000;} |
Short Hex Code | #RGB | p{color:#6A7;} |
RGB % | rgb(rrr%,ggg%,bbb%) | p{color:rgb(50%,50%,50%);} |
RGB Absolute | rgb(rrr,ggg,bbb) | p{color:rgb(0,0,255);} |
keyword | aqua, black, etc. | p{color:teal;} |
The background-color
property sets the background color of an element.
The background of an element is the total size of the element, including padding and border (but not the margin).
<html>
<head>
<style>
body {
background-color: coral;
}
</style>
</head>
<body>
<h1>The background-color Property</h1>
<p>The background color can be specified with a color name.</p>
</body>
</html>
The border-style
property sets the style of an element’s four borders. This property can have from one to four values.
Examples:
Example:
<html>
<head>
<style>
h1 {
border-style: dotted;
}
div {
border-style: dotted;
}
</style>
</head>
<body>
<h1>A Heading with a dotted border</h1>
<div>A div element with a dotted border.</div>
</body>
</html>
The CSS margin
properties are used to create space around elements, outside of any defined borders.
With CSS, you have full control over the margins. There are properties for setting the margin for each side of an element (top, right, bottom, and left).
CSS has properties for specifying the margin for each side of an element:
margin-top
margin-right
margin-bottom
margin-left
All the margin properties can have the following values:
<html>
<head>
<style>
div {
border: 1px solid black;
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
background-color: lightblue;
}
</style>
</head>
<body>
<h2>Using individual margin properties</h2>
<div>This div element has a top margin of 100px, a right margin of 150px, a bottom margin of 100px, and a left margin of 80px.</div>
</body>
</html>
The CSS height
and width
properties are used to set the height and width of an element.
The CSS max-width
property is used to set the maximum width of an element.
The height
and width
properties are used to set the height and width of an element.
The height and width properties do not include padding, borders, or margins. It sets the height/width of the area inside the padding, border, and margin of the element.
The height
and width
properties may have the following values:
auto
– This is default. The browser calculates the height and widthlength
– Defines the height/width in px, cm, etc.%
– Defines the height/width in percent of the containing blockinitial
– Sets the height/width to its default valueinherit
– The height/width will be inherited from its parent valueSet the height and width of a <div> element:
div {
height: 200px;
width: 50%;
background-color: powderblue;
An outline is a line that is drawn around elements, OUTSIDE the borders, to make the element “stand out”.
CSS has the following outline properties:
outline-style
outline-color
outline-width
outline-offset
outline
Note: Outline differs from borders! Unlike border, the outline is drawn outside the element’s border, and may overlap other content. Also, the outline is NOT a part of the element’s dimensions; the element’s total width and height is not affected by the width of the outline.
<html>
<head>
<style>
p {
border: 2px solid black;
outline: #4CAF50 solid 10px;
margin: auto;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<h2>CSS Outline</h2>
<p>This element has a 2px black border and a green outline with a width of 10px.</p>
</body>
</html>
In CSS, we use the font-family
property to specify the font of a text.
Note: If the font name is more than one word, it must be in quotation marks, like: “Times New Roman”.
<html>
<head>
<style>
.p1 {
font-family: “Times New Roman”, Times, serif;
}
.p2 {
font-family: Arial, Helvetica, sans-serif;
}
.p3 {
font-family: “Lucida Console”, “Courier New”, monospace;
}
</style>
</head>
<body>
<h1>CSS font-family</h1>
<p class=”p1″>This is a paragraph, shown in the Times New Roman font.</p>
<p class=”p2″>This is a paragraph, shown in the Arial font.</p>
<p class=”p3″>This is a paragraph, shown in the Lucida Console font.</p>
</body>
</html>
The float
property is used for positioning and formatting content e.g. let an image float left to the text in a container.
The float
property can have one of the following values:
left
– The element floats to the left of its containerright
– The element floats to the right of its containernone
– The element does not float (will be displayed just where it occurs in the text). This is defaultinherit
– The element inherits the float value of its parentIn its simplest use, the float
property can be used to wrap text around images.
The following example specifies that an image should float to the right in a text: