IP-Chapter 4-CSS
IP-Chapter 4-CSS
Chapter 4 - Cascading
Style Sheets (CSS)
Outline
1. Introduction to CSS
• What is CSS?
• Importance of CSS
2. Basics of CSS
• Basic Syntax & Structure
• Class & ID
• Types of CSS
• Multiple selector, Multilevel selector
3.Background
4.Fonts & Text
Outline (Cont.)
5. Box Model
• Margin, Border, Padding
6. List
• List Type, List with Image, List Position
7.Links
8.CSS Positioning
9. CSS Layers
10. CSS Floating Property
11. Introduction to CSS3
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 defines layout of HTML documents. For
example, CSS covers Fonts, colors, margins, lines,
height, width, background images, advanced
positions and many other things.
Importance of CSS
CSS defines HOW HTML elements are to be displayed.
Styles are normally saved in external .css files.
External style sheets enable you to change the appearance and
layout of all the pages in a Web site, just by editing one single file.
Advantages :
• Improves Website Presentation
• External CSS makes Updates Easier and Smoother
• External CSS helps Web Pages Load Faster
Disadvantages :
• Browser Dependent
• Difficult to retrofit in old websites
Basic Syntax of CSS
A CSS rule has two main parts: a selector, and one or more
declarations
Selector Declaration 1 Declaration 2
HTML CSS
<h1 id=“para1”> #para1{
Hello Friends color: blue;
</h1> }
<h1> Output
How are you Hello Friends
</h1> How are you
The “class” selector
The class selector is used to specify a style for a group of
elements.
The class selector uses the HTML class attribute, and is defined
with a ".“ in css.
HTML CSS
<h1 class=“myClass”> .myClass{
Hello Friends color: blue;
</h1> }
<h1>
How are you
</h1> Output
<h1 class=“myClass”> Hello Friends
How are you How are you
</h1> How are you
Different ways to write CSS
There are three ways of writing a style sheet:
1. Inline Style
2. Internal/Embedded Style sheet
3. External Style Sheet
1) Inline Style
It is possible to place CSS right in your HTML code, and this
method of CSS usage is referred to as inline css.
Inline CSS has the highest priority out of external, internal, and
inline CSS.
This means that you can override styles that are defined in
external or internal by using inline CSS.
If you want to add a style inside an HTML element all you have to
do is specify the desired CSS properties with the style HTML
attribute.
Example:
HTML
<p style="background: blue; color: white;"> My Inline CSS </p>
2) Internal Style Sheet
This type of CSS is only for Single Web Page.
When using internal CSS, we must add a new tag, <style>, inside
the <head> tag.
The HTML code below contains an example of <style>'s usage.
HTML
<html><head>
<style type="text/css">
p{ color: red;}
</style>
</head><body>
<p>Your page's content!</p></body>
</html>
3) External Style Sheet
When using CSS it is preferable to keep the CSS separate from
your HTML.
Placing CSS in a separate file allows the web designer to
completely differentiate between content (HTML) and design
(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>.
3) External Style Sheet (Cont.)
Example :
Demo.html test.css
<html> #para1{
<head> text-align: center;
<link rel=“stylesheet” type=“text/css” }
href=“test.css”> p
</head> {
<body> color : blue;
}
<p> Hello Friends </p>
<p id=“para1”> How are you? </p> Output
Hello Friends
</body> How are you?
</html>
3) External Style Sheet (Cont.)
Advantages:
• It keeps your website design and content separate.
• 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.
Assign Multiple Classes
We can apply different class to same html element by giving space
separated class names in the class attribute:
Demo.html test.css
<html> . class1
<head> {
<link rel=“stylesheet” type=“text/css” color : blue;
href=“test.css”> }
</head> . class2
<body> {
text-align : center;
<h1 class=“class1 class2”> }
How are you?
</h1> Output
</body>
How are you?
</html>
Multiple Selection
We can apply same css to multiple selectors using comma
separated selector list, for example :
Demo.html test.css
<html> p, h1
<head> {
<link rel=“stylesheet” type=“text/css” color : blue;
href=“test.css”> }
</head>
<body>
Demo.html test.css
<html> div h1
<head> {
<link rel=“stylesheet” type=“text/css” color : blue;
href=“test.css”> }
</head>
<body>
<h1>Hello Friends…</h1>
<div>
<h1>How are you?</h1> Output
</div> Hello Friends…
</body>
How are you?
</html>
Background Property
Property Name
test.css
body
{
background-color : red;
background-color : #FF0000;
background-color : rgb(255,0,0);
}
Background Image
The background-image property specifies an image to use as the
background of an element.
For Example,
test.css
body
{
background-image : url(‘pathToImage.jpg’);
}
Background Image Repeat
You can have a background image repeat vertically (y-axis),
horizontally (x-axis), in both directions, or in neither direction.
test.css
body
{
background-image : url(‘pathToImage.jpg’);
background-repeat : repeat;
background-repeat : repeat-x;
background-repeat : repeat-y;
background-repeat : no-repeat;
}
repeat-x
Fixed Background Image
The background-attachment property sets whether a background
image is fixed or scrolls with the rest of the page.
For Example,
test.css
body
{
background-image : url(‘pathToImage.jpg’);
background-repeat : no-repeat;
background-attachment : fixed;
}
Background Image Positioning
The background-position property sets the starting position of a
background image.
test.css
body
{
background-image : url(‘pathToImage.jpg’);
background-repeat : no-repeat;
background-position: 20px 10px;
background-position: 30%30%;
background-position: top center;
}
30% 30%
CSS Font
CSS font properties define the font family, boldness, size, and the
style of a text.
Property Name
margin-top
border-top
padding-top
padding-right
margin-right
border-right
padding-left
margin-left
border-left
Content
padding-bottom
border-bottom
margin-bottom
CSS Padding
The CSS padding properties define h4{
padding : 10px;
the space between the element }
border and the element content.
The top, right, bottom, and left h4{
padding-top : 10px;
padding can be changed padding-right : 20px;
independently using separate padding-bottom : 30 px;
padding-left : 40 px;
properties. }
A shorthand padding property can h4{
also be used, to change all padding padding : 10px 20px 30px 40px;
}
at once.
CSS Border
The CSS border properties allow you to specify h4{
the style and color of an element's border. border : 1px solid red;
Border Style Types }
• The border-style property specifies what kind of h4{
border to display. border-style : solid;
Border Width border-style : dotted;
border-style : double;
• The border-width property is used to set the width
}
of the border.
Border Color h4{
•
border-width : 7px;
The border-color property is used to set the color of
}
the border.
• Border colors can be any color defined by RGB, h4{
hexadecimal, or key terms. Below is an example of border-color : red;
each of these types. }
The top, right, bottom, and left border can be h4{
changed independently using separate border-top : 1px solid red;
properties. }
CSS Margin
The CSS margin properties define the h4{
margin: 10px;
space around elements }
ol{
list-style-position : outside;
list-style-position : inside;
}
Styling Links
Anchor/Link States a:link{
color:#FF0000;
• The four links states are:
/*unvisited link*/
1. a:link - a normal, unvisited link }
2. a:visited - a link the user has a:visited{
visited text-decoration : none;
/*visited link*/
3. a:hover - a link when the user
}
mouse over it
a:hover{
4. a:active - a link the moment it is color:#00FF00;
clicked /*mouse over link*/
}
a:active{
color:#0000FF;
/*selected link*/
}
CSS Positioning
Absolute Positioning
h1{
• With absolute positioning, you define the position : absolute;
exact pixel value where the specified left : 50px;
HTML element will appear. top : 100px;
}
• The point of origin is the top-left of the
browser's viewable area, so be sure you
are measuring from that point.
h1{
Relative Positioning
position : relative;
• Relative positioning changes the position left : 50px;
of the HTML element relative to where it top : 100px;
normally appears. }
Fixed Positioning h1{
• The element is positioned relative to the position : fixed;
browser window, in fixed position, top : 50px;
left : 100px;
element will be in the same place even
}
we scroll the screen.
CSS Layers
CSS allows you to control which item will CSS
appear on top with the use of layers. #division1{
position : absolute;
In CSS, each element is given a priority. height : 100px;
If there are two overlapping CSS positioned width : 100px;
elements, the element with the higher left : 100px;
top : 150px;
priority will appear on top of the other. background-color : red;
To manually define a priority, set the z-index z-index : 5;
value. The larger the value, the higher the }
#division2{
priority the element will have. position : absolute;
height : 200px;
HTML
width : 200px;
left : 50px;
<div id="division1">
top : 100px;
</div>
background-color : blue;
<div id="division2">
z-index : 2;
</div>
}
CSS Float Property
The CSS float property defines that an element should be taken
out of the normal flow of the document and placed along the left
or right side of its containing block.
Text and inline elements will then wrap around this element.
HTML CSS
#division1{
background-color : red;
<div id="division1"> float : left;
ABC Content width: 40%;
</div> }
<div id="division2"> #division2{
XYZ Content background-color : blue;
float : right;
</div> width: 40%;
}
Introduction to CSS3
CSS3 is the latest standard for CSS.
CSS3 is completely backwards-compatible with earlier versions of
CSS.
CSS3 has been split into "modules". It contains the "old CSS
specification" (which has been split into smaller pieces). In addition,
new modules are added.
CSS3 Transitions are a presentational effect which allow property
changes in CSS values, such as those that may be defined to occur
on :hover or :focus, to occur smoothly over a specified duration –
rather than happening instantaneously as is the normal behaviour.
Transition effects can be applied to a wide variety of CSS properties,
including background-color, width, height, opacity, and many more.
Introduction to CSS3 (Cont)
Some of the most important CSS3 modules are:
• CSS Animations and Transitions
• Calculating Values With calc()
• Advanced Selectors
• Generated Content and Counters
• Gradients
• Webfonts
• Box Sizing
• Border Images
• Media Queries
• Multiple Backgrounds
• CSS Columns
Courtesy : http://tutorialzine.com/2013/10/12-awesome-css3-features-you-can-finally-use/