CSS Full Notez
CSS Full Notez
CSS Full Notez
Unit 2 :
CSS introduction, <link> and <style> elements, CSS properties, Controlling Fonts, Text
formatting, Text- pseudo classes, Selectors, Links, Backgrounds, lists
CSS
What is CSS?
HTML was created to describe the content of a web page, like:
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
When tags like <font>, and color attributes were added to the HTML 3.2
specification, it started a nightmare for web developers. Development of large
websites, where fonts and color information were added to every single page,
became a long and expensive process. To solve this problem, the World Wide Web
Consortium (W3C) created CSS.
CSS removed the style formatting from the HTML page! The author or
designer of a web page has no way of knowing how it will be accessed. The page may
be viewed on a television screen, a computer screen or a mobile phone, any of which
may show images, text tables or a mixture of them all. HTML does not have the
facilities that are needed to cope with this diversity, but stylesheets provide them.
CSS saves a lot of work. It can control the layout of multiple web pages all at
once. External stylesheets are stored in CSS files. CSS is used to define styles for your
web pages, including the design, layout and variations in display for different devices
and screen sizes. The style definitions are normally saved in external .css files. With
an external stylesheet file, you can change the look of an entire website by changing
just one file!
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.
Page 1 of 35
S - 4 , U - 2 , CSS Notes A_C
Advantages of CSS
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.
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.
Easy maintenance - To make a global change, simply change the style, and all
the elements in all the web pages will be updated automatically.
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.
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 cell phones or for printing.
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 is created and maintained through a group of people within the W3C called the
CSS Working Group. The CSS Working Group creates documents called specifications. When
a specification has been discussed and officially ratified by the W3C members, it becomes a
recommendation.
These ratified specifications are called recommendations because the W3C has no
control over the actual implementation of the language. Independent companies and
organizations create that software.
NOTE: The World Wide Web Consortium or W3C is a group that makes recommendations
about how the Internet works and how it should evolve.
CSS Versions
Cascading Style Sheets level 1 (CSS1) came out of W3C as a recommendation in
December 1996. This version describes the CSS language as well as a simple visual
formatting model for all the HTML tags.
Page 2 of 35
S - 4 , U - 2 , CSS Notes A_C
CSS2 became a W3C recommendation in May 1998 and builds on CSS1. This version
adds support for media-specific style sheets e.g. printers and aural devices, downloadable
fonts, element positioning and tables.
CSS Syntax
The selector points to the HTML element you want to style. The declaration block
contains one or more declarations separated by semicolons. Each declaration includes a CSS
property name and a value, separated by a colon. A CSS declaration always ends with a
semicolon, and declaration blocks are surrounded by curly braces.
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.
Here, table is a selector and border is a property and the given value 1px solid #C00 is
the value of that property.
Page 3 of 35
S - 4 , U - 2 , CSS Notes A_C
The most common way to add CSS, is to keep the styles in separate CSS files & Most
commonly used methods are inline CSS and External CSS.
An inline CSS is used to apply a unique style to a single HTML element. You can use
style attribute of any HTML element to define style rules. These rules will be applied to
that element only.
Example : Following is the example of inline CSS based on the above syntax:
<head>
Page 4 of 35
S - 4 , U - 2 , CSS Notes A_C
Attributes
<head>
<style type="text/css" media="all">
h1{
color: #36C;
}
</style>
</head>
An external style sheet is used to define the style for many HTML pages. With an
external style sheet, you can change the look of an entire web site, by changing one file!
The <link> element can be used to include an external stylesheet file in your HTML
document. 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.
<head>
<link rel="stylesheet" type="text/css" href="..." media="..." />
Page 5 of 35
S - 4 , U - 2 , CSS Notes A_C
</head>
href URL Specifies the style sheet file having Style rules.
This attribute is a required.
Any rule defined in <style>...</style> tags will override the rules defined in any
external style sheet file.
Any rule defined in the external style sheet file takes the lowest priority, and the
rules defined in this file will be applied only when the above two rules are not
applicable.
CSS Comments
CSS comments are generally written to explain our code. Comments are ignored by
the browsers. 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.....*/.
You can use /* ....*/ to comment multi-line blocks in similar way you do in C and C++
programming languages.
Example
Page 6 of 35
S - 4 , U - 2 , CSS Notes A_C
Page 7 of 35
S - 4 , U - 2 , CSS Notes A_C
Pseudo-classes
Pseudo-classes are keywords which allow selection based on information that lies
outside of the document tree or that cannot be expressed by other selectors or
combinators. This information can be associated to a certain state (state and dynamic
pseudo-classes), to locations (structural and target pseudo-classes), to negations of the
former (negation pseudo-class) or to languages (lang pseudo-class). Examples include
whether or not a link has been followed (:visited), the mouse is over an element (:hover), a
checkbox is checked (:checked), etc.
CSS pseudo-classes are used to add special effects to some selectors. You do not
need to use JavaScript or any other script to use those effects. A simple syntax of pseudo-
classes is as follows :
Syntax :
selector:pseudo-class {
property:value;
}
1 :link
2 :visited
3 :hover
Use this class to add special style to an element when you mouse over it.
4 :active
Page 8 of 35
S - 4 , U - 2 , CSS Notes A_C
5 :focus
Use this class to add special style to an element while the element has focus.
6 :first-child
Use this class to add special style to an element that is the first child of some
other element.
7 :lang
Page 9 of 35
S - 4 , U - 2 , CSS Notes A_C
Note: On systems with multi-button mice, CSS3 specifies that the :active pseudo-class must
only apply to the primary button; on right-handed mice, this is typically the leftmost button.
The :focus CSS pseudo-class represents an element (such as a form input) that has
received focus. It is generally triggered when the user clicks or taps on an element or selects
it with the keyboard's "tab" key.
Note: This pseudo-class applies only to the focused element itself. Use :focus-within if you
want to select an element that contains a focused element.
HTML FILE
<input class="red-input"value="I'll be red when focused."><br>
<input class="blue-input"value="I'll be blue when focused.">
CSS FILE
.red-input:focus{
background: yellow;
color: red;
}
.blue-input:focus{
background: yellow;
color: blue;
}
List of pseudo-classes:
Name Description
:active Applies to any element being activated (i.e. clicked) by the user.
:any Allows you to build sets of related selectors by creating groups that the
included items will match. This is an alternative to repeating an entire
selector.
:target Selects the current active #news element (clicked on a URL containing that
anchor name)
:checked Applies to radio, checkbox, or option elements that are checked or toggled
into an "on" state.
:default Represents any user interface element that is the default among a group of
similar elements.
:disabled Applies to any UI element which is in a disabled state.
:empty Applies to any element which has no children.
:enabled Applies to any UI element which is in an enabled state.
:first Used in conjunction with the @page rule, this selects the first page in a
printed document.
:first-child Represents any element that is the first child element of its parent.
:first-of-type Applies when an element is the first of the selected element type inside its
parent. This may or may not be the first-child.
:focus Applies to any element which has the user's focus. This can be given by the
user's keyboard, mouse events, or other forms of input.
Page 10 of 35
S - 4 , U - 2 , CSS Notes A_C
:focus-within Can be used to highlight a whole section when one element inside it is
focused. It matches any element that the :focus pseudo-class matches or that
has a descendant focused.
:full-screen Applies to any element displayed in full-screen mode. It selects the whole
stack of elements and not just the top level element.
:hover Applies to any element being hovered by the user's pointing device, but not
activated.
:indeterminate Applies radio or checkbox UI elements which are neither checked nor
unchecked, but are in an indeterminate state. This can be due to an
element's attribute or DOM manipulation.
:in-range The :in-range CSS pseudo-class matches when an element has its value
attribute inside the specified range limitations for this element. It allows the
page to give a feedback that the value currently defined using the element is
inside the range limits.
:invalid Applies to <input> elements whose values are invalid according to the type
specified in the type= attribute.
:lang Applies to any element who's wrapping <body> element has a properly
designated lang= attribute. For the pseudo-class to be valid, it must contain a
valid two or three letter language code.
:last-child Represents any element that is the last child element of its parent.
:last-of-type Applies when an element is the last of the selected element type inside its
parent. This may or may not be the last-child.
:left Used in conjunction with the @page rule, this selects all the left pages in a
printed document.
:link Applies to any links which haven't been visited by the user.
:not() Applies to all elements which do not match the value passed to (:not(p) or
:not(.class-name) for example. It must have a value to be valid and it can only
contain one selector. However, you can chain multiple :not selectors
together.
:nth-child Applies when an element is the n-th element of its parent, where n can be an
integer, a mathematical expression (e.g n+3) or the keywords odd or even.
:nth-of-type Applies when an element is the n-th element of its parent of the same
element type, where n can be an integer, a mathematical expression (e.g
n+3) or the keywords odd or even.
:only-child The :only-child CSS pseudo-class represents any element which is the only
child of its parent. This is the same as :first-child:last-child or :nth-
child(1):nth-last-child(1), but with a lower specificity.
:optional The :optional CSS pseudo-class represents any element that does not have
the required attribute set on it. This allows forms to easily indicate optional
fields and to style them accordingly.
:out-of-range The :out-of-range CSS pseudo-class matches when an element has its value
attribute outside the specified range limitations for this element. It allows the
page to give a feedback that the value currently defined using the element is
outside the range limits. A value can be outside of a range if it is either
smaller or larger than maximum and minimum set values.
:placeholder-shown Experimental. Applies to any form element currently displaying
placeholder text.
Page 11 of 35
S - 4 , U - 2 , CSS Notes A_C
CSS Selectors
CSS selectors are used to select the contents 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. You can define selectors in various simple ways based on your comfort. Let me
put these selectors one by one.
Rather than selecting elements of a specific type, the universal selector quite simply
matches the name of any element type:
* {
color: #000000;
}
This rule renders the content of every element in our document in black.
Suppose you want to apply a style rule to a particular element only when it lies
inside a particular element. As given in the following example, the style rule will apply to
<em> element only when it lies inside the <ul> tag.
Page 12 of 35
S - 4 , U - 2 , CSS Notes A_C
ul em {
color: #000000;
}
You can define style rules based on the class attribute of the elements. All the
elements having that class will be formatted according to the defined rule.
.black {
color: #000000;
}
This rule renders the content in black for every element with class attribute set to
black in our document. You can make it a bit more particular. For example:
h1.black {
color: #000000;
}
This rule renders the content in black for only <h1> elements with class attribute set
to black.
You can apply more than one class selectors to a given element. Consider the
following example:
5. The ID Selectors
You can define style rules based on the id attribute of the elements. All the elements
having that id will be formatted according to the defined rule.
#black {
color: #000000;
}
This rule renders the content in black for every element with id attribute set to black
in our document. You can make it a bit more particular. For example:
h1 #black {
color: #000000;
}
This rule renders the content in black for only <h1> elements with id attribute set to
black.
The true power of id selectors is when they are used as the foundation for descendant
selectors. For example:
#black h2 {
color: #000000;
}
Page 13 of 35
S - 4 , U - 2 , CSS Notes A_C
In this example, all level 2 headings will be displayed in black color when those
headings will lie within tags having id attribute set to black.
You have seen the descendant selectors. There is one more type of selector, which is
very similar to descendants but have different functionality. Consider the following
example:
body > p {
color: #000000;
}
This rule will render all the paragraphs in black if they are a direct child of the
<body> element. Other paragraphs put inside other elements like <div> or <td> would not
have any effect of this rule.
You can also apply styles to HTML elements with particular attributes. The style rule
below will match all the input elements having a type attribute with a value of text:
input[type="text"]{
color: #000000;
}
The advantage to this method is that the <input type="submit" /> element is
unaffected, and the color applied only to the desired text fields.
There are following rules applied to attribute selector.
p[lang="fr"] - Selects all paragraph elements whose lang attribute has a value of
exactly "fr".
p[lang~="fr"] - Selects all paragraph elements whose lang attribute contains the
word "fr".
p[lang|="en"] - Selects all paragraph elements whose lang attribute contains values
that are exactly "en", or begin with "en-".
You may need to define multiple style rules for a single element. You can define
these rules to combine multiple properties and corresponding values into a single block as
defined in the following example:
h1 {
color: #36C;
font-weight: normal;
letter-spacing: 0.4em;
margin-bottom: 1em;
Page 14 of 35
S - 4 , U - 2 , CSS Notes A_C
text-transform: lowercase;
}
Here all the property and value pairs are separated by a semicolon (;). You can keep
them in a single line or multiple lines. For better readability, we keep them in separate lines.
For a while, don't bother about the properties mentioned in the above block. These
properties will be explained in the coming chapters and you can find the complete detail
about properties in CSS References.
8. Grouping Selectors
You can apply a style to many selectors if you like. Just separate the selectors with a
comma, as given in the following example:
h1, h2, h3 {
color: #36C;
font-weight: normal;
letter-spacing: 0.4em;
margin-bottom: 1em;
text-transform: lowercase;
}
This define style rule will be applicable to h1, h2 and h3 element as well. The order
of the list is irrelevant. All the elements in the selector will have the corresponding
declarations applied to them.
Text Properties
List Properties
Border Properties
Font Properties
I. Text Properties
The color property is used to set the color of the text. The color is specified by:
Page 15 of 35
S - 4 , U - 2 , CSS Notes A_C
The default text color for a page is defined in the body selector.
<html lang="en">
<head>
<title>Example of CSS text transformation</title>
<style type="text/css">
p.up {
text-transform: uppercase;
}
p.cap {
text-transform: capitalize;
}
p.low {
text-transform: lowercase;
}
</style>
</head>
<body>
<h1>Text-transform Effect</h1>
<p class="up">All characters of each word of this paragraph is
transformed in uppercase.</p>
<p class="cap">The first character of each word of this paragraph is
transformed in uppercase.</p>
Page 16 of 35
S - 4 , U - 2 , CSS Notes A_C
Property Description
Page 17 of 35
S - 4 , U - 2 , CSS Notes A_C
generic family - a group of font families with a similar look (like "Serif" or
"Monospace")
font family - a specific font family (like "Times New Roman" or "Arial")
Page 18 of 35
S - 4 , U - 2 , CSS Notes A_C
Serif Times New Roman Serif fonts have small lines at the ends on
Georgia some characters
Note: On computer screens, sans-serif fonts are considered easier to read than serif
fonts.
The font-family property should hold several font names as a "fallback" system. If the
browser does not support the first font, it tries the next font, and so on.
Start with the font you want, and end with a generic family, to let the browser pick a similar
font in the generic family, if no other fonts are available.
Note: If the name of a font family is more than one word, it must be in quotation marks,
like: "Times New Roman".
Example
p {
font-family: "Times New Roman", Times, serif;
}
For commonly used font combinations, look at our Web Safe Font Combinations.
Page 19 of 35
S - 4 , U - 2 , CSS Notes A_C
Example
p.normal {
font-style: normal;
}
p.italic {
font-style: italic;
}
p.oblique {
font-style: oblique;
}
The font-size property sets the size of the text. Being able to manage the text size
is important in web design. However, you should not use font size adjustments to make
paragraphs look like headings, or headings look like paragraphs. Always use the proper
HTML tags, like <h1> - <h6> for headings and <p> for paragraphs. The font-size value can be
an absolute or relative size.
Absolute size:
Relative size:
Note: If you do not specify a font size, the default size for normal text, like paragraphs,
is 16px (16px=1em).
Setting the text size with pixels gives you full control over the text size:
Page 20 of 35
S - 4 , U - 2 , CSS Notes A_C
Example
h1 {
font-size: 40px;
}
h2 {
font-size: 30px;
}
p {
font-size: 14px;
}
Tip: If you use pixels, you can still use the zoom tool to resize the entire page.
To allow users to resize the text (in the browser menu), many developers use em
instead of pixels. The em size unit is recommended by the W3C. 1em is equal to the current
font size. The default text size in browsers is 16px. So, the default size of 1em is 16px. The
size can be calculated from pixels to em using this formula: pixels/16=em
Example
h1 {
font-size: 2.5em; /* 40px/16=2.5em */
}
h2 {
font-size: 1.875em; /* 30px/16=1.875em */
}
p {
font-size: 0.875em; /* 14px/16=0.875em */
}
In the example above, the text size in em is the same as the previous example in
pixels. However, with the em size, it is possible to adjust the text size in all browsers.
Unfortunately, there is still a problem with older versions of IE. The text becomes larger
than it should when made larger, and smaller than it should when made smaller.
The solution that works in all browsers is to set a default font-size in percent for the
<body> element:
Example
body {
font-size: 100%;
}
Page 21 of 35
S - 4 , U - 2 , CSS Notes A_C
h1 {
font-size: 2.5em;
}
h2 {
font-size: 1.875em;
}
p {
font-size: 0.875em;
}
Our code now works great! It shows the same text size in all browsers, and allows all
browsers to zoom or resize the text!
The text size can be set with a vw unit, which means the "viewport width". That way
the text size will follow the size of the browser window. Resize the browser window to see
how the font size scales.
Example
<h1 style="font-size:10vw">Hello World</h1>
Viewport is the browser window size. 1vw = 1% of viewport width. If the viewport is
50cm wide, 1vw is 0.5cm.
Example
p.normal {
font-weight: normal;
}
p.thick {
font-weight: bold;
}
Page 22 of 35
S - 4 , U - 2 , CSS Notes A_C
Example
p.normal {
font-variant: normal;
}
p.small {
font-variant: small-caps;
}
unordered lists (<ul>) - the list items are marked with bullets
ordered lists (<ol>) - the list items are marked with numbers or letters
Unordered Lists:
o Coffee
o Tea
o Coca Cola
Coffee
Tea
Coca Cola
Ordered Lists:
1. Coffee
2. Tea
3. Coca Cola
I. Coffee
II. Tea
III. Coca Cola
Page 23 of 35
S - 4 , U - 2 , CSS Notes A_C
The following example shows some of the available list item markers:
Example
ul.a {
list-style-type: circle;
}
ul.b {
list-style-type: square;
}
ol.c {
list-style-type: upper-roman;
}
ol.d {
list-style-type: lower-alpha;
}
Note: Some of the values are for unordered lists, and some for ordered lists.
Page 24 of 35
S - 4 , U - 2 , CSS Notes A_C
Example
ul {
list-style-image: url('sqpurple.gif');
}
Example
ul.a {
list-style-position: outside;
}
ul.b {
list-style-position: inside;
}
Example
ul {
list-style-type: none;
Page 25 of 35
S - 4 , U - 2 , CSS Notes A_C
margin: 0;
padding: 0;
}
Example
ul {
list-style: square inside url("sqpurple.gif");
}
When using the shorthand property, the order of the property values is:
If one of the property values above are missing, the default value for the missing property
will be inserted, if any.
Example
ol {
background: #ff9999;
padding: 20px;
}
ul {
background: #3399ff;
padding: 20px;
}
ol li {
background: #ffe5e5;
padding: 5px;
margin-left: 35px;
}
ul li {
background: #cce5ff;
margin: 5px;
}
Page 26 of 35
S - 4 , U - 2 , CSS Notes A_C
Result:
1. Coffee
2. Tea
3. Coca Cola
Coffee
Tea
Coca Cola
border-width
border-style (required)
border-color
If border-color is omitted, the color applied will be the color of the text.
CSS Syntax
border: border-width border-style border-color|initial|inherit;
Value Description
border- Specifies the color of the border. Default value is the color of
color the text
Page 27 of 35
S - 4 , U - 2 , CSS Notes A_C
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about
inherit
<HTML>
<HEAD>
<STYLE>
h1 {
border: 5px solid red;
}
h2 {
border: 4px dotted blue;
}
div {
border: double;
}
</STYLE>
</HEAD>
<BODY>
<H1>A heading with a solid red border.</H1>
Page 28 of 35
S - 4 , U - 2 , CSS Notes A_C
Page 29 of 35
S - 4 , U - 2 , CSS Notes A_C
color border
V. CSS Links
With CSS, links can be styled in different ways.
Links can be styled with any CSS property (e.g. color, font-family, background,
etc.).
a {
color: hotpink;
}
In addition, links can be styled differently depending on what state they are in. The
four links states are:
Example
/* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
Page 30 of 35
S - 4 , U - 2 , CSS Notes A_C
When setting the style for several link states, there are some order rules:
Example
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a:active {
text-decoration: underline;
}
Example
a:link {
background-color: yellow;
}
a:visited {
background-color: cyan;
}
a:hover {
background-color: lightgreen;
}
a:active {
background-color: hotpink;
}
Page 31 of 35
S - 4 , U - 2 , CSS Notes A_C
Example
a:link, a:visited {
background-color: #f44336;
color: white;
padding: 14px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
}
a:hover, a:active {
background-color: red;
}
The CSS background properties are used to define the background effects for
elements.
background-color
background-image
background-repeat
background-position
background-attachment
VI.1. background-color
In the example below, the <h1>, <p>, and <div> elements have different background colors:
Page 32 of 35
S - 4 , U - 2 , CSS Notes A_C
h1 {
background-color: green;
}
div {
background-color: lightblue;
}
p {
background-color: yellow;
}
VI.2. background-image
body {
background-image: url("paper.gif");
}
Note: When using a background image, use an image that does not disturb the text.
body {
background-image: url("gradient_bg.png");
}
VI.3. background-repeat
Showing the background image only once is also specified by the background-
repeat property:
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
}
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
Page 33 of 35
S - 4 , U - 2 , CSS Notes A_C
VI.4. background-position
In the example above, the background image is shown in the same place as the text. We
want to change the position of the image, so that it does not disturb the text too much. The position
of the image is specified by the background-position property:
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
VI.5. background-attachment
To specify that the background image should be fixed (will not scroll with the rest of
the page), use the background-attachment property:
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;
}
To shorten the code, it is also possible to specify all the background properties in one
single property. This is called a shorthand property. The shorthand property for background
is background:
body {
background: #ffffff url("img_tree.png") no-repeat right top;
}
When using the shorthand property the order of the property values is:
background-color
background-image
background-repeat
background-attachment
background-position
It does not matter if one of the property values is missing, as long as the other ones are in
this order.
Property Description
Page 34 of 35
S - 4 , U - 2 , CSS Notes A_C
___________________________________________________________________________
T H E E N D
___________________________________________________________________________
Page 35 of 35