CSS3 Media Types
CSS3 Media Types
Value Description
All Used for all media type devices
Print Used for printers
Used for computer screens, tablets, smart-phones
Screen
etc.
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: pink;
body {
background-color: lightgreen;
</style>
</head>
<body>
<p>The media query will only apply if the media type is screen and the viewport is 480px wide or
wider.</p>
</body>
</html>
CSS gradients let you display smooth transitions between two or more specified colors.
Syntax
background-image: linear-gradient(direction, color-stop1, color-stop2, ...);
The following example shows a linear gradient that starts at the top. It starts red, transitioning to
yellow:
#grad {
background-image: linear-gradient(red, yellow);
}
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 200px;
}
</style>
</head>
<body>
<p>This linear gradient starts at the top. It starts red, transitioning to yellow:</p>
<div id="grad1"></div>
</body>
</html>
#grad {
background-image: linear-gradient(to right, red , yellow);
}
#grad {
background-image: linear-gradient(to bottom right, red, yellow);
}
#grad {
background-image: linear-gradient(-90deg, red, yellow);
}
#grad {
background-image: linear-gradient(red, yellow, green);
}
Using Transparency
CSS gradients also support transparency, which can be used to create
fading effects.
#grad {
background-image: repeating-linear-gradient(red, yellow 10%, green 20%);
}
To create a radial gradient you must also define at least two color stops.
Syntax
background-image: radial-gradient(shape size at position, start-color, ..., last-color);
#grad {
background-image: radial-gradient(red, yellow, green);
}
#grad {
background-image: radial-gradient(red 5%, yellow 15%, green 60%);
}
Rounded Corners
The border-radius property can be used to add a corner to each corner of a box.
#marilyn {
background: #fff;
width: 100px;
height: 100px;
border-radius: 20px;
}
Shadows
You can give parts of your page “pop” by applying shadows both to boxes and to text.
Box Shadows
box-shadow is the standard CSS property to get you going and it can have a value comprising
several parts:
The first value is the horizontal offset — how far the shadow is nudged to the right (or
left if it’s negative)
The second value is the vertical offset — how far the shadow is nudged downwards (or
upwards if it’s negative)
The third value is the blur radius — the higher the value the less sharp the shadow. (“0”
being absolutely sharp). This is optional — omitting it is equivalent of setting “0”.
The fourth value is the spread distance — the higher the value, the larger the shadow
(“0” being the inherited size of the box). This is also optional — omitting it is equivalent
of setting “0”.
The fifth value is a color. That’s optional, too.
Inner shadows
You can also apply shadows to the inside of a box by adding “inset” to the list:
CSS Transition
The CSS transitions are effects that are added to change the element gradually from one style to
another, without using flash or JavaScript.
<!DOCTYPE html>
<html>
<head>
<style>
div{
width: 100px;
height: 100px;
background: orange;
-webkit-transition: width 1s; /* For Safari 3.1 to 6.0 */
transition: width 1s;
}
div:hover {
width: 300px;
}
</style>
</head>
<body>
<div></div>
<p>Move the cursor over the div element above, to see the transition effect.</p>
</body>
</html>
Let's take an example. Here, the transition effects on width, height and transformation.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. div {
6. padding:15px;
7. width: 150px;
8. height: 100px;
9. background: violet;
10. -webkit-transition: width 2s, height 2s, -webkit-transform 2s; /* For Safari 3.1 to 6.0 */
CSS Animation property is used to create animation on the webpage. It can be used as a
replacement of animation created by Flash and JavaScript.
The animation could be bound to the selector by specifying at least these two properties:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. div {
6. width: 100px;
7. height: 100px;
8. background: red;
9. position: relative;
10. -webkit-animation: myfirst 5s; /* Chrome, Safari, Opera */
11. animation: myfirst 5s;
12. }
13. /* Chrome, Safari, Opera */
14. @-webkit-keyframes myfirst {
15. 0% {background:red; left:0px; top:0px;}
16. 25% {background:yellow; left:300px; top:0px;}
17. 50% {background:blue; left:200px; top:300px;}
18. 75% {background:green; left:0px; top:200px;}
19. 100% {background:red; left:0px; top:0px;}
20. }
21. /* Standard syntax */
22. @keyframes myfirst {
23. 0% {background:red; left:0px; top:0px;}
24. 25% {background:yellow; left:300px; top:0px;}
25. 50% {background:blue; left:300px; top:200px;}
26. 75% {background:green; left:0px; top:200px;}
27. 100% {background:red; left:0px; top:0px;}
28. }
29. </style>
30. </head>
31. <body>
32. <p><b>Note:</b> The Internet Explorer 9 and its earlier versions don't support this exam
ple.</p>
33. <div></div>
34. </body>
35. </html>
The @keyframes
Here we'll set the animation stages. Our @keyframes properties are:
For example:
1
@keyframes testFade {
2 0% {
3 opacity: 1;
4 }
5 100% {
6 opacity: 0;
}
7
}
8
or:
1 @keyframes testFade {
2
from {
3 opacity: 1;
4 }
5 to {
6 opacity: 0;
}
7
}
8
or the shorthand:
1 @keyframes testFade {
2 to {
3 opacity: 0;
4 }
}
5
The Animation
The animation property is used to call @keyframes inside a CSS selector.
Animation can have multiple properties:
For example:
1
.element {
2 animation-name: testFade;
3 animation-duration: 4s;
4 animation-delay: 1s;
5 animation-iteration-count: infinite;
6 animation-timing-function: linear;
animation-direction: alternate;
7
}
8
or shorthand:
1 .element {
3 }
The code above will create a blinking effect, with a 1 second animation delay,
a 4 second total animation duration, with alternate direction and infinite linear
loop iterations.
Multiple Animations
.element {
animation: tutsFade 4s 1s infinite linear alternate,
tutsRotate 4s 1s infinite linear alternate;
}
@keyframes tutsFade {
to {
opacity: 0;
}
}
@keyframes tutsRotate {
to {
transform: rotate(180deg);
}
}
Square to Circle
@keyframes square-to-circle {
0% {
border-radius:0 0 0 0;
background:coral;
transform:rotate(0deg);
}
25% {
border-radius:50% 0 0 0;
background:darksalmon;
transform:rotate(45deg);
}
50% {
border-radius:50% 50% 0 0;
background:indianred;
transform:rotate(90deg);
}
75% {
border-radius:50% 50% 50% 0;
background:lightcoral;
transform:rotate(135deg);
}
100% {
border-radius:50%;
background:darksalmon;
transform:rotate(180deg);
}
}
In technical terms, Opacity is defined as degree in which light is allowed to travel through an
object.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. img.trans {
6. opacity: 0.4;
7.
8. }
9. </style>
10. </head>
11. <body>
12. <p>Normal Image</p>
13. <img src="rose.jpg" alt="normal rose">
14. <p>Transparent Image</p>
15. <img class="trans" src="rose.jpg" alt="transparent rose">
16. </body>
17. </html>
CSS Overflow
The CSS overflow property specifies how to handle the content when it overflows its block
level container.
It specifies that if
overflow is clipped, a
Auto scroll bar is needed to
see the rest of the
content.
<!DOCTYPE html>
<html>
<head>
<style>
div.scroll {
background-color: #00ffff;
width: 100px;
height: 100px;
overflow: scroll;
div.hidden {
background-color: #00FF00;
width: 100px;
height: 170px;
overflow: hidden;
</style>
</head>
<body>
<p>overflow:scroll</p>
<p>overflow:hidden</p>
</body>
</html>
CSS Table
border
border-collapse
padding
width
height
text-align
color
background-color
<style>
table, th, td {
}
</style>
<style>
table, th, td {
border-collapse: collapse;
</style>
<style>
table, th, td {
border-collapse: collapse;
th, td {
padding: 10px;
</style>
We can style even and odd table cells for better look
and feel. In this code, we are displaying different
background colors on even and odd cells. Moreover,
we have changed the background-color and color of
<th> tag.
<style>
table, th, td {
border-collapse: collapse;
th, td {
padding: 10px;
tr:nth-child(even) {
background-color: #eee;
}
tr:nth-child(odd) {
background-color: #fff;
th {
color: white;
background-color: gray;
</style>
CSS - z-index
The z-index sets the stacking level of an element.
<html>
<head>
</head>
<body>
<div style = "background-
color:red;
width:300px;
height:100px;
position:relative;
top:10px;
left:80px;
z-index:2">
</div>