FSD Module 2 Css
FSD Module 2 Css
Cascading Style
Sheet
Full Stack Development & Department of Computer Science &
1
20CS2406 Engineering
Levels in CSS
Lower level style sheets can override higher level style sheets ,so the style
of the content of a tag is determined ,in effect ,through a cascade of style
sheet applications.
• Three levels of style sheets , from lowest level to highest level are
• Inline
• Document
• External
• Inline style sheets apply to content of single XHTML element.
• Document level style sheets apply to the whole body of a document.
• External Style sheets can apply to the bodies of any number of
documents.
• Inline style sheets have precedence over document style
sheets ,which have precedence over external style sheets
• Inline style specifications appear within the opening tag and apply only
to the content of that tag
• Document level style specifications appear in the document head
section and apply to the entire body of the document.
• External Style sheets stored separately and are referenced in all
documents that use them
• External style sheets are written as text files with the MIME type
text/css.
• They can be stored on any computer on the web .
• The browser fetches external style sheets just as it fetches documents.
• External style sheets are written as text files with the MIME type text/css
.
• They can be stored on any computer on the web .The browser fetches
external style sheets just as it fetches documents.
• The <link > tag is used to specify external style sheets. Within <link> , the
rel attribute is used to specify the relationship of the linked-to document
to the document in which the link appears. The href attribute of the <link>
is used to specify the URL of the style sheet document.
Selector {Property1.value1;Property2.Value2; ,
Property3.Value3 , ………………..Propertyn.Valuen;}
The selector can be any tag .If the new properties for a tag are
not mentioned within the rule list , then the browser uses
default behavior of a tag.
</head>
<body>
<p class=“one”> Computer Science and Engineering </p>
<p class=“two”> Electronics Engineering and Technology</p>
</body>
</html>
Padding:1.5 cm}
</head>
<body>
<p class=“one” > Computer Science and Engineering </p>
<h1 class=“one”> Electronics Technology and Engineering</h1>
<h6 class=“one “> Mechanical Engineering and Technology</h6>
</body>
</html>
<body>
<p id =“two” > Computer Science and Engineering </p>
<h1 id=“one”> Electronics Technology and Engineering</h1>
<body>
<p id =“three” > Computer Science and Engineering </p>
</body>
</html>
<body>
<h1 > Computer Science and Engineering </h1>
<h2 > Electronics Engineering and Technology</h2>
<h3 > Mechanical Engineering and Technology </h3>
<p> Dayananda Sagar University </p>
</body>
</html>
<h2>Welcome to My Homepage</h2>
<div>
<p>My name is Donald.</p>
<p>I live in Duckburg.</p>
</div>
</body>
</html>
CONTEXTUAL SELECTORS ARE AS FOLLOWS
The element element selector in CSS is used to select elements
inside the elements i.e
Element Element Selector it combines two selectors such that elements matched by the
(Also called Space selectors) second selector are selected if they have an ancestor element
matching the first selector.
<!DOCTYPE html>
<html>
<head>
<style>
div p {
background-color: yellow;
}
</style>
</head>
<body>
<div >
<p >My name is Donald.</p>
<p >I live in Duckburg.</p>
<h1>ghsadjs</h1>
</div>
</body>
</html>
<!-- HTML code to illustrates space selector -->
<!DOCTYPE html>
<html>
<head>
<title>
element element Selector
</title>
<style>
div p {
background-color: green;
color: white;
}
</style>
</head>
<div>
<h2>element element Selector</h2>
<div id="container">
<p>First</p>
<div>
<p>Child Paragraph</p>
</div>
<p>Second</p>
<p>Third</p>
</div>
CSS
element>element
Selector
element>elementSelector
div > p {
background-
color: yellow;
}
<!DOCTYPE html>
<html>
The element>element <head>
selector is used to select elements <style>
with a specific parent. div > p {
Note: Elements that are not background-color: yellow;
directly a child of the specified }
parent, are not selected.
</style>
</head>
Note: The <div> tag is used as a
container for HTML elements <body>
<div>
<h2>My name is Donald</h2>
<p>I live in Duckburg.</p>
</div>
<div>
<span><p>I will not be styled.</p></span>
Note for ur knowledge::The <p> tag
</div>
cannot contain tables and other block-
level elements.
<p>My best friend is Mickey.</p>
</body>
</html>
div#container > p {
border: 1px solid black;
It will target elements which are }
DIRECT children of a particular
element.
It will target all P element which are
direct children of container div, not
children of child div.
The first-child selector specifies the first child of the element to whose
name it is attached. For example, consider the following:
p:first-child {property-value list}
The properties in the list are applied to the first child element of each p element.
The last-child selector specifies the last child of the element to whose
name it is attached. For example, consider the following:
p:last-child {property-value list}
The properties in the list are applied to the last child element of each p element.
The only-child selector specifies the child of the element to whose name
it is attached, but only if that child is the only child of the element. For example,
consider the following:
p:only-child {property-value list}
The properties in the list are applied to the child element of each p element that
has just one child element.
The empty selector specifies the element to which it is attached when that
element has no child elements. For example, consider the following:
p:empty {property-value list}
The properties in the list are applied to each p element that has no child elements.
NOTE: DO THE EXAMPLES
Child selectors can be specified over any number
of elements in a family
hierarchy (not just one generation). For example,
the following selector selects em
elements that are children of h1 elements that
are children of paragraph elements:
div > h1 > em {property-value list}
<!DOCTYPE html>
<html>
<head>
EXAMPLE OF P:__ <style>
p:first-child {
background-color: yellow;
}
</style>
</head>
<body>
<div>
<p>This paragraph is the first child of its parent (div).</p>
<p>This paragraph is not the first child of its parent (div).</p>
</div>
</body>
</html>
GO THROUGH + AND ~
SYMBOLS IN CSS SELECTORS
FOR YOUR MORE
KNOWLEDGE
Style Specification Format:
Pseudo Classes : Pseudo class selectors are used if properties are to be
changed dynamically .
Example : When mouse movement happens ,in other words hover
happens or focus happens .
< !DOCTYPE html>
<html>
<head> <body>
<title> Sample CSS </title> <form action=“”>
<style type =“text/css”> <p>
Input : focus <label>
{ font-family : lucida Calligraphy ; NAME:
color : purple ; <input type=“text”/>
font-size :100pt; </label>
} </p>
</style> </form>
</head> </body>
</html>
selector:pseudo-class{
property: value;
<!DOCTYPE html>
<html>
<head>
<title>CSS Bullets</title>
<style type = "text/css">
li.one {list-style-type:disc}
li.two{list-style-type:square}
li.three{list-style-type:circle}
<!DOCTYPE html>
<html>
<head>
<title>CSS Bullets-Image</title>
<style type = "text/css">
li.image {list-style-image: url(arrow.png); font-size:25pt;}
</style>
</head>
•
Full Stack Development & Department of Computer Science &
67
20CS2406 Engineering
Color:
navy 000080
● border-width: thin;
○ all four borders are thin
Note: Always declare the border-style property before the border-width property. An element must have borders
before you can set the width.
Example :
<html>
<head>
<title> Table with border effects </title>
<style type = "text/css">
table
{
border-width:thick;
border-top-color:red;
border-left-color:orange;
border-bottom-color:violet;
border-right-color:green;
border-top-style:dashed;
border-bottom-style:double;
border-right-style:dotted;
}
p.one {
border-style: solid;
border-width: 5px 20px; /* 5px top and bottom, 20px on the sides
*/
/*try the following also, top|left&right|bottom
border-width:5px 10px 20px; */
}
p.two {
border-style: solid;
border-width: 20px 5px; /* 20px top and bottom, 5px on the sides
*/
}
p.three {
border-style: solid;
border-width: 25px 10px 4px 35px; /* 25px top, 10px right, 4px
bottom and 35px left */
}Full Stack Development & Department of Computer Science &
81
20CS2406 Engineering
You can also specify all the individual border
properties for just one side:
Left Border
p{
background-color: lightgrey;
Value Description
</div>
<h2>box-shadow: 5px 10px 18px #888888:</h2>
<div id="example2">
<p>More blurred.</p>
</div>
<h2>box-shadow: 5px 10px 18px red:</h2>
<div id="example3">
<p>More blurred and red.</p>
</div>
</body>
</html>
– padding-left.
– padding-right
– padding-top
– padding -bottom
p.three
{margin:0.3in;
background-color:#FFFF00;
}
p.four
{
padding:0.3in;
background-color:#FF9900;
}
</style>
</head>
<body>
</body>
</html>
Back Ground Image:
• The background-image property is used to place an
image in the background of an element.
• Example code to display background image
<html>
<head>
<title>Background Image</title>
<style type = "text/css">
body {background-image:url(DSU.jpg);}
p
{text-align: justify; color:blue;font-size:20pt;}
</style>
</head>
<body>
•
Full Stack Development & Department of Computer Science &
93
20CS2406 Engineering
Back Ground Image:
<p >WELCOME TO DAYANANDA SAGAR UNIVERSIT
Dayananda Sagar Institutions founded in the 60s by one such visionary, late Sri Dayananda
Sagar committed to take knowledge to the people, transforms today’s students into
responsible citizens and professional leaders of tomorrow. Dayananda Sagar University
created by an Act of the Karnataka State in 2014, built on this adorable legacy and inspired
by its own milestones, meeting the needs of quality higher education in this part of the
world.Universities of great legacy across the world are the invaluable contribution of certain
visionaries to the world. Universities don’t manufacture products with specific use and
determined life cycle. They share & impart multitudes of streams of knowledge and create
wonderful human beings – learned practitioners & Disseminators of knowledge to make the
world a better place to be. These Universities of great significancehave lived through the
centuries building centers of knowledge and great alumni of such Universities.</p>
</body>
</html>
}
p#two
{
border: 1px dashed red ;
padding:30px;
background-color:purple;
box-shadow:-5px -10px 10px green;
margin:50px;
background-image:url('smiley.gif');
background-repeat:repeat-y;
</style>
</head>
<p id="one"> I will have a box shadow</p>
<p id="two"> I will have a box shadow</p>
Back Ground Image:
• The keyword values are top, center ,bottom ,left and right .all
of which can be used in many different combinations.
• Both these specifications apply to all h3 elements in the body of the document.
The browser has to resolve this conflict
2) Another source of conflict can arise from the fact that there can be several different origins of the
specification of property values. For example, they may come from a style sheet written by the author of
the document itself but they may come from the browser user and from the browser.
For example, an FX user can set a minimum font size in the Tools-Options-Advanced window.
Furthermore, browsers allow their users to write and use their own style sheets
Origin of Cascading
Origin of Style sheets may have three different origins: author, user, and user agent.
* Author: The author specifies style sheets for a source document according to the conventions of the document language. For
Cascading instance, in HTML, style sheets may be included in the document or linked externally.
* User: The user may be able to specify style information for a particular document. For example, the user may specify a file
that contains a style sheet or the user agent may provide an interface that generates a user style sheet (or behave as if it did).
* User agent: Conforming user agents must apply a default style sheet (or behave as if they did) prior to all other style sheets
for a document. A user agent's default style sheet should present the elements of the document language in ways that satisfy
general presentation expectations for the document language (e.g., for visual browsers, the EM element in HTML is presented
using an italic font). See "A sample style sheet for HTML 4.0" for a recommended default style sheet for HTML 4.0 documents.
Note that the default style sheet may change if system settings are modified by the user (e.g., system colors). However, due to
limitations in a user agent's internal implementation, it may be impossible to change the values in the default style sheet.
Style sheets from these three origins will overlap in scope, and they interact according to the cascade.
The CSS cascade assigns a weight to each style rule. When several rules apply, the one with the greatest weight takes
precedence.
By default, rules in author style sheets have more weight than rules in user style sheets. Precedence is reversed, however, for "!
important" rules. All rules user and author rules have more weight than rules in the UA's default style sheet.
Conflict Resolution:
● There can be several different origins of the specification of property values
• One of the value may come from a style sheet created by the author or it can be
specified by the user using the options provided by the browser.
• The property values with different origin have different precedence
.myclass {
background-color: gray;
}
p{
background-color: red !important;
}
</style>
</head>
<body>
</body>
</html>
• If there are other conflicts even after sorting ,The next step is
sorting by specificity
• Rules are
1. id selectors
2. Class and pseudo class selectors
3. Contextual selectors (more element type names means
that they are more specific)
4. Universal selectors
• If there still conflicts, they are resolved by giving precedence
to most recently seen specification
<html>
<head>
<style>
img {
opacity: 0.2;
}
</style>
</head>
<body>
<h1>Image Transparency</h1>
<p>The opacity property specifies the transparency of an element. The
lower the value, the more transparent:</p>
<p>Image with 20% opacity:</p>
<img src="klematis.jpg" alt="Forest" width="170" height="100">
</body>
</html>
<html>
<head>
<style>
img {
opacity: 0.5;
}
</style>
</head>
<body>
<h1>Image Transparency</h1>
<p>The opacity property specifies the transparency of an element. The
lower the value, the more transparent:</p>
<p>Image with 50% opacity:</p>
<img src="klematis.jpg" alt="Forest" width="170" height="100">
</body>
</html>
<html>
<head>
<style>
img {
opacity: 1.0;
}
</style>
</head>
<body>
<h1>Image Transparency</h1>
<p>The opacity property specifies the transparency of an element. The
lower the value, the more transparent:</p>
<p>Image with 100% opacity:</p>
<img src="klematis.jpg" alt="Forest" width="170" height="100">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
#rcorners1 {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}
img{
opacity:.4;
}
p#example1
{
border:solid 5px red;
border-width:5px 20px;
border-top-width:50px;
background-color:yellow;
background-image:url('smiley.gif');
background-repeat:no-repeat;
border-radius:10px;
background-position:center;
</style>
<body>
</body>
CSS Attribute Selector
• Style HTML elements with specific Attributes
It is possible to Style HTML elements that have
specific Attributes or Attribute values .
• CSS [Attribute] selector
The [attribute] selector is used to select elements
with a specified attribute.
• The following example selects all <a> elements with a target
attribute
a[target]{
background-color :yellow;
}
<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>