Unit II Style Sheets Full
Unit II Style Sheets Full
Unit II Style Sheets Full
Syllabus
Need for CSS, introduction to CSS, basic syntax and
structure using CSS, background images, colors and
properties, manipulating texts using fonts, borders and
boxes, margins, padding, lists, positioning using CSS, CSS2.
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.
External stylesheets are stored in CSS files.
Need for CSS
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, layout designs, and variations
in display for different devices and screen sizes as well as a variety of other effects.
CSS is easy to learn and understand but it provides powerful control over the
presentation of an HTML document. Most commonly, CSS is combined with the
markup languages HTML or XHTML.
Advantages of CSS
CSS plays an important role, by using CSS you simply got to specify a repeated style for
element once & use it multiple times as because CSS will automatically apply the required
styles.
The main advantage of CSS is that style is applied consistently across variety of sites. One
instruction can control several areas.
It creates a stunning website.
Web designers needs to use few lines of programming for every page improving site speed.
Cascading sheet not only simplifies website development, but also simplifies the
maintenance time.
CSS changes are device friendly. With people employing a batch of various range of smart
devices to access websites over the web, there’s a requirement for responsive web design.
It has the power for re-positioning. It helps us to determine the changes within the position
of web elements who are there on the page.
Easy for the user to customize the online page.
It reduces the file transfer size.
CSS - Syntax
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
value either red or #F1F1F1 etc.
CSS Style Rule Syntax as follows −
selector { property: value }
Example
p{
color: red;
text-align: center;
}
Explanation of Example
p is a selector in CSS (it points to the HTML element you want to style: <p>).
color is a property, and red is the property value
text-align is a property, and center is the property value
Example
<!DOCTYPE html>
<html>
<head>
<title>This is document title</title>
<style>
h1 {
color: #36CFFF;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
< p> Introduction to CSS <p>
</body>
</html>
CSS Selectors:
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
The element Selector:
The element selector selects elements based on the element name.
You can select all <p> elements on a page like this (in this case, all <p> elements
will be center-aligned, with a red text color):
Example:
p{
text-align: center;
color: red;
}
Example
<!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 id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>
The id Selector:
The id selector uses the id attribute of an HTML element to select a specific element.
An id should be unique within a page, so the id selector is used if you want to select
a single, unique element.
To select an element with a specific id, write a hash character (#), followed by the id
of the element.
The style rule below will be applied to the HTML element with id="para1":
Example:
#para1 {
text-align: center;
color: red;
}
Example
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<p id="para1">Hello CSS.com</p>
<p>This paragraph will not be affected.</p>
</body>
</html>
The class Selector:
The class selector selects elements with a specific class attribute.
To select elements with a specific class, write a period character.(full stop symbol), followed
by the name of the class:
Syntax:
<style type=”text/css”>
.classname {style_attribute: value;}
</style>
In the example below, all HTML elements with class="center" will be red and center-aligned:
Example1:
.center{
text-align: center;
color: red;
}
In the example below, all <p> elements with class="center" will be center-aligned:
Example 2:
p.center{
text-align: center;
color: red;
}
Example
<!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>
Grouping Selectors:
Size of the style sheets can be reduced using grouping
One can group selectors in comma-separates list.
If you have elements with the same style definitions, like this:
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p{
text-align: center;
color: red;
}
You can group the selectors, to minimize the code.
To group selectors, separate each selector with a comma.
In the example below we have grouped the selectors from the code above:
Example:
h1, h2, p {
text-align: center;
color: red;
}
CSS Universal Selector
The universal selector is used as a wildcard character. It selects all the elements on
the pages.
Example:
*{
color: green;
font-size: 20px;
}
In this example, all HTML elements are applied with this style.
Methods/Types of CSS in webpage
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
An external style sheet can be written in any text editor, and must be saved with a .css extension.
The external .css file should not contain any HTML tags.
Here is the "mystyle.css" file:
"mystyle.css"
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Internal / Embedded CSS
An internal style sheet may be used if one single HTML page has a unique style.
The internal style is defined inside the <style> element, inside the head section or
<head> tag.
Example
Internal styles are defined within the <style> element, inside the <head> section of an
HTML page:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Inline CSS
An inline style may be used to apply a unique style for a single element.
To use inline styles, add the style attribute to the relevant element. The style
attribute can contain any CSS property.
Example
Inline styles are defined within the "style" attribute of the relevant element:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
CSS Background
CSS background property is used to define the background effects on element.
There are 5 CSS background properties that affects the HTML elements:
1. background-color
2. background-image
3. background-repeat
4. background-attachment
5. background-position
1) CSS background-color
The background-color property is used to specify the background color of the element.
Example
<!DOCTYPE html>
<html>
<head>
<style>
h2,p{
background-color: #b0d4de;
}
</style>
</head>
<body>
<h2>My first CSS page.</h2>
<p>Hello, This is an example of CSS background-color.</p>
</body>
</html>
2) CSS background-image
The background-image property is used to set an image as a background of an element. By default the
image covers the entire element.
Example
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body {
background-image: url(“image.jpg");
margin-left:100px;
}
</style>
</head>
<body>
<h1>Background Image</h1>
</body>
</html>
3) CSS background-repeat
By default, the background-image property repeats the background image horizontally
and vertically. Some images are repeated only horizontally or vertically.
The background looks better if the image repeated horizontally only.
background-repeat: repeat-x;
For vertical
background-repeat: repeat-y;
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
</style>
</head>
<body>
<h1>background - horizontal.com</h1>
</body>
</html>
4) CSS background-attachment
Example:
background: white url ('good-morning.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background: white url(‘image1.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}
</style>
</head>
<body>
<h1>background - position.com</h1>
</body>
</html>
CSS colors
The color property in CSS is used to set the color of HTML elements. Typically,
this property is used to set the background color or the font color of an element.
There are different coloring schemes in CSS
Three widely used techniques are as follows
RGB
This starts with RGB and takes 3 parameter
3 parameter basically corresponds to red, green and blue
The value of each parameter may vary from 0 to 255.
Eg: RGB(255,0,0); means color red
HEX
Hex code starts with # and comprises of 6 numbers which are further divided into 3 sets
Sets basically correspond to Red, Green, and Blue
Single set value can vary from 00 to 09 and AA to FF
Eg: #ff0000 ; means color red
RGBA
This starts with RGB and takes 4 parameter
4 parameter basically corresponds to red, green, blue and alpha
Value of the first three parameters may vary from 0 to 255 and the last parameter ranges from
0 to 1 that is from 0.1, 0.2,…..0.9
Eg: RGB(255,0,0,0); means color red
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML</title>
<style>
#center{ color:#ff0099; text-align: center;}
h1{ color:rgba(255,0,0,0.5);}
</style>
</head>
<body>
<h1>This heading is CSS colors</h1>
<p style="color:rgb(255,0,0)">This paragraph will be red</p>
<p id="center">This paragraph will be pink and center-aligned</p>
</body>
</html>
CSS Font
CSS Font property is used to control the look of texts. By the use of CSS font
property you can change the text size, color, style and more.
These are some important font attributes:
1. CSS Font color: This property is used to change the color of the text. (standalone
attribute)
2. CSS Font family: This property is used to change the face of the font.
3. CSS Font size: This property is used to increase or decrease the size of the font.
4. CSS Font style: This property is used to make the font bold, italic or oblique.
5. CSS Font variant: This property creates a small-caps effect.
6. CSS Font weight: This property is used to increase or decrease the boldness and
lightness of the font.
1) CSS Font Color
CSS font color is a standalone attribute in CSS
It is used to change the color of the text.
There are three different formats to define a color:
1. By a color name
2. By hexadecimal value
3. By RGB
Set the text-color for different elements:
Example
<style>
h4 { color: red; }
h5 { color: #9000A1; }
h6 { color: rgb(0, 220, 98); }
</style>
2) CSS Font Family
The font family of a text is set with the font-family property.
<style>
h4 { font-family: sans-serif; }
h5 { font-family: serif; }
h6 { font-family: arial; }
</style>
3) CSS Font Size
CSS font size property is used to change the size of the font.
These are the possible values that can be used to set the font size:
Example
h{ font-weight: bolder; }
p { font-weight: 100; }
CSS TEXT
CSS is a language that describes the style of an HTML document. The following are the
CSS text properties of an element:
The color property is used to set the color of a text.
The direction property is used to set the text direction.
The letter-spacing property is used to add or subtract space between the letters that make
up a word.
The word-spacing property is used to add or subtract space between the words of a
sentence.
The text-indent property is used to indent the text of a paragraph.
The text-align property is used to align the text of a document.
The text-decoration property is used to underline, overline, and strikethrough text
The text-transform property is used to capitalize text or convert text to uppercase or lowercase
letters.
The white-space property is used to control the flow and formatting of text.
The text-shadow property is used to set the text shadow around a text.
syntax
text-shadow: h-shadow v-shadow blur-radius color| none | initial | inherit;
Example
<html>
<head> <title>Manipulating Text using CSS</title>
</head>
<body>
<p style = "color:red;">This text will be written in red.</p>
<p style = "direction:rtl;">This text will be rendered from right to left. </p>
<p style = "letter-spacing:5px;">This text is having space between letters.</p>
<p style = "word-spacing:5px;">This text is having space between words. </p>
<p style = "text-indent:1cm;">
This text will have first line indented by 1cm and this line will remain at
its actual position this is done by CSS text-indent property.
</p>
<p style = "text-align:right;"> This will be right aligned.</p>
<p style = "text-align:center;">This will be center aligned.</p>
<p style = "text-align:left;“>This will be left aligned.</p>
<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>
<p style = "text-decoration:blink;">This text will have blinking effect.</p>
<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>
<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>
</html>
Output
CSS Border
The border properties allow you to specify how the border of the box representing
an element should look. There are three properties of a border you can change −
The border-color specifies the color of a border.
The border-style specifies whether a border should be solid, dashed line, double
line, or one of the other possible values.
The border-width specifies the width of a border.
CSS border-color
The border-color property is used to set the color of the four borders.
The color can be set by:
name - specify a color name, like "red"
HEX - specify a HEX value, like "#ff0000"
RGB - specify a RGB value, like "rgb(255,0,0)"
HSL - specify a HSL value, like "hsl(0, 100%, 50%)"
Transparent.
Example
p.one {
border-style: solid;
border-color: red;
}
CSS border-width
The border-width property specifies the width of the four borders.
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.
Example
p.one {
border-style: solid;
border-width: 5px;
}
p.two {
border-style: solid;
border-width: medium;
}
CSS border-style
The border-style property specifies what kind of border to display. The following values
are allowed:
dotted - Defines a dotted border
dashed - Defines a dashed border
solid - Defines a solid border
double - Defines a double border
groove - Defines a 3D grooved border. The effect depends on the border-color value
ridge - Defines a 3D ridged border. The effect depends on the border-color value
inset - Defines a 3D inset border. The effect depends on the border-color value
outset - Defines a 3D outset border. The effect depends on the border-color value
none - Defines no border
hidden - Defines a hidden border
Example
<html>
<head>
</head>
<body>
<p style = "border-width:4px; border-style: double; border-color:#009900; ">
This is a double border. </p>
<p style = "border:4px solid red;">
This example is showing shorthand property for border.
</p>
</body>
</html>
CSS box model
The CSS box model is essentially a box that wraps around every HTML element.
It consists of: margins, borders, padding, and the actual content. The image below
illustrates the box model:
Box model
Explanation of the different parts:
Content - The content of the box, where text and images appear
Padding - Clears an area around the content. The padding is transparent
Border - A border that goes around the padding and content
Margin - Clears an area outside the border. The margin is transparent
The box model allows us to add a border around elements, and to define space
between elements.
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
background-color: lightgrey;
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
</style>
</head>
<body>
<h2>The Box Model</h2>
<p>The CSS box model is essentially a box that wraps around every HTML element. It consists of:
borders, padding, margins, and the actual content.</p>
</body>
</html>
CSS-Margins
CSS Margin property is used to define the space around elements. It is completely
transparent and doesn't have any background color. It clears an area around the
element.
Top, bottom, left and right margin can be changed independently using separate
properties.
There are following CSS margin properties:
Property Description
margin This property is used to set all the properties in one declaration.
margin-left it is used to set left margin of an element.
margin-right It is used to set right margin of an element.
margin-top It is used to set top margin of an element.
margin-bottom It is used to set bottom margin of an element.
All the margin properties can have the following values:
auto - the browser calculates the margin
length - specifies a margin in px, pt, cm, etc.
% - specifies a margin in % of the width of the containing element
inherit - specifies that the margin should be inherited from the parent element
Example
1. margin: 50px 100px 150px 200px;
It identifies that:
top margin value is 50px
right margin value is 100px
bottom margin value is 150px
left margin value is 200px
4. margin 50px;
top right bottom and left margin values are 50px
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
background-color: pink;
}
p.ex {
margin: 50px 100px 150px;
}
</style>
</head>
<body>
<p>This paragraph is not displayed with specified margin. </p>
<p class="ex">This paragraph is displayed with specified margin.</p>
</body>
</html>
CSS-Padding
CSS Padding property is used to define the space between the element content and the
element border.
CSS padding is affected by the background colors. It clears an area around the content.
Top, bottom, left and right padding can be changed independently using separate
properties. It is possible to change all properties at once by using shorthand padding
property.
Property Description
padding It is used to set all the padding properties in one declaration.