Unit-Ii CSS
Unit-Ii CSS
What is CSS?
• CSS stands for Cascading Style Sheets
• CSS describes how HTML elements are to be displayed on screen, paper, or in
other media
• CSS saves a lot of work. It can control the layout of multiple web pages all at
once
• External style sheets are stored in CSS files
Why Use CSS?
• CSS is used to define styles for your web pages, including the design, layout and
variations in display for different devices and screen sizes.
• CSS saves time: You can write CSS once and reuse the same sheet in multiple
HTML pages.
• Easy Maintenance: To make a global change simply change the style, and all
elements in all the webpages will be updated automatically.
• Search Engines: CSS is considered a clean coding technique, which means search
engines won’t have to struggle to “read” its content.
• Offline Browsing: CSS can store web applications locally with the help of an
offline cache. Using this we can view offline websites.
Introduction to CSS
CSS Syntax:
• CSS comprises style rules that are interpreted by the browser and then applied
to the corresponding elements in your document.
• A style rule set consists of a selector and declaration block.
Introduction to CSS
• Selector: A selector in CSS is used to target and select specific HTML elements to
apply styles.
• Declaration: A declaration in CSS is a combination of a property and its
corresponding value.
// HTML Element
<h1>GeeksforGeeks</h1>
// CSS Style
h1 { color: blue; font-size: 12px; }
Where -
Selector - h1
Declaration - { color: blue; font-size: 12px; }
• The selector points to the HTML element that you want to style.
• The declaration block contains one or more declarations separated by
semicolons.
• Each declaration includes a CSS property name and a value, separated by a
colon.
CSS declaration always ends with a semicolon, and declaration blocks are
surrounded by curly braces.
Introduction to CSS
</style>
</head>
<body>
<p>Hello World!</p>
<p>These paragraphs are styled with CSS.</p>
</body>
</html>
Output:
• p is a selector in CSS (it points to the HTML element you want to style: <p>).
• color is a property, and red is the property value
• text-align is a property, and center is the property value
CSS Selectors
CSS Selectors:
• CSS selectors are used to "find" (or select) the HTML elements you want to style.
• We can divide CSS selectors into five categories:
1. Element Selector
2. ID selector
3. class selector
4. Universal selector
5. Attribute Selector
CSS element Selector:
• The element selector selects HTML elements based on the element name.
CSS Element Selector
CSS id Selector:
• The id selector uses the id attribute of an HTML element to select a specific
element.
• The id of an element is unique within a page, so the id selector is used to select
one unique element!
• To select an element with a specific id, write a hash (#) character, followed by
the id of the element.
Example 1: Styling a Specific Element by ID using ID selector
<!DOCTYPE html>
<html>
<head>
<style>
#special-paragraph {
color: red;
font-weight: bold;
}
</style>
CSS ID Selector
</head>
<body>
<p id="special-paragraph">This is a special paragraph.</p>
<p>This is another paragraph.</p>
</body>
</html>
Output:
CSS ID Selector
<body>
<p class="highlight">This paragraph has a class "highlight".</p>
<p>This paragraph doesn't have any class.</p>
<p class="highlight">Another paragraph with the class "highlight".</p>
</body>
</html>
Output:
CSS Class Selector
</style>
</head>
<body>
<div class="container">
<p>This paragraph has some default margins and padding.</p>
</div>
</body>
</html>
Output:
In this example, the universal selector * is used to reset margins and padding for all
elements to 0 and to include the border-box sizing model. This ensures consistent
spacing throughout the document.
CSS Universal Selector
li {
color: red;
}
</style>
</head>
<body>
<h1>Welcome to My Website</h1>
<p>This is a paragraph.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</body>
</html>
In this example, the universal selector * is used to apply a common font family and
font size to all elements. Specific styles are then applied to headings (h1), paragraphs
(p), and list items (li).
CSS Attribute Selector
</head>
<body>
<a href="#">Link 1</a>
<a href="https://www.w3schools.com/">Link 2</a>
<a>Link 3</a>
</body>
</html>
Output:
In this example, the attribute selector a[href] targets <a> elements that have an
href attribute. It sets the color of these links to blue.
CSS Attribute Selector
Example 2: Selecting Elements with a Specific Attribute Value Using Attribute Selector
<!DOCTYPE html>
<html>
<head>
<style>
input[type="text"] {
border: 1px solid green;
}
input[type="password"] {
border: 1px solid red;
}
</style>
</head>
<body>
<input type="text" name="username">
<input type="password" name="password">
</body>
</html>
In this example, attribute selectors are used to target <input> elements with specific type
attribute values. It sets the border color of text inputs to green and password inputs to red.
CSS Colors
RGBA Colors: RGBA (Red, Green, Blue, Alpha) notation is similar to RGB, but
includes an additional alpha value representing the opacity of the color, ranging
from 0 (fully transparent) to 1 (fully opaque).
example: color: rgba(255, 0, 0, 0.5); /* semi-transparent red */
HSL Colors: HSL (Hue, Saturation, Lightness) notation represents colors using values
for hue (0 to 360), saturation (0% to 100%), and lightness (0% to 100%).
example:
color: hsl(0, 100%, 50%); /* red */
HSLA Colors: HSLA (Hue, Saturation, Lightness, Alpha) notation is similar to HSL, but
includes an additional alpha value representing the opacity of the color, ranging
from 0 (fully transparent) to 1 (fully opaque).
example:
color: hsla(0, 100%, 50%, 0.5); /* semi-transparent red */
CSS Colors
Output:
CSS Colors
Shades of gray are often defined using equal values for all the 3 light sources
Example3:
<!DOCTYPE html>
<html>
<head>
<style>
.colored-box {
width: 200px;
height: 200px;
background-color: rgb(255, 0, 0);
color: white;
text-align: center;
line-height: 200px; /* Vertical center align */
}
</style>
</head>
<body>
<div class="colored-box">
This is a colored box.
</div>
</body>
</html>
CSS Colors
RGBA Value:
• RGBA color values are an extension of RGB color values with an alpha channel -
which specifies the opacity for a color.
• An RGBA color value is specified with:
rgba(red, green, blue, alpha)
• The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not
transparent at all):
CSS Colors
Output:
CSS Colors
Output:
CSS Colors
Saturation:
• Saturation can be described as the intensity of a color.
• 100% is pure color, no shades of gray.
• 50% is 50% gray, but you can still see the color.
• 0% is completely gray; you can no longer see the color.
CSS Colors
Lightness:
• The lightness of a color can be described as how much light you want to give the
color, where 0% means no light (black), 50% means 50% light (neither dark nor
light) and 100% means full lightness (white).
CSS Colors
Example:
<!DOCTYPE html>
<html>
<body>
<h1>Specify colors using HSL values</h1>
<h2 style="background-color:hsl(0, 100%, 50%);">hsl(0, 100%, 50%)</h2>
<h2 style="background-color:hsl(240, 100%, 50%);">hsl(240, 100%, 50%)</h2>
<h2 style="background-color:hsl(147, 50%, 47%);">hsl(147, 50%, 47%)</h2>
<h2 style="background-color:hsl(300, 76%, 72%);">hsl(300, 76%, 72%)</h2>
<h2 style="background-color:hsl(39, 100%, 50%);">hsl(39, 100%, 50%)</h2>
<h2 style="background-color:hsl(248, 53%, 58%);">hsl(248, 53%, 58%)</h2>
</body>
</html>
CSS Colors
Output:
CSS Background
CSS Background:
• The CSS background properties are used to add background effects for
elements.
CSS background-color:
• The background-color property specifies the background color of an element.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightblue;
}
</style>
</head>
CSS Background
<body>
<h1>Hello World!</h1>
<p>This page has a light blue background color!</p>
</body>
</html>
Output:
CSS Background
CSS background-image:
• The background-image property specifies an image to use as the background of an
element.
• By default, the image is repeated so it covers the entire element.
Example: Set the background image for a page:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url('paper.jpg');
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>This page has an image as the background!</p>
</body>
</html>
CSS Background
CSS background-repeat:
• By default, the background-image property repeats an image both horizontally and
vertically.
• Some images should be repeated only horizontally or vertically, or they will look
strange, like this:
body {
background-image: url(“paper.jpg");
}
• If the image above is repeated only horizontally (background-repeat: repeat-x;), the
background will look better:
Example:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url(“paper.jpg");
background-repeat: repeat-x;
}
CSS Background
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>Here, a background image is repeated only horizontally!</p>
</body>
</html>
Output:
CSS Border:
• The CSS border properties allow you to specify the style, width, and color of an
element's border.
CSS Border Style:
• The border-style property specifies what kind of border to display. 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
• The border-style property can have from one to four values (for the top border, right
border, bottom border, and the left border).
CSS Border
Example:
<!DOCTYPE html>
<html>
<head>
<style>
p.dotted {border-style: dotted;}
p.dashed {border-style: dashed;}
p.solid {border-style: solid;}
p.double {border-style: double;}
p.groove {border-style: groove;}
p.ridge {border-style: ridge;}
p.inset {border-style: inset;}
p.outset {border-style: outset;}
p.none {border-style: none;}
p.hidden {border-style: hidden;}
p.mix {border-style: dotted dashed solid double;}
</style>
</head>
CSS Border
<body>
<h2>The border-style Property</h2>
<p>This property specifies what kind of border to display:</p>
<p class="dotted">A dotted border.</p>
<p class="dashed">A dashed border.</p>
<p class="solid">A solid border.</p>
<p class="double">A double border.</p>
<p class="groove">A groove border.</p>
<p class="ridge">A ridge border.</p>
<p class="inset">An inset border.</p>
<p class="outset">An outset border.</p>
<p class="none">No border.</p>
<p class="hidden">A hidden border.</p>
<p class="mix">A mixed border.</p>
</body>
</html>
CSS Border
p.three{
border-style: double;
border-width: thick;
}
</style>
</head>
<body>
<h2>The border-width Property</h2>
<p>This property specifies the width of the four borders:</p>
<p class="one">Some text.</p>
<p class="two">Some text.</p>
<p class="three">Some text.</p>
</body>
</html>
Output:
CSS Border-color
p.two {
border-style: solid;
border-color: green;
}
p.three {
border-style: dotted;
border-color: blue;
}
</style>
</head>
<body>
<h2>The border-color Property</h2>
<p>This property specifies the color of the four borders:</p>
<p class="one">A solid red border</p>
<p class="two">A solid green border</p>
<p class="three">A dotted blue border</p>
</body>
</html>
CSS Text
</head>
<body>
<h1>This is heading 1</h1>
<p>This is an ordinary paragraph. </p>
<p>Another paragraph.</p>
</body>
</html>
Output:
CSS Text
Text Alignment:
• The text-align property is used to set the horizontal alignment of a text.
• A text can be left or right aligned, centered, or justified.
• The following example shows center aligned, and left and right aligned text (left
alignment is default if text direction is left-to-right, and right alignment is default
if text direction is right-to-left):
Example:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-align: center;
}
h2 {
text-align: left;
}
CSS Text
h3 {
text-align: right;
}
</style>
</head>
<body>
<h1>Heading 1 (center)</h1>
<h2>Heading 2 (left)</h2>
<h3>Heading 3 (right)</h3>
</body>
</html>
CSS Text
p.ex {
text-decoration: overline underline;
}
</style>
</head>
<body>
<h1>Overline text decoration</h1>
<h2>Line-through text decoration</h2>
<h3>Underline text decoration</h3>
<p class="ex">Overline and underline text decoration.</p>
</body>
</html>
Output:
CSS Fonts
CSS Fonts:
• Font Selection is Important
• Choosing the right font has a huge impact on how the readers experience a
website.
• The right font can create a strong identity for your brand.
• Using a font that is easy to read is important. The font adds value to your text. It is
also important to choose the correct color and text size for the font.
Generic Font Families:
In CSS there are five generic font families:
• Serif fonts have a small stroke at the edges of each letter. They create a sense of
formality and elegance.
• Sans-serif fonts have clean lines (no small strokes attached). They create a modern
and minimalistic look.
• Monospace fonts - here all the letters have the same fixed width. They create a
mechanical look.
• Cursive fonts imitate human handwriting.
• Fantasy fonts are decorative/playful fonts.
CSS Fonts
<body>
<h1>CSS font-family</h1>
<p class="p1">This is a paragraph, shown in the Times New Roman font.</p>
<p class="p2">This is a paragraph, shown in the Arial font.</p>
</html>
Output:
CSS Fonts
</style>
</head>
<body>
<h1>The font-style property</h1>
<p class="normal">This is a paragraph in normal style.</p>
<p class="italic">This is a paragraph in italic style.</p>
<p class="oblique">This is a paragraph in oblique style.</p>
</body>
</html>
Output:
CSS Gradients & Shadows
CSS Gradients:
CSS gradients let you display smooth transitions between two or more specified
colors.
CSS defines three types of gradients:
• Linear Gradients (goes down/up/left/right)
• Radial Gradients (defined by their center)
• Conic Gradients (rotated around a center point)
CSS Linear Gradients:
• To create a linear gradient you must define at least two color stops.
• Color stops are the colors you want to render smooth transitions among. You
can also set a starting point and a direction (or an angle) along with the gradient
effect.
Syntax:
Background-image: linear-gradient(direction, color-stop1, color-stop2, ...);
Direction - Top to Bottom (this is default)
CSS Gradients & Shadows
}
</style>
</head>
<body>
<h1>Radial Gradient - Evenly Spaced Color Stops</h1>
<div id="grad1"></div>
</body>
</html>
Output:
CSS Gradients & Shadows
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 200px;
width: 200px;
background-color: red;
background-image: conic-gradient(red, yellow, green);
}
</style>
</head>
<body>
<h1>Conic Gradient - Three Colors</h1>
<div id="grad1"></div>
</body>
</html>
CSS Gradients & Shadows
Multiple Shadows:
• To add more than one shadow to the text, you can add a comma-separated list of
shadows.
• The following example shows a red and blue neon glow shadow:
Example:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-shadow: 0 0 3px #FF0000, 0 0 5px #0000FF;
}
</style>
</head>
<body>
<h1>Text-shadow with red and blue neon glow!</h1>
</body>
</html>
CSS Shadows
</style>
</head>
<body>
<h1>The box-shadow Property</h1>
<div>This is a div element with a box-shadow</div>
</body>
</html>
Output:
CSS Shadows
Multiple Shadows:
• An element can also have multiple shadows:
<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
border: 1px solid;
padding: 10px;
box-shadow: 5px 5px blue, 10px 10px red, 15px 15px green;
margin: 20px;
}
</style>
</head>
<body>
CSS Shadows
<h1>Multiple Shadows</h1>
<div id="example1">
<h2>Multiple shadows</h2>
<p>box-shadow: 5px 5px blue, 10px 10px red, 15px 15px green:</p>
</div>
</body>
</html>
Output: