WP - Unit 2
WP - Unit 2
➢ OVERVIEW OF 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.
CSS is the language we use to style an HTML document. CSS describes how HTML elements should be
displayed. This tutorial will teach you CSS from basic to advanced.
Example
p{
color: red;
text-align: center;
}
Example Explained
● 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
1
F.Y.C.S-SEM II WEB PROGRAMMING - I NOTES - UNIT 2
➢ CSS Selectors
CSS selectors are used to "find" (or select) the HTML elements you want to style.
We can divide CSS selectors into five categories:
● Simple selectors (select elements based on name, id, class)
● Combinator selectors (select elements based on a specific relationship between them)
● Pseudo-class selectors (select elements based on a certain state)
● Pseudo-elements selectors (select and style a part of an element)
● Attribute selectors (select elements based on an attribute or attribute value)
Here we will explain the most basic CSS selectors.
All CSS Simple Selectors
Selector Example Example description
#id #firstname Selects the element with id="firstname"
.class .intro Selects all elements with class="intro"
element.class p.intro Selects only <p> elements with class="intro"
* * Selects all elements
Element/type p Selects all <p> elements
element,element,.. div, p Selects all <div> elements and all <p> elements
Example
Here, all <p> elements on the page will be center-aligned, with a red text color:
p{
text-align: center;
color: red;
}
Example
The CSS rule below will affect every HTML element on the page:
*{
text-align: center;
color: blue;
}
2
F.Y.C.S-SEM II WEB PROGRAMMING - I NOTES - UNIT 2
Example 2
In this example only <p> elements with class="center" will be red and center-aligned:
p.center {
text-align: center;
color: red;
}
HTML elements can also refer to more than one class.
Example 3
In this example the <p> element will be styled according to class="center" and to class="large":
3
F.Y.C.S-SEM II WEB PROGRAMMING - I NOTES - UNIT 2
color: red;
}
p{
text-align: center;
color: red;
}
It will be better to group the selectors, to minimize the code.
Example 2
In this example we have grouped the selectors from the code above:
h1, h2, p {
text-align: center;
color: red;
}
4
F.Y.C.S-SEM II WEB PROGRAMMING - I NOTES - UNIT 2
➢ Inserting CSS in an HTML Document – Internal Style:
An internal CSS is used to define a style for a single HTML page. An internal CSS is defined in the <head>
section of an HTML page, within a <style> element. The following example sets the text color of ALL the
<h1> elements (on that page) to blue, and the text color of ALL the <p> elements to red. In addition, the
page will be displayed with a "powderblue" background color:
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Example 1:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
The external style sheet can be written in any text editor. The file must not contain any HTML code, and
must be saved with a .css extension.
Here is what the "styles.css" file looks like:
5
F.Y.C.S-SEM II WEB PROGRAMMING - I NOTES - UNIT 2
Example 2:
"styles.css":
body {
background-color: powderblue;
}
h1 {
color: blue;
}
p{
color: red;
}
With an external style sheet, you can change the look of an entire web site, by changing one file!
CSS Syntax
background: bg-color bg-image position/bg-size bg-repeat bg-origin bg-clip bg-attachment initial|inherit;
Example
Set different background properties in one declaration:
body {
background: lightblue url("img_tree.gif") no-repeat fixed center;
}
6
F.Y.C.S-SEM II WEB PROGRAMMING - I NOTES - UNIT 2
If one of the properties in the shorthand declaration is the bg-size property, you must use a / (slash) to
separate it from the bg-position property, e.g. background:url(smiley.gif) 10px 20px/50px 50px; will result
in a background image, positioned 10 pixels from the left, 20 pixels from the top, and the size of the image
will be 50 pixels wide and 50 pixels high.
If using multiple background-image sources but also want a background-color, the background-color
parameter needs to be last in the list.
Property Values:
Value Description
background-color Specifies the background color to be used
background-image Specifies ONE or MORE background images to be used
background-position Specifies the position of the background images
background-size Specifies the size of the background images
background-repeat Specifies how to repeat the background images
background-origin Specifies the positioning area of the background images
background-clip Specifies the painting area of the background images
background-attachment Specifies whether the background images are fixed or scrolls with the rest of
the page
initial Sets this property to its default value.
inherit Inherits this property from its parent element.
7
F.Y.C.S-SEM II WEB PROGRAMMING - I NOTES - UNIT 2
CSS Syntax
font: font-style font-variant font-weight font-size/line-height font-family|caption|icon|menu|message-
box|small-caption|status-bar|initial|inherit;
Property Values:
Property/Value Description
font-style Specifies the font style. Default value is "normal"
font-variant Specifies the font variant. Default value is "normal"
font-weight Specifies the font weight. Default value is "normal"
font-size/line-height Specifies the font size and the line-height. Default value is "normal"
font-family Specifies the font family. Default value depends on the browser
caption Uses the font that are used by captioned controls.
8
F.Y.C.S-SEM II WEB PROGRAMMING - I NOTES - UNIT 2
(like buttons,drop-downs, etc.)
icon Uses the font that are used by icon labels
menu Uses the fonts that are used by dropdown menus
message-box Uses the fonts that are used by dialog boxes
small-caption A smaller version of the caption font
status-bar Uses the fonts that are used by the status bar
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit
Example
A demonstration of some of the other font property values.
<p style="font:caption">The browser font used in captioned controls.</p>
<p style="font:icon">The browser font used in icon labels.</p>
<p style="font:menu">The browser font used in dropdown menus.</p>
<p style="font:message-box">The browser font used in dialog boxes.</p>
<p style="font:small-caption">A smaller version of the caption font.</p>
<p style="font:status-bar">The browser font used in the status bar.</p>
Here, we will demonstrate some commonly used CSS properties. You will learn more about them later. The
CSS color property defines the text color to be used. The CSS font-family property defines the font to be
used. The CSS font-size property defines the text size to be used.
Example
Use of CSS color, font-family and font-size properties:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: blue;
font-family: verdana;
font-size: 300%;
}
p{
color: red;
font-family: courier;
font-size: 160%;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
9
F.Y.C.S-SEM II WEB PROGRAMMING - I NOTES - UNIT 2
➢ Box Model
In CSS, the term "box model" is used when talking about design and layout.
The CSS box model is essentially a box that wraps around every HTML element. It consists of: content,
padding, borders and margins. The image below illustrates the box model:
Example
Demonstration of the box model:
div {
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
Example
Use of CSS border and padding properties:
p{
border: 2px solid powderblue;
padding: 30px;
}
The CSS margin property defines a margin (space) outside the border.
10
F.Y.C.S-SEM II WEB PROGRAMMING - I NOTES - UNIT 2
Example
Use of CSS border and margin properties:
p{
border: 2px solid powderblue;
margin: 50px;
}
Example
This <div> element will have a total width of 350px and a total height of 80px:
div {
width: 320px;
height: 50px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}
11
F.Y.C.S-SEM II WEB PROGRAMMING - I NOTES - UNIT 2
The total width of an element should be calculated like this:
Total element width = width + left padding + right padding + left border + right border
The margin property also affects the total space that the box will take up on the page, but the margin is not
included in the actual size of the box. The box's total width and height stops at the border.
• Fixed position: Any HTML element with position: fixed property will be positioned relative to the
viewport. An element with fixed positioning allows it to remain in the same position even we scroll
the page. We can set the position of the element using the top, right, bottom, and left.
• Static: This method of positioning is set by default. If we don’t mention the method of positioning for
any element, the element has the position: static method by default. By defining Static, the top, right,
bottom, and left will not have any control over the element. The element will be positioned with the
normal flow of the page.
• Relative: An element with position: relative is positioned relatively with the other elements which are
sitting on top of it. If we set its top, right, bottom, or left, other elements will not fill up the gap left
by this element. An element with its position set to relative and when adjusted using top, bottom,
left, and right will be positioned relative to its original position.
• Absolute: An element with position: absolute will be positioned with respect to its nearest Non-static
ancestor. The positioning of this element does not depend upon its siblings or the elements which
are at the same level.
• Sticky: Element with position: sticky and top:0 played a role between fixed & relative based on the
position where it is placed. If the element is placed in the middle of the document then when the
user scrolls the document, the sticky element start scrolling until it touched the top. When it touches
the top, it will be fixed at the top place in spite of further scrolling. we can stick the element at the
bottom, with the bottom property.
12
F.Y.C.S-SEM II WEB PROGRAMMING - I NOTES - UNIT 2
➢ CSS Properties for positioning an element – Static Positioning:
HTML elements are positioned static by default. Static positioned elements are not affected by the top,
bottom, left, and right properties.
An element with position: static; is not positioned in any special way; it is always positioned according to the
normal flow of the page.
Example
div.static {
position: static;
border: 3px solid #73AD21;
}
Example
div.fixed {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
border: 3px solid #73AD21;
}
Example
div.relative {
position: relative;
left: 30px;
border: 3px solid #73AD21;
}
➢ CSS Properties for positioning an element – Absolute Positioning:
An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of
positioned relative to the viewport, like fixed). However; if an absolute positioned element has no positioned
ancestors, it uses the document body, and moves along with page scrolling. Absolute positioned elements
are removed from the normal flow, and can overlap elements.
Example
13
F.Y.C.S-SEM II WEB PROGRAMMING - I NOTES - UNIT 2
div.relative {
position: relative;
width: 400px;
height: 200px;
border: 3px solid #73AD21;
}
div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
}
Internet Explorer does not support sticky positioning. You must also specify at least one of top, right, bottom
or left for sticky positioning to work.
In this example, the sticky element sticks to the top of the page (top: 0), when you reach its scroll position.
Example
div.sticky {
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
background-color: green;
border: 2px solid #4CAF50;
}
14
F.Y.C.S-SEM II WEB PROGRAMMING - I NOTES - UNIT 2
Example
img {
opacity: 0.5;
}
The opacity property is often used together with the ':hover' selector to change the opacity on mouse-over:
Example
img {
opacity: 0.5;
}
img:hover {
opacity: 1.0;
}
When using the opacity property to add transparency to the background of an element, all of its child
elements inherit the same transparency. This can make the text inside a fully transparent element hard to
read:
Example
div {
opacity: 0.3;
}
You learned from our CSS Colors Chapter, that you can use RGB as a color value. In addition to RGB, you can
use an RGB color value with an alpha channel (RGBA) - which specifies the opacity for a color.
An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha parameter is a number
between 0.0 (fully transparent) and 1.0 (fully opaque).
15
F.Y.C.S-SEM II WEB PROGRAMMING - I NOTES - UNIT 2
Example
div {
background: rgba(76, 175, 80, 0.3) /* Green background with 30% opacity */
}
Example:
<html>
<head>
<style>
div.background {
background: url(klematis.jpg) repeat;
border: 2px solid black;
}
div.transbox {
margin: 30px;
background-color: #ffffff;
border: 1px solid black;
opacity: 0.6;
}
div.transbox p {
margin: 5%;
font-weight: bold;
color: #000000;
}
</style>
</head>
<body>
<div class="background">
<div class="transbox">
<p>This is some text that is placed in the transparent box.</p>
</div>
</div>
</body>
</html>
16
F.Y.C.S-SEM II WEB PROGRAMMING - I NOTES - UNIT 2
➢ Self-Learning Topics: Customizing controls using CSS
17