CSS Notes
CSS Notes
CSS
CSS stands for Cascading Style Sheet.
Used to give styling to the web pages which is structured by the HTML
language.
There are three ways in CSS to Style the Web pages
1. Inline CSS
2. Internal CSS
3. External CSS
1. Inline CSS: Inline CSS code is written in the opening tag of the HTML
element by using style attribute.
Syntax: <p style=" "> </p>
2. Internal CSS: Internal CSS code is written in the head section of an HTML
element using <style> tag.
Syntax: <head>
<style>
tag_name{
property: value;
}
<style>
</head>
3. External CSS: External CSS styling can be done by creating a external CSS
file with file_name.css extention and providing the link between that CSS
file to HTML file by <link> tag in the head section of HTML element.
Syntax: <head>
<link rel="stylesheet" href=" file_name.css">
</head>
SELECTOR
Selectors are used to select the particular HTML element and to style
them.
There are five types of Selectors
1. Simple Selectors
2. Combinator Selectors
3. Attribute Selectors
4. Pseudo Element Selectors.
5. Pseudo Classes Selectors.
Simple Selectors
Simple selectors style the HTML element in five ways:
1. Id Selector.
2. Class Selector.
3. Tagname.
4. Groupname.
5. Universe.
Id selectors:
Id selector targets only individual element in the HTML
document.
Prefix symbol used for id selector is # (hash)
Example: #demo{
TRAINER
color: red;
background: yellow;
}
Class Selector
Class selector targets multiple elements in the HTML document.
Prefix symbol used for id selector is . (dot)
Example: .test{
color: red;
background: yellow;
}
Tagname Selector
Tagname selector targets the HTML elements by tag name.
There is no symbol used in tagname selectors.
Example: h1{
color: magenta;
background: green;
}
h2{
color: red;
background: black;
}
h3{
color: yellow;
background: orange;
}
Grouping Selector
Groupname selector targets a group of HTML elements by
tagname separated by commas.
Example: p, div, h4{
TRAINER
color: magenta;
background: green;
}
Universal Selector
Universal Selectors targets every HMTL element.
The symbol of Universal Selector is *
There is no selector is declared in css.
Example: *{
color: yellow;
background: magenta;
}
Combinator Selector
Combinator Selectors are used to style the combination of HTML
elements.
There are four Combinator Selectors:
1. Descendent Selectors
2. Child Selectors
3. Adjacent Sibling Selectors
4. General Sibling Selectors
1. Descendent Selectors: This Selector targets both direct and indirect children
of a parent tag. The symbol is (space)
Syntax: parent_tag child_tag{property: value;}
TRAINER
2. Child Selectors: This Selector targets the only the direct children of the
parent tag. The symbol is > (greater than)
Syntax: parent_tag > child_tag{property: value;}
3. Adjacent Selectors: This Selector targets the element which is the first
sibling of the targeted HTML tag or element.
(or)
This Selector targets the element which is exactly adjacent to the targeted
HTML tag or element.
The symbol is + (plus)
Syntax: parent_tag + target_tag{property: value;}
4. General Sibling Selectors: This Selector targets all the siblings of the target
HTML tag or element.
(or)
This Selector targets all the adjacent tags of the target HTML tag or element.
The symbol is ~ (tilde)
Syntax: parent_tag ~ target_tag{property: value;}
Attribute Selector: Attribute Selectors are used to style the HTML element by
targeting the respective attributes and with their values.
Types of Attribute Selector:
1. tag_name[attribute_name]: Represents elements with an attribute name
of attribute_name.
TRAINER
First Letter: The first letter styles the very first letter of the content in the
targeted HTML element.
Example: p : : first-letter{
font-size: 30px;
color: black;
}
First Line: The first line styles the very first line of the content in the targeted
HTML element.
Example: p : : first-line{
background-color: black;
color: white;
}
Before: The before pseudo element is used to place the specific content before
the targeted HTML element.
Example: p : : before{
content: “&phone”;
background-color: black;
TRAINER
color: white;
}
After: The after pseudo element is used to place the specific content after the
targeted HTML element.
Example: p : : after{
content: “Thank You”;
color: blue;
}
Selection: The selection pseudo element is used to style the content when the
cursor is dragged on the content (when the content is selected) on the targeted
HTML element.
Also copying the text content from the user can also be disabled, by using the
property user-select: none.
Example: p : : selection{
background-color: pink;
color: magenta;
}
Marker: Marker pseudo element is used to style the lists in the HTML
document.
Marker is only used to style the list type not the content.
Declaration of selector is not mandatory here.
Background color will not work for Marker.
Example: li : : marker{
color: red;
font-size: 30px;
}
(or)
: : marker{
color: red;
font-size: 30px;
}
TRAINER
Pseudo Classes
There are three types of Pseudo Classes.
1. Dynamic Pseudo Classes.
2. User Interface Pseudo Classes.
o Each dynamic pseudo class selector is declared with the single colon :
o Link, Visited only applicable for anchor tag.
o Link is used to style the unvisited link
o Link doesn’t style the already visited link.
o Active applies the style at the time of click event.
o Hover is applicable for all the HTML elements.
o While using all the Link, Visited, Hover and Active selectors. Hover should be
declared after Link and Visited. Along with that the Active should be declared
after Hover, because to make the elements effective.
TRAINER
Example:
a : link {
color: aqua;
background-color : red;
}
a : visited {
color : green;
}
a : hover {
color : yellow;
}
a : active {
color : chocolate;
}
User Interface Pseudo Classes: The UI pseudo classes is used to style the HTML
element in the case of user input.
Enabled: The enabled pseudo class enables the properties to the user input
field with the specific style only if the field is available to take user input.
Example: input : enabled{
color: blueviolet;
background-color: aqua;
}
Disabled: The disabled pseudo class styles with the specific properties to the
user for which the input type is disabled.
Example: input : disabled{
color:white;
background-color: gray;
}
Checked: The checked pseudo class styles with the outline property to radio
and checkbox.
TRAINER
BOX Model
Box Model is essentially a box that wraps around every HTML
element. Box Model is used to design and layout.
The CSS Box Model consists of Margin, Border, Padding and the actual Content.
Content: It is the content of the HTML element.
Padding: It is the space after the content.
Border: It is the Border of the HTML element.
Margin: It is the space after the Border of an HTML element.
Padding and Margin allow up to four values.
Text Properties
text-align- Horizontal alignment of the text (here we have right, left, center,
justify).
Example- text-align: center;
background-position: left;
background-position: right;
background-position: 10% 40%;
background-position: 50px 100px;
CSS GRADIENTS
CSS defines three types of gradients:
Linear Gradients
background-image: linear-gradient(direction, color-stop1, color-stop2, ...);
direction: to top, to bottom, to left, to right,
and in degree:
0deg ->to top
90deg ->to right
180deg ->to bottom
270deg ->to left
Repeating a linear-gradient
The repeating-linear-gradient()
background-image: repeating-linear-gradient(red, yellow 10%, green 20%);
Radial Gradient
background-image: radial-gradient(shape size at position, start-color, ..., last-
color);
background-image: radial-gradient(circle, red, yellow, green);
shape:circle, ellipse
The size parameter defines the size of the gradient. It can take four values:
closest-side
farthest-side
closest-corner
farthest-corner
Repeating a radial-gradient
The repeating-radial-gradient() function is used to repeat radial gradients:
background-image: repeating-radial-gradient(red, yellow 10%, green 15%);
TRAINER
The [from angle] specifies an angle that the entire conic gradient is rotated by.
The following example shows a conic gradient with a from angle of 90deg:
background-image: conic-gradient(from 90deg, red, yellow, green);
background-image: conic-gradient(at 60% 45%, red, yellow, green);
The CSS height and width properties are used to set the height and width of
an element.
The height and width properties do not include padding, borders, or margins.
It sets the height/width of the area inside the padding, border, and margin of
the element.
The height and width properties may have the following values:
auto - This is default. The browser calculates the height and width
length - Defines the height/width in px, cm, etc.
% - Defines the height/width in percent of the containing block
initial - Sets the height/width to its default value
inherit - The height/width will be inherited from its parent value
Height
TRAINER
Width
Max-height
Max-width
Min-height
Min-width
Max-height
If the content is smaller than the maximum height, the max-height property
has no effect.
Note: This prevents the value of the height property from becoming larger
than max-height. The value of the max-height property overrides the height
property.
Max-width
If the content is larger than the maximum width, it will automatically change
the height of the element.
If the content is smaller than the maximum width, the max-width property
has no effect.
Note: This prevents the value of the width property from becoming larger
than max-width. The value of the max-width property overrides the width
property.
When the width exceeds the view port of the browser the horizontal scroll
bar is added.
Min-height
If the content is smaller than the minimum height, the minimum height will
be applied.
If the content is larger than the minimum height, the min-height property
has no effect.
Note: This prevents the value of the height property from becoming smaller
than min-height.
Min-width
If the content is smaller than the minimum width, the minimum width will be
applied.
If the content is larger than the minimum width, the min-width property has
no effect.
Note: This prevents the value of the width property from becoming smaller
than min-width.
Transition Properties
Transform Properties
CSS Animation
CSS Animation is used to animate HTML elements from one style to another style.
Without java script we can give 5% and 10% of functionality to the web pages through CSS
animation.
animation-duration: Specifies the time in seconds to perform the animation only till
that specified period of time.
0%{ property:value;}
25%{ property:value;}
50%{ property:value;}
100%{ property:value;}
}
TRAINER
This specifies that at 0% at the given animation-duration the particular task should be
performed, at 25% at the given animation-duration the particular task should be performed,
so on.
The Flexible Box Layout Module, makes it easier to design flexible responsive layout
structure without using float or positioning.
A Flexible Layout must have a parent element with the display property set to flex.
Ex: div {
display: flex;
}
flex-direction
flex-wrap
flex-flow
justify-content
align-items
align-content