CSS
CSS
CSS Versions
● CSS1: The foundation, released in 1996, introduced basic styling
● CSS4 (Ongoing):
What are the types of CSS? OR What are the three ways to add CSS
in HTML?
tags using the style attribute. While convenient for quick styling, it's
generally not recommended for larger projects due to maintenance and
organisation issues.
Example:
<p style="color: red; font-size: 18px;">This is a paragraph.</p>
Internal or Embedded CSS: Internal CSS is placed within the <style> element in
the <head> section of an HTML document. It applies styles to the elements of that
particular HTML document only.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
p{
color: blue;
font-size: 16px;
}
</style>
</head>
<body>
<p>This is a paragraph.</p>
</body>
</html>
2.The href attribute specifies the path to the external CSS file. In
this case, it's "styles.css", assuming it's located in the same directory
as the HTML file. If it's located in a different directory, you would
3.The <link> element is placed within the <head> section of the HTML
document
CSS selectors
CSS selectors are patterns used to select and style elements in an HTML
document.
Here are the main types of selectors in CSS:
1. Element Selectors: These selectors target HTML elements based on their
element type. They match all instances of the specified element in the
document.
Example:
p{
color: blue;
}
2. ID Selectors: ID selectors target elements based on their unique id attribute.
IDs should be unique within a document, and each ID selector selects only one
element.
Example-
#header {
background-color: gray;
}
3. Class Selectors: Class selectors target elements based on their class
attribute. Multiple elements can share the same class, and elements can have
multiple classes.
Example: .btn {
background-color: red;
}
4. Universal Selector The universal selector (*) in CSS matches any element
type. It applies style to all elements in the HTML document.
example-
/* Apply a border to all elements */
*{
border: 1px solid black;
}
5. Group Selectors: Group selectors in CSS allow you to apply the same style
rules to multiple selectors simultaneously. This can help reduce redundancy in
your CSS code and make it more concise and readable. You can group
selectors by separating them with commas.
Example-
/* Grouping selectors */
h1, h2, h3 {
font-family: Arial, sans-serif;
color: #333;
}
Example-
a:hover {
text-decoration: underline;
}
Syntax for pseudo element:
Selector::Pseudo-Element{
Property:Value;
}
Example-
p::first-line {
font-weight: bold;
8. CSS Combinators
Example-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>General Sibling Selector Example</title>
<style>
/* Select all <p> elements that are siblings of the <h2> element */
h2 ~ p {
font-style: italic;
color: blue;
}
</style>
</head>
<body>
<h2>Heading</h2>
<p>This paragraph will be styled.</p>
<div>
<p>This paragraph will NOT be styled.</p>
</div>
<p>This paragraph will be styled.</p>
</body>
</html>
Example-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Adjacent Sibling Selector Example</title>
<style>
/* Select the <p> element that immediately follows an <h2> element */
h2 + p {
font-weight: bold;
color: red;
}
</style>
</head>
<body>
<h2>Heading</h2>
<p>This paragraph will be styled.</p>
<div>
<p>This paragraph will NOT be styled because it's not immediately
preceded by an <h2> element.</p>
</div>
<h2>Another Heading</h2>
<p>This paragraph will also be styled because it immediately follows an
<h2> element.</p>
</body>
</html>
Example-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Child Selector Example</title>
<style>
/* Select all <li> elements that are direct children of the <ul> element */
ul > li {
color: blue;
}
</style>
</head>
<body>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>
Item 3
<ul>
<li>Subitem 1</li>
<li>Subitem 2</li>
</ul>
</li>
</ul>
</body>
</html>
Syntax:
ancestor-element descendant-element {
/* Styles */
}
Example-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Descendant Selector Example</title>
<style>
/* Select all <li> elements that are descendants of the <ul> element */
ul li {
color: blue;
}
</style>
</head>
<body>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>
Item 3
<ul>
<li>Subitem 1</li>
<li>Subitem 2</li>
</ul>
</li>
</ul>
</body>
</html>
COLORS
1. Using colour name
2. Using hexadecimal values
3. Using rgb values
4. Using rgba values
5. Using hsl
6. Using hsla
The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not
transparent at all)
Units used in CSS
1.Absolute Units- px
Font styling
The CSS font property is a shorthand property that allows you to set multiple
font-related properties at once, including font-style, font-variant, font-weight,
font-size, line-height, and font-family.
selector {
p{
Fallback fonts- Fallback fonts, also known as fallback font families, are
alternative fonts specified in case the primary font is not available or cannot be
rendered by the browser.
body {
}
To use a custom font on your website, you can follow these steps:
1. Choose a Font: Find a font that you want to use. There are many websites
where you can find free and paid fonts, such as Google Fonts, Adobe Fonts,
Font Squirrel, and DaFont.
2. Download the Font Files: Once you've chosen a font, download the font files.
Typically, you'll get a ZIP file containing various font file formats like .ttf
(TrueType), .otf (OpenType), .woff (Web Open Font Format), and .woff2.
Make sure to download all relevant formats for better browser compatibility.
3. Upload Font Files to Your Web Server: Upload the font files to a directory on
your web server where your website files are hosted. Note down the file paths
as you'll need them later.
4. Declare the Font in CSS: Use the @font-face rule in your CSS to declare the
custom font and specify the paths to the font files
@font-face {
font-family: 'YourFontName';
url('path/to/font.woff') format('woff'),
url('path/to/font.ttf') format('truetype');
Font Effects
First add effect=effectname to the Google API, then add a special class
name to the element that is going to use the special effect. The class name
always starts with font-effect- and ends with the effectname.
<head>
<link rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Sofia&effect=fire">
<style>
body {
font-family: "Sofia", sans-serif;
font-size: 30px;
}
</style>
</head>
<body>
</body>
To request multiple font effects, just separate the effect names with a pipe
character (|), like this:
<head>
<link rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Sofia&effect=neon|o
utline|emboss|shadow-multiple">
<style>
body {
font-size: 30px;
</style>
</head>
<body>
</body>
Text Formatting
1. Text Colour
2. Text Alignment:- (default-left, right, center,justify)
The text-align-last property specifies how to align the last
line of a text.(default-left, right, center,justify)
Text Direction- The direction and unicode-bidi properties can be
used to change the text direction of an element
p {
direction: rtl;
unicode-bidi: bidi-override;
}
The vertical-align property sets the vertical alignment of an
element.(default- baseline, text-top,text-bottom,sub,super)
3. Text Decoration
The text-decoration-line property is used to add a decoration
line to text.(overline;line-through;underline;overline
underline;)
The text-decoration-color property is used to set the color of
the decoration line
The text-decoration-style property is used to set the style of
the decoration line.(solid,double,dashed,dotted,wavy)
The text-decoration-thickness property is used to set the
thickness of the decoration line.(auto,%,px)
text-decoration-color (optional)
text-decoration-style (optional)
text-decoration-thickness (optional)
4. Text Transform
The text-transform property is used to specify uppercase and
lowercase letters in a text.(uppercase,lowercase,capitalize)
5. Text Spacing
The text-indent property is used to specify the indentation of
the first line of a text:
The letter-spacing property is used to specify the space
between the characters in a text.
The line-height property is used to specify the space between
lines:
The word-spacing property is used to specify the space between
the words in a text.
The white-space property specifies how white-space inside an
element is handled.(nowrap)
6. Text Shadow
selector {
text-shadow: [horizontal offset] [vertical offset] [blur radius]
[color];
}
Example- h1 {
}
Displaying Vertical text-
<style>
.vertical-text {
writing-mode: vertical-rl; /* Vertical text from top to bottom */
text-orientation: upright; /* Keep text upright */
}
</style>
</head>
<body>
<div class="vertical-text">
Vertical Text
</div>
</body>