CSS pdf
CSS pdf
*** Overview ::
Syntax : selector {
property : value ;
}
. which each declaration includes a CSS property "name and a value", separated by
a "colon".
Example : p {
color : blue ;
font-family : monospace ;
}
In the above example, styling is done to the p tag, which is a paragraph tag and
acts as a selector. Styling is done using two properties. One is the text colour as
property, and blue is the value for that, same as font-family as property and
monospace.
Below is an example of how an HTML document looks before adding CSS styling to it.
Browser after adding the CSS styling to the above HTML code.
CSS: h1 {
font-family: monospace;
h2 {
color: blue;
}
:: Sample Code ::
Sample.html ::
<!DOCTYPE html>
<html>
<head>
<title>My WEb</title>
</head>
<body>
<h1>My book</h1>
<p>My data</p>
</body>
</html>
SAMPLE1.CSS ::
h1 {
color: blue;
}
p {
background-color: aqua;
}
The browser formats the HTML document based upon the information in
the stylesheet. The browser access the stylesheets from the HTML document itself.
There are three ways to add CSS styles to your document. Each of them can contain
multiple properties:
. Inline styles
. Internal styles
. External styles
1. Inline style ::
Example : <p style = " color:blue ; font-size:40px "> Inline CSS </p>
2. Internal style ::
An internal or in-page stylesheet holds the CSS code for the web page.
The internal stylesheet contains the styles for that HTML written inside the HTML
document and can be used only for that document. They cannot be reused.
>> Internal CSS can be applied using <style> tag inside the <head> tag.
Example : <style>
h1 { color : blue ; }
h2 { color : red ; }
p { color : green ; }
</style>
3. External styles ::
>> To use external stylesheet, a reference is provided to file inside the <link>
element:
NOTE: The <link> element goes inside the <head> section of the HTML document. The
'styles.css' contains the CSS syntax code only.
:: Cascading ::
Cascading order priority : Inline > ( internal ≅ external ) > browser default
The internal and external CSS are treated equally by the browser. But the order of
their definition decides which one’s property will get preference.
>> If the link to external CSS is defined before the internal CSS, then properties
of internal CSS will get the preference, i.e. internal CSS > external CSS.
>> If the link to external CSS is defined after the internal CSS, then properties
of external CSS will get preference over internal CSS, i.e. external CSS > internal
CSS.
*** Overview ::
. Element selector
. Class selector
. Id selector
Specificity defines which style will be applied to the HTML element when
multiple styles are applied from different selectors. If the specificity is the
same, then the latest rule is applied.
Specificity Order : Inline > id selector > class selector > tag selector > browser
default.
NOTE: If the same property is defined inside the same type of selector, then the
property defined at the last will be used by the browser.
Example : p{
color : red ;
font-size : 20px ;
color : blue ;
}
In the above example colour of the p tag is given twice, it could be by mistake,
and the colour blue will be the final colour of that paragraph.
1. Element Selector ::
"The element selector selects all elements with the specified element
name". This will select all the elements in the HTML document. This method is not
used so commonly. So, to apply styles to only some elements, we need to use some
restrictions.
EX:
HTML : <h1> Blue Color </h1>
CSS : h1 {
color: blue;
}
2. Class Selector ::
The class selector selects multiple elements which have the same class with
a specific class attribute. To select elements with a specific class, write a
period(.) character, followed by the name of the class.
>> To use a class selector, the class attribute is used in the HTML element's
opening tag.
>> The value of the class attribute contains the name of the class. There can be
multiple classes added to the tag by giving space in between.
EX:
Output: Class 1
Class 2
Class 3
Class 4
3. Id Selector ::
>> To use the id selector, the id attribute is used in the HTML element's opening
tag. The value of the id attribute contains the name of the id. There can only be
one id in the tag. The id is unique in an HTML page. If another element is given
the same id, the styles would not be applied by the browser.
EX:
CSS : #id1 {
color: red;
}
#id2 {
color: blue;
}
#id3 {
color: green;
background-color: red;
}
Output: Text 1
Text 2
Text 3
4. Grouping selector ::
p{
color: red ;
background : black ;
}
. The universal selector (*) selects all HTML elements on the page.
Example : * {
text-align: center;
color: blue;
}
Topic - 1: Inheritance:
:: Inheritance in CSS ::
*** Overview ::
CSS rulesets cascade down the CSS hierarchy from parent selectors to
their children selectors. These CSS rulesets are inherited from their parent
selectors.
>> If the CSS property is not specified, the child element will naturally inherit a
CSS property with its value from the parent element.
Sample.html ::
<!DOCTYPE html>
<html>
<head>
<title>My WEb</title>
<link rel="stylesheet" type="text/css" href="SAMPLE1.css">
</head>
<body>
<h1>My book</h1>
<h1>Data</h1>
<p>My data</p>
<div id="Parent">
Parent Div
<div id="Child1">Child1 Div-l</div>
<div id="Child2">Child Div-ll</div>
</div>
</body>
</html>
SAMPLE.css ::
h1 {
color: blue;
}
p {
background-color: aqua;
}
#Parent {
color: red;
}
Output:
My book
Data
My data
Parent Div
Child1 Div-l
Child Div-ll
>> You can see we only specified the colour: red for the parent div but the child
div’s also got that property. This is because of CSS Inheritance.
. font-size
. font-family
. font-weight
. color
. height
. width
. border
. margin
. padding etc.
There is a way by which these properties can also be inherited. For that, we need
to use the inherit keyword.
:: Inherit keyword ::
The inherit keyword specifies that a property should inherit its value
from its parent element.
>> The inherit keyword can be used for any CSS property and on any HTML element.
EX:
CSS : #Parent {
height: 100px;
color: red;
}
#Child1 {
height: inherit;
}
Child Div-ll
>> Parent div has the properties of color:red and height:100px , the color property
was automatically inherited by the child div’s, but the height property is not
inheritable. To inherit the height property inherit keyword is used. So the height
of the Child-I div becomes 100px.
NOTE : Only direct child elements can inherit a CSS property from its parent
element using the inherit value if the CSS property is set by its parent element.
This is to ensure that the CSS property to be inherited is inheritable.
*** Overview ::
Box model describes the layout of the elements. The HTML elements are
considered as boxes.
The CSS box model is essentially a box that wraps around every HTML
element. It consists of: margins, borders, padding, and content. The image below
illustrates the box model.
MARGIN
BORDER
PADDING
CONTENT
. Content - The content of the box, where text and images appear
>> The element’s "total width" is equal to the total of the horizontal borders,
padding, and content width of the element.
>> The "total height" of the element is equal to the total of the vertical
borders, padding, and content height of the element.
EX:
HTML : <div>
Welcome to Coding Ninjas !
At Coding Ninjas, our mission is to continuously innovate
the best ways to train the next generation of developers and to transform the way
tech education is delivered.
</div>
CSS : div {
width: 300px;
border: 15px solid red;
padding: 50px;
margin: 20px;
}
By using CSS’s width and height properties, only the height and width
of the content box is set. To calculate the total size of an element, you must also
add padding, borders and margins.
:: Background ::
Margin
Border
Padding
Content
. background-image
. background-repeat
. background-size
. background-position
. background-attachment
1. background-color ::
CSS: div {
background-color : red;
}
(or)
div {
background : red;
}
2. background-image ::
>> url('URL') - specifies the URL of the image. You can specify more than one image
by separating the URLs with a comma
CSS : div{
height: 150px;
width: 200px;
background-image: url('cn.jpg');
}
>> You must be curious this was not the expected result. The image should cover the
whole area and should not repeat for that let’s move on and learn how to do that.
3. background-repeat ::
The background-repeat property is used to specify how/if a background
image will be repeated.
. repeat - This is the default value. The background image is repeated both
vertically and horizontally. The last image will be clipped if it does not fit.
. round - the image is repeated and shrink or stretch to fill the space
CSS : div{
height: 150px;
width: 200px;
background-image: url('cn.jpg');
background-repeat: no-repeat;
}
The repetition of the image is stopped. Now let’s see how to cover the image in the
whole div.
4. background-size ::
. auto - This is a default value. The image is displayed in its original size
. length - sets the width and height of the background image. The first value sets
the width, the second value sets the height
. percentage - sets the width and height of the background image in per cent. The
first value sets the width, the second value sets the height. If only one value is
given, the second is set to "auto"
. cover - resizes the background image to cover the horizontal width of the
container
. contain - resizes the background image to make sure the image is fully visible
Use the background-size : cover if the image is small and repeating itself, and
background-size : contain if the image is overflowing the area.
CSS : div{
height: 150px;
width: 200px;
background-image: url('cn.jpg');
background-repeat: no-repeat;
background-size: cover;
}
>> This was the expedited result where the image should not repeat and cover the
whole area.
5. background-position ::
>> The values this property can take are(X represents horizontal position and Y
represents vertical position):
. X Y - they both can each take value from one of the following - left, right,
top, bottom, centre. If one value is specified, the other value will be “centre.”
. Xpos Ypos - specifies the horizontal and vertical position relative to the
viewport. Units can be any of the CSS units. If you only specify one value, the
other value will be 50%.
CSS : div{
height: 150px;
width: 200px;
background-image: url('cn.jpg');
background-repeat: no-repeat;
background-position: 20% 30%;
}
Here the backgroundimage position will be displayed with the help of the X and Y
value of background-position.
6. background-attachment ::
. scroll - This is default value. The background image will scroll with the page
. fixed - The background image will not scroll with the page
. local - The background image will scroll with the element's contents
Topic - 4: Borders ::
:: Borders in CSS ::
*** Overview ::
The border property is used to set the element's border. CSS borders
allow you to specify the "style, width, and colour" of an element's border.
>> The border-width property specifies the width of the four borders.
>> The width can be set as an absolute or relative size or using one of the three
predefined values: "thin, medium, or thick".
2. Border Style ::
>> The values for border-style are: "dotted, dashed, solid, double, groove, ridge,
inset,
outset, none, hidden".
3. Border Color ::
>> The border-color property specifies the color of the four borders.
>> The value of the property is the same as that of the color property. But now
you can provide different colours to different border sides.
Using the border property, we can provide width, style and color to
all the borders separately, but still, we have to give some value to all the sides
of the border. CSS border also have property to give border value individually to
each of the border sides. The border property for the sides are:
. border-top
. border-right
. border-bottom
. border-left
This is further broken to provide style, width and color separately to the border
sides. Some of them are: "border-top-style, border-right-width, border-left-color",
etc.
4. border Radius ::
*** Overview ::
:: writing modes ::
:: Text align ::
CSS: #center {
text-align: center;
}
#left {
text-align: left;
}
#right {
text-align: right;
}
Output: center
left
right
Topic - 6: Overflow ::
*** Overview ::
. visible - This is the default value. The content overflows and is seen outside
the box
. hidden - Only the content that fits inside the box is visible, and the overflow
is clipped
. scroll - All the content is visible through a scroll bar added to the box
auto - a scroll-bar gets added if content overflows
1. visible ::
CSS: div {
background-color: lightgreen;
width: 200px;
height: 200px;
Overflow: visible;
}
2. hidden ::
In overflow: hidden the overflow content outside the box is not rendered
on the web page.
Changing CSS of the above mentioned example :
3. scroll ::
4. auto ::
The overflow : auto is similar to scroll, but it only adds the scrollbar if
needed.
** Interview Questions **
1. What is the Box model in CSS? Which CSS properties are a part of it?
Ans 1 .
A rectangle box is wrapped around every HTML element. The box
model is used to determine the height and width of the rectangular box. The CSS Box
consists of Width and height (or, in the absence of that, default values and the
content inside), padding, borders, margin.
Margin
Border
Padding
Content
>> Content: Actual Content of the box where the text or image is placed.
>> Padding: Area surrounding the content (Space between the border and content).
>> Border: Area surrounding the padding.
>> Margin: Area surrounding the border
Ans 2 .
It’s a CSS unit used to measure the height and width in percentage with
respect to the viewport. It is used mainly in responsive design techniques. "The
measure VH is equal to 1/100 of the height of the viewport". If the height of the
browser is 1000px, 1vh is equal to 10px. Similarly, if the width is 1000px, then 1
vw is equal to 10px.
Ans 4 .
The overflow property in CSS is used for specifying whether
the content has to be clipped or the scrollbars have to be added to the content
area when the content size exceeds the specified container size where the content
is enclosed. If the value of overflow is hidden, the content gets clipped post the
size of the container thereby making the content invisible.
For example,
div {
width: 150px;
height: 50px;
overflow: hidden;
}
If the content of the div is very large and exceeds the height of 50px, the content
gets clipped post 50px and the rest of the content is not made visible
Ans 5 .
The box-sizing CSS property sets how the total width and
height of an element are calculated.
>> Content-box: The default width and height values apply to the element's content
only. The padding and border are added to the outside of the box.
>> Padding-box: Width and height values apply to the element's content and its
padding. The border is added to the outside of the box. Currently, only Firefox
supports the padding-box value.
>> Border-box: Width and height values apply to the content, padding, and border
*** Overview ::
>> CSS units are used for "expressing the size" of different properties.
>> The units are expressed by a "number followed by the unit symbol". Many CSS
properties take "length" values, such as width, margin, padding, font-size, border-
width, etc.
>> Whitespace cannot appear between the number and the unit.
>> For some CSS properties, negative lengths are allowed, like margin.
1. Absolute
2. Relative
1. Absolute Units ::
. cm - centimeters
. mm - millimeters in inches (1in = 96px = 2.54cm)
. px - pixels (1px = 1/96th of 1in)
. pt - points (1pt = 1/72 of 1in)
. pc - picas (1pc = 12 pt)
. px is most commonly used.
2. Relative Units ::
. em - Relative to the font size of the element (2em means 2 times the size of the
current font)
. rem - Relative to the font size of the root element
. vw - Relative to 1% of the width of the browser window size
. vh - Relative to 1% of the height of the browser window size
. % - Relative to the parent element
*** Overview ::
Using CSS sizes can be given to the elements, divs, images etc., in many
ways, and we will cover them in this module.
1. Absolute sizing ::
Absolute sizing can be given to the images, divs, elements etc., "which
means that the size will be fixed and will not change according to screen sizes and
will not adjust to fit the content inside it".
CSS: div {
background-color: lightblue;
Width: 150px;
height: 150px;
}
2. Relative sizing ::
. Percentage
. Rem
. Min and max
. Viewport
:: Percentage ::
CSS: #parent {
background-color: lightgrey;
width: 150px;
height: 150px;
}
#child {
background-color: lightblue;
width: 70%;
height: 70%;
}
Output: Welcome to
Coding Ninjas !
:: rem ::
This is used for font sizing, relative to the root element’s font size
(generally 14px).
Height or width properties are used to set the height and width of the
element to a specific size. But its size becomes fixed with this, and this brings
problems in smaller devices. The browser then gets a scrollbar to scroll through
the entire content.
So, to overcome this problem, CSS provides a min and max property like min-height,
max-width etc. This specifies the maximum height/width that an element can have. If
the browser window's width becomes smaller than the element’s width, the element
width adjusts with the browser width.
But, a very small element is very difficult to read. So, the CSS provides another
property min-width that specifies the minimum width that the element can have.
Let’s understand it with an example, suppose you have a box with some content, and
the words of that paragraph are not fixed, so the size of the box can not be fixed,
but you want to set a minimum height. The box will always be at least this height
but will then grow taller if there is more content than the box has space for at
its minimum height
CSS: div {
background-color: lightblue;
Width: 150px;
height: 150px;
}
:: Viewport ::
Viewport means the visible area on the web browser and elements can
have a size relative to the viewport.
. vh - Viewport height
. vw - Viewport width
Sizing of the items can be done relative to the viewport height/width like height :
80vh , width : 50vw etc.
:: Color ::
The color property can be specified in 6 different ways. Each one of them provides
has some difference from the other. Although color can also be applied to
backgrounds and borders.
1. By name ::
All modern browsers support 140 different colors named in CSS. Unlike
HTML, CSS will completely ignore unknown keywords.
>> The color keywords all represent plain, solid colors, without transparency
like orangered, green, blue, lightgrey, etc.
2. Using rgb ::
RGB stands for "Red, Green and Blue". It is a color model where the
combination of red, green and blue forms a color. The intensity of each color has
values ranging from 0 to 255. This provides a very large number of colors dataset.
>> RGB value for black : rgb(0, 0, 0) and for white : rgb(255, 255, 255).
3. By hex code ::
The colors can be represented by "6 digits hexadecimal code". The codes
are made using the "3 colors(Red, Green and Blue)". First 2 digits are red, next 2
are green and last 2 are blue. "So, the syntax for hex code is: #RRGGBB".
The color can also be specified using the HSL (Hue, Saturation and Lightness)
components.
. Hue, is a degree on the color wheel from 0 to 360. 0 is red, 120 is green, and
240 is blue.
5. Using rgba ::
The alpha parameter is a number between 0.0 (transparent) and 1.0 (opaque).
6. Using hsla ::
EX: hsla(0, 100, 50%, 0.6) is a red color with 0.6 opacity
If an image is placed inside a box that is smaller or larger than the dimensions of
the image, it will either appear smaller than the box, or overflow out of the box.
Here comes the role of CSS to adjust the size.
. picture overflow
Solution 1 :
In this case we can set the size of the image to max-width : 100%
and max-height:100% of the outer div. By this the overflow image will adjust to the
size of div but the smaller image will remain in the same position and problem
sustains.
Solution 2 :
Using the object-fit property. When using object-fit the
replaced element can be sized to fit a box in a variety of ways.
>> object-fit : cover - Sizes the image down, maintaining the aspect ratio so
that it neatly fills the box.
>> object-fit : contain - image will be scaled down until it is small enough to
fit inside the box.
Picture Overflow ::
CSS: div {
height: 100px;
width: 150px;
border: 2px solid red;
}
img {
height: 100%;
width: 100%;
}
.contain {
object-fit: contain;
}
CSS: div {
height: 250px;
width: 650px;
border: 2px solid red;
}
img {
height: 100%;
width: 100%;
}
.cover {
object-fit: cover;
}
A basic HTML form looks very simple but usually, we don’t see forms on
websites that simple, they are styled which is done using the CSS.
HTML :: <form>
<label for="fname"> First name: </label><br>
<input type="text" id="fname" name="fname"> <br>
CSS: input {
border : 2px solid red;
}
The example above applies to all <input> elements. If you only want to
style a specific input type, you can use attribute selectors:
CSS : input[type=password] {
** Padded Inputs ::
** Coloured Inputs ::
** Focused Inputs ::
By default, some browsers will add a blue outline around the input
when it gets focused (clicked on). You can remove this behaviour by adding outline:
none; to the input or you can change the property on focus.
Buttons are commonly used in forms for submitting the form data.
>>> You can color the button , give borders to it , make rounded borders etc.
EX: HTML : <input type="submit" value="BUtton">
CSS : input[type=submit] {
background-color: red;
padding: 5px;
border: 2px solid black;
border-radius: 10px;
}
Overview ::
:: Text Decoration ::
CSS: a {
text-decoration: none;
}
Before : Link to Coding Ninjas -> underline
:: Background color ::
We can also specify background color for the link and by this the
link will look like it is enclosed in a coloured rectangular div.
EX: CSS: a {
background-color: lightgreen;
}
Now by using the background colour property the link can also be made to
look like a button.
:: Link as a Button ::
Modifying the above example by giving some padding to the anchor tag,
which will make the rectangle look like a button.
EX: CSS: a {
text-decoration: none;
background-color: lightgreen;
padding: 10px;
border-radius: 10px;
}
** Overview ::
Text properties define the layout and presentation of the text on the HTML page.
1. font-size ::
This defines the size of the text. The font-size value can be an
absolute or relative size, i.e., values can be applied in px, %, em, etc.
EX: CSS: h1 {
font-size: 40px;
}
h2 {
font-size: 1.875em;
Output: h1
h2
2. font-family ::
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.
CSS: p {
font-family : monospace;
}
3. font-weight ::
4. font-style ::
The font-style property specifies the style for a text. The values for
this property are: normal, italic, oblique, initial, inherit.
EX: CSS: p {
font-style : italic;
** color ::
We have already discussed applying color to text using the color property.
The color can be defined either by name, hex code, rgb, rgba, hsl, or hsla.
** text-align ::
** text-indent ::
CSS : p {
text-indent: 100px;
}
>> capitalize - turns the first letter of each word to uppercase and other to
lowercase
** text-decoration ::
____________________________________
Welcome to Coding Ninjas
** line-height ::
The font-size value can be an absolute or relative size, i.e., values can be
applied in px, %, em, etc. If no unit is specified then it is multiplied by the
element’s font size.
** letter-spacing ::
The letter-spacing property is used to specify the space between the
characters in a text. This is used to increase or decrease the space between the
characters only.
EX: CSS: p {
letter-spacing: 10px;
Output: C o d i n g N i n j a s
** word-spacing ::
EX: CSS: p {
word-spacing: 10px;
}
** text-shadow ::
>> The value contains 3 values, which are the position of the horizontal shadow,
the
EX: CSS : p {
text-shadow: 2px 1px red;
}
2. Why background and color are the separate properties if they should
always be set together?
Ans 2.
>> It enhances the legibility of style sheets. The background property is a complex
property in CSS, and if it is combined with color, the complexity will further
increase.
>> Color is an inherited property while the background is not. So this can make
confusion further.
Ans 3 .
Example : <style>
img{
Opacity : 0.6 ;
}
</style>
There are some text effect properties in CSS that are listed below:
>> word-break
>> word-wrap
>> text-overflow
>> writing-mode
1. word break ::
It specifies how words should break at the end of the line. It defines the
line break rules.
. break-all : It inserts the word break between the characters in order to prevent
the word overflow.
EX: HTML : <div> Lorem ipsum is simply dummy text of the printing
and typesetting. </div>
CSS : div {
height: 150px; width: 150px;
border: 2px solid black;
word-break: break-all;
}
CSS : div {
height: 150px; width: 150px;
border: 2px solid black;
word-break: keep-all;
}
2. word wrap ::
This property is used to break the long words and shift them onto the
next line. This property is used to prevent overflow when an unbreakable string is
too long to fit in the containing box in the very same line.
EX: HTML : <div> Lorem ipsum is simply dummy text of the printing and
typesetting </div>
CSS : div {
height: 150px;
width: 150px;
border: 2px solid black;
word-wrap: break-word;
}
3. text-overflow ::
It specifies how the overflowed text will look like, which is not
visible to the user. text-overflow decides whether the text should be clipped or
show some dots (....).
EX: HTML: <p> Lorem ipsum is simply dummy text of the printing and
typesetting </p>
CSS : p {
white-space: nowrap;
width: 200px;
border: 1px solid #000000;
overflow: hidden;
text-overflow: ellipis;
}
Transform | Remarks
Method |
______________________________________________________________________
1. translate( ) ::
This method moves an element from its current position according to the
given parameters, i.e. X-axis and Y-axis.
Example : div{
transform: translate(20,50) ;
}
2. rotate( ) ::
Example : div{
transform: rotate(60) ;
}
3. scale( ) ::
Example : div{
transform: scale(2 , 4) ;
}
The size of the element becomes : twice the original width, thrice the actual
height.
Example2: div{
transform: scale(0.5 , 2) ;
}
Size of the element becomes : half the original width, twice the original height
>> scaleX( ) increases or decreases the size of an element along the X-axis, i.e.
width.
>> scaleY( ) increases or decreases the size of an element along the Y-axis,i.e.
height.
Example : scaleX(2) ;
Increases the width of element by 2 times.
scaleY(0.5);
>> skewX( ) : skews an element along the X-axis by the given angle.
>> skewY( ) : skews an element along the Y-axis by the given angle.
EX: skewX(30deg);
skewY(30deg);
6. matrix( ) ::
This method combines all the 2D transform methods and passes them into
parameters.
>> It takes six parameters : rotate, scale, move (translate), and skew elements.
CSS transitions are added to change the element gradually with the time
specified from one style to another. Similar work can also be done using
JavaScript.
Properties of transition :
CSS : div{
height: 100px;
width: 100px;
border:2px solid black ;
transition-property: height ;
transition-duration: 3s;
}
div : hover{
height: 200px;
}
CSS : div{
height: 100px;
width: 100px;
border:2px solid black ;
transition-property: height , width ;
transition-duration: 3s;
}
div : hover{
height: 200px;
width : 200px;
}
>> ease : Transition effect with a slow start, then fast, then end slowly
(Default)
>> linear : Transition effect with the same speed from start to end
>> ease-in : Transition effect with a slow start
>> ease-out : Transition effect with a slow end
>> ease-in-out : Transition effect with a slow start and end
>> cubic-bezier(n,n,n,n) : Defines own values in a cubic-bezier function
CSS : div{
transition-property : height , width ;
transition-duration : 3s ;
transition-timing-function: ease-in-out;
}
Transition shorthand
All the transition features could also be written in a single CSS line.
Toipc - 4: Animation ::
1. @keyframes :::
Animation is created with a specific name and then its functionality is
described using the @keyframes rule. We need to mention the animation from and to.
Let’s take an example where a square div changes its shape to a circle.
@keyframes sample{
from{border-radius: 0;}
to{border-radius: 50%;}
}
Let’s see that with an example where the animation duration is 3 seconds and at
each second colour of the div changes.
CSS : div {
width: 100px;
height: 100px;
background-color: red;
animation-name: sample;
animation-duration: 3s;
}
@keyframes sample {
0% {background-color: red;}
50% {background-color: blue;}
100% {background-color: green;}
}
3. animation-iteration-count :::
This property specifies the number of times animation should play.
In the below example the square changes to circle 3 times just like a loop.
@keyframes sample{
from{border-radius: 0;}
to{border-radius: 50%;}
}
In the below example technically square should have been converted into a circle
but after applying the property “animation-direction : reverse”, the circle is
converted to a square.
CSS : div{
height: 100px;
width: 100px;
border:2px solid black ;
animation-name: sample ;
animation-duration: 5s ;
animation-direction : reverse ;
}
@keyframes sample{
from{border-radius: 0;}
to{border-radius: 50%;}
}
4. Animation timing function :::
This property specifies the speed curve of the animation. It is the same as the
transition timing function.
. ease : Animation with a slow start, then fast, then end slowly (Default)
*** Overview ::
Using the flex in a container gives the ability to alter its items width/height and
order as well to best fit in the space available. A flex container expands items to
fill available free space or shrinks them to prevent overflow.
The flex is a value for the display property. It has to be provided in the
container for the flex to work. Only if it is defined inside the container, flex
properties will work. Flex properties are defined on the child elements.
The Flexible Box Layout Module makes designing a flexible, responsive layout
structure easier without using float or positioning.
display: flex ;
display: inline-flex; for the inline variation.
In the below example, a parent div contains 3 divs with display property as flex.
CSS: .parent {
display: flex;
background-color: lightgreen;
}
Output: Div 1
Div 2
Div 3
1. row ::
. The row value stacks the flex items "horizontally, from left to right".
. For forward direction, add the flex-direction: row ; to the flex-container class.
It will not change anything as this is the default value and the layout will still
look the same.
2. column ::
. The column value stacks the flex items "vertically, from top to bottom".
Output: Div 1
Div 2
Div 3
3. row-reverse ::
. The row-reverse value stacks the flex items "horizontally, from right to left".
For backward direction, add the flex-direction: row-reverse ; to the flex container
class.
4. column-reverse ::
. The column-reverse value stacks the flex items "vertically, from bottom to top".
Output: Div 3
Div 2
Div 1
** ORDER ::
The order property is used to specify the order of the flex items in
the flex container. This property value must be a whole number. By default, the
number is 0(zero).
The higher the number the latter would the flex item appear in the flex container.
>> First, the items not having order property or order : 0 ; property is displayed
in sequence in which they appear in the source order.
>> Then the items are displayed in ascending order of the value of the order
property. The items having the same order value are displayed in the sequence in
which they appear in the source order.
CSS: .parent {
display: flex;
background-color: lightgreen;
}
The flex-wrap property specifies whether the flex items should wrap or
not.
By default, flex items "try to fit into one line". This property allows you to
change that and allow the items to flow into multiple lines as needed with this
property.
. nowrap (Default)
. wrap
. wrap-reverse
In the above example width of the container is 250 px, and the width of 6 divs is
70px each that sums up to 420px which is greater than the width of the container.
And as per the property of flex, it is adjusted in a single line now let’s see the
results for the wrap.
<div>
CSS: .flex-container {
display: flex;
width: 250px;
height:200px;
background-color:
lightgreen;
}
.flex-container div {
width: 70px;
height:70px;
background-color: #7ff9ae;
border : 1px solid black;
}
The nowrap value specifies that the flex items will not wrap, i.e. it
will make all the flex-items appear in a single line, no matter how many items are
present inside the flex-container. It is a default value but you can use flex-wrap:
nowrap ; as well in the flex-container class.
2. wrap ::
The wrap value specifies that the flex items will wrap if necessary,
i.e. if the total flex items are width is larger than the width of the container,
the items will arrange themselves in the next row.
The vertical spacing of the items will be automatically decided by this property.
EX: CSS {
flex-wrap : wrap;
}
3. wrap-reverse ::
The wrap-reverse value specifies that the flex items will wrap in
reverse order if necessary, i.e. if the total flex items are width is larger than
the width of the container, the items will arrange themselves in the next column
but the items will start from the bottom of the container. Use flex-wrap: wrap-
reverse ; in the flex-container class to wrap the items.
0(zero) means that the flex items' width would not change.
>> Here, only the div2 and div3 grows, when the width of the container is
increased. Also, div3 increases twice as much as div2. Now, width of div1 is 70px,
div2 is 120px and div3 is 170px.
The flex-shrink property specifies the ratio with which a "flex item
shrinks relative to the other flex items" when there is some extra space available.
This is just the opposite of flex-grow.
Here, also the range of value if from "0(zero) to any positive number". Zero means
width would not change.
>>> Here, only the div2 and div3 shrink, when the width of the container is
decreased. Also, div3 decreases twice as much as div2. Now, width of div1 is 100px,
div2 is 70px and div3 is 40px.
1. flex-start (Default)
2. flex-end
3. center
4. space-between
5. space-around
6. space-evenly
1. flex-start :::
The flex-start value aligns the items towards the start of the main
axis. This is the default value. Else use justify-content: flex-start ; in the
flex-container class.
CSS: .flex-container {
display: flex;
justify-content : flex-start;
}
2. flex-end ::
The flex-end value aligns the items towards the end of the main axis.
Use justify-content: flex-end ; in the flex-container class.
3. center :::
The center value aligns the items towards the center of the main axis.
Use justify-content: center ; in the flex-container class.
4. space-between :::
5. space-around :::
The space-around value distributes the remaining space around the items
evenly
along the main axis. Visually the spaces do not seem to be equal. This is because
all the flex items have equal space on both sides.
Suppose each item gets a spacing of 10px on both sides of each other. So, the first
item will have 10px on the left side. The first item will have 10px on the right
side and the second item will have 10px on its left side. This will total to 20px
spacing between them. Similarly, the last item has only 10px spacing on its right
side.
6. space-evenly :::
The space-evenly value distributes the remaining "space between the items and edges
of the container evenly along the main axis".
The align-items property is used to align the flex items along the
cross-axis. The cross-axis is perpendicular to the main axis.
If the main axis is horizontal, then the cross-axis is vertical. If the main axis
is vertical, then the cross axis is horizontal.
1. flex-start
2. flex-end
3. center
4. stretch (Default)
5. baseline
1. stretch ::
The stretch value stretches the flex items to fill the container along the cross
axis.
This is the default value. Else use align-items: stretch ; in the flex-container
class.
.flex-container div {
width: 50px;
min-height: 50px;
}
2. flex-start :::
The flex-start value aligns the items towards the start of the cross axis.
EX: .flex-container {
display: flex;
align-items: flex-start;
}
3. flex-end ::
The flex-end value aligns the items towards the end of the cross axis.
EX: .flex-container {
display: flex;
align-items:flex-end;
}
Output:
div 1 div 2 div 3
4. center ::
The center value aligns the items towards the center of the cross axis
EX: .flex-container {
display: flex;
align-items: center;
}
5.baseline :::
The baseline value aligns the flex items such as the "baselines of the content
inside the items". This means that the bottom base of the content inside the flex
item will be the axis to align the items.
To work with this property, we need to make some variations in the above code. The
changes are highlighted.
CSS: .flex-container {
display: flex;
align-items: baseline;
}
Align-content property is used to align the flex lines, i.e., multiple lines
of flex items.
This property aligns flex lines when there is extra space in the cross-axis,
similar to how
1. stretch ( Default )
2. flex-start
3. flex-end
4. center
5. space-between
6. space-around
1. stretch :::
The stretch value stretches the flex items present in multiple lines
to fill the container equally along the cross axis. This is the default value. Else
use align-content: stretch ;
2.flex-start
The flex-start value aligns the multiple lines of flex items towards the start of
the cross axis. Use align-content: flex-start ; in the flex-container class.
3. flex-end :::
The flex-end value aligns the multiple lines of flex items towards the
end of the cross axis. Use align-content: flex-end ; in the flex-container class.
4.center :::
The center value aligns the multiple lines of flex items towards the
center of the cross axis.
5. space-between :::
6.space-around :::
The align-self property is used for the alignment of selected flex items
inside the flex container. This overrides the default alignment set by the flex
container.
1. auto ( Default )
2. flex-start
3. flex-end
4. center
5. stretch
6. baseline
CSS: .flex-container {
display: flex;
align-items: stretch;
}
#div2 {
align-self: flex-start;
}
Output: div 1 div 2 div 3
>> In general align-items were stretch but we did align-self as flex-start for 2nd
div.
2. flex-end ::
The flex-end value aligns the selected flex items towards the end of the
cross axis.
#div2 {
align-self: flex-end;
}
3. center ::
The center value aligns the selected flex items towards the center of the
cross axis.
#div2 {
align-self: center;
}
4. stretch ::
The stretch value stretches the selected flex items to fill the container
along the cross axis.
CSS: .flex-container {
display: flex;
align-items: stretch;
}
#div2 {
align-self: stretch;
}
Output: div 1 div 2 div 3
5. baseline ::
The baseline value aligns the flex items such as the baselines of the
content inside the items. This means that the bottom base of the content inside the
flex item will be the axis to align the items.
To see this property work, we need to make some variation in the above code. The
changes are highlighted:
CSS: .flex-container {
display: flex;
align-items: stretch;
}
#div1 {
align-self: baseline;
}
#div3 {
align-self: baseline;
}
Here, h2 is not given the baseline property. Therefore, it is not aligned and is
stretched by default.
Shorthand properties are used to combine some flex properties together, so that
they can be defined in one line.
Flex Flow
The flex-flow property is a shorthand property to combine flex-direction and flex
wrap
Flex
The flex property is a shorthand property to combine the flex child properties,
i.e. flex-grow, flex-shrink and flex-basis.