Introduction to CSS-1
Introduction to CSS-1
(CSS): Part 1
What is CSS?
• CSS stands for Cascading Style Sheets. It is the language for describing the presentation of Web pages, including
colors, layout, and fonts, thus making our web pages presentable to the users.
• CSS is designed to make style sheets for the web. It is independent of HTML and can be used with any XML-
based markup language.
Cascading: Falling of Styles
Style: Adding designs/Styling our HTML tags
Sheets: Writing our style in different documents
CSS Syntax
Selector { Property 1 : value; Property 2 : value; Property 3 : value; }
Selector: selects the element you want to target. It could be any tag like <h1>, <title> etc
Declaration Block: The declaration block can contain one or more declarations separated by a semicolon. For the
below example, there are two declarations:
color: yellow; font-size: 11 px;
Each declaration contains a property name and value, separated by a colon.
Property: A Property is a type of attribute of HTML element. properties(attributes) like color, font-size,
background, width, height ,etc.
Value: Values are assigned to CSS properties. In the above example, value "yellow" is assigned to color
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.
There are several different types of selectors in CSS.
• CSS Element Selector
• CSS Id Selector
• CSS Class Selector
• CSS Universal Selector
• CSS Group Selector
Element Type Selectors
• The most basic CSS selectors are Element Type Selectors. That's a fancy name for simply using an
HTML tag, without the angle braces.
p{
text-align: center;
color: blue;
}
Id Selectors
Match an element that has the specified id.
To match a specific id attribute, we always start the selector with a hash symbol (#), to signify that we are
looking for an id value.
An id is always unique within the page so it is chosen to select a single, unique element.
For example, if we wanted the element with an id of "content", we would use the following CSS rule:
#content { border: 2px solid green; }
#para1 {
text-align: center;
color: blue;
}
Class Selectors
Match an element that has the specified class. The class selector selects HTML elements with a specific
class attribute.
It is used with a period character . (full stop symbol) followed by the class name.
Note: A class name should not be started with a number.
.center {
text-align: center;
color: blue;
}
CSS Class Selector for specific element
If you want to specify that only one specific HTML element should be affected then you should use the element
name with class selector.
p.center {
text-align: center;
color: blue;
}
Pseudo-classes
A pseudo-class is used to define a special state of an element.
For example, it can be used to:
• Style an element when a user mouses over it
• Style visited and unvisited links differently
• Style an element when it gets focus
Syntax
/* selected link */
A pseudo-class starts with a colon (:). Let's see its syntax.
selector: pseudo-class { a:active {
property: value; color: blue;
}
Ex: }
<!DOCTYPE html> </style>
<html>
</head>
<head>
<style> <body>
/* unvisited link */
<h2>Styling a link depending on state</h2>
a:link {
color: red; <p><b><a href="http://www.rvrjc.ac.in"
} target="_blank">This is a link</a></b></p>
/* visited link */
<p><b>Note:</b> a:hover MUST come after a:link and
a:visited {
a:visited in the CSS definition in order to be effective.</p>
color: green;
} <p><b>Note:</b> a:active MUST come after a:hover in
/* mouse over link */ the CSS definition in order to be effective.</p>
a:hover {
</body>
color: hotpink;
Universal Selector
h1 {
The universal selector is used as a wildcard
character. It selects all the elements on the pages. text-align: center; color: blue;
*{
}
color: green;
font-size: 20px;
h2 {
} text-align: center; color: blue;
CSS Group Selector }
The grouping selector is used to select all the p{
elements with the same style definitions.
Grouping selector is used to minimize the code.
text-align: center; color: blue;
Commas are used to separate each selector in }
grouping.
It can be grouped in following way
h1,h2,p {
text-align: center; color: blue;
}
CSS How-To
There are 3 ways to write CSS in our HTML file.
• Inline CSS
• Internal CSS
• External CSS
Priority order
• Inline > Internal > External
Inline CSS
• Uniquely applied on each element
Ex:
<h3 style=” color: red”> Have a great day </h3>
<p style =” color: green”> I did this , I did that </p>
Internal CSS
Uniquely applied on a single document. With the help of style tag, we can apply styles within the HTML
file
< style>
h1{ color:red; }
</style>
<h3> Have a great day </h3>
External CSS
External CSS is used to apply CSS on multiple pages or all pages. Here, we write all the CSS code in a
css file. Its extension must be .css for example style.css.
With the help of <link> tag in the head tag, we can apply styles
<head>
<link rel="stylesheet" type="text/css" href="name of the Css file">
</head>
h1{ color:red; //.css file }
Inline CSS
• We can apply CSS in a single element by inline CSS technique.
• The inline CSS is also a method to insert style sheets in HTML document. This method mitigates some
advantages of style sheets so it is advised to use this method sparingly.
• Not an efficient way to write as it has a lot of redundancy
<!DOCTYPE html>
<html>
<body>
<h1 style="color:red;margin-left:40px;">Inline CSS is applied on this heading.</h1>
<p>This paragraph is not affected.</p>
</body>
</html>
Inline CSS is applied on this heading.
This paragraph is not affected.
Ex2:
<!DOCTYPE html>
<html>
<head>
<title>Inline Styles</title>
</head>
<body>
<p>This text does not have any style applied to it.</p>
<p>This text has the <em>font-size</em> style applied to it, making it 20pt.
</p>
<p style = "font-size: 20pt; color: deepskyblue;">
This text has the <em>font-size</em> and
<em>color</em> styles applied to it, making it 20pt and deep sky blue.</p>
</body>
</html>
Embedded Style Sheets (Internal CSS)
A second technique for using style sheets is embedded style sheets.
The internal style sheet is used to add a unique style for a single document. It is defined in <head> section of the HTML page inside
the <style> tag.
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: linen;
}
h1 {
color: Red;
margin-left: 80px;
}
</style>
</head>
<body>
<h1>The internal style sheet is applied on this heading.</h1>
<p>This paragraph will not be affected.</p>
Ex2:
<!DOCTYPE html>
<html>
<head>
<title>Embedded Style Sheet</title>
<style type = "text/css">
em { font-weight: bold; color: black; }
h1 { font-family: tahoma, helvetica, sans-serif; }
p { font-size: 12pt; font-family: arial, sans-serif; }
.special { color: purple; }
</style>
</head>
<body>
<h1 class = "special">Deitel & Associates, Inc.</h1>
<p>Deitel & Associates, Inc. is an authoring and corporate training organization specializing in programming languages, Internet
and web technology,
iPhone and Android app development, and object technology education.</p>
<h1>Clients</h1>
<p class = "special"> The company's clients include many <em>Fortune 1000 companies</em>, government agencies, branches of
the military and business organizations.</p>
</body>
External CSS
• The external style sheet is generally used when you want to make changes on multiple pages. It is
ideal for this condition because it facilitates you to change the look of the entire web site by changing
just one file.
• It uses the <link> tag on every pages and the <link> tag should be put inside the head section.
/* styles.css */
/* External style sheet */
body { font-family: arial, helvetica, sans-serif; }
a.nodec { text-decoration: none; }
a:hover { text-decoration: underline; }
li em { font-weight: bold; }
h1, em { text-decoration: underline; }
ul { margin-left: 20px; }
ul ul { font-size: .8em; }
Attaching a specific style class with a specific element
A.nodec { text-decoration: none}
• Overrides an element function
• If A is used with nodec class the text will not be underlined
<P><A CLASS = "nodec" HREF = "http://food.com">Go to the Grocery store</A></P>
• text-decoration property
Applies decorations to text within an element
None, Overline, line-through
Pseudo-class
Extends function of an element
Allows author access to content not specifically declared in the document
A:hover { text-decoration: underline; color: red; background-color: #CCFFCC}
The hover pseudo class is dynamically activated when the user mover the mouse cursor over an A element
Element within Element
LI EM { color: red; font-weight:bold}
Declare a style for EM when used as children of LI
Applying to multiple elements requires separating element with comma eg LI EM
<!DOCTYPE html> </ul>
<html> </li>
<head> <li>Carrots</li>
<title>Linking External Style Sheets</title>
<li>Yogurt</li>
<link rel = "stylesheet" type = "text/css"
<li>Pizza <em>with
href = "styles.css">
mushrooms</em></li>
</head>
</ul>
<body>
<h1>Shopping list for <em>Monday</em>:</h1>
<p><em>Go to the</em>
<ul> <a class = "nodec" href =
<li>Milk</li>
"http://www.bigbasket.com">
<li>Bread Grocery store</a>
<ul> </p>
<li>white bread</li> </body>
<li>Rye bread</li> </html>
<li>Whole wheat bread</li>
Positioning Elements: Absolute Positioning, z-index
Before CSS, controlling element positioning in HTML documents was difficult—the browser
determined positioning.
CSS introduced the position property and a capability called absolute positioning, which gives you
greater control over how document elements are displayed.
The z-index property specifies the stack order of an element. (which element should be placed in front
of, or behind, the others).
An element can have a positive or negative stack order.
The z-index property allows you to layer overlapping elements. Elements that have higher z-index
values are displayed in front of elements with lower z-index values.
If you do not specify a z-index or if elements have the same z-index value, the elements are placed from
background to foreground in the order in which they’re encountered in the document. The default z-
index value is 0.
<!DOCTYPE html>
<html>
<head>
<style>
img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<img src="https://media.newyorker.com/photos/Trees.jpg"width="100" height="140">
<p>Because the image has a z-index of -1, it will be placed behind the text.</p>
</body>
<html> </style>
<head> </head>
<title>Absolute Positioning</title> <body>
<style type = "text/css">
<p><img src = "background_image.jpg" class = "background_image"
.background_image { position: absolute;
alt = "First positioned image" width="1000" height="1000"/></p>
top: 0px;
left: 0px;
<p><img src = "foreground_image.jpg" class = "foreground_image"
z-index: 1; }
alt = "Second positioned image" /></p>
.foreground_image { position: absolute;
top: 300px;
left: 350px; <p class = "text">DEPT OF CSE</p>
z-index: 2; } </body>
.text { position: absolute; </html>
top: 400px;
left: 400px;
z-index: 3;
font-size: 20pt;
font-family: tahoma, geneva, sans-serif; }
Positioning Elements: Relative Positioning, span
Absolute positioning is not the only way to specify page layout, in which elements are positioned
relative to other elements.
Element span is a grouping element by default, it does not apply any formatting to its contents. Its
primary purpose is to apply CSS rules or id attributes to a section of text.
Element span is an inline-level element it does not change the flow of elements in the document.
Examples of inline elements include span, img, a, em and strong.
<html>
<head>
<title>Relative Positioning</title>
<style type = "text/css">
p { font-size: 1.3em;
font-family: verdana, arial, sans-serif; }
span { color: red;
font-size: .6em;
height: 1em; }
.super { position: relative;
top: -1ex; }
.sub { position: relative;
bottom: -1ex; }
.shiftleft { position: relative;
left: -1ex; }
.shiftright { position: relative;
right: -1ex; }
</style>
</head>
<body>
<p>The text at the end of this sentence
<span class = "super">is in superscript</span>.</p>
<p>The text at the end of this sentence
<span class = "sub">is in subscript</span>.</p>
<p>The text at the end of this sentence
<span class = "shiftleft">is shifted left</span>.</p>
<p>The text at the end of this sentence
<span class = "shiftright">is shifted right</span>.</p>
</body>
Backgrounds in CSS
CSS provides control over the backgrounds of block-level elements. CSS can set a background
color or add background images to HTML5 elements.
The background property is a shorthand property for
• background-color
• background-image
• background-position
• background-size
• background-repeat
• background-origin
• background-clip
• background-attachment
The background-color property sets the background color of an element.
background-color: color;
The background-image property sets one or more background images for an element.
By default, a background-image is placed at the top-left corner of an element, and repeated both
vertically and horizontally.
background-image: url|none;
The background-position property sets the starting position of a background image.
background-position: value;
The background-size property specifies the size of the background images.
There are four different syntaxes you can use with this property: the keyword syntax ("auto",
"cover" and "contain")
background-size: auto|length|cover|contain;
The background-repeat property sets if/how a background image will be repeated.By default, a
background-image is repeated both vertically and horizontally.
background-repeat: repeat|repeat-x|repeat-y|no-repeat;
The background-attachment property sets whether a background image scrolls with the rest of
the page, or is fixed.
background-attachment: scroll|fixed|local;
<html>
<head>
<title>Background Images</title>
<style type = "text/css">
body { background-image: url(rvr.jpg);
background-position: bottom right;
background-repeat: no-repeat;
background-attachment: fixed;
background-color: blue; }
p { font-size: 18pt;color: Darkblue;text-indent: 1em;
font-family: arial, sans-serif; }
.dark { font-weight: bold; }
</style>
</head>
<body>
<p>Established by the renowned <span class = "dark">Nagarjuna Education Society (1967)</span> in the year 1985,
the College drew its initial impetus from people's representatives, local doctors, charitable trusts and commercial
houses of Guntur District.</p>
</body>
Element Dimensions
Dimensions in CSS: To specify the height and width of an element you can use height and
width properties. It does not specify the borders, margins, and padding, it just sets the height
and width inside the border, margins, and padding for an HTML element.
CSS Dimension Properties CSS Height and Width Values
PROPERTY DESCRIPTION
This is the default. The browser
Sets the height of an Auto
height calculates the height and width
element
Sets the maximum height of
max-height Specify the height and width in px,
an element Length
cm, etc.
Sets the maximum width of
max-width
an element
Specify the height and width in
%
Sets the minimum height of percent of the containing block
min-height
an element