Introduction To CSS3
Introduction To CSS3
Introduction To CSS3
1
Cascading Style Sheets
CSS1 introduced styles for the following document
features:
Fonts
Text
Color
Backgrounds
Block-level Elements
2
Cascading Style Sheets
CSS2 introduced styles for the following document
features:
Positioning
Visual Formatting
Media Types
Interfaces
CSS 2.1 did not add any new features to the language
3
Cascading Style Sheets
CSS3 (which is still in development) will introduce styles
for the following document features:
User Interfaces
Accessibility
Columnar layout
International Features
Mobile Devices
Scalable Vector Graphics
4
CSS3
Some of the most important CSS3 modules are:
Selectors
Box Model
Backgrounds and Borders
Image Values and Replaced Content
Text Effects
2D/3D Transformations
Animations
Multiple Column Layout
User Interface
5
Inserting CSS in HTML document
There are three ways of inserting a style sheet:
External style sheet
Internal style sheet
Inline style
6
External Style Sheet
7
Linking
<head>
<link rel="stylesheet" type="text/css"
href="myStyle.css">
</head>
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
8
Importing
Helps to access the style rules from other CSS style sheets
<style type=“text/css”>
@import url(“myStyle.css”)
H1{
color:blue;}
}
</style>
9
Internal Style Sheet
An internal style sheet should be used when a single document has a unique style
Internal styles are defined in the head section of an HTML page, inside the <style> tag
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
10
Inline Styles
11
Multiple Style Sheets
14
1. Universal
15
2. Type
16
3. class
17
You can also specify that only specific HTML elements
should be affected by a class.
p.center {
text-align: center;
color: red;
}
Ex.2 :<h1 class=“intro”>Header 1</h1>
.intro { font-family:sans-serif}
OR
H1.intro { font-family:sans-serif}
18
4.ID
The id selector uses the id attribute of an HTML element
to select a specific element.
An id should be unique within a page, so the id selector is
used if you want to select a single, unique element.
To select an element with a specific id, write a hash
character (#), followed by the id of the element.
<p id=“para1”> hello students</p>
#para1 {
text-align: center;
color: red;
}
19
Combinator
A combinator is something that explains the relationship
between the selectors.
A CSS selector can contain more than one simple selector.
Between the simple selectors, we can include a combinator.
There are four different combinators in CSS3:
20
5.Child
A child selector matches when an element is
the immediate child of some element
A child selector is made up of two or more selectors
separated by ">".
The following rule sets the style of all P elements that
are children of BODY
body > P { line-height: 1.3 }
21
div > p {
background-color: yellow;
}
<body>
<div>
<p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
<span>
<p>Paragraph 3 in the div.</p>
</span> <!--not Child but Descendant -->
</div>
</body>
22
6.Desecndent
The descendant selector matches all elements that are
descendants of a specified element.
The combinator we use in a descendant selector is a
whitespace character: a space, horizontal tab, carriage
return, line feed, or form feed
E F : Matches any F element that is a descendant of an
E element.
table p { font-family:sans-serif}
23
div p {
background-color: yellow;
}
<body>
<div>
<p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
<span>
<p>Paragraph 3 in the div.</p>
</span>
</div>
<p>Paragraph 4. Not in a div.</p>
<p>Paragraph 5. Not in a div.</p>
</body>
24
7. Adjacent Sibiling
Adjacent sibling combinator uses the plus symbol (+)
as a combinator & is same as general sibling selector
The difference is that the targeted element must be an
immediate sibling, not just a general sibling.
Ex.1: p + p { text-indent: 1.5em; margin-bottom: 0; }
Ex .2 : h2+p { font-family:sans-serif}
<h2>heading</h2>
<p>selector above matches this paragraph</p>
<p> selector above does not match this paragraph</p>
25
div + p {
background-color: yellow;
}
div + p : Selects all <p> elements that are placed
immediately after <div> elements
<body>
<div>
<p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
</div>
<p>Paragraph 3. Not in a div.</p>
<p>Paragraph 4. Not in a div.</p>
</body>
26
div ~ p {
background-color: yellow;
}
<body>
<div>
<p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
</div>
<p>Paragraph 3. Not in a div.</p>
<p>Paragraph 4. Not in a div.</p>
</body>
27
8:Query
Returns the first element within the document (using
depth-first pre-order traversal of the document's
nodes) that matches the specified group of selectors
Returns null if no matches are found
syntax: element = document.querySelector(selectors);
where
element is an element object.
selectors is a string containing one or more CSS selectors separated by
commas.
Get the first element in the document with class="example":
document.querySelector(".example");
28
To return all the matches, use querySelectorAll() method instead
<h2>A h2 element</h2>
<h3>A h3 element</h3>
document.querySelector("h2, h3").style.backgroundColor = "red";
<h3>A h3 element</h3>
<h2>A h2 element</h2>
document.querySelector("h2, h3").style.backgroundColor = "red";
29
Query Selector Program
30
31
Query Selector Program
<!DOCTYPE html>
<html>
<head><title>hello</title>
</head>
<body>
<h1>Working with Query selector</h1>
<div id="div1" style="padding:50px;width=100px;height=100px;border:1px solid
black">
</div>
<p>Move the cursor over color name</p>
<label id="label1">Blue</label>
<label id="label2">Red</label>
<label id="label3">Yellow</label>
<input id="text">
<script type="text/javascript">
if(document.querySelector)
{ var lbl1=document.querySelector('#label1')
var lbl2=document.querySelector('#label2')
32
var lbl3=document.querySelector('#label3')
{
document.querySelector('#text').value="This is blue color";
document.querySelector('#text').style.color="blue";
document.querySelector('#div1').style.background="blue";
}
lbl2.onmouseover=function()
{
document.querySelector('#text').value="This is Red color";
document.querySelector('#text').style.color="red";
document.querySelector('#div1').style.background="red";
}
lbl3.onmouseover=function()
{
document.querySelector('#text').value="This is green color";
document.querySelector('#text').style.color="green";
document.querySelector('#div1').style.background="green";
}
}
</script>
</body>
</html>
33
<style>
p.ex1 {
margin: 2cm 4cm 3cm 4cm;
}
</style>
</head>
<body>
<p>A paragraph with no specified margins.</p>
<p class="ex1">A paragraph with specified margins.</p>
<p>A paragraph with no specified margins.</p>
</body>
34
CSS3 Pseudo-classes
/* visited link */
a:visited {
color: #00FF00;
}
/* mouse over link */
a:hover {
color: #FF00FF;
}
/* selected link */
a:active {
color: #0000FF;
}
37
Pseudoclass2.html
<!DOCTYPE html>
<head>
<style type="text/css">
a:focus{border-width:10; border-style: solid;border-color: black;background-color:lime;}
p:hover {background-color:yellow;color:green;}
</style>
</head>
<body>
<h1><a href="pseudoclass11.html">Dynamic Pseudo classes page11</a1></h1>
<p>
demo text demo text demo text demo text demo text
demo text demo text demo text demo text demo text
demo text demo text demo text demo text demo text
demo text demo text demo text demo text demo text
demo text demo text demo text demo text demo text
demo text demo text demo text demo text demo text
</p>
</body>
</html>
38
Pseudoclass11.html
<!DOCTYPE html>
<head>
<style type="text/css">
a:link {color:blue;}
a:visited {color:green;}
a:active{color:orange;}
</style>
</head>
<body>
<h1><a href="pseudoclass2.html">Dynamic Pseudo classes page2</a1></h1>
</body>
</html>
39
CSS3 Pseudo-elements
47
<body>
<h1>Welcome to My Homepage</h1>
<p>This paragraph is not the first child of its
parent.</p>
<div>
<p>This paragraph is the first child of its
parent (div).</p>
<p>This paragraph is not the first child of its
parent.</p>
</div>
</body>
48
CSS3 :last-child Selector
49
<body>
</body>
50
CSS3 :nth-child() Selector
51
<body>
<p>The first paragraph.</p>
<p>The second paragraph.</p>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>
</body>
52
CSS3 :nth-last-child() Selector
53
<body>
</body>
54
CSS3 :nth-last-of-type() Selector
55
<style>
p:nth-last-of-type(3n) {
background: red;
}
</style></head>
<body>
<p>The first paragraph.</p>
<p>The second paragraph.</p>
<p>The third paragraph.</p>
<p>The fourth paragraph.</p>
<p>The fifth paragraph.</p>
<p>The sixth paragraph.</p>
<p>The seventh paragraph.</p>
<p>The eight paragraph.</p>
<p>The ninth paragraph.</p>
</body>
56
Typography
57
Difference Between A Font And A Typeface
A font is what you use, a typeface is what you see
Typefaces include Times Roman, Helvetica, and Courier
The typeface represents one aspect of a font
The font also includes such characteristics as size, weight,
italics, and so on.
There are two general categories of typefaces: serif and
sans serif
Sans serif typefaces are composed of simple lines, whereas
serif typefaces use small decorative marks to embellish
characters and make them easier to read
Helvetica is a sans serif type and Times Roman is a serif type.
58
59
font-family
It is used to declare the fonts that should be used to display text
The value of the font-family property contains multiple font
names
The first declared font, starting from the left, is the primary
font choice
If the first font is unavailable, alternative fonts are declared
after it in order of preference from left to right
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; }
60
Font Size
It provides the ability to set the size of text using common length values,
including pixels, em units, percentages, points, or font-size keywords
body { font-size: 14px; }
61
Setting the Font Size
Many Web page designers opt to use relative units, which
are expressed relative to the size of other objects within
the Web page
Em unit
Percentages
Relative keywords
Larger
Smaller
62
Font Style
p.italic {
font-style: italic;
}
p.oblique {
font-style: oblique;
}
63
Example of Oblique font-style
64
Font Variant
65
p.normal { font-size:20px; font-variant:normal}
p.scaps { font-size:20px; font-variant:small-caps}
66
span {
font-variant:small-caps;
}
67
Font Weight
It is used to style text as bold or to change the specific
weight of a typeface
The font-weight property accepts either keyword or
numeric values.
Keyword values include normal, bold, bolder, lighter, and
inherit.
.intro { font-weight: bold; }
OR
.intro { font-weight: 600; }
68
Values Description
100 Represents thin font
200 Represents extra light font
300 Represents light font
400 Represents normal font
500 Represents medium font
600 Represents semi-bold font
700 Represents bold font
800 Represents extra bold font
900 Represents black font
69
p.normal {font-weight:normal;}
p.light {font-weight:lighter;}
p.thick {font-weight:bold;}
p.thicker {font-weight:900;}
70
Font-stretch
Used to change the width of a font
It enables to condense or expand the width of the font by
specifying following values:
P{
Font-stretch:condense;
}
71
CSS3 Web Fonts - The @font-face Rule
Web fonts allow Web designers to use fonts that are not
installed on the user's computer
In the CSS3 @font-face rule you must first define a name
for the font (e.g. myFirstFont), and then point to the font
file.
72
Different Font Formats
TrueType Fonts (TTF)
TrueType is a font standard developed in the late 1980s, by Apple and Microsoft. TrueType is the
most common font format for both the Mac OS and Microsoft Windows operating systems.
OpenType Fonts (OTF)
OpenType is a format for scalable computer fonts. It was built on TrueType, and is a registered
trademark of Microsoft. OpenType fonts are used commonly today on the major computer
platforms.
The Web Open Font Format (WOFF)
WOFF is a font format for use in web pages. It was developed in 2009, and is now a W3C
Recommendation. WOFF is essentially OpenType or TrueType with compression and additional
metadata. The goal is to support font distribution from a server to a client over a network with
bandwidth constraints.
SVG Fonts/Shapes
SVG fonts allow SVG to be used as glyphs when displaying text. The SVG 1.1 specification
define a font module that allows the creation of fonts within an SVG document. You can also
apply CSS to SVG documents, and the @font-face rule can be applied to text in SVG documents.
Embedded OpenType Fonts (EOT)
EOT fonts are a compact form of OpenType fonts designed by Microsoft for use as embedded
fonts on web pages.
73
@font-face {
font-family: myFirstFont;
src: url(sansation_light.woff);
}
div {
font-family: myFirstFont;
}
74
Using Google fonts
1. Choose the styles you want:
Lobster
Normal 400
2. Choose the character sets you want:
3. Add this code to your website:
<link href='http://fonts.googleapis.com/css?family=Lobster'
rel='stylesheet' type='text/css'>
4. Integrate the fonts into your CSS:
font-family: 'Lobster', cursive;
75
Example
<head>
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?
family=Tangerine">
<style>
body{
font-family:'Tangerine', serif;
font-size:45px;
// font-weight:900;
text-shadow: 4px 4px 4px #FF0000;
}
</style>
</head>
76
CSS3 Borders
77
Border-radius
78
Box-shadow Property
79
border-image Property
80
81
Text Effects
text-shadow
word-wrap
82
Text Shadow
83
Word Wrapping
84
CSS Colors
85
Hexadecimal Colors
Hexadecimal color values are supported in all major browsers.
A hexadecimal color is specified with: #RRGGBB
where the RR (red), GG (green) and BB (blue) hexadecimal
integers specify the components of the color
All values must be between 0 and FF.
#p1 {background-color: #ff0000;} /* red */
#p2 {background-color: #00ff00;} /* green */
#p3 {background-color: #0000ff;} /* blue */
86
RGB Colors
87
RGBA Colors
88
HSL Colors
89
HSLA Colors
90
Example
<style>
h1{background-color: hsla(0,55%,50%,0.0);}
h2{background-color: hsla(0,55%,50%,0.2);}
h3{background-color: hsla(0,55%,50%,0.4);}
h4{background-color: hsla(0,55%,50%,0.6);}
h5{background-color: hsla(0,55%,50%,0.8);}
h6{background-color: hsla(0,55%,50%,1);}
</style>
91
Floating an Element
92
Floating an Element
To float an element, use the style
float: position
where position is none (to turn off floating), left or right
To display an element clear of a floating element, use the
style
clear: position
where position is none, left, right, or both
93
Formatting Backgrounds
The syntax for inserting a background image is:
background-image: url(url)
URL is the location and filename of the graphic file you want to
use for the background of the Web page
94
Background Image Options
By default, background images are tiled both horizontally
and vertically until the entire background of the element is
filled up
You can specify the direction of the tiling using the style:
background-repeat: type
95
Background Image Options
96
Background Color
The background-color property specifies the background color of an element
body {
background-color: #b0c4de;
}
With CSS, a color is most often specified by:
Examples
h1 {
background-color: #6495ed;
}
p {
background-color: #e0ffff;
}
div {
background-color: #b0c4de;
}
97
CSS3 The background-size Property
98
CSS3 The background-origin Property
99
CSS3 Multiple Background Images
100
background:
url("http://www.commentsyard.com/cy/01/8734_original.gif"),linear-gradient(to
right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1)), url("http://7-
themes.com/data_images/out/52/6947542-cute-white-flowers.jpg");
background-position: left top, right;
background-repeat: repeat-x,repeat-y;
background-size: 10% 90%, 300px 400px, auto;
101
body{
background-image:
url("http://www.commentsyard.com/cy/01/8734_original.gif"),
url("http://7-themes.com/data_images/out/52/6947542-cute-white-
flowers.jpg");
background-position: left top, right;
background-repeat: no-repeat;
102
Text shadows
The text-shadow property adds shadow to text.
text-shadow: h-shadow v-shadow blur-radius color|none|
initial|inherit;
.element {
text-shadow: 1px 1px 1px #cccccc;
}
103
CSS3 Gradients
104
CSS3 Linear Gradients
105
The following example shows a
linear gradient that starts at the
top. It starts red, transitioning to
green
Example
A linear gradient from top to
bottom:
#grad {
background: linear-gradient(red,
green); /* Standard syntax */
}
106
background:linear-gradient
(to right,red,green);
107
Linear Gradient - Diagonal
You can make a gradient diagonally by specifying both
the horizontal and vertical starting positions.
Following example shows linear gradient that starts at top
left (and goes to bottom right). It starts red, transitioning
to blue:
#grad {
background: linear-gradient(to bottom right, red , green); /*
Standard syntax */
}
108
body{
background:linear-gradient(to
bottom right,red,green);
}
109
Using Angles
background: linear-
gradient(angle, color-stop1, color-
stop2);
The angle is specified as an
angle between a horizontal line
and the gradient line, going
counter-clockwise
0deg creates a bottom to top
gradient
90deg generates a left to right
gradient.
background: linear-
gradient(180deg, red, green); /*
Standard syntax */
110
Using Multiple Color Stops
111
creating a linear gradient
with the color of the rainbow
background: linear-gradient(to
right,
red,orange,yellow,green,blue,in
digo,violet);
112
CSS3 gradients also support
transparency, which can be
used to create fading effects
To add transparency, we use
the rgba() function to define
the color stops
background: linear-gradient(to
right, rgba(255,0,0,0),
rgba(255,0,0,1)); /*Standard*/
113
body{
background:
url("http://www.commentsyard.com/cy/01/8734_original.gif"),linear-
gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1)),
url("http://7-themes.com/data_images/out/52/6947542-cute-white-
flowers.jpg");
background-position: left top, right;
background-repeat: repeat-x,repeat-y;
114
Repeating a linear-gradient
The repeating-linear-
gradient() function is used to
repeat linear gradients:
background: repeating-linear-
gradient(red, violet 10%, blue
20%);
115
CSS3 Radial Gradients
116
A radial gradient with
differently spaced color
stops
background: radial-gradient(red
5%, green 15%, blue 60%); /*
Standard syntax */
The shape parameter defines
the shape. It can take the
value circle or ellipse. The
default value is ellipse
background: radial-
gradient(circle, red, yellow,
green); /* Standard syntax */
117
CSS3 Transforms
118
translate() Method
With the translate() method, the element moves from
its current position, depending on the parameters
given for the left (X-axis) and the top (Y-axis)
position
div {
transform: translate(50px,100px);
}
119
transform: scale(2,4)
120
CSS hover effects
CSS hover effects gives us the ability to animate changes
to a CSS property value.
121
<html>
<head>
<style type="text/css">
a:hover
{ text-decoration: none; }
</style>
</head>
<body> Try mouse the mouse over to
the link.
<br>
<a
href="http://corelangs.com/html/defau
lt.html">Corelangs</a>
</body>
</html>
122
<head>
<style type="text/css">
.picColor
{ width:320px;
height:240px;
border:5px solid #000000;
}
.picColor:hover
{ background:#FF2400; width:320px;
height:240px;
}
</style>
</head>
<body>
<div class="picColor"> </div> </body>
123
Class Work
change the image link while on mouse hover.
the CSS code that display text on image while mouse
hover
how to implement Opacity on an Image while mouse
hover
124
CSS3 Transitions
126
transition-timing-function: ease|linear|ease-in|ease-out|
ease-in-out|cubic-bezier();
127
http://css3.bradshawenterprises.com/transitions/
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_
Transitions/Using_CSS_transitions
128
#content a
{
transition-property: all;
transition-duration: 1s;
transition-timing-function: ease;
transition-delay: 0s;
}
129
<div id="div1"
style="padding:70px;border:2p
x solid black;">
</div>
#div1
{
background: red;
float:left;
}
#div1:hover
{
background-color: green;
cursor: pointer;
transition: background-color 2s
linear;
130
The transition shorthand property
transition: all 1s ease 0s;
131
<style>
div {
background-color:red;
color: white;
transition-property: color, text-shadow;
transition-duration: 1s, 5s;
}
div:hover
{
background-color: green;
cursor: pointer;
color: black;
text-shadow:10px 5px 5px #ffffff;
}
</style>
132
*{
transition: all 1s;
}
The above eg. uses the CSS universal selector * to select
everything and then setting a transition on all elements
over 1 second (1s).
Timing function ‘ease’ will be used by default
133
CSS Cursors
<html>
<head>
<style type="text/css">
.xlink {cursor:crosshair}
.hlink{cursor:help}
</style>
</head>
<body>
<b>
<a href="mypage.htm" class="xlink">CROSS LINK</a>
<br>
<a href="mypage.htm" class="hlink">HELP LINK</a>
</b>
</body>
</html>
134
CSS3 2D transformations
A transformation is an effect that lets an element change shape, size
and position.
Transitions smooth the change from one state to another, while
transformations are defining what the element will become
The CSS3 2D Transforms Module allows us to use the following
transformations:
scale: used to scale an element (larger or smaller)
translate: move an element on the screen (up, down, left, and right)
rotate: rotate the element by a specified amount (defined in degrees)
skew: used to skew an element with its X and Y co-ordinates
matrix: allows you to move and shape transformations with pixel
precision
135
scale
With the scale() method, the
element increases or decreases
the size, depending on the
parameters given for the width
(X-axis) and the height (Y-axis)
div {
transform: scale(2,4);
}
The value scale(2,4) transforms
the width to be twice its original
size, and the height 4 times its
original size.
136
translate
translate tells the browser to move the element by an amount, defined in either
pixels or percentages
The syntax is applied first from the left to the right (40px
here) and then from the top to the bottom (0px here so it stays in line with the other
navigation elements).
Positive values given within parentheses move the element right or down; negative
values move it left or up
transform: translate(50px,100px);
The value translate(50px,100px) moves the element 50 pixels from the left, and 100
pixels from the top.
137
Rotate()
With the rotate() method,
the element rotates
clockwise at a given
degree. Negative values
are allowed and rotates the
element counter-clockwise
transform: rotate(30deg);
138
Matrix
It essentially allows you to
combine a number of other
transforms (scale, rotate,
skew, and so on) into a
single declaration
transform: matrix(0.866,
0.5,-0.5,0.866,0,0);
139
Skew
Skew
It allows an element to be
skewed on either or both
of its axes.
The element turns in a
given angle, depending on
the parameters given for
the horizontal (X-axis) and
the vertical (Y-axis) lines:
transform: skew(30deg,
20deg);
140
CSS3 Animation
CSS3 animations can replace animations created by Flash and
JavaScript in existing web pages
Enables to animate an element.
Every animation consists of the animation properties, e.g. delay
and duration, and the related keyframes, which define its
appearance
The @keyframes rule is where the animation is created
When an animation is created in the @keyframe rule, you must bind
it to a selector, otherwise the animation will have no effect.
Bind the animation to a selector (element) by specifying at least
these two properties:
the name of the animation
the duration of the animation
141
Bind the "myfirst" animation to the <div> element. Animation duration:
5 seconds:
div {
animation: myfirst 5s;
}
/* Standard syntax */
@keyframes myfirst {
from {background: red;}
to {background: yellow;}
}
If the duration part is not specified, the animation will have no effect, because
the default value is 0.
You can specify when the change will happen in percent, or you can use the
keywords "from" and "to" .
0% represents the start of the animation, 100% is when the animation is
complete.
142
Change the background color when the animation is 25%,
and 50%, and again when the animation is 100% complete
@keyframes myfirst {
0% {background: red;}
25% {background: yellow;}
50% {background: blue;}
100% {background: green;}
}
CSS Syntax
animation: name duration timing-function delay iteration-
count direction fill-mode play-state;
143
144
Media query
With a @media query, you can write different CSS code for
different media types
This helps when you want different layout for different media
types such as a screen or a printer, but also when you want
different layout for different devices, which is very useful
when making web pages with responsive design.
You can also have different layout when a user resizes the
browser window up or down to a certain width, or height.
CSS syntax :
@media mediatype and|not|only (media feature) {
CSS-Code;
}
145
Media query
Change the background-color if the document is smaller than 300 pixels wide
@media screen and (max-width: 300px) {
body {
background-color: lightblue;
}
}
The web page will have a lightblue background if the orientation is in landscape mode:
@media only screen and (orientation: landscape) {
body {
background-color: lightblue;
}
}
146
Example of Media query
<style>
@media screen and (min-width:1001px) and (max-width:1400px) {
body {
background-color:red;
}
}
@media screen and (min-width:501px) and (max-width:1000px) {
body {
background-color:blue;
}
}
@media screen and (min-width:240px) and (max-width:500px) {
body {
background-color: green;
}
}
147
148
Media features
width: The viewport width.
height: The viewport height.
device-width: The rendering surface's width (typically the screen width
of a device).
device-height: The rendering surface's height (typically the screen height
of a device).
orientation: checks whether a device is portrait or landscape in
orientation.
aspect-ratio: The ratio of width to height based upon the viewport width
and height. A 16:9 widescreen display can be written as aspect-ratio:
16/9.
device-aspect-ratio: This capability is similar to aspect-ratio but is based
upon the width and height of the device rendering surface, rather than
viewport
149
color: The number of bits per color component.
For example, min-color:16 will check that the device has 16-bit color.
color-index: The number of entries in the color lookup table of the device.
Values must be numbers and cannot be negative.
monochrome: This capability tests how many bits per pixel are in a
monochrome frame buffer. The value would be a number (integer), for
example monochrome: 2, and cannot be negative.
resolution: This capability can be used to test screen or print resolution;
For example, min-resolution: 300dpi. It can also accept measurements in
dots per centimetre; for example, min-resolution: 118dpcm.
scan: This can be either progressive or interlace features largely particular to
TVs. For example, a 720p HD TV (the p part of 720p indicates
"progressive") could be targeted with scan: progressive whilst a 1080i HD
TV (the i part of 1080i indicates "interlaced") could be targeted with scan:
interlace.
grid: This capability indicates whether or not the device is grid or bitmap
based.
150
@import url("phone.css") screen and (min-width:200px) and
(maxwidth: 360px);
The phone.css file will only be imported for screen
devices with a minimum viewport width of 200 pixels and
a maximum viewport width of 360 pixels.
151
viewport
Viewport relates to the content area within the browser
window, excluding the toolbars, tabs
Screen size refers to the physical display area of a device
A viewport controls how a webpage is displayed on a
mobile device
Without a viewport, mobile devices will render the page at
a typical desktop screen width, scaled to fit the screen
Setting a viewport gives control over the page's width and
scaling on different devices
152
153
Syntax and attributes
@viewport { /* viewport-descriptor: viewport-value; */ }
Descriptors :
min-width Used in the determination of the width of the viewport when
the document is first displayed
max-width Used in the determination of the width of the viewport when
the document is first displayed
Width A shorthand descriptor for setting both min-width and max-width
min-height Used in the determination of the height of the viewport when
the document is first displayed
max-height Used in the determination of the height of the viewport when
the document is first displayed
Height A shorthand descriptor for setting both min-height and max-height
154
Ex.1 :@viewport { min-width: 640px; max-width: 800px; }
Ex.2: @viewport { zoom: 0.75; min-zoom: 0.5; max-zoom:
0.9; }
Ex.3: @viewport { orientation: landscape; }
155
A @viewport rule can be used inside media queries as follows:
@media screen and (max-width:400px)
{
@-ms-viewport { width:320px;}
}
This enables media query to lay out any viewport narrower than 400px
to a scaled width of 320px.
156
Layouts
When designing a website layout for a large audience, the
designer must consider the following potential differences
among visitors:
Screen resolution,
Browser choice,
Whether or not the browser is maximized,
Extra toolbars open in the browser (History, Bookmarks, etc.),
Even the operating system and hardware.
Layouts
Fixed Layout
Fluid Layout
157
FIXED WEBSITE LAYOUTS
159