Cascading Style Sheets
Cascading Style Sheets
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 their id,
class, type, attribute, etc.
There are several different types of selectors in CSS.
<!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>
Class Selector
.center {
text-align: center;
color: red;
}
Class Selector
p.center {
text-align: center;
color: red;
}
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<h1 class="center">This heading </h1>
<p class="center">This paragraph </p>
</body>
</html>
CSS Universal Selector
*{
text-align: center;
color: blue;
}
Universal Selector
<!DOCTYPE html>
<html>
<head>
<style>
* { color: green;
font-size: 20px; }
</style>
</head>
<body>
<h2>This is heading</h2>
<p>This style will be applied on every
paragraph.</p>
<p>Me too!</p>
<p>And me!</p>
</body>
</html>
CSS Group Selector
h1, h2, p {
text-align: center;
color: red;
}
Group Selector
<!DOCTYPE html>
<html>
<head>
<style>
h1, h2, p {
text-align: center;
color: blue; }
</style>
</head>
<body>
<h1>Hello Javatpoint.com</h1>
<h6>Hello Javatpoint.com</h6>
<h2>Hello Javatpoint.com (In smaller font)</h2>
<p>This is a paragraph.</p>
</body>
</html>
HOW TO ADD CSS
<!DOCTYPE html>
<html>
<body>
<h1 style="color:blue;text-align:center;">This is
a heading</h1>
<p style="color:red;">This is a paragraph.</p>
</body>
</html>
Internal CSS
Inline CSS is used to apply CSS on a single line or element.
To use inline styles, add the style attribute to the relevant
element. The style attribute can contain any CSS property.
<html>
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
}
</style></head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
External CSS
"mystyle.css”
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
External
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
CSS Properties
CSS Colors:
CSS Background Color
<h1 style="background-color:DodgerBlue;">Hello World</h1>
<p style="background-color:Tomato;">Lorem ipsum...</p>
CSS Background:
CSS background property is used to define the background effects on
element. There are 5 CSS background properties that affects the HTML
elements:
1. background-color
2. background-image
3. background-repeat
4. background-attachment
5. background-position
CSS Properties
1. CSS background-color:
<!DOCTYPE html>
<html>
<head>
<style>
h2,p{
background-color: #b0d4de;
}
</style>
</head>
<body>
<h2>My first CSS page.</h2>
<p>Hello Javatpoint.</p>
</body>
</html>
CSS Properties
2. CSS background-image:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url(paper.gif);
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>This page has an image as the background!</p>
</body>
</html>
CSS Properties
3. CSS background-repeat:
<html>
<head>
<style>
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x/no-repeat;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>Here, a background image is repeated only horizontally!</p>
</body>
</html>
CSS Properties
4. CSS background-attachment:
<html><head>
<style>
body {
background-attachment: fixed;🡪(scroll)
}
</style></head>
<body>
<h1>The background-attachment Property</h1>
<p>The background-attachment property specifies whether the background
image should scroll or be fixed (will not scroll with the rest of the page).</p>
</body>
</html>
CSS Properties
5. CSS background-position
The background-position property is used to define the initial position of the
background image. By default, the background image is placed on the top-left
of the webpage.
You can set the following positions: center,top,bottom,left,right
Example: <html>
<head><style>
body {
background-position: center;
}
</style></head>
<body>
<p>This is a fixed background-image. Scroll down the page.</p>
<p>This is a fixed background-image. Scroll down the page.</p>
</body></html>
CSS Properties
CSS background-Shorthand property:
it is also possible to specify all the background properties in one single
property. This is called a shorthand property.
Example: <html>
<head><style>
body {
background: #ffffff url("img_tree.png") no-repeat right top;
margin-right: 200px;
}
</style></head>
<body>
<p>The background property is a shorthand property for specifying all the
background properties in one declaration.</p>
</body>
</html>
CSS Border
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
1. Create a webpage by using CSS and follow the
instructions given below:
Insert a background image
Background image should not be repeated
Background attachment should be fixed
Text color should be blue
Font size should be 25px
Text align should be justify
TASK-1
Letter spacing should be 3px and word spacing should be
6px
Add a border style for each side of the paragraph. The
top border should be dashed and in red color, the right
border should be double and in green color, the bottom
border should be dotted and in cyan color, and the left
border should be solid and in orange color.
2. Create an Image gallery of 6 images by considering the
following CSS properties:
Set a 3px, solid and red border around the images.
Apply top left border radius of 40px, top right
border radius of 70px, bottom right border radius of
50px and bottom left border radius of 90px to all the
images.
TASK-2
The height and width of all images should be 300px
and 400pxrespectively.
Apply a transparency effect of 0.7 to all the images.
Margin around images should be of 10px.
Apply horizontal box shadow of 12px, vertical box
shadow of 16px, blur effect of 18px and color of box
shadow blue.
CSS Margin
CSS Margin property is used to define the space around
elements. It is completely transparent and doesn't have
any background color. It clears an area around the
element.
CSS Padding
CSS Padding property is used to define the space between
the element content and the element border.
It is different from CSS margin in the way that CSS
margin defines the space around elements. CSS padding is
affected by the background colors. It clears an area
around the content.
Margin
<html>
<head> <style>
p.ex {
margin-top: 50px;
margin-bottom: 50px;
margin-right: 100px;
margin-left: 100px;
}
</style> </head>
<body>
<p>This paragraph is not displayed with specified margin.
</p>
<p class="ex">This paragraph is displayed with specified
margin.</p>
</body>
</html>
Padding
<html>
<head> <style>
p.padding {
padding-top: 50px;
padding-right: 100px;
padding-bottom: 150px;
padding-left: 200px;
}
</style>
</head>
<body>
<p>This is a paragraph with no specified padding.</p>
<p class="padding">This is a paragraph with specified
paddings.</p>
</body>
</html>
CSS Font
CSS Font property is used to control the look of texts. By the use of
CSS font property you can change the text size, color, style and more.
Properties;
CSS Font color: This property is used to change the color of the text.
(standalone attribute)
CSS Font family: This property is used to change the face of the font.
CSS Font size: This property is used to increase or decrease the size of
the font.
CSS Font style: This property is used to make the font bold, italic or
oblique.
CSS Font variant: This property creates a small-caps effect.
CSS Font weight: This property is used to increase or decrease the
boldness and lightness of the font.
CSS Font color:
There are three different formats to define a color:
By a color name
By hexadecimal value
By RGB