CSS Notes
CSS Notes
What is CSS?
Cascading Style Sheets, fondly referred to as CSS, is a simple design language
intended to simplify the process of making web pages presentable.
CSS handles the look and feel part of a web page. Using CSS, you can control
the color of the text, the style of fonts, the spacing between paragraphs, how
columns are sized and laid out, what background images or colors are used, as
well as a variety of other effects.
CSS is easy to learn and understand but it provides a powerful control over the
presentation of an HTML document. Most commonly, CSS is combined with the
markup languages HTML or XHTML.
Advantages of CSS
1. CSS saves time - You can write CSS once and then reuse the same sheet
in multiple HTML pages. You can define a style for each HTML element
and apply it to as many web pages as you want.
2. Pages load faster - If you are using CSS, you do not need to write HTML
tag attributes every time. Just write one CSS rule of a tag and apply it to all
the occurrences of that tag. So, less code means faster download times.
3. Easy maintenance - To make a global change, simply change the style,
and all the elements in all the web pages will be updated automatically.
4. Superior styles to HTML - CSS has a much wider array of attributes than
HTML, so you can give a far better look to your HTML page in comparison
to HTML attributes.
5. Multiple Device Compatibility - Style sheets allow content to be
optimized for more than one type of device. By using the same HTML
document, different versions of a website can be presented for handheld
devices such as PDAs and cellphones or for printing.
6. Global web standards – Now HTML attributes are being deprecated and
it is being recommended to use CSS. So it’s a good idea to start using CSS
in all the HTML pages to make them compatible with future browsers.
CSS Syntax
CSS Notes Santosh Kumar
A CSS comprises of style rules that are interpreted by the browser and then
applied to the corresponding elements in your document. A style rule is made
of three parts:
✓ Selector: A selector is an HTML tag at which a style will be applied.
This could be any tag like <h1> or <table> etc.
✓ Property: A property is a type of attribute of HTML tag. Put simply,
all the HTML attributes are converted into CSS properties. They could
be color, border, etc.
✓ Value: Values are assigned to properties. For example, color property
can have the value either red or #F1F1F1 etc.
You can put CSS Style Rule Syntax as follows:
selector { property : value }
CSS Selector
Selectors are the names given to styles in internal and external style sheets. The
style rules associated with that selector will be applied to the elements that match
the selector pattern.
Selectors are one of the most important aspects of CSS as they allow you to target
specific elements on your web page in various ways so that they can be styled.
Types of selectors:
1. Universal Selector
2. Element Type Selectors
3. Id Selectors
4. Class Selectors
5. Descendant Selectors
6. Child Selectors
7. Grouping Selectors
1) Universal Selector
Universal selector, denoted by an asterisk (*), matches every single
element on the page.
CSS Notes Santosh Kumar
Example:
*{
margin: 0;
padding: 0;
}
Example:
p{
color: blue;
}
The style rules inside the p selector will be applied on every <p> element
(or paragraph) in the document and color it blue, regardless of their position
in the document tree.
3) Id Selectors
The id selector is used to define style rules for a single or unique element.
The id selector is defined with a hash sign (#) immediately followed by the
id value.
Example:
#mystyle {
color: red;
}
This style rule renders the text of an element in red, whose id attribute is
set to mystyle.
CSS Notes Santosh Kumar
4) Class Selectors
The class selectors can be used to select any HTML element that has a class
attribute. All the elements having that class will be formatted according to
the defined rule.
The class selector is defined with a period sign (.) immediately followed
by the class value.
Example:
.blue {
color: blue;
}
The above style rules renders the text in blue of every element in the
document that has class attribute set to blue.
You can make it a bit more particular. For example:
p.blue {
color: blue;
}
The style rule inside the selector p.blue renders the text in blue of only
those <p> elements that has class attribute set to blue, and has no effect on
other paragraphs.
5) Descendant Selectors
You can use these selectors when you need to select an element that is the
descendant of another element.
For example, if you want to target only those anchors that are contained
within an unordered list, rather than targeting all anchor elements.
Example:
ul li a {
text-decoration : none;
color : green;
}
6) Child Selectors
A child selector is used to select only those elements that are the direct
children of some element.
CSS Notes Santosh Kumar
7) Grouping Selectors
Often several selectors in a style sheet share the same style rules declarations.
You can group them into a comma-separated list to minimize the code in your
style sheet. It also prevents you from repeating the same style rules over and
over again.
h1 { h1, h2, h3 {
font-size: 36px; font-weight: normal;
font-weight: normal; }
} h1 {
h2 { font-size: 36px;
font-size: 28px; }
font-weight: normal; h2 {
} font-size: 28px;
h3 { }
font-size: 22px; h3 {
font-weight: normal; font-size: 22px;
} }
CSS Notes Santosh Kumar
Applying CSS
There are three ways to associate styles with your HTML document.
1) In-line CSS
2) Internal CSS
3) External CSS
In-line CSS
In-line styles are plonked straight into the HTML tags using the style attribute.
Syntax:
<element style=” …..style rules….. “>
Example
<p style=“color : red”>This is my CSS Tag</p>
This will show the text of paragraph in red.
Internal CSS
Embedded, or internal styles are used for the whole page. Inside the head tags,
the style tags surround all of the styles for the page.
Syntax:
<style type="text/css" media="...">
Style Rules
............
</style>
Attributes associated with <style> elements are:
Attribute Value Description
Type Text/css Specifies the style sheet language as a content-
type (MIME type). This is a required attribute.
media screen Specifies the device, the document will be
tv displayed
print on. Default value is all. This is an optional
all attribute.
CSS Notes Santosh Kumar
External CSS
External styles are used for the whole, multiple-page website.
An external style sheet is a separate text file with .css extension. You define all
the Style rules within this text file and then you can include this file in any HTML
document using <link> element.
CSS Comments
Many times, you may need to put additional comments in your style sheet blocks.
So, it is very easy to comment any part in the style sheet. You can simply put
your comments inside /*.....this is a comment in style sheet.....*/.
Example:
/* This is an external style sheet file */
h1, h2, h3 {
color: #36C;
font-weight: normal;
text-transform: lowercase;
}
/* end of style rules. */
CSS Notes Santosh Kumar
CSS Properties
none, capitalize,
text-transform Controls the letters in an element
uppercase, lowercase
xx-small, x-small,
set the size of font for the text content of an
font-size small, medium, large, x-
element.
large, xx-large or px
border-color specifies the color of the border area Color-name, Hax, RGB
padding-left specifies the padding from left side only auto, length, percentage
padding-right specifies the padding from right side only auto, length, percentage
padding-top specifies the padding from top side only auto, length, percentage
padding- specifies the padding from bottom side
auto, length, percentage
bottom only
margin-left specifies the margin on left side only auto, length, percentage
margin-right specifies the margin on right side only auto, length, percentage
margin-top specifies the margin on top side only auto, length, percentage