CSS notes
CSS notes
CSS (Cascading Style Sheets) is a language used to style and design the look of a
webpage. It controls things like colors, fonts, layout, and spacing, making a webpage look good
and organized.
HÃ¥kon
Wium Lie
What is CSS
Cascading means one on top of other. We can design one element only with different ways
and all those styling will be applied on that element.
All that styling will be applied in the form of layers one after other, and if we provide same
property through different ways, all will be applied and the one with the highest priority will
be visible to the user.
Selector {
propertyName1 :value;
propertyName1 :value;
.
.
.
propertyName1 :value;
1. Inline CSS
2. Internal CSS
3. External CSS
1. Inline CSS :
It is a way of applying the CSS in the same line. By using style attribute .
<html>
<head>
<title>Document</title>
2. Internal CSS: <style>
h1 {
It is a way of adding the CSS in the same color: white;
html file itself. By using Style Tag. We can background-color: blue;
use style tag anywhere in the html file. }
</style>
</head>
It’s recommended to use in a head tag. <body>
<h1>Let's dive into CSS</h1>
</body>
</html>
Types of CSS
@import:
Instead of just creating a single css file, we can create multiple files for
css for different different component and like those css files together
using @import rule.
The @import rule also supports media queries, so you can allow the import to
be media-dependent. The @import rule must be at the top of the document
To apply the css Properties, we can target the html elements in many ways.
They are:
1. Simple Selector
2. Combinator Selector
3. Pseudo Class Selector
4. Pseudo Element Selector
5. Attribute Selector
Simple selectors
1. Simple Selectors:
To target the element based on classes / id’s / tagname we have basic selectors.
They are:
1. TagName
2. Grouping
3. Universal
4. Id Name
5. Class Name
● TagName:
○ To target the element based on tagname itself we have to use tagName
selector.
○ The symbol was the tagname itself.
● Grouping:
○ To target multiple elements at a time we have to use a grouping selector.
○ Whenever we need to pass similar properties for multiple elements we can use a grouping selector.
○ The symbol used to combine all elements is comma (,)
Simple selector
● Universal:
○ It will target all the elements in the document including body tag too.
○ The symbol used is asterisk (*).
● Id Name:
○ To target the elements uniquely we have to use an id name.
○ In CSS id name can be duplicated also there is no problem,
○ But once we moved to js it will cause problem.
○ Repetition of id name will not work. So it is highly recommended not to use it from now on.
○ The Symbol used is hash (#).
● Class Name:
○ To target the specific elements on the basis of class name we have to use class name.
○ Class attribute allows multiple identifier names in the same attribute.
○ Class names can be repetitive also.
○ The symbol used is dot (.).
Selector specificity
We can provide css to that same element, through multiple ways like inline css, or in
external/internal using id, class, tagname or universal. So which one will be visible to the
user, if provide css to an element using all the possible ways.
If we provide different properties all will be applied and will be visible, but if we provide
same css properties.
The highest preference is given to the inline css, as it applied direct in the element, after
that id is given the highest preference as it is used to uniquely identify an element, then
we have class which is used to group multiple elements together than we have tagname
and at the last we have universal which applies to all.
2. Combinator Selectors:
It is a combination of 2 simple selectors. Based on the relation b/w 2 elements the css will target the
elements.
They are:
1.Descendent Selector
2. Direct Child Selector
3. Adjacent Sibling Selector
4. General Sibling Selector
● Descendent Selector :
○ It will target all the children, grandchildren , great grandchildren and so on.
○ The symbol used is space ( ).
Absolute Units:
These units are static, It will not depend on any other values.
Absolute Units are:
1. mm
2. cm
3. in 1in 16px
4. px
CSS Units
Relative Units:
1. color
2. text-align
3. text-transform
4. text-decoration
5. text-indent
6. text-wrap
7. letter-spacing
8. word-spacing
9. line-height
10. text-shadow
Text Properties
color: It will change the color of the text.
Syntax:
color : value;
Values = Any Color.
Syntax:
text-decoration: text-decoration-line text-decoration-style text-decoration-color
Values=
text-decoration-line ⇒ underline , overline , line-through
text-decoration-style ⇒ solid , dashed , dotted , double , wavy.
text-decoration-color ⇒ any color.
Note: out of 3 properties text-decoration-line is mandatory.
Text Properties
text-indent: It will provide the space at the initial position of the content.
Syntax:
text-indent : value; Values = any number.
text-wrap: It will indicate whether to wrap the content into the next
line or not.
Syntax: text-wrap : value;
Values = wrap , nowrap.
font-style: It will specify the style of text like it should be italic or normal.
Syntax: font-style : value;
Values = normal , italic / oblique
Font Properties
RGBA: Similar to rgb, the fourth values indicates alpha which is nothing but opacity of color.
Alpha ranges from 0.0-1.0
1. border:
It is a shorthand property of border-width , border-style and border-color.
Syntax: border: border-width border-style border-color .
2. border-width:
It will specify the width of the border.
Syntax: border-width: any number;
3. border-style:
It will specify the style of the border.
Syntax: border-style: Values;
Values: solid , dashed , dotted , double , groove , ridge , none.
Border Properties
4. border-color:
It will specify the color of the border.
Syntax: border-style: any color;
5. border-top/bottom/left/right:
If we need to specify the border only at one side we can use border - top / border -
bottom / border - left / border-right.
6. border-collapse:
This is a property to apply effects on tables. It will decide whether the table cells have to separate
or not. Syntax: border-collapse:
values; Values: separate , collapse.
7. border-spacing:
It is an alternative to the cellspacing attribute of a table tag.
It will specify the space b/w cells in a table.
Syntax: border-spacing : any number;
Border radius
The border-radius property defines the radius of the element's corners , this
property allows you to add rounded corners to elements!
border-top-left-radius: “value”;
border-top-right-radius: “value”;
border-bottom-right-radius: “value”;
border-bottom-left-radius: “value” ;
1. Linear Gradient
A linear gradient transitions between colors along a straight line (horizontal, vertical, or diagonal)
2. Radial Gradient
A radial gradient transitions between colors radiating out from a central point.
3. Conic Gradient
A conic gradient is a gradient with color transitions rotated around a center point.
A conic gradient is a type of gradient where colors are blended in a circular or pie-like pattern
around a central point, rather than in a straight line or radiating out from the center.
background-image: conic-gradient([from angle] [at position,] color [degree], color [degree], ...);
Just add border-radius: 50% to make the conic gradient look like a pie:
2. padding:
It specifies the space between border and content of an element
3. border:
A line that surrounds the padding and content. Borders can have different thicknesses, styles, and
colors.
4. margin:
It specifies the space around the element. Helps to change position of element
margin is a shorthand property of margin-top , margin-bottom , margin-left , margin-right
Width Calculation:
Total width of element = margin-left + border-left + padding-left + content-width +
padding-right + border-right + margin-right
Box Model
Box-Sizing:
It will specify the size of an element. It will accept values like cotent-box, border-box
The default value is content-box
Border-box will fix the height and width, so padding will not affect the size of an element
Note - To remove the default margin and padding of each element we have to use
margin:0; padding:0;
So we have to use
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
Visibility
The visibility property specifies whether or not an element
is visible.
They are:
1.Static
2. Relative
3. Absolute
4. Fixed
5. Sticky
2. Relative:
a. It will change the position of an element with respect to the original position.
b. It will leave an empty space. It doesn’t allow other elements to occupy its place.
3. Absolute:
It will change the position of an element with respect to the parent.
b. By default the parent is a viewport, if we need to change we have to pass position:relative
for parent element.
c. It will not leave any empty space. It allows other elements to occupy its place.
d. If we provide position:absolute for all the elements , all elements will float on each other.
e. To specify which element has to float top or bottom , we have to use the z-index property..
Positions
4. Fixed:
a. It will fix the position of an element.
b. To fix the position we have to mention the helping properties.
5. Sticky:
A .Initially these elements will scroll until it reaches to some point.
b. Then it will stick at that position.
Z-index
The z-index property specifies the stack order of an element.
#myid {
background-color: blue;
}
.myclass {
background-color: gray;
}
p {
background-color: red !
important;
}
Display property
Display
Display property allows to change the behavior of an element.
It accepts the values like
● None
● Inline
● Block
● Inline-block
● Flex
● Grid
Display property
None:
● This will not display anything in the browser.
● As well as it will not occupy any space for the element.
Inline:
● It will convert the element into inline-level.
● So then the element will occupy only content height and width.
● We cannot assign height & width properties.
Inline-Block:
● It will convert the elements into inline-block level.
● So then it will have features of both inline & block level.
● Because of inline → It will display in the same line.
● Because of block → we can assign height & width properties.
Display property
Flex:
● It will layouts the webpages in one dimensional
format.
Grid:
● It will layouts the webpages in two dimensional
format
FLEX
Flex:
• It will layouts the webpages in one dimensional
format.
• To work with flex boxes, We need a flex container
and all the direct children will become the flex-
items. (ONLY DIRECT CHILDREN).
FLEX
Flex Container Properties:
1. Display : flex
2. justify-content
3. align-items
4. flex-direction
5. flex-wrap
6. align-content
7. column-gap
8. row-gap
9. gap
FLEX
1. display: flex;
This is the main property we must use to work with
flex boxes. By assigning this property we are able to
use all flex properties.
2. Justify-content
It will align the flex-items on the main axis.
Syntax:
justify-content: values;
Values = start , center , end , space-between ,
space-evenly , space-around.
The default value is start.
FLEX
2. justify-content: values;
FLEX
3. align-Items: It will align the flex-items on the cross
axis.
Syntax:
align-items: values;
Values = start , center , end , stretch ,
baseline.
The default value is start.
FLEX
3. align-items: values;
FLEX
4. flex-direction: It will specify the direction of flex-
items. Whether to display in row format or column
format. It will decide the main axis.
Syntax:
flex-direction: values;
Values = row , column , row-reverse ,
column-reverse.
The default value is row.
FLEX
4. flex-direction: values;
FLEX
5. flex-wrap: It will specify whether to wrap the flex-
items into the next line or not.
Syntax:
flex-wrap: values;
Values = nowrap , wrap , wrap-reverse.
The default value is nowrap.
FLEX
5. flex-wrap: values;
FLEX
6. align-content: It will specify the position of
wrapped flex items.
Syntax:
align-content: values;
Values = start , center , end , space-between ,
space-around , stretch
FLEX
6. align-content: values;
FLEX
7. gap:
It is a shorthand property of row-gap & column-gap. It
will specify the space between the flex items.
Ex: row-gap:20px & column-gap:20px;
FLEX
gap : 20px;
FLEX
Flex items Properties:
1. order
2. align-self
3. flex-grow
4. flex-basis
5. flex-shrink
6. flex
FLEX
1. Order : It will arrange the flex-items in an order.
Ex:
FLEX
2. align-self:
By using this property, we can align the flex-items
individually.
It will override the default alignment which is specified
by align-items.
It will accept all the values which can be acceptable
by align-items property.
The only difference is
i. align-items for container for all flex-items.
ii. align-self for items for individual flex-item.
FLEX
2. align-self: value;
Values :
FLEX
3. Flex-grow:
2. grid-template-columns
It will specify no.of columns along with the width of each column.
As the width of the cell is defined here, It is not required to
mention again for the grid item.
3. grid-template-rows
It will specify no.of rows along with the height of each row.
As the height of the cell is defined here, It is not required to
mention again for the grid item.
GRID
/* CSS Code */
section {
display: grid;
grid-template-columns: 200px 200px 200px 200px;
/* It will consider as 4 columns each with 200px width */
grid-template-rows: 150px 50px 150px 30px 100px;
/* It will consider as 5 rows with widths => 1st - 150px , 2nd - 50px , 3rd - 150px , 4th -
30px , 5th - 100px*/
}
GRID
We can pass the values for grid-template-rows and grid-
template-columns in multiple ways.
grid-template-columns: 200px 100px 150px 100px 80px 20px;
.item1 {
grid-column-start: 1;
grid-column-end: 5;
======= or =======
grid-column: 1/5;
}
GRID
3. grid-area :
It is the shorthand property of grid-row-start , grid-column-start , grid-
row-end and grid-column-end.
Syntax: grid-row-start/grid-column-start/grid-row-end/grid-column-end.
Ex: grid-column: 1/1/3/5;
.item1 {
grid-area: 1/1/3/5;
}
Transform
Transform
Transform will change the state of an element like changing position ,
applying rotations , altering sizes, etc…
======== or ===========
It will transform the elements original behaviour with new behaviours like
changing positions , applying rotations , altering sizes , etc..
Syntax: transform: value;
Values:
translateX() translateY() translate()
scaleX() scaleY() scale()
skewX() skexY() skew()
rotateX() rotateY() rotate()
Transform
Translate():
It will change the position of an element according to x-axis and y-axis.
It is a shorthand property of
Ex:
transform: scaleX(2); // It will increase the width into double.
transform: scaleY(0.5); // It will decrease the height into half.
transform: scale(2,0.5); // It will increase the width & decrease the
height.
Transform
Skew():
It will slant the elements in x and y axises.
Ex: transform: skewX(40deg);
transform: skewY(20deg);
transform: skew(40deg , 20deg);
Transition-property
Transition-duration
Transition-delay
Transition-timing-function
Transition
Transition-property:
It will specify which properties have to be targeted. (In hover
we will have many properties , for which property transition have to
apply).
If we are not targeted specifically, It will target all.
Transition-duration:
It specifies how long the transition has to occur.
The default is 0 secs
Transition-delay:
It specifies when the transition has to apply.
Basically, it provides a delay time. So transition will start after
that time.
The default is 0 secs.
Transition
Transition-timing-function:
It specifies the speed format of transition.
We can assign the values like
Animation-name
Animation-duration
Animation-delay
Animation-timing-function
Animation-direction
Animation-iteration-count
Animation
As we said, animation will change from one style to another and so on.
To store all these styles we have to use @keyframes rule.
Syntax:
Animation
Animation-name:
It specifies the animation-name which is nothing the @keyframes rule
identifier name.
We can create @keyframes rules with different styles.
Which animation you want to use, you can specify that identifier name.
Animation-duration:
It specifies how the animation has to apply
Animation-delay:
It specifies when the animation has to start.
Animation
Animation-timing-function:
It specifies the speed format of animation.
We can assign the values like
MediaOperator:
This will combine multiple media features.
It will accept the values like and , or , not.
And → it will check if all the conditions are true or not.
Or → it will check if any of the conditions are true or not
CSS Responsiveness
MediaFeatures:
It will provide some breakpoints based on screen sizes.
It will accept the values like:
1. min-width
2. max-width
3. width
4. min-height
5. max-height
6. height
7. orientation: landscape / portrait
CSS Responsiveness
Thank you