Introduction to CSS Basics and Usage
Introduction to CSS Basics and Usage
What is CSS?
h1 {
color: white;
text-align: center;
}
p{
font-family: verdana;
font-size: 20px;
}
</style>
</head>
<body>
<h1>My First CSS Example</h1>
<p>This is a paragraph.</p>
</body>
</html>
HTML was NEVER intended to contain tags for formatting a web page!
HTML was created to describe the content of a web page, like:
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
When tags like <font>, and color attributes were added to the HTML 3.2 specification, it started a nightmare for
web developers. Development of large websites, where fonts and color information were added to every single
page, became a long and expensive process.
To solve this problem, the World Wide Web Consortium (W3C) created CSS.
CSS removed the style formatting from the HTML page!
CSS Syntax
CSS Syntax
Example
In this example all <p> elements will be center-aligned, with a red text color:
p{
color: red;
text-align: center;
}
Example Explained
● 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 are used to "find" (or select) the HTML elements you want to style.
We can divide CSS selectors into five categories:
The element selector selects HTML elements based on the element name.
Example
Here, all <p> elements on the page will be center-aligned, with a red text color:
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-align: center;
color: red;
}
</style>
</head>
<body>
<p>Every paragraph will be affected by the style.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>
b) The 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
The CSS rule below will be applied to the HTML element with id="para1":
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
<p id="para1">Hello World!</p>
<p>This paragraph is not affected by the style.</p>
</body>
</html>
c) The CSS class Selector
The class selector selects HTML elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed by the class name.
Example
In this example all HTML elements with class="center" will be red and center-aligned:
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1 class="center">Red and center-aligned heading</h1>
<p class="center">Red and center-aligned paragraph.</p>
</body>
</html>
You can also specify that only specific HTML elements should be affected by a class.
Example
In this example only <p> elements with class="center" will be red and center-aligned:
[Link] {
text-align: center;
color: red;
}
HTML elements can also refer to more than one class.
Example
In this example the <p> element will be styled according to class="center" and to class="large":
The universal selector (*) selects all HTML elements on the page.
Example
The CSS rule below will affect every HTML element on the page:
*{
text-align: center;
color: blue;
}
Example:<!DOCTYPE html>
<html>
<head>
<style>
*{
text-align: center;
color: blue;
}
</style>
</head>
<body>
<h1>Hello world!</h1>
<p>Every element on the page will be affected by the style.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>
3)The CSS Grouping Selector
The grouping selector selects all the HTML elements with the same style definitions.
Look at the following CSS code (the h1, h2, and p elements have the same style definitions):
o group selectors, separate each selector with a comma.
Example
In this example we have grouped the selectors from the code above:
h1, h2, p {
text-align: center;
color: red;
}
5)CSS Pseudo-Class Selector:It is used to style a special type of state of any element. For example- It is
used to style an element when a mouse cursor hovers over it.
h1:hover{
background-color: aqua;
}
How to add CSS
CSS is added to HTML pages to format the document according to information in the style sheet. There are
three ways to insert CSS in HTML documents.
1. Inline CSS
2. Internal CSS
3. External CSS
● External CSS
● Internal CSS
● Inline CSS
1)External CSS
With an external style sheet, you can change the look of an entire website by changing just one file!
Each HTML page must include a reference to the external style sheet file inside the <link> element, inside the
head section.
Example
External styles are defined within the <link> element, inside the <head> section of an HTML page:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="[Link]">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
An external style sheet can be written in any text editor, and must be saved with a .css extension.
The external .css file should not contain any HTML tags.
Here is how the "[Link]" file looks:
"[Link]"
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Note: Do not add a space between the property value (20) and the unit (px):
Incorrect (space): margin-left: 20 px;
Correct (no space): margin-left: 20px;
2)Internal CSS
An internal style sheet may be used if one single HTML page has a unique style.
The internal style is defined inside the <style> element, inside the head section.
Example
Internal styles are defined within the <style> element, inside the <head> section of an HTML page:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
3)Inline CSS
An inline style may be used to apply a unique style for a single element.
To use inline styles, add the style attribute to the relevant element. The style attribute can contain any CSS
property.
Example
Inline styles are defined within the "style" attribute of the relevant element:
<!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>
Tip: An inline style loses many of the advantages of a style sheet (by mixing content with presentation). Use
this method sparingly.
**CSS Colors
Colors are specified using predefined color names, or RGB, HEX, HSL, RGBA, HSLA values.
In CSS, a color can be specified by using a predefined colorname:CSS/HTML support 140 standard color
names. For example:
1)<h1 style="background-color:Tomato;">Tomato</h1>
<h1 style="background-color:Orange;">Orange</h1>
2)set the color of borders:
<h1 style="border: 2px solid Tomato;">Hello World</h1>
<h1 style="border: 2px solid DodgerBlue;">Hello World</h1>
An RGB color value represents RED, GREEN, and BLUE light sources.
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):
For example:
<h2 style="background-color:rgba(255, 99, 71, 0);">rgba(255, 99, 71, 0)</h2>
<h2 style="background-color:rgba(255, 99, 71, 0.2);">rgba(255, 99, 71, 0.2)</h2>
A hexadecimal color is specified with: #RRGGBB, where the RR (red), GG (green) and BB (blue) hexadecimal
integers specify the components of the color.
In CSS, a color can be specified using hue, saturation, and lightness (HSL) in the form:
hsl(hue, saturation, lightness)
Hue is a degree on the color wheel from 0 to 360. 0 is red, 120 is green, and 240 is blue.
Saturation is a percentage value. 0% means a shade of gray, and 100% is the full [Link] 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.
Lightness is also a percentage. 0% is black, 50% is neither light or dark, 100% is [Link] 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).
For example:
<h2 style="background-color:hsl(0, 100%, 0%);">hsl(0, 100%, 0%)</h2>
<h2 style="background-color:hsl(0, 100%, 25%);">hsl(0, 100%, 25%)</h2>
<h2 style="background-color:hsl(0, 100%, 50%);">hsl(0, 100%, 50%)</h2>
6)CSS HSLA colors:HSLA color values are an extension of HSL color values with an alpha channel - which
specifies the opacity for a [Link] HSLA color value is specified with:
CSS background sets the background of elements. It is used to define the background effects for elements in
single line. There are lots of properties to design the background.
● background-color
● background-image
● background-repeat
● background-attachment
● background-position
● background (shorthand property)
1)CSS background-color
Example
body {
background-color: lightblue;
}
With CSS, a color is most often specified by:
● a valid color name - like "red"
● a HEX value - like "#ff0000"
● an RGB value - like "rgb(255,0,0)"
Opacity / Transparency
The opacity property specifies the opacity/transparency of an element. It can take a value from 0.0 - 1.0. The
lower value, the more transparent:
<head>
<style>
div {
background-color: green;
}
[Link] {
opacity: 0.1;
}
[Link] {
opacity: 0.3;
}
[Link] {
opacity: 0.6;
}
</style>
</head>
<body>
<div class="first">
<h1>opacity 0.1</h1>
</div>
<div class="second">
<h1>opacity 0.3</h1>
</div>
<div class="third">
<h1>opacity 0.6</h1>
</div>
<div>
<h1>opacity 1 (default)</h1>
</div>
</body>
</html>
2)CSS background-image
By default, the background-image property repeats an image both horizontally and [Link] the image is
repeated only horizontally (background-repeat: repeat-x;), the background will look better:
<html>
<head>
<style>
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>Here, a background image is repeated only horizontally!</p>
</body>
</html>
Showing the background image only once is also specified by the background-repeat property:
Example Show the background image only once:
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
}
4)CSS background-position
The background-position property is used to specify the position of the background [Link]
background-position are right top
Example
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
5)CSS background-attachment
The background-attachment property specifies whether the background image should scroll or be fixed (will not
scroll with the rest of the page):
Example
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;
}
6)CSS background - Shorthand property
To shorten the code, it is also possible to specify all the background properties in one single property. This is
called a shorthand property.
Instead of writing:
body {
background-color: #ffffff;
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
Example
Use the shorthand property to set the background properties in one declaration:
body {
background: #ffffff url("img_tree.png") no-repeat right top;
}
CSS Fonts
CSS fonts offer a range of options to style the text content within HTML elements. It gives you the
ability to control different aspects of fonts, including font family, font size, font weight, font style, and
more.
There are many font properties in CSS which are mentioned and briefly discussed below:
● CSS font-family Property: The font-family property specifies the font of an element.
● CSS font-style Property: If we want to give design to any type of text then we can make use of CSS
font-style property.
● CSS font-weight Property: The font-weight property of the CSS is used to set the weight or thickness
of the font being used with the HTML Text.
● CSS font-variant Property: The font-variant property is used to convert all lowercase letters into
uppercase letters.
● CSS font-size Property: The font-size property in CSS is used to set the font size of the text in an
HTML document.
● CSS font-stretch Property: The font-stretch property in CSS is used to set the text wider or narrower.
● CSS font-kerning Property: This property is used to control the usage of the Kerning Information that
has been stored in the Font
1)CSSfont-family:we use the font-family property to specify the font of a [Link] ensure maximum
compatibility between browsers/operating systems. Start with the font you want, and end with a generic family
(to let the browser pick a similar font in the generic family, if no other fonts are available). The font names
should be separated with a comma.
example: "Times New Roman", Times, serif;Arial, Helvetica, sans-serif;"Lucida Console", "Courier New",
monospace;
example:
.p1 {
font-family: "Times New Roman", Times, serif;
}
2)CSS font-style: It is used to specify the font style of an HTML element. It can be “normal, italic or oblique”.
Syntax: font-style: style name;
3)CSSfont-weight: It is used to set the boldness of the font. Its value can be “normal, bold, lighter, bolder”.
Syntax:font-weight: font weight value;
4)CSS font-variant: It is used to create the small-caps effect. It can be “normal or small-caps”.
Syntax:font-variant: font variant value;
5)CSS font-size: It is used to set the font size of an HTML element. The font-size can be set in different ways
like in “pixels, percentage, em or we can set values like small, large” etc.
Syntax:font-size: font size value;
6)The CSS Font Property(shorthand property)To shorten the code, it is also possible to specify all the
individual font properties in one [Link] font property is a shorthand property for:
● font-style
● font-variant
● font-weight
● font-size/line-height
● font-family
Example:Use font to set several font properties in one declaration:
<html>
<head>
<style>
p.a {
font: 20px Arial, sans-serif;
}
p.b {
font: italic small-caps bold 12px/30px Georgia, serif;
}
</style>
</head>
<body>
<h1>The font Property</h1>
<p class="a">This is a paragraph. The font size is set to 20 pixels, and the font family is Arial.</p>
<p class="b">This is a paragraph. The font is set to italic, small-caps and bold, the font size is set to 12
pixels, the line height is set to 30 pixels, and the font family is Georgia.</p>
</body>
</html>
color The color property can be applied to set the color in your text.
direction The direction property can be applied to set the direction of your text.
letter-spacing The letter-spacing property can be applied to add or subtract space between letters,
which will form a word.
text-decoration The text-decoration property can be applied for underlining, overlining, and
strikethrough text.
text-shadow The text-shadow property can be applied for setting the text-shadow on your text.
text-transform The text-transform property can be applied to capitalize text or convert text to
uppercase or lowercase letters.
word-spacing The word-spacing property can be applied to add or subtract space between different
words in your sentence.
text-indent The text-indent property can be applied to indent the text of your paragraph.
text-align The text-align property can be applied to align text in your document.
white-space The white-space property can be applied to control flow, as well as to the formatting of
your text.
Let us discuss these in detail and see how to implement them and how it looks in the browser.
Example:
Copy Codep {
color: red;
}
body {
color: aqua;
}
CSS Text Alignment
This property is implemented to specify a horizontal alignment in your text. Text alignment can be of these four values.
1. left
2. right
3. center or
4. justify
Example:
Copy Codep {
text-align:left;
}
body {
text-align: center;
}
h2 {
text-align: right;
}
It is to be noted that when the text-align property is assigned with a justify value, the line is stretched to fit the page where
the right and the left margins become straight.
Example:
Copy Codea {
text-decoration: none;
}
Other code snippets are:
Example:
Copy Codeh2 {
text-decoration: line-through;
}
p{
text-decoration: overline;
}
body {
text-decoration: underline;
}
CSS Text Transformation
This CSS property is implemented for specifying the uppercase as well as the lowercase letters of your text. The
transformation can be either converting every text to lowercase or upper case or capitalize the starting letter of every word.
Example:
Copy [Link] {
text-transform: uppercase;
}
[Link] {
text-transform: lowercase;
}
[Link] {
text-transform: capitalize;
}
Example:
Copy Codep {
text-indent: 40px;
}
CSS Line Height
This CSS line-height property is applied to assign space between lines:
Example:
Copy [Link] {
line-height: 0.6;
}
[Link] {
line-height: 1.5;
}
CSS Letter and Word Spacing
The letter-spacing property is implemented for specifying the space involving the characters within your HTML text. And the
word-spacing property is implemented for specifying the space involving the words between your texts.
Example:
Copy Codeh2 {
letter-spacing: 2px;
}
h3 {
letter-spacing:3px;
}
h1 {
word-spacing: 10px;
}
CSS Text Shadow
The text-shadow property of CSS allows including shadow to your text. Here is a code snippet:
Example:
Copy Codeh2 {
text-shadow: 2px 1px gray;
}
Here, in the example above, the first position value designates the horizontal shadow (2px), and the second parameter
value shows the vertical shadow (1px). The last value designates the color of your shadow. Note that space separates each
of these values.
CSS Lists:
The List in CSS specifies the listing of the contents or items in a particular manner i.e., it can either be
organized orderly or unorder way, which helps to make a clean webpage. It can be used to arrange the huge with
a variety of content as they are flexible and easy to manage. The default style for the list is borderless.
HTML Lists and CSS List Properties
● unordered lists (<ul>) - the list items are marked with bullets
● ordered lists (<ol>) - the list items are marked with numbers or letters
Property Description
ul.b {
list-style-type: square;
}
ol.c {
list-style-type: upper-roman;
}
ol.d {
list-style-type: lower-alpha;
}
Example
ul {
list-style-image: url('[Link]');
}
3)Position The List Item Markers
The list-style-position property specifies the position of the list-item markers (bullet points).
"list-style-position: outside;" means that the bullet points will be outside the list item. The start of each line of a
list item will be aligned vertically. This is default:
"list-style-position: inside;" means that the bullet points will be inside the list item. As it is part of the list item,
it will be part of the text and push the text at the start:
Example
ul.a {
list-style-position: outside;
}
ul.b {
list-style-position: inside;
}
4)List - Shorthand property
The list-style property is a shorthand property. It is used to set all the list properties in one declaration:
Example
ul {
list-style: square inside url("[Link]");
}
5)Styling List WithColors
We can also style lists with colors, to make them look a little more interesting.
Anything added to the <ol> or <ul> tag, affects the entire list, while properties added to the <li> tag will affect
the individual list items:
Example
ol {
background: #ff9999;
padding: 20px;
}
ul {
background: #3399ff;
padding: 20px;
}
ol li {
background: #ffe5e5;
color: darkred;
padding: 5px;
margin-left: 35px;
}
ul li {
background: #cce5ff;
color: darkblue;
margin: 5px;
}
CSS Tables:
A table in CSS is used to apply the various styling properties to the HTML Table elements to arrange the data in
rows and columns, or possibly in a more complex structure in a properly organized manner. Tables are widely
used in communication, research, and data analysis. The table-layout property in CSS can be utilized to display
the layout of the table. This property is basically used to sets the algorithm that is used to layout <table>cells,
rows, and columns.
1)Table Borders:To specify table borders in CSS, use the border property.
The example below specifies a solid border for <table>, <th>, and <td> elements:
Example
table, th, td {
border: 1px solid;
}
The border-collapse property sets whether the table borders should be collapsed into a single border:
Example
table {
border-collapse: collapse;
}
3)CSS Table Size
Table Width and Height
The width and height of a table are defined by the width and height properties.
Example
table {
width: 100%;
}
th {
height: 70px;
}
4)CSS Table Alignment
Horizontal Alignment
The text-align property sets the horizontal alignment (like left, right, or center) of the content in <th> or <td>.
By default, the content of <th> elements are center-aligned and the content of <td> elements are left-aligned.
Example
td {
text-align: center;
}
Vertical Alignment
The vertical-align property sets the vertical alignment (like top, bottom, or middle) of the content in <th> or
<td>.
By default, the vertical alignment of the content in a table is middle (for both <th> and <td> elements).
To control the space between the border and the content in a table, use the padding property on <td> and <th>
elements:
Example
th, td {
padding: 15px;
text-align: left;
}
b)Hoverable Table
Use the :hover selector on <tr> to highlight table rows on mouse over:
Example
tr:hover {background-color: coral;}
c)Striped Tables
For zebra-striped tables, use the nth-child() selector and add a background-color to all even (or odd) table rows:
Example
tr:nth-child(even) {background-color: #f2f2f2;}
d)Table Color
The example below specifies the background color and text color of <th> elements:
Example
th {
background-color: #04AA6D;
color: white;
}
***CSS Box Modeling
All HTML elements can be considered as [Link] CSS, the term "box model" is used when talking about
design and [Link] CSS box model is essentially a box that wraps around every HTML element. It consists
of: content, padding, borders and margins. The image below illustrates the box model:
● Content - The content of the box, where text and images appear
● Padding - Clears an area around the content. The padding is transparent
● Border - A border that goes around the padding and content
● Margin - Clears an area outside the border. The margin is transparent
The box model allows us to add a border around elements, and to define space between elements.
<html>
<head>
<style>
div {
background-color: lightgrey;
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
</style>
</head>
<body>
<h2>Demonstrating the Box Model</h2>
<p>The CSS box model is essentially a box that wraps around every HTML element. It consists of: borders,
padding, margins, and the actual content.</p>
<div>This text is the content of the box.</div>
</body>
</html>
Display Positioning
1)CSS Layout :The display Property
The display property is used to specify how an element is shown on a web [Link] HTML element has a
default display value, depending on what type of element it is. The default display value for most elements
is block or [Link] display property is used to change the default display behavior of HTML elements.
Example:
body {
display: inline;
}
p{
display: inherit;
}
Block-level Elements
A block-level element ALWAYS starts on a new line and takes up the full width available (stretches out to the
left and right as far as it can).
● <div>
● <h1> - <h6>
● <p>
● <form>
● <header>
● <footer>
● <section>
Inline Elements:An inline element DOES NOT start on a new line and only takes up as much width as
necessary.
● <span>
● <a>
● <img>
2)CSS Layout - The position Property:The position property specifies the type of positioning method used
for an element.
● static
● relative
● fixed
● absolute
● sticky
Elements are then positioned using the top, bottom, left, and right properties. However, these properties will not
work unless the position property is set first. They also work differently depending on the position value.
1)position: static;
HTML elements are positioned static by [Link] positioned elements are not affected by the top, bottom,
left, and right [Link] element with position: static; is not positioned in any special way; it is always
positioned according to the normal flow of the page:
2)position: relative;An element with position: relative; is positioned relative to its normal [Link] the
top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from
its normal position. Other content will not be adjusted to fit into any gap left by the element.
Example
[Link] {
position: relative;
left: 30px;
border: 3px solid #73AD21;
}
3)position: fixed;An element with position: fixed; is positioned relative to the viewport, which means it always
stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to
position the element.
A fixed element does not leave a gap in the page where it would normally have been located.
Notice the fixed element in the lower-right corner of the page. Here is the CSS that is used:
Example
[Link] {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
border: 3px solid #73AD21;
}
This <div> element has position: fixed;
4)position: absolute;An element with position: absolute; is positioned relative to the nearest positioned
ancestor (instead of positioned relative to the viewport, like fixed).However; if an absolute positioned element
has no positioned ancestors, it uses the document body, and moves along with page scrolling.
Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.
Here is a simple example:
5)position: sticky;An element with position: sticky; is positioned based on the user's scroll position.
A sticky element toggles between relative and fixed, depending on the scroll position. It is positioned relative
until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).
In this example, the sticky element sticks to the top of the page (top: 0), when you reach its scroll position.
Example
[Link] {
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
background-color: green;
border: 2px solid #4CAF50;
}
6)Positioning Text In an Image
CSS gradients display smooth transitions between two or more specified [Link] defines three types of
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, ...);
The following example shows a linear gradient that starts at the top. It starts red, transitioning to yellow:
Example
#grad {
background-image: linear-gradient(red, yellow);
}
c)Direction - Diagonal
You can make a gradient diagonally by specifying both the horizontal and vertical starting positions.
The following example shows a linear gradient that starts at top left (and goes to bottom right). It starts red,
transitioning to yellow:
Example
#grad {
background-image: linear-gradient(to bottom right, red, yellow);
}
d)Using Angles
If you want more control over the direction of the gradient, you can define an angle, instead of the predefined
directions (to bottom, to top, to right, to left, to bottom right, etc.). A value of 0deg is equivalent to "to top". A
value of 90deg is equivalent to "to right". A value of 180deg is equivalent to "to bottom".
Syntax
background-image: linear-gradient(angle, color-stop1, color-stop2);
Example
#grad {
background-image: linear-gradient(180deg, red, yellow);
}
e)Using Multiple Color Stops
The following example shows a linear gradient (from top to bottom) with multiple color stops:
Example
#grad {
background-image: linear-gradient(red, yellow, green);
}
The following example shows how to create a linear gradient (from left to right) with the color of the rainbow
and some text:
Example
#grad {
background-image: linear-gradient(to right, red,orange,yellow,green,blue,indigo,violet);
}
f)Using Transparency
CSS gradients also support transparency, which can be used to create fading effects.
To add transparency, we use the rgba() function to define the color stops. The last parameter in the rgba()
function can be a value from 0 to 1, and it defines the transparency of the color: 0 indicates full transparency, 1
indicates full color (no transparency).
The following example shows a linear gradient that starts from the left. It starts fully transparent, transitioning
to full color red:
Example
#grad {
background-image: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1));
}
g)Repeating a linear-gradient
#grad {
background-image: repeating-linear-gradient(red, yellow 10%, green 20%);
}
2)CSS Radial Gradients
A radial gradient is defined by its [Link] create a radial gradient you must also define at least two color stops.
Syntax
background-image: radial-gradient(shape size at position, start-color, ..., last-color);
Example
#grad {
background-image: radial-gradient(red, yellow, green);
}
Example
#grad {
background-image: radial-gradient(red 5%, yellow 15%, green 60%);
}
c)Set Shape
The shape parameter defines the shape. It can take the value circle or ellipse. The default value is ellipse.
The following example shows a radial gradient with the shape of a circle:
Example
#grad {
background-image: radial-gradient(circle, red, yellow, green);
}
d)Use of Different Size Keywords
The size parameter defines the size of the gradient. It can take four values:
● closest-side
● farthest-side
● closest-corner
● farthest-corner
Example
#grad1 {
background-image: radial-gradient(closest-side at 60% 55%, red, yellow, black);
}
#grad2 {
background-image: radial-gradient(farthest-side at 60% 55%, red, yellow, black);
}
e)Repeating a radial-gradient
Example
A conic gradient is a gradient with color transitions rotated around a center [Link] create a conic gradient you
must define at least two colors.
Syntax
background-image: conic-gradient([from angle] [at position,] color [degree], color [degree], ...);
By default, angle is 0deg and position is [Link] no degree is specified, the colors will be spread equally
around the center point.
Example
#grad {
background-image: conic-gradient(red, yellow, green);
}
b)Conic Gradient: Three Colors and Degrees
The following example shows a conic gradient with three colors and a degree for each color:
Example
A conic gradient with three colors and a degree for each color:
#grad {
background-image: conic-gradient(red 45deg, yellow 90deg, green 210deg);
}
c)Create Pie Charts
Just add border-radius: 50% to make the conic gradient look like a pie:
Example
#grad {
background-image: conic-gradient(red, yellow, green, blue, black);
border-radius: 50%;
}
Shadows
1)CSS Shadow Effects
● text-shadow
● box-shadow
Example
h1 {
text-shadow: 2px 2px;
}
Text shadow effect!
Example
h1 {
text-shadow: 2px 2px red;
}
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:
The CSS box-shadow property is used to apply one or more shadows to an element.
In its simplest use, you only specify a horizontal and a vertical shadow. The default color of the shadow is the
current text-color.
Example
div {
box-shadow: 10px 10px;
}
Example
div {
box-shadow: 10px 10px lightblue;
}
4)Add a Blur Effect to the Shadow
The blur parameter defines the blur radius. The higher the number, the more blurred the shadow will be.
Example
div {
box-shadow: 10px 10px 5px lightblue;
}
CSS Animations
What are CSS Animations?
The following table lists the @keyframes rule and all the CSS animation properties:
Property Description
animation-fill-mode Specifies a style for the element when the animation is not
playing (before it starts, after it ends, or both)
When you specify CSS styles inside the @keyframes rule, the animation will gradually change from the current
style to the new style at certain times.
To get an animation to work, you must bind the animation to an element.
The following example binds the "example" animation to the <div> element. The animation will last for 4
seconds, and it will gradually change the background-color of the <div> element from "red" to "yellow":
Example
/* The animation code */
@keyframes example {
from {background-color: red;}
to {background-color: yellow;}
}
The animation-duration property defines how long an animation should take to complete. If
the animation-duration property is not specified, no animation will occur, because the default value is 0s (0
seconds).
In the example above we have specified when the style will change by using the keywords "from" and "to"
(which represents 0% (start) and 100% (complete))
2)Delay an Animation
The animation-iteration-count property specifies the number of times an animation should run.
The animation-direction property specifies whether an animation should be played forwards, backwards or in
alternate cycles.
The animation-direction property can have the following values:
Example
div {
animation-name: example;
animation-duration: 5s;
animation-timing-function: linear;
animation-delay: 2s;
animation-iteration-count: infinite;
animation-direction: alternate;
}
CSS 3D Transforms
CSS Transform Properties
Property Description
backface-visibility Defines whether or not an element should be visible when not facing the screen
With the CSS transform property you can use the following 3D transformation methods:
● rotateX()
● rotateY()
● rotateZ()
The rotateX() method rotates an element around its X-axis at a given degree:
Example
#myDiv {
transform: rotateX(150deg);
}
The rotateY() method rotates an element around its Y-axis at a given degree:
Example
#myDiv {
transform: rotateY(150deg);
}
CSS Transitions
CSS transitions allows you to change property values smoothly, over a given duration.
the following properties of CSS transistions :
The following table lists all the CSS transition properties:
Property Description
transition-property Specifies the name of the CSS property the transition effect
is for
Note: If the duration part is not specified, the transition will have no effect, because the default value is 0.
The following example shows a 100px * 100px red <div> element. The <div> element has also specified a
transition effect for the width property, with a duration of 2 seconds:
Example
div {
width: 100px;
height: 100px;
background: red;
transition: width 2s;
}
The transition effect will start when the specified CSS property (width) changes value.
Now, let us specify a new value for the width property when a user mouses over the <div> element:
Example
div:hover {
width: 300px;
}
Notice that when the cursor mouses out of the element, it will gradually change back to its original style.
The following example adds a transition effect for both the width and height property, with a duration of 2
seconds for the width and 4 seconds for the height:
Example
div {
transition: width 2s, height 4s;
}
b)Specify the Speed Curve of the Transition
The transition-timing-function property specifies the speed curve of the transition effect.
The transition-timing-function property can have the following values:
● ease - specifies a transition effect with a slow start, then fast, then end slowly (this is default)
● linear - specifies a transition effect with the same speed from start to end
● ease-in - specifies a transition effect with a slow start
● ease-out - specifies a transition effect with a slow end
The transition-delay property specifies a delay (in seconds) for the transition effect.
The following example has a 1 second delay before starting:
Example
div {
transition-delay: 1s;
}
d)Transition + Transformation
Example
div {
transition: width 2s, height 2s, transform 2s;
}
CSS Floats:
The float CSS property is used to position the elements to the left, right, of its container along with permitting
the text and inline elements to wrap around it. The float property defines the flow of content in the page. The
remaining elements will be part of the flow if the element is removed from the normal flow of the content. This
property is ignored by the absolutely positioned elements. It can be written in a CSS file or can be directly
specified in the style of an element.
Syntax:
float: none|left|right|initial|inherit;
Property values:
● none: This is the default value & the element does not float.
● left: Element floats on the left side of the container.
● right: Element floats on the right side of the container.
● initial Element will be set to its default value.
● inherit: Element inherits floating property of its parent property.
left: The element will be positioned to the left side of its containing block.
<html>
<body>
<div class="GFG"
style="font-size:40px;
color:#006400;
float:left;">
hello India!!
</div>
</body>
</html>