CSS Tutorial
CSS Tutorial
By Logeshwaran M -9884000593
CSS
Introduction to CSS
What is CSS and why it is significance?
CSS provides more detailed attributes than plain HTML to define the look
and feel of the website.
CSS Syntax
1. color: brown;
2. font-size: 11 px;
1. Selector{Property1: value1; Property2: value2; ..........;}
1. In-line style
1. In-line style
Here we add style attribute directly into the relevant html tag.
e.g.
We can declare the style properties inside <style> </style> tag to apply
styles to particular page alone, this is called internal style sheet.
<!DOCTYPE>
<html>
<head>
<style>
h1{
color:green;
background-color:yellow;
padding:5px;
}
p{
color:blue;
}
.mystyle { font-family: 'Comic Sans MS', Fallback, sans-serif; color:red;}
</style>
</head>
<body>
<h1>Example for Lennox</h1>
<p>This is Paragraph</p>
<p class="mystyle">This is my style</p>
</body>
</html>
4|Page I n t r o d u c ti o n t o C a s c a d i n g S t y l e S h e e t s
By Logeshwaran M -9884000593
This is the ideal and best way to apply style to many pages. We can change the
look of the entire website by changing in one file.
Each must have below link tag inside head tag, to apply external style.
<head>
</head>
h1
{
font-size:100px;
position: fixed;
left: 550px;
top: 200px;
color:orange;
}
This file should be stored in appropriate path and have extension of .css and
it should not contain any html tags.
CSS Selector
CSS selectors are used to select the content you want to style. Selectors are the part of
CSS rule set. CSS selectors select HTML elements according to its id, class, type, attribute
etc.
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-align: center;
color: blue;
}
</style>
</head>
<body>
<p>This style will be applied on every paragraph.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>
2. CSS Id Selector
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<p id="para1">Hello Javatpoint.com</p>
<p>This paragraph will not be affected.</p>
</body>
</html>
6|Page I n t r o d u c ti o n t o C a s c a d i n g S t y l e S h e e t s
By Logeshwaran M -9884000593
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<h1 class="center">This heading is blue and center-aligned.</h1>
<p class="center">This paragraph is blue and center-aligned.</p>
</body>
</html>
<style>
* {
color: green;
font-size: 20px;
}
</style>
h1 {
text-align: center;
color: blue;
}
h2 {
text-align: center;
color: blue;
}
p {
text-align: center;
color: blue;
}
As you can see, you need to define CSS properties for all the elements. It can be grouped in
following ways:
h1,h2,p {
text-align: center;
color: blue;
}
Sizes of element
We can specify size of the elements such as <div> <p> with the units px or
%. px for fixed width or height, % will take the size of the screen.
Divisions
Divisions are a block level HTML element used to define sections of an HTML
file. A division can contain all the parts that make up your website. Including
additional divisions, spans, images, text and so on.
You define a division within an HTML file by placing the following between
the <body></body> tags:
8|Page I n t r o d u c ti o n t o C a s c a d i n g S t y l e S h e e t s
By Logeshwaran M -9884000593
<div>
</div>
Though most likely you will want to add some style to it. You can do that in
the following fashion:
<div id=”container”>
</div>
#container{
width: 70%;
margin: auto;
padding: 20px;
border: 1px solid #666;
background: #ffffff;
}
Now everything within that division will be styled by the “container” style
rule, I defined within my CSS file. A division creates a linebreak by default.
You can use both classes and IDs with a division tag to style sections of your
website.
Color
You can set the color of text with the following:
color: value;
img {
position: absolute;
left: 10px;
top: 10px;
z-index: -1;
}
<!DOCTYPE html>
<html>
<head>
<style>
div.a {
width: 200px;
height: 100px;
background-color: yellow;
transform: rotate(30deg);
}
div.b {
width: 200px;
height: 100px;
background-color: yellow;
transform: skewY(15deg);
10 | P a g e I n t r o d u c ti o n t o C a s c a d i n g S t y l e S h e e t s
By Logeshwaran M -9884000593
div.c {
width: 200px;
height: 100px;
background-color: yellow;
transform: scaleY(1.5);
}
</style>
</head>
<body>
<h2>transform: rotate(20deg):</h2>
<br>
<div class="a">Hello Lennox Trainers!</div>
<br>
<h2>transform: skewY(20deg):</h2>
<div class="b">Hello Lennox Trainers!</div>
<br>
<h2>transform: scaleY(1.5):</h2>
<div class="c">Hello Lennox Trainers!</div>
</body>
</html>
transition-property
transition-duration
transition-timing-function
transition-delay
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
transition: width 3s;
div:hover {
width: 300px;
}
</style>
</head>
<body>
<p>Hover over the div element below, to see the transition effect:</p>
<div></div>
</body>
</html>
CSS padding Property
An element's padding is the space between its content and its border.
padding-top
padding-right
padding-bottom
padding-left
<!DOCTYPE html>
<html>
<head>
<style>
p.ex1 {
border: 1px solid red;
padding: 35px;
}
p.ex2 {
border: 1px solid red;
margin: 35px;
}
</style>
13 | P a g e I n t r o d u c ti o n t o C a s c a d i n g S t y l e S h e e t s
By Logeshwaran M -9884000593
</head>
<body>
<!DOCTYPE html>
<html>
<head>
<style>
h2.a {
visibility: visible;
}
h2.b {
visibility: hidden;
}
</style>
</head>
<body>
<p>Notice that the hidden heading still takes up space on the page.</p>
</body>
</html>
CSS Functions
CSS functions are used as a value for various CSS properties.
Function Description
(HSLA)
linear-gradient() Sets a linear gradient as the background image. Define at least two
colors (top to bottom)
radial-gradient() Sets a radial gradient as the background image. Define at least two
colors (center to edges)