Professional Documents
Culture Documents
Cascading Style Sheet
Cascading Style Sheet
Cascading Style Sheet
1. INTRODUCTION TO CSS
CSS stands for Cascading Style Sheets
CSS describes how HTML elements are to be displayed on screen, paper, or in other media
CSS saves a lot of work. It can control the layout of multiple web pages all at once
HTML, CSS and JavaScript are used for web designing. It helps the web designers to apply style on HTML
tags.
WHAT DOES CSS DO?
They define which HTML elements you are going to be manipulating with CSS
code.
The selector name creates a direct relationship with the HTML tag you want to edit.
If you wanted to change the way a paragraph tag behaved, the CSS code would look
like:
p { PROPERTY: VALUE }
The above example is a template that you can use whenever you are manipulating
the paragraph HTML element.
3.1 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.
There are several different types of selectors in CSS.
CSS Element Selector
CSS Id Selector
CSS Class Selector
CSS Universal Selector
CSS Group Selector
3.1.1CSS ELEMENT SELECTOR
The element selector selects the HTML element by name.
<!DOCTYPE html>
<html> <head>
<style>
p{ text-align: center; color: blue; }
</style>
</head>
<body>
<p>This style will be applied on every paragraph.</p>
<p>Me too!</p>
<p>And me!</p>
</body> </html>
3.1.2.1 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.
<!DOCTYPE html>
<html> <head> <style>
#para1 {
text-align: center;
color: blue;
}
</style> </head> <body>
<p id="para1">Hello Javatpoint.com</p>
<p>This paragraph will not be affected.</p>
</body> </html>
3.1.3.2 CSS CLASS SELECTOR
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.
<!DOCTYPE html>
<html> <head> <style>
.center {
text-align: center;
color: blue;
}
</style> </head> <body>
<h1 class="center">This heading is blue and center-aligned.</h1>
<p class="center">This paragraph is blue and center-aligned.</p>
</body> </html>
3.1.3 CSS CLASS SELECTOR FOR SPECIFIC
ELEMENT
If you want to specify that only one specific HTML element should be affected then you
should use the element name with class selector.
<!DOCTYPE html>
<html> <head> <style>
p.center {
text-align: center;
color: blue; }
</style> </head> <body>
<h1 class="center">This heading is not affected</h1>
<p class="center">This paragraph is blue and center-aligned.</p>
</body> </html>
3.1.4 CSS UNIVERSAL SELECTOR
The universal selector is used as a wildcard character. It selects all the elements on the pages.
<!DOCTYPE html>
<html> <head> <style>
* {
color: green;
font-size: 20px;
}
</style> </head> <body>
<h2>This is heading</h2>
<p>This style will be applied on every paragraph.</p>
<p>And me!</p>
</body> </html>
3.1.5 CSS GROUP SELECTOR
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.
<!DOCTYPE html>
<html> <head> <style>
h1, h2, p {
text-align: center;
color: blue; }
</style> </head> <body>
<h1>Hello Javatpoint.com</h1>
<h2>Hello Javatpoint.com (In smaller font)</h2>
<p>This is a paragraph.</p>
</body> </html>
4. CSS RULE / SYNTAX
body {
font-family: Tahoma, Arial, sans-serif;
color: black;
background: white;
margin: 8px;
}
Value
Attribute Name
DIFFERENT STYLE OF CSS
<html>
<head>
<style type="text/css">
p {color: white; }
body {background-color: black; }
</style>
</head>
<body>
<p>Your page's content!</p>
</body>
</html>
Save: “Filename.html”
EXTERNAL CSS
External CSS is a file that contains only CSS code and is saved with a ".css" file
extension.
This CSS file is then referenced in your HTML using the <link> instead of <style>.
EXTERNAL CSS CODE
Save: “test.css”
HTML CODE
<html>
<head>
</head>
<body>
<h3> A White Header </h3>
<p> This paragraph has a blue font. The background color of this page is gray because
we changed it with CSS! </p>
</body>
</html>
ADVANTAGES OF EXTERNAL CSS
It's much easier to reuse your CSS code if you have it in a separate file. Instead of
typing the same CSS code on every web page you have, simply have many pages
refer to a single CSS file with the "link" tag.
You can make drastic changes to your web pages with just a few changes in a single
CSS file.
INLINE CSS
We can apply CSS in a single element by inline CSS technique.
The inline CSS is also a method to insert style sheets in HTML document. This method mitigates
some advantages of style sheets so it is advised to use this method sparingly.
If you want to use inline CSS, you should use the style attribute to the relevant tag.
<!DOCTYPE html>
<html> <body>
<h1 style="color:red;margin-left:40px;">Inline CSS is applied on
this heading.</h1>
<p>This paragraph is not affected.</p>
</body> </html>
DISADVANTAGES OF INLINE CSS
You cannot use quotations within inline CSS. If you use quotations
the browser will interpret this as an end of your style value.
These styles cannot be reused anywhere else.
These styles are tough to be edited because they are not stored at a
single place.
It is not possible to style pseudo-codes and pseudo-classes with
inline CSS.
Inline CSS does not provide browser cache advantages.
CSS BACKGROUND
CSS background properties are used to define the background effects of an element.
background-color
background-image
background-repeat
background-attachment
background-position
CSS BACKGROUND PROPERTIES
Property Description
body {background-color:”red”;}
body {background-image:url('paper.gif');}
body
{
background-image:url('gradient2.png');
background-repeat:repeat-x; //Repeat, no-repeat, repeat-x and repeat-y
}
body { background: white url('bbb.gif');
background-repeat: no-repeat;
background-attachment: fixed; }
body {background: white url('good-morning.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center; } //top, bottom, right and left
CSS TEXT
Property Description
color Sets the color of text
direction Specifies the text direction/writing direction
body {color:blue;}
p {text-indent:50px;}
CSS FONT
Property Description
Font color Specifies the font color for text
font-family Specifies the font family for text
font-size Specifies the font size of text
font-style Specifies the font style for text
Specifies whether or not a text should be displayed in a small-caps
font-variant
font
font-weight Specifies the weight of a font
EXAMPLE
p {font-style:normal;}(italic)
p {font-size:14px;}
h1 { color: red; }
<p style="font-weight:bold;">This font is bold.</p>
<p style="font-weight:bolder;">This font is bolder.</p>