CSS3 and its properties
CSS3 and its properties
Properties of CSS3
<!DOCTYPE html>
<html>
<head>
<style>
#rcorners1 {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}
#rcorners2 {
border-radius: 25px;
border: 2px solid #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}
#rcorners3 {
border-radius: 25px;
background: url(paper.gif);
background-position: left top;
background-repeat: repeat;
padding: 20px;
width: 200px;
height: 150px;
}
</style>
</head>
<body>
<h1>The border-radius Property</h1>
<p>Rounded corners for an element with a specified background
color:</p>
<p id="rcorners1">Rounded corners!</p>
<p>Rounded corners for an element with a border:</p>
<p id="rcorners2">Rounded corners!</p>
<p>Rounded corners for an element with a background image:</p>
<p id="rcorners3">Rounded corners!</p>
</body>
</html>
The value applies to all four corners, which are rounded equally
EX:
<!DOCTYPE html>
<html>
<head>
<style>
#rcorners1 {
border-radius: 15px 50px 30px 5px;
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}
#rcorners2 {
border-radius: 15px 50px 30px;
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}
#rcorners3 {
border-radius: 15px 50px;
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}
#rcorners4 {
border-radius: 15px;
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}
</style>
</head>
<body>
<h1>The border-radius Property</h1>
<p>Four values - border-radius: 15px 50px 30px
5px:</p>
<p id="rcorners1"></p>
<p>Three values - border-radius: 15px 50px
30px:</p>
<p id="rcorners2"></p>
<p>Two values - border-radius: 15px 50px:</p>
<p id="rcorners3"></p>
<p>One value - border-radius: 15px:</p>
<p id="rcorners4"></p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
#borderimg {
padding: 15px;
</style>
</head>
<body>
<h1>The border-image Property</h1>
</body>
</html>
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
<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
background-repeat: no-repeat,repeat ;
//background-size: 100px,80px,auto;
//background-size:contain;
// background-size:cover;
background-origin: border-box;
background-origin: Content-box;
padding: 15px;
background: yellow;
background-clip: padding-box;
background-clip: Content-box;
</style>
</head>
<body>
<h1>Multiple Backgrounds</h1>
<div id="example1">
</p>
<p>The College offers courses in B.A, B.Sc. B.Com and B.C.A. It has
well equipped laboratories, a good computerized library and a vast sports
field. Opportunities are provided to students to express their talents in
Fine Arts and Popular Science.</p>
</div>
</body>
</html>
Full Size Background Image
Fill the entire page with the image (no white space)
Scale image as needed
Center image on page
Do not cause scrollbars
<!DOCTYPE html>
<html>
<head>
<style>
html {
background-size: cover;
body {
color: white;
</style>
</head>
<body>
</body>
</html>
Hero Image
You could also use different background properties on a <div> to create a hero
image (a large image with text), and place it where you want.
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
.hero-image {
background-size: cover;
height: 500px;
position: relative;
.hero-text {
text-align: center;
position: absolute;
top: 50%;
left: 50%;
color: white;
}
</style>
</head>
<body>
<div class="hero-image">
<div class="hero-text">
<button>Hire me</button>
</div>
</div>
<p>Page content..</p>
</body>
</html>
CSS Colors
CSS supports 140+ color names, HEX values, RGB values, RGBA
values, HSL values, HSLA values, and opacity.
RGBA Colors
Opacity
The CSS opacity property sets the opacity for the whole element
(both background color and text will be opaque/transparent).
The opacity property value must be a number between 0.0 (fully
transparent) and 1.0 (fully opaque).
<!DOCTYPE html>
<html>
<head>
<style>
#p1 {background-color:rgb(255,0,0);
opacity=0.6%;}
#p2 {background-color:rgba(0,255,0,0.3);}
#p3 {background-color:rgba(0,0,255,0.3);}
#p4 {background-color:rgba(192,192,192,0.3);}
#p5 {background-color:rgba(255,255,0,0.3);}
#p6 {background-color:rgba(255,0,255,0.3);}
</style>
</head>
<body>
<p id="p1">Red</p>
<p id="p2">Green</p>
<p id="p3">Blue</p>
<p id="p4">Grey</p>
<p id="p5">Yellow</p>
<p id="p6">Cerise</p>
</body>
</html>
HSL Colors
HSLA Colors
<!DOCTYPE html>
<html>
<head>
<style>
#p1 {background-color:hsl(120,100%,50%);}
#p2 {background-color:hsl(120,100%,75%);}
#p3 {background-color:hsl(120,100%,25%);}
#p4 {background-color:hsl(120,60%,70%);}
#p5 {background-color:hsl(290,100%,50%);}
#p6 {background-color:hsl(290,60%,70%);}
#p7 {background-color:hsla(290,60%,70%,0.3);}
</style>
</head>
<body>
<p id="p1">Green</p>
<p id="p5">Violet</p>
</body>
</html>
CSS Gradients
Linear Gradients
Syntax
background-image:linear-gradient(direction, color-stop1, c
olor-stop2, ...);
Direction - Diagonal
<html>
<head>
<style>
#grad1 {
height: 200px;
background-color: red;
background-image: linear-gradient(red, yellow);
background-image: linear-gradient(to right, red ,
yellow);
}}
</style>
</head>
<body>
<h1>Linear Gradient - Top to Bottom</h1>
<p>This linear gradient starts red at the top,
transitioning to yellow at the bottom:</p>
<div id="grad1"></div>
</body>
</html>
Using Angles
Syntax
Ex:
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 100px;
background-color: red;
#grad2 {
height: 100px;
background-color: red;
#grad3 {
height: 100px;
background-color: red;
#grad4 {
height: 100px;
background-color: red;
</style>
</head>
<body>
</body>
</html>
Using Multiple Color Stops
Repeating a linear-gradient
Ex:
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 200px;
background-color: red; /* For browsers that do not
support gradients */
background-image:repeating-linear-gradient(red, yellow
10%, green 20%);
#grad2 {
height: 200px;
#grad3 {
height: 200px;
</style>
</head>
<body>
<div id="grad1"></div>
<div id="grad2"></div>
<div id="grad3"></div>
</body>
</html>
Ex:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
h2 {
color: white;
h3 {
color: yellow;
</style>
</head>
<body>
<h1>Text-shadow effect!</h1>
<h2>Text-shadow</h2>
<h3>Hello!</h3>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
padding: 15px;
background-color: yellow;
</style>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div.card {
width: 250px;
text-align: center;
div.header {
background-color: #4CAF50;
color: white;
padding: 10px;
font-size: 40px;
}
div.container {
padding: 10px;
</style>
</head>
<body>
<h2>Cards</h2>
<div class="card">
<div class="header">
<h1>1</h1>
</div>
<div class="container">
<p>January 1, 2016</p>
</div>
</div>
</body>
</html>
EX:
<!DOCTYPE html>
<html>
<head>
<style>
div.polaroid {
width: 250px;
text-align: center;
}
div.container {
padding: 10px;
</style>
</head>
<body>
<div class="polaroid">
<div class="container">
<p>Hardanger, Norway</p>
</div>
</div>
</body>
</html>
You can display the overflowed content when hovering over the
element.
<!DOCTYPE html>
<html>
<head>
<style>
p.test1 {
white-space: nowrap;
width: 200px;
overflow: hidden;
text-overflow: clip;
p.test2 {
white-space: nowrap;
width: 200px;
overflow: hidden;
text-overflow: ellipsis;
}
div.test {
white-space: nowrap;
width: 200px;
overflow: hidden;
div.test:hover {
overflow: visible;
</style>
</head>
<body>
<h2>text-overflow: clip:</h2>
<p class="test1">This is some long text that will not fit in the
box</p>
<h2>text-overflow: ellipsis:</h2>
<p class="test2">This is some long text that will not fit in the
box</p>
<h3>text-overflow:clip:</h3>
<div class="test">This is some long text that will not fit in the
box</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p.test {
width: 11em;
word-wrap: break-word;
</style>
</head>
<body>
</body>
</html>
<html>
<head>
<style>
p.test1 {
width: 140px;
word-break: keep-all;
p.test2 {
width: 140px;
word-break: break-all;
</style>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p.test1 {
writing-mode: horizontal-tb;
span.test2 {
writing-mode: vertical-rl;
}
p.test2 {
writing-mode: vertical-rl;
</style>
</head>
<body>
</body>
</html>
CSS 2D Transforms
CSS transforms allow you to move, rotate, scale, and skew
elements.
The translate() Method
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
-ms-transform: translate(50px,100px); /* IE 9 */
</style>
</head>
<body>
<div>
This div element is moved 50 pixels to the right, and 100 pixels
down from its current position.
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
div#myDiv {
-ms-transform: rotate(20deg); /* IE 9 */
div#myDiv1 {
-ms-transform: rotate(-20deg); /* IE 9 */
</style>
</head>
<body>
<div>
</div>
<div id="myDiv">
</div>
<div>
<div id="myDiv1">
</div>
</body>
</html>
<html>
<head>
<style>
div {
margin: 150px;
width: 200px;
height: 100px;
background-color: yellow;
-ms-transform: scale(2,3); /* IE 9 */
</style>
</head>
<body>
<div>
This div element is two times of its original width, and three times of
its original height.
</div>
</body>
</html>
transform: scaleX(2)
or
div {
transform: scaleX(0.5);
}
div {
transform: scaleY(3);
}
div {
transform: skewY(20deg);
}
div {
transform: skew(20deg,10deg);
}
div {
transform: matrix(1, -0.3, 0, 1, 0, 0);
}
CSS 3D Transforms
rotateX()
rotateY()
rotateZ()
#myDiv {
transform: rotateY(130deg);
}
#myDiv {
transform: rotateZ(90deg);
}
CSS Transitions
CSS transitions allow you to change property
values smoothly, over a given duration.
div {
transition-delay: 1s;
}
Transition + Transformation
div {
transition: width 2s, height 2s,
transform 2s;
}
CSS Tooltip
A tooltip is often used to specify extra information about something when the
user moves the mouse pointer over an element
Basic Tooltip
Create a tooltip that appears when the user moves the mouse over an
element:
Ex:
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
.tooltip:hover .tooltiptext {
visibility: visible;
</style>
<body style="text-align:center;">
</div>
</body>
</html>
When the user mouse over this <div>, it will show the
tooltip text.
with class="tooltip".
Positioning Tooltips
The tooltip is placed to the right (left:105%) of the
"hoverable" text (<div>).
Also top:-5px is used to place it in the middle of its
container element.
We use the number 5 because the tooltip text has a top
and bottom padding of 5px.
If you increase its padding, also increase the value of
the top property to ensure that it stays in the middle.
The same applies if you want the tooltip placed to the
left.
Right Tooltip
.tooltip.tooltiptext {
top: -5px;
left: 105%;
Left Tooltip
.tooltip.tooltiptext {
top: -5px;
right: 105%;
Top Tooltip
.tooltip .tooltiptext {
width: 120px;
bottom: 100%;
left: 50%;
margin-left: -60px; /* Use half of the
width (120/2 = 60), to center the tooltip */
}
Bottom Tooltip
.tooltip .tooltiptext {
width: 120px;
top: 100%;
left: 50%;
margin-left: -60px;
}
div {
column-count: 3;
}
div {
column-gap: 40px;
}
div {
column-rule-style: solid;
}
div {
column-rule-width: 1px;
}
div {
column-rule-color: lightblue;
}
Specify How Many Columns an
Element Should Span
The column-span property specifies how many columns an element should
span across.
h2 {
column-span: all;
}
div {
column-width: 100px;
}