CSS Doc1
CSS Doc1
CSS stands for Cascading Style Sheets. It is a style sheet language which is used to
describe the look and formatting of a document written in markup language. It
provides an additional feature to HTML. It is generally used with HTML to change the
style of web pages and user interfaces.
Before CSS, tags like font, color, background style, element alignments, border and
size had to be repeated on every web page. This was a very long process. For
example: If you are developing a large website where fonts and color information are
added on every single page, it will be become a long and expensive process. CSS was
created to solve this problem. It was a W3C recommendation.
CSS style definitions are saved in external CSS files so it is possible to change the entire
website by changing just one file.
CSS provides more detailed attributes than plain HTML to define the look and feel of
the website.
CSS Syntax
A CSS rule set contains a selector and a declaration block.
1
Types of CSS:
CSS (Cascading Style Sheet) describes the HTML elements which are displayed
on screen, paper, or in other media. It saves a lot of time. It controls the layout of
multiple web pages at one time. It sets the font-size, font-family, color, background
color on the page.
Without using CSS, the website will not look attractive. There are 3 types of CSS which
are below:
o Inline CSS
o Internal/ Embedded CSS
o External CSS
2
1. Internal CSS
The Internal CSS has <style> tag in the <head> section of the HTML document. This
CSS style is an effective way to style single pages. Using the CSS style for multiple web
pages is time-consuming because we require placing the style on each web page.
Example:
<html>
<head>
<style>
body{background-color : #345678}
h1{color : black}
</style>
</head>
<body>
</body>
</html>
3
OUTPUT:
2. External CSS
In external CSS, we link the web pages to the external .css file. It is created by text
editor. The CSS is more efficient method for styling a website. By editing the .css file,
we can change the whole site at once.
Example:
body { background-color:black}
h1{color:white;
text-align:center}
<html>
<head>
</head>
<body>
<h1>
4
THIS IS AN EXAMPLE FOR EXTERNAL CSS
</h1>
</body>
</html>
Save it with .html and execute the program then directly demo1.css color will be
applied for the webpage.
OUTPUT:
3. Inline CSS
Inline CSS is used to style a specific HTML element. Add a style attribute to each
HTML tag without using the selectors. Managing a website may difficult if we use
only inline CSS. However, Inline CSS in HTML is useful in some situations. We have not
access the CSS files or to apply styles to element.
Example:
<html>
<body style="background-color:blue">
5
<h1 style="color:white;text-align:center">
</body>
</html>
Output:
CSS Colors:
The color property in CSS is used to set the color of HTML elements. Typically, this
property is used to set the background color or the font color of an element.
In CSS, we use color values for specifying the color. We can also use this property for
the border-color and other decorative effects.
o RGB format.
o RGBA format.
o Hexadecimal notation.
6
o HSL.
o HSLA.
o Built-in color.
Built-in Color
As its name implies, built-in color means the collection of previously defined colors
that are used by using a name such as red, blue, green, etc.
Syntax
1. color: color-name;
Hexadecimal notation
The black color notation in hexadecimal is #000000, and the white color notation in
hexadecimal is #FFFFFF. Some of the codes in hexadecimal notation are #FF0000,
#00FF00, #0000FF, #FFFF00, and many more.
1. color:#(0-F)(0-F)(0-F)(0-F)(0-F)(0-F);
RGB Format
RGB format is the short form of 'RED GREEN and BLUE' that is used for defining the
color of an HTML element simply by specifying the values of R, G, B that are in the
range of 0 to 255.
The color values in this format are specified by using the rgb() property. This property
allows three values that can either be in percentage or integer (range from 0 to 255).
This property is not supported in all browsers; that's why it is not recommended to use
it.
Syntax
RGBA Format
7
It is almost similar to RGB format except that RGBA contains A (Alpha) that specifies
the element's transparency. The value of alpha is in the range 0.0 to 1.0, in which 0.0 is
for fully transparent, and 1.0 is for not transparent.
Syntax
1. color:rgba(R, G, B, A);
HSL
Hue: It can be defined as the degree on the color wheel from 0 to 360. 0 represents
red, 120 represents green, 240 represents blue.
Saturation: It takes value in percentage in which 100% represents fully saturated, i.e.,
no shades of gray, 50% represent 50% gray, but the color is still visible, and 0%
represents fully unsaturated, i.e., completely gray, and the color is invisible.
Lightness: The lightness of the color can be defined as the light that we want to
provide the color in which 0% represents black (there is no light), 50% represents
neither dark nor light, and 100% represents white (full lightness).
Syntax
1. color:hsl(H, S, L);
HSLA
It is entirely similar to HSL property, except that it contains A (alpha) that specifies the
element's transparency. The value of alpha is in the range 0.0 to 1.0, in
which 0.0 indicates fully transparent, and 1.0 indicates not transparent.
Syntax
1. color:hsla(H, S, L, A);
Example:
8
<html>
<body>
<br>
</br>
<h1 style=Background-color:#569823;>
<h1 style=Background-color:rgb(0,225,105);>
<h1 style=Background-color:rgba(27,45,100,0.2);>
<h1 style=Background-color:hsl(250,50%,40%);>
<h1 style=Background-color:hsla(350,58%,50%,0.2);>
</body>
</html>
9
OUTPUT:
CSS Selector
CSS selectors are used to select the content you want to style. Selectors are the part of
CSS rule set. CSS selectors select HTML elements according to its id, class, type,
attribute etc.
10
2) CSS Id Selector
The id selector selects the id attribute of an HTML element to select a specific element.
An id is always unique within the page so it is chosen to select a single, unique element.
It is written with the hash character (#), followed by the id of the element.
The class selector selects HTML elements with a specific class attribute. It is used with
a period character . (full stop symbol) followed by the class name.
The universal selector is used as a wildcard character. It selects all the elements on the
pages.
The grouping selector is used to select all the elements with the same style definitions.
Grouping selector is used to minimize the code. Commas are used to separate each
selector in grouping.
Example:
<html>
<head>
<br>
</br>
<style>
h1{background-color:green;color:yellow}
#head2{background-color:black;color:white}
.head3{background-color:blue;color:white}
11
*{color:red}
h2,h3{background-color:brown;color:white}
</style>
</head>
<body>
</body>
</html>
OUTPUT:
12
CSS Background Property
CSS background property is used to define the background effects on element. There
are 5 CSS background properties that affects the HTML elements:
1. background-color
2. background-image
3. background-repeat
4. background-attachment
5. background-position
1) CSS background-color
The background-color property is used to specify the background color of the element.
2) CSS background-image
13
3) CSS background-repeat
4) CSS background-attachment
5) CSS background-position
1. center
2. top
3. bottom
4. left
5. right
CSS Border
The CSS border is a shorthand property used to set the border on an element.
The CSS border properties are use to specify the style, color and size of the border of
an element. The CSS border properties are given below
o border-style
o border-color
o border-width
o border-radius
14
1) CSS border-style
The Border style property is used to specify the border type which you want to display
on the web page.
There are some border style values which are used with border-style property to define
a border
Value Description
2) CSS border-width
The border-width property is used to set the border's width. It is set in pixels. You can
also use the one of the three pre-defined values, thin, medium or thick to set the width
of the border.
3) CSS border-color
15
CSS border-radius property
This CSS property sets the rounded borders and provides the rounded corners around
an element, tags, or div. It defines the radius of the corners of an element.
This CSS property includes the properties that are tabulated as follows:
Property Description
Example:
<html>
<head>
<style>
.dotted{border-style:dotted;border-width:10px;border-color:blue}
.dashed{border-style:dashed;border-width:10px;border-color:green}
.solid{border-style:solid;border-width:10px;border-color:red;border-radius:25px}
.double{border-style:double;border-width:10px;border-color:green}
16
.groove{border-style:groove;border-width:10px;border-color:red}
.ridge{border-style:ridge;border-width:10px;border-color:blue}
.inset{border-style:inset;border-width:10px;border-color:pink}
.outset{border-style:outset;border-width:10px;border-color:red}
.short{border-style:double dashed;border-width:10px}
</style>
</head>
<body>
</body>
</html>
17
OUTPUT:
CSS Margin
CSS Margin property is used to define the space around elements. It is completely
transparent and doesn't have any background color. It clears an area around the element.
Top, bottom, left and right margin can be changed independently using separate
properties. You can also change all properties at once by using shorthand margin
property.
Property Description
margin This property is used to set all the properties in one declaration.
18
There are four types to specify the margin property. You can use one of them.
Example:
<html>
<head>
<style>
h1{ border-style:solid;
margin-top:500px;
margin-bottom:50px;
margin-right:400px;
margin-left:400px;}
h2{ border-style:solid;
margin:50px 500px 50px 100px }
</style>
</head>
<body>
<h1>THIS IS MARGIN TOPIC</h1>
<h2>this is a margin topic</h2>
</body>
</html>
19
OUTPUT:
CSS Padding:
CSS Padding property is used to define the space between the element content and the
element border.
It is different from CSS margin in the way that CSS margin defines the space around
elements. CSS padding is affected by the background colors. It clears an area around the
content.
Top, bottom, left and right padding can be changed independently using separate
properties. You can also change all properties at once by using shorthand padding
property.
20
Property Description
Example:
<html>
<head>
<style>
h1{ border-style:solid;
padding-top:50px;
padding-bottom:70px;
padding-right:60px;
padding-left:50px }
h2{ border-style:solid;
padding:50px 25px 90px 100px}
</style>
</head>
<body>
<h1>THIS IS PADDING TOPIC</h1>
<h2>this is padding topic</h2>
21
</body>
</html>
OUTPUT:
CSS Font:
CSS Font property is used to control the look of texts. By the use of CSS font property you can
change the text size, color, style and more. You have already studied how to make text bold
or underlined. Here, you will also know how to resize your font using percentage.
1. CSS Font color: This property is used to change the color of the text. (standalone
attribute)
2. CSS Font family: This property is used to change the face of the font.
3. CSS Font size: This property is used to increase or decrease the size of the font.
4. CSS Font style: This property is used to make the font bold, italic or oblique.
22
5. CSS Font weight: This property is used to increase or decrease the boldness and
lightness of the font.
CSS font color is a standalone attribute in CSS although it seems that it is a part of CSS fonts.
It is used to change the color of the text.
o By a color name
o By hexadecimal value
o By RGB
o By HSL
CSS font size property is used to change the size of the font.
CSS Font style property defines what type of font you want to display. It may be italic,
oblique, or normal.
CSS font weight property defines the weight of the font and specify that how bold a font
is. The possible values of font weight may be normal, bold, bolder, lighter or number (100,
200..... upto 900).
EXAMPLE:
23
<html>
<head>
<style>
font-style:normal;
font-weight:bold;
font-size:40px}
font-style:normal;
font-weight:normal;
font-size:40px}
</style>
</head>
<body>
</body>
</html>
OUTPUT:
24
CSS Links
Link is a connection from one web page to another web pages. CSS property can be used
to style the links in various different ways.
States of Link: Before discussing CSS properties, it is important to know the states of a
link. Links can exist in different states and they can be styled using pseudo classes.
There are four state of links given below:
Example:
<html>
<head>
<style>
a:link{color:blue;text-decoration:none}
a:visited{color:brown}
a:hover{color:green}
a:active{color:red}
</style>
</head>
<body>
<h1>
25
<a href="https://www.pythonlife.in/">PYTHONLIFE</a></h1>
</body>
</html>
OUTPUT:
26