Unit - III Css
Unit - III Css
CSS Syntax
A CSS rule-set consists of a selector and a declaration block:
CSS Selectors
CSS selectors are used to "find" (or select) HTML elements based on their element name, id, class,
attribute, and more.
The id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element should be unique within a page, so the id selector is used to select one
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":
You can also specify that only specific HTML elements should be affected by a class. In the
example below, only <p> elements with class="center" will be center-aligned:
The class name can be used by CSS and JavaScript to perform certain tasks for elements with the
specified class name.
Example: Using CSS to style all elements with the class name "city":
<html>
<head><title>CSS Class Selector</title>
<style>
.city { background-color: tomato; color: white; padding: 10px; }
</style>
</head>
<body> <h2 class="city">London</h2>
<p>London is the capital of England.</p>
<h2 class="city">Paris</h2>
<p>Paris is the capital of France.</p>
<h2 class="city">Tokyo</h2>
<p>Tokyo is the capital of Japan.</p>
</body>
</html>
Grouping Selectors
If you have elements with the same style definitions, like this:
CSS Comments
Comments are used to explain the code, and may help when you edit the source code at a later
date.
Comments are ignored by browsers.
A CSS comment starts with /* and ends with */.
Comments can also span multiple lines:
Example:
The most common way to add CSS, is to keep the styles in separate CSS files.
Inline CSS
An inline CSS is used to apply a unique style to a single HTML element.
An inline CSS uses the style attribute of an HTML element.
This example sets the text color of the <h1> element to blue:
Tip: An inline style loses many of the advantages of a style sheet (by mixing content with presentation).
Internal CSS
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:
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>
External CSS
An external style sheet is used to define the style for many HTML pages.
With an external style sheet, you can change the look of an entire web site, by changing one
file!
Example:
<!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>
An 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 how the "styles.css" looks:
body { background-color: powderblue; }
h1 { color: blue; }
p { color: red; }
Note: An inline style (inside a specific HTML element) has the highest priority, which means that it will
override a style defined inside the <head> tag, or in an external style sheet, or a browser default value.
CSS Backgrounds
The CSS background properties are used to define the background effects for elements.
CSS background properties:
background-color
background-image
background-repeat
background-attachment
background-position
Background Color
Background Image
By default, the background-image property repeats an image both horizontally and vertically.
Showing the background image only once is also specified by the background-repeat property:
Example body { background-image: url("img_tree.png"); background-repeat: no-repeat; }
In the example above, the background image is shown in the same place as the text.
We want to change the position of the image, so that it does not disturb the text too much.
The position of the image is specified by the background-position property:
Example
body { background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top; }
To specify that the background image should be fixed (will not scroll with the rest of the page),
use the background-attachment property:
Example
body { background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed; }
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
The border-style property can have from one to four values (for the top border, right border,
bottom border, and the left border).
Example
Gajendra Chourey Page 43
p.dotted {border-style: dotted;}
p.dashed {border-style: dashed;}
p.solid {border-style: solid;}
p.double {border-style: double;}
p.groove {border-style: groove;}
p.ridge {border-style: ridge;}
p.inset {border-style: inset;}
p.outset {border-style: outset;}
p.none {border-style: none;}
p.hidden {border-style: hidden;}
p.mix {border-style: dotted dashed solid double;}
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.
The border-width property can have from one to four values (for the top border, right border,
bottom border, and the left border).
Example:
p.one { border-style: solid; border-width: 5px; }
p.two { border-style: solid; border-width: medium; }
p.three { border-style: solid; border-width: 2px 10px 4px 20px; }
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)"
transparent
The border-color property can have from one to four values (for the top border, right border,
bottom border, and the left border).
If border-color is not set, it inherits the color of the element.
Example:
p.one { border-style: solid; border-color: red; }
p.two { border-style: solid; border-color: green; }
p.three { border-style: solid; border-color: red green blue yellow; }
From the examples above you have seen that it is possible to specify a different border for each
side. In CSS, there are also properties for specifying each of the borders (top, right, bottom, and
left):
Example:
As you can see from the examples above, there are many properties to consider when dealing
with borders.
To shorten the code, it is also possible to specify all the individual border properties in one
property.
The border property is a shorthand property for the following individual border properties:
border-width
border-style (required)
border-color
Rounded Borders
The border-radius property is used to add rounded borders to an element:
Example: p { border: 2px solid red; border-radius: 5px; }
<!DOCTYPE html>
<html>
<head>
<style>
p.dotted {border-style: dotted;}
p.dashed {border-style: dashed; border-width: medium; }
p.solid {border-style: solid; border-width: 5px; border-color: green;}
p.double {border-style: double; border-width: 2px 10px 4px 20px; border-radius: 25px;
}
p#groove {border-style: groove;}
p#ridge {border-style: ridge;}
p#inset {border-style: inset;}
p#outset {border-style: outset;}
p#none {border-style: none;}
p#hidden {border-style: hidden;}
p#mix {border-style: dotted dashed solid double; border-color: red green blue yellow; }
</style>
</head>
<body>
<p class='dotted'>Bilaspur</p>
<p class='dashed'>Bilaspur</p>
<p class='solid'>Bilaspur</p>
<p class='double'>Bilaspur</p>
<p id='groove'>Bilaspur</p>
<p id='ridge'>Bilaspur</p>
<p id='inset'>Bilaspur</p>
<p id='outset'>Bilaspur</p>
<p id='none'>Bilaspur</p>
<p id='hidden'>Bilaspur</p>
<p id='mix'>Bilaspur</p>
</body>
</html>
#example2 {
border: 2px solid red;
border-top-left-radius: 50px 20px;
}
Note: If you set two values, the first one is for the top border, and the second one for the right border. If the second
value is omitted, it is copied from the first. If either length is zero, the corner is square, not rounded.
<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
border: 2px solid red;
padding: 10px;
border-top-right-radius: 25px;
}
#example2 {
border: 2px solid red;
padding: 10px;
border-top-right-radius: 50px 20px;
}
</style>
</head>
<body>
<h2>border-top-right-radius: 25px:</h2>
<div id="example1">
<p>The border-top-right-radius property defines the radius of the top-right corner.</p>
</div>
</body>
</html>
CSS Margins:
The CSS margin properties are used to create space around elements, outside of any defined
borders.
With CSS, you have full control over the margins.
There are properties for setting the margin for each side of an element (top, right, bottom, and
left).
Margin - Individual Sides
CSS has properties for specifying the margin for each side of an element:
margin-top
margin-right
Gajendra Chourey Page 46
margin-bottom
margin-left
The following example sets different margins for all four sides of a <p> element:
Example:
p{ margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
Note: Do not add a space between the property value and the unit (such as margin: 50 px;). The correct
way is: margin: 50px;
You can set the margin property to auto to horizontally center the element within its container.
The element will then take up the specified width, and the remaining space will be split equally
between the left and right margins:
Example: div { width: 300px; margin: auto; border: 1px solid red; }
This example lets the left margin of the <p class="ex1"> element be inherited from the parent
element (<div>):
CSS Padding
The CSS padding properties are used to generate space around an element's content, inside of
any defined borders.
With CSS, you have full control over the padding.
Gajendra Chourey Page 47
There are properties for setting the padding for each side of an element (top, right, bottom, and
left).
inherit - specifies that the padding should be inherited from the parent element.
The following example sets different padding for all four sides of a <div> element:
Example:
div { padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}
The height and width properties are used to set the height and width of an element.
The height and width can be set to auto (this is default. Means that the browser calculates the
height and width), or be specified in length values, like px, cm, etc., or in percent (%) of the
containing block.
This element has a height of 200 pixels and a width of 50%.
Example:
div { height: 200px;
width: 50%;
background-color: powderblue;
}
<!DOCTYPE html>
<html>
<head>
The box model allows us to add a border around elements, and to define space between elements.
Example
div { width: 300px;
border: 25px solid green;
padding: 25px;
margin: 25px;
}
Text Transformation
The text-transform property is used to specify uppercase and lowercase letters in a text.
Letter Spacing
The letter-spacing property is used to specify the space between the characters in a text.
The following example demonstrates how to increase or decrease the space between characters:
Example
h1 { letter-spacing: 3px; }
h2 { letter-spacing: -3px; }
Line Height
The line-height property is used to specify the space between lines:
Example: p.small { line-height: 0.8; }
p.big { line-height: 1.8; }
Word Spacing
The word-spacing property is used to specify the space between the words in a text.
The following example demonstrates how to increase or decrease the space between words:
Example h1 { word-spacing: 10px; }
h2 { word-spacing: -5px; }
Text Shadow
The text-shadow property adds shadow to text.
The following example specifies the position of the horizontal shadow (3px), the position of the
vertical shadow (2px) and the color of the shadow (red):
Example:
h1 { text-shadow: 3px 2px red; }
Font Family
The font family of a text is set with the font-family property.
The font-family property should hold several font names as a "fallback" system.
If the browser does not support the first font, it tries the next font, and so on.
Start with the font you want, and end with a generic family, to let the browser pick a similar font
in the generic family, if no other fonts are available.
If the name of a font family is more than one word, it must be in quotation marks, like: "Times
New Roman".
More than one font family is specified in a comma-separated list:
Example: p { font-family: "Times New Roman", Times, sans-serif; }
You can also use Google font. (Browse Fonts - Google Fonts)
Font Style
The font-style property is mostly used to specify italic text. This property has three values:
Example:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-
awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<i class="fa fa-cloud"></i>
<i class="fa fa-heart"></i>
<i class="fa fa-car"></i>
<i class="fa fa-file"></i>
<i class="fa fa-bars"></i>
</body>
</html>
Bootstrap Icons
Example:
<!DOCTYPE html>
<html>
<head>
<link rel ="stylesheet
"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
Styling Links
Links can be styled with any CSS property (e.g. color, font-family, background, etc.).
In addition, links can be styled differently depending on what state they are in.
The four links states are:
a:link - a normal, unvisited link
a:visited - a link the user has visited
a:hover - a link when the user mouse over it
a:active - a link the moment it is clicked
Example:
<!DOCTYPE html>
<html>
<head>
<style> a:link { color: red; }
a:visited { color: green; }
a:hover { color: hotpink; }
a:active { color: blue; }
</style>
</head>
<body>
<a href="mywebpage.html"> ABVU </a>
Background Color
The background-color property can be used to specify a background color for links:
Example:
a:link { background-color: yellow; }
a:visited { background-color: cyan; }
a:hover { background-color: lightgreen; }
a:active { background-color: hotpink; }
<!DOCTYPE html>
<html>
<head>
<style>
a:link { background-color: yellow; }
a:visited { background-color: cyan; }
a:hover { background-color: lightgreen; }
a:active { background-color: hotpink; }
</style>
</head>
<body>
<a href="mywebpage.html"> ABVU </a>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style> ul.a { list-style-type: circle; }
ul.b { list-style-type: square; }
ol.c { list-style-type: upper-roman; }
ol.d { list-style-type: lower-alpha; }
</style>
</head>
<body>
<p>Example of unordered lists:</p>
<ul class="a">
<li>Coffee</li>
CSS Floats
The float property specifies how an element should float.
It is common to do entire web layouts using the CSS float property.
Float is easy to learn - you just need to remember how the float and clear properties work.
Disadvantages: Floating elements are tied to the document flow, which may harm the flexibility.
Note: Absolutely positioned elements ignore the float property!
Note: Elements after a floating element will flow around it. To avoid this, use the clear property
or the clearfix hack.
Property Values
Value Description
The element does not float, (will be displayed just where it occurs in the text). This is
none
default
Example 1:
<!DOCTYPE html>
<html>
<head>
<style>
div.container { width: 100%; border: 1px solid gray; }
header, footer { padding: 1em; color: white; background-color: black; clear: left; text-align:
center;}
nav { float: left; max-width: 160px; margin: 0; padding: 1em;}
nav ul { list-style-type: none; padding: 0; }
nav ul a { text-decoration: none; }
article { margin-left: 170px; border-left: 1px solid gray; padding: 1em; overflow: hidden; }
</style>
</head>
<body>
<div class="container">
<header>
<h1>City Gallery</h1>
Example 2:
<!DOCTYPE html>
<html>
<head>
<style>
* { box-sizing: border-box; }
<div class="clearfix">
<div class="column menu">
<ul>
<li>The Flight</li>
<li>The City</li>
<li>The Island</li>
<li>The Food</li>
<div class="footer">
<p>Footer Text</p>
</div>
</body>
</html>
Example 3: Do not allow floating elements on the left or the right side of a specified <p> element:
<!DOCTYPE html>
<html>
<head>
<style>
img { float: left; }
</body>
</html>
Example 4: If a floating element is taller than the containing element, it will overflow outside its
container. It is possible to fix this with the "clearfix hack":
<!DOCTYPE html>
<html>
<head>
<style>
div { border: 3px solid #4CAF50; padding: 5px; }
<p>Here, the floating image is taller than the element containing it, so it overflows outside its
container:</p>
<div>
<img class="img1" src="pineapple.jpg" alt="Pineapple" width="170" height="170">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum
interdum...</div>
<p style="clear:right">Add the clearfix hack to the containing element, to fix this problem:</p>
<div class="clearfix">
<img class="img2" src="pineapple.jpg" alt="Pineapple" width="170" height="170">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum
interdum...</div>
</body>
</html>
CSS Units
CSS has several different units for expressing a length.
Many CSS properties take "length" values, such as width, margin, padding, font-size, etc.
Length is a number followed by a length unit, such as 10px, 2em, etc.
Note: A whitespace cannot appear between the number and the unit. However, if the value
is 0, the unit can be omitted.
For some CSS properties, negative lengths are allowed.
There are two types of length units: absolute and relative.
Absolute Lengths
The absolute length units are fixed and a length expressed in any of these will appear as
exactly that size.
Absolute length units are not recommended for use on screen, because screen sizes vary so
much. However, they can be used if the output medium is known, such as for print layout.
Unit Description
Cm Centimetres
Mm Millimetres
In inches (1in = 96px = 2.54cm)
px pixels (1px = 1/96th of 1in)
pt points (1pt = 1/72 of 1in)
pc picas (1pc = 12 pt)
Pixels (px) are relative to the viewing device. For low-dpi devices, 1px is one device pixel
(dot) of the display. For printers and high resolution screens 1px implies multiple device
pixels.
Relative Lengths
em Relative to the font-size of the element (2em means 2 times the size of the current font)
The em and rem units are practical in creating perfectly scalable layout!
Viewport = the browser window size. If the viewport is 50cm wide, 1vw = 0.5cm
flex-grow A number specifying how much the item will grow relative to the rest of the
flexible items
flex-shrink A number specifying how much the item will shrink relative to the rest of the
flexible items
flex-basis The length of the item. Legal values: "auto", "inherit", or a number followed by
"%", "px", "em" or any other length unit
Example 1:
<!DOCTYPE html>
<html>
<head>
<style>
<h1>Responsive Flexbox</h1>
<p>In this example, we change the percentage of flex to create different layouts for different screen
sizes.</p>
<p><b>Resize the browser window to see that the direction changes when the screen size is 800px or
smaller.</b></p>
<div class="flex-container">
<div class="flex-item-left">1</div>
<div class="flex-item-right">2</div>
</div>
</body>
</html>
Value Description
number A length unit, or percentage, specifying the initial length of the flexible item(s)
auto Default value. The length is equal to the length of the flexible item. If the item has no
length specified, the length will be according to its content
Value Description
row Default value. The flexible items are displayed horizontally, as a row
Value Description
static Default value. Elements render in order, as they appear in the document flow
absolute The element is positioned relative to its first positioned (not static) ancestor
element
relative The element is positioned relative to its normal position, so "left:20px" adds 20
pixels to the element's LEFT position
Value Description
visible The overflow is not clipped. It renders outside the element's box. This is default
hidden The overflow is clipped, and the rest of the content will be invisible
scroll The overflow is clipped, but a scroll-bar is added to see the rest of the content
auto If overflow is clipped, a scroll-bar should be added to see the rest of the content
Value Description
auto Allows a straight jump "scroll effect" between elements within the scrolling box.
This is default
smooth Allows a smooth animated "scroll effect" between elements within the scrolling
box.
CSS Syntax: @media not|only mediatype and (mediafeature and|or|not mediafeature) { CSS-Code; }
not: The not keyword inverts the meaning of an entire media query.
only: The only keyword prevents older browsers that do not support media queries with
media features from applying the specified styles. It has no effect on modern browsers.
and: The and keyword combines a media feature with a media type or other media features.
They are all optional. However, if you use not or only, you must also specify a media type.
Media Types
Value Description
All Default. Used for all media type devices
Print Used for printers
Screen Used for computer screens, tablets, smart-phones etc.
Speech Used for screenreaders that "reads" the page out loud
Media Features
Value Description
any-hover Does any available input mechanism allow the user to hover over elements? (added in Media
Queries Level 4)
any-pointer Is any available input mechanism a pointing device, and if so, how accurate is it? (added in Media
Queries Level 4)
aspect-ratio The ratio between the width and the height of the viewport
Color The number of bits per color component for the output device
color-gamut The approximate range of colors that are supported by the user agent and output device (added in
Media Queries Level 4)
Hover Does the primary input mechanism allow the user to hover over elements? (added in Media
Queries Level 4)
inverted-colors Is the browser or underlying OS inverting colors? (added in Media Queries Level 4)
max-aspect-ratio The maximum ratio between the width and the height of the display area
max-color The maximum number of bits per color component for the output device
max-height The maximum height of the display area, such as a browser window
max-monochrome The maximum number of bits per "color" on a monochrome (greyscale) device
max-width The maximum width of the display area, such as a browser window
min-aspect-ratio The minimum ratio between the width and the height of the display area
min-color The minimum number of bits per color component for the output device
min-height The minimum height of the display area, such as a browser window
min-monochrome The minimum number of bits per "color" on a monochrome (greyscale) device
min-width The minimum width of the display area, such as a browser window
overflow-block How does the output device handle content that overflows the viewport along the block axis
(added in Media Queries Level 4)
overflow-inline Can content that overflows the viewport along the inline axis be scrolled (added in Media Queries
Level 4)
Pointer Is the primary input mechanism a pointing device, and if so, how accurate is it? (added in Media
Queries Level 4)
Update How quickly can the output device modify the appearance of the content (added in Media Queries
Level 4)
Example 1:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
div.example { background-color: yellow; padding: 20px; }
<p>When the browser's width is 600px wide or less, hide the div element. Resize the browser window
to see the effect.</p>
</body>
</html>
/* On screens that are 992px wide or less, go from four columns to two columns */
@media screen and (max-width: 992px) { .column { width: 50%; }}
/* On screens that are 600px wide or less, make the columns stack on top of each other instead of
next to each other */
@media screen and (max-width: 600px) { .column { width: 100%; } }
</style>
</head>
<body>
</body>
</html>
</body>
</html>
/* Header/logo Title */
.header { padding: 60px; text-align: center; background: #1abc9c; color: white;}
/* Column container */
.row { display: flex; flex-wrap: wrap; }
/* Footer */
.footer { padding: 20px; text-align: center; background: #ddd; }
/* Responsive layout - when the screen is less than 700px wide, make the two columns stack on
top of each other instead of next to each other */
@media (max-width: 700px) { .row, .navbar { flex-direction: column; }}
</style>
</head>
<body>
</body>
</html>
Syntax:
align-self: auto|stretch|center|flex-start|flex-end|baseline|initial|inherit;
Property Values
Value Description
Value Description
all-scroll The cursor indicates that something can be scrolled in any direction
cell The cursor indicates that a cell (or set of cells) may be selected
col-resize The cursor indicates that the column can be resized horizontally
e-resize The cursor indicates that an edge of a box is to be moved right (east)
ne-resize The cursor indicates that an edge of a box is to be moved up and right
(north/east)
nw-resize The cursor indicates that an edge of a box is to be moved up and left (north/west)
no-drop The cursor indicates that the dragged item cannot be dropped here
not- The cursor indicates that the requested action will not be executed
allowed
progress The cursor indicates that the program is busy (in progress)
row-resize The cursor indicates that the row can be resized vertically
s-resize The cursor indicates that an edge of a box is to be moved down (south)
se-resize The cursor indicates that an edge of a box is to be moved down and right
(south/east)
sw-resize The cursor indicates that an edge of a box is to be moved down and left
(south/west)
URL A comma separated list of URLs to custom cursors. Note: Always specify a generic
cursor at the end of the list, in case none of the URL-defined cursors can be used
w-resize The cursor indicates that an edge of a box is to be moved left (west)
<!DOCTYPE html>
<html>
<head>
<style>
.alias {cursor: alias;}
.all-scroll {cursor: all-scroll;}
.auto {cursor: auto;}
.cell {cursor: cell;}
.context-menu {cursor: context-menu;}
.col-resize {cursor: col-resize;}
.copy {cursor: copy;}
.crosshair {cursor: crosshair;}
.default {cursor: default;}
.e-resize {cursor: e-resize;}
.ew-resize {cursor: ew-resize;}
.grab {cursor: -webkit-grab; cursor: grab;}
.grabbing {cursor: -webkit-grabbing; cursor: grabbing;}
.help {cursor: help;}
.move {cursor: move;}
Gajendra Chourey Page 70
.n-resize {cursor: n-resize;}
.ne-resize {cursor: ne-resize;}
.nesw-resize {cursor: nesw-resize;}
.ns-resize {cursor: ns-resize;}
.nw-resize {cursor: nw-resize;}
.nwse-resize {cursor: nwse-resize;}
.no-drop {cursor: no-drop;}
.none {cursor: none;}
.not-allowed {cursor: not-allowed;}
.pointer {cursor: pointer;}
.progress {cursor: progress;}
.row-resize {cursor: row-resize;}
.s-resize {cursor: s-resize;}
.se-resize {cursor: se-resize;}
.sw-resize {cursor: sw-resize;}
.text {cursor: text;}
.url {cursor: url(Help-RedPlastic.cur),auto;} [Open Cursor Library - 128690 cursors (rw-designer.com)]
.w-resize {cursor: w-resize;}
.wait {cursor: wait;}
.zoom-in {cursor: zoom-in;}
.zoom-out {cursor: zoom-out;}
</style>
</head>
<body>
<p class="alias">alias</p>
<p class="all-scroll">all-scroll</p>
<p class="auto">auto</p>
<p class="cell">cell</p>
<p class="context-menu">context-menu</p>
<p class="col-resize">col-resize</p>
<p class="copy">copy</p>
<p class="crosshair">crosshair</p>
<p class="default">default</p>
<p class="e-resize">e-resize</p>
<p class="ew-resize">ew-resize</p>
<p class="grab">grab</p>
<p class="grabbing">grabbing</p>
<p class="help">help</p>
<p class="move">move</p>
<p class="n-resize">n-resize</p>
<p class="ne-resize">ne-resize</p>
<p class="nesw-resize">nesw-resize</p>
<p class="ns-resize">ns-resize</p>
<p class="nw-resize">nw-resize</p>
<p class="nwse-resize">nwse-resize</p>
<p class="no-drop">no-drop</p>
<p class="none">none</p>
<p class="not-allowed">not-allowed</p>
<p class="pointer">pointer</p>
</body>
</html>
animation-name Specifies the name of the keyframe you want to bind to the selector
animation-direction Specifies whether or not the animation should play in reverse on alternate cycles
animation-fill-mode Specifies what values are applied by the animation outside the time it is executing
initial Sets this property to its default value. Read about initial
Value Description
Example 1:
<!DOCTYPE html>
<html>
<head>
<style>
div { width: 100px; height: 100px; background: red; position: relative;
animation: mymove 5s infinite; }
<div></div>
</body>
</html>
Example 2:
<!DOCTYPE html>
<html>
<head>
<style>
div { width: 100px; height: 100px; background: red; position: relative;
animation: mymove 5s infinite; }
<p>The !important rule is ignored in a keyframe. Try to remove it, and see the result.</p>
<div></div>
</body>
</html>
Example 3:
<!DOCTYPE html>
<html>
<head>
<style>
div { width: 100px; height: 100px; background: red; position: relative;
animation: mymove 5s infinite;
}
@keyframes mymove { 0% {top: 0px; background: red; width: 100px;}
100% {top: 200px; background: yellow; width: 300px;}
}
</style>
</head>
<body>
<div></div>
</body>
</html>
Example 4:
<!DOCTYPE html>
<html>
<head>
<style>
div { width: 100px; height: 100px; background: red; position: relative;
animation: mymove 5s infinite;
}
@keyframes mymove { 0% {top: 0px; left: 0px; background: red;}
25% {top: 0px; left: 100px; background: blue;}
<p><strong>Note:</strong> The @keyframes rule is not supported in Internet Explorer 9 and earlier
versions.</p>
<div></div>
</body>
</html>
Value Description
url|string A url or a string representing the location of the resource to import. The url
may be absolute or relative
Example 1: Import the "printstyle.css" style sheet ONLY if the media is print:
@import "printstyle.css" print;
Example 2: Import the "mobstyle.css" style sheet ONLY if the media is screen and the viewport is
maximum 768 pixels:
@import "mobstyle.css" screen and (max-width: 768px);
grid-template-rows
grid-template-columns
grid-template-areas
grid-auto-rows
grid-auto-columns
grid-auto-flow
<!DOCTYPE html>
Gajendra Chourey Page 75
<html>
<head>
<style>
.item1 { grid-area: header; }
.item2 { grid-area: menu; }
.item3 { grid-area: main; }
.item4 { grid-area: right; }
.item5 { grid-area: footer; }
<div class="grid-container">
<div class="item1">Header</div>
<div class="item2">Menu</div>
<div class="item3">Main</div>
<div class="item4">Right</div>
<div class="item5">Footer</div>
</div>
Value Description
number Specifies the opacity. From 0.0 (fully transparent) to 1.0 (fully opaque)
<!DOCTYPE html>
<html>
<head>
<style>
div { background-color: #4CAF50; padding: 10px; }
<p>The opacity property adds transparency to the background of an element, and to all of its child
elements as well. This makes the text inside a transparent element hard to read:</p>
</body>
</html>
Property Values
Value Description
number Default value 0. Specifies the order for the flexible item
<!DOCTYPE html>
<html>
<head>
<style>
#example1 { border: 1px solid; padding: 10px; box-shadow: 5px 10px 8px #888888; }
#example2 { border: 1px solid; padding: 10px; box-shadow: 5px 10px 18px #888888; }
#example3 { border: 1px solid; padding: 10px; box-shadow: 5px 10px 18px red; }
</style>
</head>
<body>
</body>
</html>
The box-sizing property defines how the width and height of an element are calculated:
should they include padding and borders, or not.
Value Description
content-box Default. The width and height properties (and min/max properties) includes only the content.
Border and padding are not included
border-box The width and height properties (and min/max properties) includes content, padding and border
Example 1:
<!DOCTYPE html>
<html>
<head>
<style>
div.container { width: 100%; border: 2px solid black; }
div.box { box-sizing: border-box; width: 50%; border: 5px solid red; float: left; }
</style>
</head>
<body>
</body>
</html>
Example 2:
<!DOCTYPE html>
<html>
<head>
<style>
.div1 { width: 300px; height: 100px; border: 1px solid blue; box-sizing: border-box; }
.div2 {width: 300px; height: 100px; padding: 50px; border: 1px solid red; box-sizing: border-box;}
</style>
</head>
<body>
CSS allows you to add multiple background images for an element, through the background-
image property.
The different background images are separated by commas, and the images are stacked on
top of each other, where the first image is closest to the viewer.
Property Description
The following example has two background images, the first image is a flower (aligned to the
bottom and right) and the second image is a paper background (aligned to the top-left
corner):
Example 1: #example1 { background-image: url(img_flwr.gif), url(paper.gif);
background-position: right bottom, left top;
background-repeat: no-repeat, repeat;
}
Multiple background images can be specified using either the individual background properties
(as above) or the background shorthand property.
The following example uses the background shorthand property (same result as example
above):
Example 2: #example1 { background: url(img_flwr.gif) right bottom no-repeat,
url(paper.gif) left top repeat;
}
The CSS background-size property allows you to specify the size of background images.
The size can be specified in lengths, percentages, or by using one of the two keywords:
contain or cover.
The contain keyword scales the background image to be as large as possible (but both its
width and its height must fit inside the content area). As such, depending on the proportions
of the background image and the background positioning area, there may be some areas of
the background which are not covered by the background image.
The cover keyword scales the background image so that the content area is completely
covered by the background image (both its width and height are equal to or exceed the
content area). As such, some parts of the background image may not be visible in the
background positioning area.
The background-size property also accepts multiple values for background size (using a
comma-separated list), when working with multiple backgrounds.
The following example has three background images specified, with different background-size value for
each image:
<!DOCTYPE html>
<html>
<head>
<style>
#example1 { background: url(img_tree.gif) left top no-repeat, url(img_flwr.gif) right bottom
no-repeat, url(paper.gif) left top repeat; padding: 15px; background-size: 50px, 130px,
auto;
}
</body>
</html>
Now we want to have a background image on a website that covers the entire browser
window at all times.
The requirements are as follows:
Fill the entire page with the image (no white space)
Scale image as needed
Center image on page
Do not cause scrollbars
Example
.hero-image { background: url(img_man.jpg) no-repeat center;
background-size: cover;
height: 500px;
position: relative;
}
The CSS background-origin property specifies where the background image is positioned.
The property takes three different values:
border-box: the background image starts from the upper left corner of the border
padding-box: (default) the background image starts from the upper left corner of the
padding edge
content-box: the background image starts from the upper left corner of the content
</body>
</html>
Example 2:
<!DOCTYPE html>
<html>
<head>
<style>
#borderimg1 { border: 10px solid transparent; padding: 15px; border-image: url(border.png)
50 round;
}
<p><strong>Note:</strong> Internet Explorer 10, and earlier versions, do not support the border-
image property.</p>
</body>
</html>
<div id="grad1"></div>
</body>
</html>
Using Angles
If you want more control over the direction of the gradient, you can define an angle, instead
of the predefined directions (to bottom, to top, to right, to left, to bottom right, etc.).
A value of 0deg is equivalent to "to top".
A value of 90deg is equivalent to "to right".
A value of 180deg is equivalent to "to bottom".
Using Transparency
Repeating a linear-gradient
Example:
#grad { background-image: repeating-linear-gradient(red, yellow 10%, green 20%); }
Set Shape
The shape parameter defines the shape. It can take the value circle or ellipse. The default
value is ellipse.