Css
Css
Cascading Style Sheets, fondly referred to as CSS, is a simple design language intended to
simplify the process of making web pages presentable.
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, layout designs.
Advantages of CSS
CSS saves time − We can write CSS once and then reuse same sheet in
multiple HTML pages. We can define a style for each HTML element and
apply it to as many Web pages as we want.
Pages load faster − We 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 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.
Global web standards − Now HTML attributes are being deprecated and it is
being recommended to use CSS. So its a good idea to start using CSS in all the
HTML pages to make them compatible to future browsers.
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 value either red or #F1F1F1 etc.
selector { property: value }
Inline CSS - The style Attribute
We can use style attribute of any HTML element to define style rules.
<html>
<head>
</head>
<body>
<h1 style = "color:#36C;"> This is inline CSS </h1>
</body>
</html>
Embedded CSS - The <style> Element
We can put your CSS rules into an HTML document using the <style>
element.
Rules defined using this syntax will be applied to all the elements available in
the document.
<html>
<head>
The <link> element can be used to include an external stylesheet file in your
HTML document.
We 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 = "mystyle.css" />
</head>
Imported CSS - @import Rule
<head>
@import "mystyle.css";
</head>
CSS Rules Overriding
Any inline style sheet takes highest priority. So, it will override any rule
defined in <style>...</style> tags or rules defined in any external style sheet
file.
Any rule defined in <style>...</style> tags will override rules defined in any
external style sheet file.
Any rule defined in external style sheet file takes lowest priority, and rules
defined in this file will be applied only when above two rules are not
applicable.
The Type Selectors
The Descendant Selectors
h1 {
Suppose you want to apply a style rule to
color: #36CFFF;
a particular element only when it lies
}
inside a particular element.
ul li {
The Universal Selectors
color: #000000;
*{
}
color: #000000;
}
The Class Selectors
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;
}
h1.black {
color: #000000;
}
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;
}
h1#black {
color: #000000;
}
The Child Selectors
body > p {
color: #000000;
}
This rule will render all the paragraphs in black if they are direct child of
<body> element.
Other paragraphs put inside other elements like <div> or <td> would not
have any effect of this rule.
Multiple Style Rules: combine multiple properties and corresponding values
into a single block
h1 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
All the property and value pairs are separated by a semi colon (;).
Grouping Selectors: apply a style to many selectors . Just separate the
selectors with a comma
h1, h2, h3 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
CSS - Measurement Units
Typically, these are used to set a color either for the foreground of an element
(i.e., its text) or else for the background of the element.
They can also be used to affect the color of borders and other decorative
effects.
Format Syntax Example
<html>
<head>
<body>
<p style = "background-color:yellow;">
This text has a yellow background color.</p>
</body>
</head>
<html>
Background-Image Properties
Set the Background Image
<html>
<head>
<style>
body {
background-image: url("/css/images/css.jpg");
}
</style>
<body>
<h1>Hello World!</h1>
</body>
</head>
<html>
Repeat the Background Image
<style>
body {
background-image: url("/css/images/css.jpg");
background-repeat: repeat-y;
}
</style>
<style>
body {
background-image: url("/css/images/css.jpg");
background-repeat: repeat-x;
}
</style>
Set the Background Attachment
Background attachment determines whether a background image is fixed or scrolls with the rest of the
page.
<style>
body {
background-image: url('/css/images/css.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
}
</style>
<style>
body {
background-image: url('/css/images/css.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-attachment:scroll;
}
</style>
Font Properties
Set the Font Style
<body>
<p style="font-style:italic;">
This text will be rendered in italic style
</p>
</body>
Possible values could be xx-small, x-small, small, medium, large, x-large, xx-
large, smaller, larger, size in pixels or in %.
<body>
<p style="font-size:20px;">This font size is 20 pixels</p>
<p style="font-size:small;">This font size is small</p>
<p style="font-size:large;">This font size is large</p>
</body>
Set the Font Weight
<body>
<p style="font-weight:bold;">This font is bold.</p>
<p style="font-weight:bolder;">This font is bolder.</p>
<p style="font-weight:500;">This font is 500 weight.</p>
</body>
Set the Font Stretch
Possible values could be normal, wider, narrower, ultra-condensed, extra-
condensed, condensed, semi-condensed, semi-expanded, expanded, extra-
expanded, ultra-expanded.
<body>
<p style="font-stretch:ultra-expanded;">
If this doesn't appear to work, it is likely that your computer doesn't have a
extra-condensed Makes the text narrower than condensed, but not as narrow as ultra-condensed
condensed Makes the text narrower than semi-condensed, but not as narrow as extra-condensed
semi-condensed Makes the text narrower than normal, but not as narrow as condensed
<p style="text-align:center;">
This will be center aligned.
</p>
<p style="text-align:left;">
This will be left aligned.
</p>
</body>
Decorating the Text
<body>
<p style="text-decoration:underline;">
This will be underlined
</p>
<p style="text-decoration:line-through;">
This will be striked through.
</p>
<p style="text-decoration:overline;">
This will have a over line.
</p>
</body>
Set the Text Cases
<body>
<p style="text-transform:capitalize;">
This will be capitalized
</p>
<p style="text-transform:uppercase;">
This will be in uppercase
</p>
<p style="text-transform:lowercase;">
This will be in lowercase
</p>
</body>
Set the Text Shadow
<body>
<p style="text-shadow:4px 4px 8px blue;">
If your browser supports the CSS text-shadow property, this text will have
a blue shadow.
</p>
</body>
Border Property
The Image Border Property
<body>
<img style="border:0px;" src="/css/images/logo.png" />
<br />
<img style="border:3px dashed
red;height:100px;width:100px;opacity:0.6” src="/css/images/logo.png" />
</body>
The Image Height Property
<body>
<img style="border:1px solid red; height:100px;"
src="/css/images/logo.png" />
<br />
<img style="border:1px solid red; height:50%;"
src="/css/images/logo.png" />
</body>
The Image Width Property
<body>
<img style="border:1px solid red; width:150px;"
src="/css/images/logo.png" />
<br />
<img style="border:1px solid red; width:100%;"
src="/css/images/logo.png" />
</body>
The opacity Property
opacity:x
x can be a value from 0.0 - 1.0. A lower value makes the element more
transparent.
<body>
<img style="border:1px solid red;opacity:0.4;"
src="/css/images/logo.png" />
</body>
#id1{ property:value; }------ Id selector
.mycolour{ property:value;}-----class selector
The width can be set as a specific size (in px, pt, cm, em,
etc) or by using one of the three pre-defined values: thin,
medium, or thick.
p.two {
border-style: solid;
border-width: medium;
}
Four sides with
p.three { different width
border-style: solid;
border-width: 2px 10px 4px 20px;
}
Border Color
The border-color property is used to set the color of the four borders.
The border-color property can have from one to four values (for the top
border, right border, bottom border, and the left border).
p.two {
border-style: solid;
border-color: green;
} Four sides with
different colors
p.three {
border-style: solid;
border-color: red green blue yellow;
}
Border - Individual Sides
From the examples above you have seen that it is possible to
specify a different border for each side.
With CSS, you have full control over the margins. There are
properties for setting the margin for each side of an element (top,
right, bottom, and left).
CSS has properties for specifying the margin for each side
of an element:
• margin-top
• margin-right
• margin-bottom
• margin-left
margin: 25px 50px 75px 100px;
The element will then take up the specified width, and the remaining space
will be split equally between the left and right margins:
div {
width: 300px;
margin: auto;
border: 1px solid red;
}
CSS Padding
The CSS padding properties are used to generate space around an
element's content, inside of any defined borders.
With CSS, you have full control over the padding. There are properties for
setting the padding for each side of an element (top, right, bottom, and
left).
CSS has properties for specifying the padding for each side of an
element:
• padding-top
• padding-right
• padding-bottom
• padding-left
div {
div {
padding-top: 50px;
padding: 25px 50px 75px 100px;
padding-right: 30px; }
padding-bottom: 50px;
padding-left: 80px;
}
The CSS width property specifies the width of the element's content area. The
content area is the portion inside the padding, border, and margin of an
element (the box model).
div {
width: 300px;
padding: 25px;
}
Setting height and width
The height and width properties are used to set the height and
width of an element.
The height and width can be set to auto (this is default. Means
that the browser calculates the height and width), or be specified
in length values, like px, cm, etc., or in percent (%) of the
containing block.
div {
height: 200px;
width: 50%;
background-color: powderblue;
}
div {
height: 100px;
width: 500px;
background-color: powderblue;
}
div {
width: 300px;
border: 25px solid green;
padding: 25px;
margin: 25px;
}
The display Property
The display property specifies if/how an element is displayed.
Value Description Example
inline Displays an element as an inline element li {
(like <span>). Any height and width display: inline;
properties will have no effect
}
a:visited { a:hover {
background-color: cyan; background-color: lightgreen;
} }
a:active {
background-color: hotpink;
}
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;
}
Task:
Create a navigation link as below
CSS Pseudo-classes
A pseudo-class is used to define a special state of an
element.
a.highlight:hover {
color: #ff0000;
}
<style>
div {
background-color: green;
color: white;
padding: 25px;
text-align: center;
}
div:hover {
background-color: blue;
}
</style>
<style>
p{
display: inline;
background-color: yellow;
padding: 20px;
}
div:hover p {
display: block;
}
</style>
CSS Pseudo-elements
A CSS pseudo-element is used to style specified parts of an
element.
p::first-line {
color: #ff0000;
font-variant: small-caps;
}
p::first-letter {
color: #ff0000;
font-size: xx-large;
}
Multiple Pseudo-elements
p::first-letter {
color: #ff0000;
font-size: xx-large;
}
p::first-line {
color: #0000ff;
font-variant: small-caps;
}
h1::before { insert some content
content: url(smiley.gif); before the content of
} an element.
::selection {
color: red; Highlight the portion
background: yellow; of an element that is
} selected by a user.
CSS Opacity / Transparency
img {
opacity: 0.5; img {
filter: alpha(opacity=50); opacity: 0.5;
} filter: alpha(opacity=50); /* For IE8 and earlier
*/
}
img:hover {
opacity: 1.0;
filter: alpha(opacity=100); /* For IE8 and
earlier */
}
Transparent Box
When using the opacity property to add transparency to the background of an element, all of its child elements inherit the
same transparency.
<style>
div {
background-color: #4CAF50;
padding: 10px;
}
div.first {
opacity: 0.1;
filter: alpha(opacity=10); /* For IE8 and earlier
*/
}
div.second {
opacity: 0.3;
filter: alpha(opacity=30);}
div.third {
opacity: 0.6;
filter: alpha(opacity=60);}
</style>
div.first {
background: rgba(76, 175, 80, 0.1);
}
div.second {
background: rgba(76, 175, 80, 0.3);
}
div.third {
background: rgba(76, 175, 80, 0.6);
}
</style>
Task 2: Create a similar div in your web page
<style>
div.background {
<body>
background: url(klematis.jpg) repeat;
border: 2px solid black;
<div class="background">
}
<div class="transbox">
<p>This is some text that is placed in the transparent
div.transbox {
box.</p>
margin: 30px;
</div>
background-color: #ffffff;
</div>
border: 1px solid black;
opacity: 0.6;
filter: alpha(opacity=60); /* For IE8 and earlier */
}
div.transbox p {
margin: 5%;
font-weight: bold;
color: #000000;
}
</style>
</head>
CSS Navigation Bar
<ul>
<li><a href="default.asp">Home</a></li>
<li><a href="news.asp">News</a></li>
<li><a href="contact.asp">Contact</a></li>
<li><a href="about.asp">About</a></li>
</ul>
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
li a {
display: block;
width: 60px;
background-color: #dddddd;
}
</style>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
li a:hover {
background-color: #555;
color: white;
}
</style>
ul { li a {
list-style-type: none; display: block;
margin: 0; color: white;
padding: 0; text-align: center;
overflow: hidden; padding: 14px 16px;
background-color: #333; text-decoration: none;
} }
<div class="gallery">
<a target="_blank" href="img_mountains.jpg">
<img src="img_mountains.jpg" alt="Mountains" width="600" height="400">
</a>
<div class="desc">Add a description of the image here</div>
</div>
</body>
</html>
Linking to a Page Section
Download Links
<body>
<a href = “handout.pdf”>Download PDF File</a>
</body>
CSS Gradients
Gradients are a type of image that gradually transitions from one color to the
next horizontally, vertically or diagonally.
background-image: linear-gradient(direction, color-stop1, color-stop2, ...);
CSS transitions allows you to change property values smoothly, over a given
duration.
Mouse over the element below to see a CSS transition effect
Note: If the duration part is not specified, the transition will have no effect, because the
default value is 0.
The following example shows a 100px * 100px red <div> element. The <div> element has
also specified a transition effect for the width property, with a duration of 2 seconds:
<style> div:hover {
div { width:
width: 100px; 300px;
height: 100px; height:
background: red; 300px;
transition: width 2s, height 4s; }
} </style>
Transition + Transformation
CSS transforms allow you to move, rotate, scale, and
skew elements.
<style>
div {
width: 100px;
height: 100px;
background: red;
transition: width 2s, height 2s, transform 2s;
} div:hover {
width: 300px;
height: 300px;
transform: rotate(180deg);
}
CSS Animations
CSS allows animation of HTML elements without using
JavaScript or Flash!
• An animation lets an element gradually change from one style to another.
• You can change as many CSS properties you want, as many times you want.
• To use CSS animation, you must first specify some keyframes for the
animation.
• Keyframes hold what styles the element will have at certain times