Css Unit4 Notes
Css Unit4 Notes
CSS Introduction
CSS stands for Cascading Style Sheets. It is a style sheet language that describes the
look and document's formatting written in the markup language. It helps us to add
new looks to our old HTML documents. By making some changes in the CSS code, we
can easily change the look of the website. It provides an additional feature to HTML.
CSS reduces the work by controlling the layout of multiple web pages. CSS is easy to
maintain and has good community support.
It is generally used with HTML to change the style of web pages and user interfaces. It
can also be used with any kind of XML documents, including plain XML, SVG, and XUL.
CSS is used along with HTML and JavaScript in most websites to create user interfaces
for web applications and user interfaces for many mobile applications.
Both HTML and CSS are client-side web scripting languages that are used for creating
web pages. There are many ways that create the difference between HTML and CSS,
such as the implementing methods, their syntactical structure, ease of use, and the
features like attributes.
Benefits of CSS
• CSS saves time: You can write CSS once and reuse the same sheet in
multiple HTML pages.
• Easy Maintenance: To make a global change simply change the style,
and all elements in all the webpages will be updated automatically.
• Search Engines: CSS is considered a clean coding technique, which
means search engines won’t have to struggle to “read” its content.
• 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.
• Offline Browsing: CSS can store web applications locally with the help
of an offline cache. Using this we can view offline websites.
CSS versions release years:
CSS 1 is the very first version of the cascading style sheet and recommendation of W3C. It was launched
in 1996 with the capabilities of font properties. It is also used for adding color to the background and
text side. In CSS 1, there were text alignment functionalities. It also has capabilities of padding,
positioning, and generic classifications. But now, this version is outdated and not maintained by W3C.
CSS2
W3C developed the next version of CSS and named it CSS2, and launched it in 1998. It has more
features and functionalities than the previous version. And now users could use new features like
relative, absolute, and also fixed positioning. There were media types, and bidirectional text features
were also there. This version also saw many revisions in the same, and updates came as CSS2.1.
CSS3
CSS3 is the latest version of CSS officially by W3C, and it was launched in 1999.
It has a vast collection of font types, and you can use any font type from Google
and Typecast. Also, this version is divided into many modules that make it easy
to handle, and it also saves time formatting the web pages. Currently, most
companies and organizations use CSS3 and HTML5 for their web development and
designing tasks.
What is CSS3?
The first version of CSS didn’t exist until after HTML had been around for a
few years, becoming official in 1996. Like HTML5 and its relationship to
earlier versions of HTML, CSS3 is a natural extension of the versions of CSS
that preceded it. CSS3 is more powerful than earlier versions of CSS and
introduces numerous visual effects, such as drop shadows, text shadows,
rounded corners, and gradients.
Implementation It is for web page structure and It is mainly for design and presentation.
content.
Architecture HTML uses tags that are It consists of selectors that are declared
surrounding the content of any using block statement syntax.
web page element.
Approach We use it mainly to develop the Mainly it is used for page style formats,
basic content of the web page. designing of the web, layouts, and many
more.
Support HTML has a lot of community It also has large community support and
support that helps to utilize a huge backup for continuous
different web page structure improvements in web designing.
approaches.
The other common differences between HTML and CSS are given as follows:
o HTML is easy to learn with a clear syntax, whereas the CSS sometimes create
complications in code and get messy.
o HTML files can include the CSS code, but it is not the same with CSS because CSS can
never contain the HTML codes.
o HTML uses tags, whereas the CSS uses selectors.
o HTML is used to create web pages, whereas the CSS controls the layout and styling of
web pages.
Browser-Specific Prefixes
Browser vendors sometimes add prefixes to experimental or
nonstandard CSS properties and JavaScript APIs, so developers
can experiment with new ideas while—in theory—preventing
their experiments from being relied upon and then breaking web
developers’ code during the standardization process. Developers
should wait to include the unprefixed property until browser
behavior is standardized.
Sample usage:
The reason that you always end your declaration with the
normal, non-prefixed version of the CSS property is so that when
a browser does support the rule, it will use that one. The later
rules take precedence over earlier ones if the specificity is the
same, so a browser would read the vendor version of a rule and
use that if it does not support the normal one, but once it does,
it will override the vendor version with the actual CSS rule.
-webkit-border-top-left-radius: 10px;
But now that browsers have come to fully support this feature,
you really only need the standardized version:
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.
You can put CSS Style Rule Syntax as follows −
CSS Selector
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.
<!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>
Output:
Me too!
And me!
2) CSS Id Selector
The id selector selects the id attribute of an HTML element to select a
specific element. An id is always unique within the page so it is chosen to
select a single, unique element.
It is written with the hash character (#), followed by the id of the element.
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<p id="para1">Hello World</p>
<p>This paragraph will not be affected.</p>
</body>
</html>
Output:
Hello World
<!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>
Output:
If you want to specify that only one specific HTML element should be
affected then you should use the element name with class selector.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
p.center {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<h1 class="center">This heading is not affected</h1>
<p class="center">This paragraph is blue and center-aligned.</p>
</body>
</html>
Output:
<!DOCTYPE html>
<html>
<head>
<style>
*{
color: green;
font-size: 20px;
}
</style>
</head>
<body>
<h2>This is heading</h2>
<p>This style will be applied on every paragraph.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>
Output:
This is heading
Me too!
And me!
h1 {
text-align: center;
color: blue;
}
h2 {
text-align: center;
color: blue;
}
p{
text-align: center;
color: blue;
}
As you can see, you need to define CSS properties for all the elements. It
can be grouped in following ways:
h1,h2,p {
text-align: center;
color: blue;
}
<!DOCTYPE html>
<html>
<head>
<style>
h1, h2, p {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<h1>Hello World</h1>
<h2>Hello World(In smaller font)</h2>
<p>This is a paragraph.</p>
</body>
</html>
Use the child selector, if you want to select all elements immediate
children of a specified element.
div > p
Example
<!DOCTYPE html>
<html>
<head>
<style>
div > p {
background-color: orange;
}
</style>
</head>
<body>
<div>
<p>Para 1 in the div.</p>
<!-- This is not a Child -->
<span><p>Para 2 in the div.</p></span>
</div>
<p>Para 3 outside the div.</p>
</body>
</html>
Output
Descendant Selector
The descendant selector in CSS is used to match all elements that are
descendants of a specified element.
Example
<!DOCTYPE html>
<html>
<head>
<style>
div p {
background-color: orange;
}
</style>
</head>
<body>
<div>
<p>Para 1 in the div</p>
<p>Para 2 in the div</p>
</div>
<p>Para 3 outside the div.</p>
</body>
</html>
Output
Attribute Selectors
Style HTML Elements With Specific Attributes
Example
a[target] {
background-color: yellow;
}
Program:
<!DOCTYPE html>
<html>
<head>
<style>
a[target] {
background-color: yellow;
}
</style>
</head>
<body>
<a href="https://www.w3schools.com">w3schools.com</a>
<a href="http://www.disney.com" target="_blank">disney.com</a>
<a href="http://www.wikipedia.org" target="_top">wikipedia.org</a>
</body>
</html>
Output:
CSS [attribute="value"] Selector
The [attribute="value"] selector is used to select elements with a specified
attribute and value.
Example
a[target="_blank"] {
background-color: yellow;
}
The following example selects all elements with a title attribute that
contains a space-separated list of words, one of which is "flower":
Example
[title~="flower"] {
border: 5px solid yellow;
}
CSS [attribute|="value"] Selector
The [attribute|="value"] selector is used to select elements with the
specified attribute, whose value can be exactly the specified value, or the
specified value followed by a hyphen (-).
Note: The value has to be a whole word, either alone, like class="top", or
followed by a hyphen( - ), like class="top-text".
Example
[class|="top"] {
background: yellow;
}
The following example selects all elements with a class attribute value that
starts with "top":
Example
[class^="top"] {
background: yellow;
}
The following example selects all elements with a class attribute value that
ends with "test":
The following example selects all elements with a class attribute value that
contains "te":
Example
[class*="te"] {
background: yellow;
}
CSS - Colors
CSS uses color values to specify a color. 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.
You can specify your color values in various formats. Following table lists all the
possible formats −
#000000
#FF0000
#00FF00
#0000FF
#FFFF00
#00FFFF
#FF00FF
#C0C0C0
#FFFFFF
CSS Colors - Short Hex Codes
This is a shorter form of the six-digit notation. In this format, each digit is replicated
to arrive at an equivalent six-digit value. For example: #6A7 becomes #66AA77.
A hexadecimal value can be taken from any graphics software like Adobe Photoshop,
Jasc Paintshop Pro, or even using Advanced Paint Brush.
Each hexadecimal code will be preceded by a pound or hash sign '#'. Following are
the examples to use Hexadecimal notation.
#000
#F00
#0F0
#0FF
#FF0
#0FF
#F0F
#FFF
rgb(255,0,0)
rgb(0,255,0)
rgb(0,0,255)
rgb(255,255,0)
rgb(0,255,255)
rgb(255,0,255)
rgb(192,192,192)
rgb(255,255,255)
CSS3 – Colors
CSS3 has Supported additional color properties as follows −
• RGBA colors
• HSL colors
• HSLA colors
• Opacity
RGBA stands for Red Green Blue Alpha.It is an extension of CSS2,Alpha specifies
the opacity of a color and parameter number is a numerical between 0.0 to 1.0. A
Sample syntax of RGBA as shown below −
#d1 {background-color: rgba(255, 0, 0, 0.5);}
#d2 {background-color: rgba(0, 255, 0, 0.5);}
#d3 {background-color: rgba(0, 0, 255, 0.5);}
HSL stands for hue, saturation, lightness.Here Huge is a degree on the color wheel,
saturation and lightness are percentage values between 0 to 100%. A Sample syntax
of HSL as shown below −
#g1 {background-color: hsl(120, 100%, 50%);}
#g2 {background-color: hsl(120, 100%, 75%);}
#g3 {background-color: hsl(120, 100%, 25%);}
HSLA stands for hue, saturation, lightness and alpha. Alpha value specifies the
opacity as shown RGBA. A Sample syntax of HSLA as shown below −
#g1 {background-color: hsla(120, 100%, 50%, 0.3);}
#g2 {background-color: hsla(120, 100%, 75%, 0.3);}
#g3 {background-color: hsla(120, 100%, 25%, 0.3);}
opacity is a thinner paints need black added to increase opacity. A sample syntax of
opacity is as shown below −
#g1 {background-color:rgb(255,0,0);opacity:0.6;}
#g2 {background-color:rgb(0,255,0);opacity:0.6;}
#g3 {background-color:rgb(0,0,255);opacity:0.6;}
The following example shows rgba color property.
<html>
<head>
<style>
#p1 {background-color:rgba(255,0,0,0.3);}
#p2 {background-color:rgba(0,255,0,0.3);}
#p3 {background-color:rgba(0,0,255,0.3);}
</style>
</head>
<body>
<p>RGBA colors:</p>
<p id = "p1">Red</p>
<p id = "p2">Green</p>
<p id = "p3">Blue</p>
</body>
</html>
<html>
<head>
<style>
#g1 {background-color:hsl(120, 100%, 50%);}
#g2 {background-color:hsl(120,100%,75%);}
#g3 {background-color:hsl(120,100%,25%);}
</style>
</head>
<body>
<p>HSL colors:</p>
<p id = "g1">Green</p>
<p id = "g2">Normal Green</p>
<p id = "g3">Dark Green</p>
</body>
</html>
<html>
<head>
<style>
#d1 {background-color:hsla(120,100%,50%,0.3);}
#d2 {background-color:hsla(120,100%,75%,0.3);}
#d3 {background-color:hsla(120,100%,25%,0.3);}
</style>
</head>
<body>
<p>HSLA colors:</p>
<p id = "d1">Less opacity green</p>
<p id = "d2">Green</p>
<p id = "d3">Green</p>
</body>
</html>
<html>
<head>
<style>
#m1 {background-color:rgb(255,0,0);opacity:0.6;}
#m2 {background-color:rgb(0,255,0);opacity:0.6;}
#m3 {background-color:rgb(0,0,255);opacity:0.6;}
</style>
</head>
<body>
<p>HSLA colors:</p>
<p id = "m1">Red</p>
<p id = "m2">Green</p>
<p id = "m3">Blue</p>
</body>
</html>
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.
<!DOCTYPE html>
<html>
<head>
<style>
h2,p{
background-color: #b0d4de;
}
</style>
</head>
<body>
<h2>My first CSS page.</h2>
<p> This is an example of CSS background-color.</p>
</body>
</html>
Output:
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. You can set the background image for a page like
this.
Ex:<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("smiley.jpg");
margin-left:100px;
}
</style>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>
o/p:
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.
background-repeat: repeat-x;
Ex:<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
</style>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>
o/p:
background-repeat: repeat-y;
Ex:<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-y;
}
</style>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>
o/p:
4) CSS background-attachment
The background-attachment property is used to specify if the background image is fixed or
scroll with the rest of the page in browser window. If you set fixed the background image then
the image will not move during scrolling in the browser. Let?s take an example with fixed
background image.
5) CSS background-position
The background-position property is used to define the initial position of the background
image. By default, the background image is placed on the top-left of the webpage.
1. center
2. top
3. bottom
4. left
5. right
CSS - Cursors
The cursor property of CSS allows you to specify the type of cursor that should be displayed
to the user.
One good usage of this property is in using images for submit buttons on forms. By default,
when a cursor hovers over a link, the cursor changes from a pointer to a hand. However, it
does not change form for a submit button on a form. Therefore, whenever someone hovers
over an image that is a submit button, it provides a visual clue that the image is clickable.
The following table shows the possible values for the cursor property −
Shape of the cursor depends on the context area it is over. For example an I over text, a hand
over a link, and so on...
2
crosshair
3
default
An arrow
4
pointer
5
move
The I bar
6
e-resize
7
ne-resize
The cursor indicates that an edge of a box is to be moved up and right (north/east)
8
nw-resize
The cursor indicates that an edge of a box is to be moved up and left (north/west)
<html>
<head>
</head>
<body>
<p>Move the mouse over the words to see the cursor change:</p>
</body>
</html>
CSS - Text
This chapter teaches you how to manipulate text using CSS properties. You can set following
text properties of an element −
<html>
<head>
</head>
<body>
<p style = "color:red;">
This text will be written in red.
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<p style = "direction:rtl;">
This text will be rendered from right to left
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<p style = "letter-spacing:5px;">
This text is having space between letters.
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<p style = "word-spacing:5px;">
This text is having space between words.
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<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>
</body>
</html>
<html>
<head>
</head>
<body>
<p style = "text-align:right;">
This will be right aligned.
</p>
<html>
<head>
</head>
<body>
<p style = "text-decoration:underline;">
This will be underlined
</p>
<html>
<head>
</head>
<body>
<p style = "text-transform:capitalize;">
This will be capitalized
</p>
CSS - Lists
Lists are very helpful in conveying a set of either numbered or bullet points. This chapter
teaches you how to control list type, position, style, etc., using CSS.
We have the following five CSS properties, which can be used to control lists −
Here are the values which can be used for an unordered list −
Sr.No. Value & Description
1
none
NA
2
disc (default)
A filled-in circle
3
circle
An empty circle
4
square
A filled-in square
Here are the values, which can be used for an ordered list −
Here is an example −
<html>
<head>
</head>
<body>
<ul style = "list-style-type:circle;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
1
None
NA
2
Inside
If the text goes onto a second line, the text will wrap underneath the marker. It will also appear
indented to where the text would have started if the list had a value of outside.
3
Outside
If the text goes onto a second line, the text will be aligned with the start of the first line (to the
right of the bullet).
Here is an example −
<html>
<head>
</head>
<body>
<ul style = "list-style-type:circle; list-stlye-position:outside;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
Here is an example −
<html>
<head>
</head>
<body>
<ul>
<li style = "list-style-image: url(/https/www.scribd.com/images/bullet.gif);">Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ol>
<li style = "list-style-image: url(/https/www.scribd.com/images/bullet.gif);">Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
</body>
</html>
Here is an example −
<html>
<head>
</head>
<body>
<ul style = "list-style: inside square;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<html>
<head>
</head>
<body>
<p style = "font-family:georgia,garamond,serif;">
This text is rendered in either georgia, garamond, or the
default serif font depending on which font you have at your system.
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<p style = "font-style:italic;">
This text will be rendered in italic style
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<p style = "font-variant:small-caps;">
This text will be rendered as small caps
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<p style = "font-weight:bold;">
This font is bold.
</p>
<html>
<head>
</head>
<body>
<p style = "font-size:20px;">
This font size is 20 pixels
</p>
Shorthand Property
You can use the font property to set all the font properties at once. For example −
<html>
<head>
</head>
<body>
<p style = "font:italic small-caps bold 15px georgia;">
Applying all the properties on the text at once.
</p>
</body>
</html>
CSS - Links
You can set following properties of a hyper link −
We will revisit the same properties when we will discuss Pseudo-Classes of CSS.
Remember a:hover MUST come after a:link and a:visited in the CSS definition in order to be
effective. Also, a:active MUST come after a:hover in the CSS definition as follows −
<style type = "text/css">
a:link {color: #000000}
a:visited {color: #006600}
a:hover {color: #FFCC00}
a:active {color: #FF00CC}
</style>
Now, we will see how to use these properties to give different effects to hyperlinks.
<html>
<head>
<style type = "text/css">
a:link {color:#000000}
</style>
</head>
<body>
<a href = "">Link</a>
</body>
</html>
<html>
<head>
<style type = "text/css">
a:visited {color: #006600}
</style>
</head>
<body>
<a href = ""> link</a>
</body>
</html>
It will produce the following link. Once you will click this link, it will change its color to green.
Change the Color of Links when Mouse is Over
The following example demonstrates how to change the color of links when we bring a
mouse pointer over that link. Possible values could be any color name in any valid format.
<html>
<head>
<style type = "text/css">
a:hover {color: #FFCC00}
</style>
</head>
<body>
<a href = "">Link</a>
</body>
</html>
It will produce the following link. Now, you bring your mouse over this link and you will see
that it changes its color to yellow.
<html>
<head>
<style type = "text/css">
a:active {color: #FF00CC}
</style>
</head>
<body>
<a href = "">Link</a>
</body>
</html>
It will produce the following link. It will change its color to pink when the user clicks it.