Selectors
A CSS selector is used to style the HTML element(s). CSS selectors select
HTML elements based on their id, class, type, attribute and so on. CSS
selectors select HTML elements based on their id, class, type, attribute, and
so on.
CSS selectors are classified into four categories:
- Simple selectors
- Combinator selectors
- Pseudo selectors
- Attribute selectors
Here all elements on the page will have a red text color.
p {
color: red;
}
ID Selector
The id selector uses an HTML element's id attribute to select a specific
element.
#para1 {
color: red;
}
Class Selector
The class selector is used to select HTML elements that have a specific
class attribute.
.center {
color: red;
}
Universal Selector
All HTML elements on the page are selected by the universal selector
(*).
* {
color: blue;
}
Group Selector
The grouping selector selects multiple the HTML elements with the
same style definitions.
h1, h2, p {
color: red;
}
Element SelectorCSS includes four different combinators:
- Descendant chooser (space)
- Selector for children (>)
- Adjacent sibling selector (+)
- General sibling selector (~)
The descendant selector looks for all elements that are descendants of a
given element.
The following code selects all elements contained within
elements:
div p {
background-color: yellow;
}
Selector for children (>)
The child selector returns all elements that are children of a given
element.
The example below selects all elements that are children of a
element:
div > p {
background-color: yellow;
}
Adjacent sibling selector (+)
“adjacent" means "immediately following The adjacent sibling selector is
used to select an element that comes immediately after another. Sibling
elements must share the same parent element. , ".
The following example selects the first element that comes after a
element:
div + p {
background-color: yellow;
}
General sibling selector (~)
The general sibling selector returns all elements that are the next
siblings of a given element.
The example below selects all < p> elements that are next siblings of
elements:
div ~ p {
background-color: yellow;
}
Pseudo Class Selectors
A pseudo-class is used to define an element's special state. For example
- When a user hovers their mouse over an element, it is styled.
- Different styles should be applied to visited and unvisited links.
- When an element receives attention, style it.
Syntax:
selector:pseudo-class {
property: value;
}
Selector Description
:active Selects the active link
:checked Selects every checked element
:disabled Selects every disabled element
:empty Selects every element that has no children
:enabled Selects every enabled element
:first-child Selects every
elements that is the first child of
its parent
:first-of-type Selects every
element that is the first
element of its parent
:focus Selects the element that has focus
:hover Selects links on mouse over
:in-range Selects elements with a value within a
specified range
:invalid Selects all elements with an invalid value
:lang Selects every
element with a lang attribute
value starting with "it"
:last-child Selects every
elements that is the last child of
its parent
:last-of-type Selects every
element that is the last
element of its parent
:link Selects all unvisited links
:not Selects every element that is not a
element
:nth-child Selects every
element that is the second child
of its parent
:nth-last-child Selects every
element that is the second child
of its parent, counting from the last child
:nth-last-of-type Selects every
element that is the second
element of its parent, counting from the last child
:nth-of-type Selects every
element that is the second
element of its parent
:only-of-type Selects every
element that is the only
element of its parent
:only-child Selects every
element that is the only child of
its parent
:optional Selects elements with no "required"
attribute
:out-of-range Selects elements with a value outside a
specified range
:read-only Selects elements with a "readonly"
attribute specified
:read-write Selects elements with no "readonly"
attribute
:required Selects elements with a "required"
attribute specified
:root Selects the document's root element
:targetctive Selects the current active #news element (clicked
on a URL containing that anchor name)
:valid Selects all elements with a valid value
:visited Selects all visited links
Pseudo Element Selector
A CSS pseudo-element is used to style specific elements.
It can, for example, be used to:
- The first letter or line of an element should be styled.
- Insert content before or after an element's content.
Syntax:
selector::pseudo-element {
property: value;
}
Selector Description
::after -> Insert content after each element that has been chosen.
::before -> Add content before each selected element.
::first-letter -> Chooses the first letter of each element.
::first-line -> Selects the first line of each element.
::selection -> Selects the portion of an element that a user has chosen.
Attribute Selectors
The [attribute] selector is used to find elements that have a particular
attribute.
Syntax:
a[target] {
background-color: yellow;
}
CSS [attribute="value"] Selector
The [attribute="value"] selector is used to select elements that have
both an attribute and a value specified.
a[target="_blank"] {
background-color: yellow;
}
CSS [attribute~="value"] Selector
The [attribute="value"] selector is used to find elements that have an
attribute value that contains a specific word.
[title~="flower"] {
border: 5px solid yellow;
}
CSS [attribute|="value"] Selector
The [attribute|="value"] selector is used to select elements that have
the specified attribute, the value of which can be the exact specified
value or the specified value followed by a hyphen (-).
[class|="top"] {
background: yellow;
}
CSS [attribute^="value"] Selector
The [attribute^="value"] selector is used to find elements that have
the specified attribute and whose value begins with the specified
value.
[class^="top"] {
background: yellow;
}
CSS [attribute$="value"] Selector
The [attribute$="value"] selector is used to find elements with
attribute values that end with a specific value.
[class$="test"] {
background: yellow;
}
CSS [attribute*="value"] Selector
The [attribute*="value"] selector is used to find elements with
attribute values that contain a specific value.
[class*="te"] {
background: yellow;
}
Background
As the name implies, these properties are related to the background
settings where you can change the document's colour, image, position, size,
and so on. There are lots of properties to design the background.
Background-color
The background-color property specifies the colour of an element's
background.
Syntax:
body {
background-color: lightblue;
}
Background-image
The background-image property specifies an image to be used as an
element's background.
Syntax:
body {
background-image: url("image.jpg");
}
Background-Repeat
The background-image property, by default, repeats an image both
horizontally and vertically or stop the repetition of image.
Background-repeat: repeat-x;
Background-repeat: repeat-y;
Background-repeat: no-repeat;
Syntax:
body {
background-image: url("image.png");
background-repeat: repeat-x;
}
Background-size
The background-size property, by default, set the size of image used as
background.
body {
background-image: url("image.png");
background-size: 200px;
}
Background-Attachment
The background-attachment CSS property sets whether a background
image's position is fixed within the viewport, or scrolls with its containing
block.
body {
background-image: url("image.png");
background-size: 200px;
background-attachment: 200px;
}
Border
CSS border properties allow you to specify the border style, width, and
colour of an element..
Border Style
CSS border properties allow you to specify the border style, width, and
colour of an element.
The border-style property specifies the type of border that will be
displayed.
A dotted border.
A dashed border.
A solid border.
A double border.
A groove border.
A ridge border.
An inset border.
An outset border.
No border.
A hidden border.
A mixed border.
A dotted border.
A dashed border.
A solid border.
A double border.
A groove border.
A ridge border.
An inset border.
An outset border.
No border.
A hidden border.
A mixed border.
Border Width
The width of the four borders is specified by the border-width property.
The width can be specified as a specific size (in px, pt, cm, em, and so on) or
as one of three pre-defined values: either thin, medium, or thick.
Welcome to CodingNito
Welcome to CodingNito
Border Color
The border-color property specifies the colour of the four borders.
The colour can be changed by:
- name - enter a colour name, such as "red"
- HEX - enter a hex value, such as "#ff0000"
- RGB - enter an RGB value, such as "rgb(255,0,0)"
- HSL - enter an HSL value, such as "hsl(0, 100%, 50%)"
- Transparent
A solid red border
A solid blue border
A solid grey border
A solid red border
A solid blue border
A solid grey border
Border Sides
You can specify a different border for each side, as demonstrated by the examples on the previous pages.
There are also properties in CSS for specifying each border (top, right, bottom, and left):
4 different border styles.
4 different border styles.
Box Model
Padding
CSS padding properties are used to create space around an element's content while staying within any defined
borders. You have complete control over the padding with CSS. Padding can be set for each side of an element
using properties (top, right, bottom, and left).
CSS allows you to specify padding for each side of an element:
- padding-top
- padding-right
- padding-bottom
- padding-left
This div element has a top padding of 50px, a right padding of 30px, a bottom padding of 50px, and a left padding of 80px.
>This div element has a top padding of 50px, a right padding of 30px, a bottom padding of 50px, and a left
padding of 80px.
Margin
CSS margin properties are used to create space around elements that are not bound by any defined borders. You
have complete control over the margins with CSS. There are
Properties that allow you to specify the margin for each side of an element (top, right, bottom, and left).
CSS has properties for specifying the margin for each side of an element:
- margin-top
- margin-right
- margin-bottom
- margin-left
The following values can be assigned to all margin properties:
- auto - the browser calculates the margin length - specifies a margin in pixels, points, centimetres, and
so on.
- % - specifies a margin in percentage of the width of the containing element
- inherit - specifies that the margin is inherited from the parent element
This div element has a top margin of 100px, a right margin of 150px, a bottom margin of 100px, and a left margin of 80px.
This div element has a top margin of 100px, a right margin of 150px, a bottom margin of 100px, and a left
margin of 80px.
Working With Box-Model
div {
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
calculation:
320px (width)
+ 20px (left + right padding)
+ 10px (left + right border)
+ 0px (left + right margin)
= 350px
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Unde mollitia odit vitae explicabo architecto doloribus quasi quisquam tempore cum voluptatibus.
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Unde mollitia odit vitae explicabo architecto
doloribus quasi quisquam tempore cum voluptatibus.
Height and Width
Height and Width
CSS height and width properties are used to specify an element's height and width.
The following values can be assigned to the height and width properties:
- auto - This is the default setting. The browser computes the height and width length - Defines the height/width in pixels, centimetres, and so on.
- % - Specifies the height/width as a percentage of the initial height/width of the containing block.
- initial- Returns the height/width to their default values.
- inherit - The height/width will be inherited from the value of its parent.
This div element has a height of 100px and a width of 500px.
This div element has a height of 100px and a width of 500px.
Max-Width
The max-width property is used to specify an element's maximum width.
This div element has a height of 100px and a max-width of 500px.
This div element has a height of 100px and a max-width of 500px.
Max-Height
The max-Height property is used to specify an element's maximum Height.
This div element has a height of 100px and a max-Height of 500px.
This div element has a height of 100px and a max-Height of 500px.
Outline
outline-style
The outline-style property, which can have one of the following values, specifies the outline's style.
A dotted outline
A dashed outline
A solid outline
A double outline
A groove outline. The effect depends on the outline-color value.
A ridge outline. The effect depends on the outline-color value.
An inset outline. The effect depends on the outline-color value.
An outset outline. The effect depends on the outline-color value.
A dashed outline
A dotted outline
A solid outline
A double outline
A groove outline. The effect depends on the outline-color value.
A ridge outline. The effect depends on the outline-color value.
An inset outline. The effect depends on the outline-color value.
An outset outline. The effect depends on the outline-color value.
outline-width
The width of the outline is specified by the outline-width property.
A thin outline.
Outline-color
The Outline-color property specifies the colour of the four borders.
The colour can be changed by:
- name - enter a colour name, such as "red"
- HEX - enter a hex value, such as "#ff0000"
- RGB - enter an RGB value, such as "rgb(255,0,0)"
- HSL - enter an HSL value, such as "hsl(0, 100%, 50%)"
- Transparent
A solid red outline.
A dotted blue outline.
An outset grey outline.
A solid red outline.
A dotted blue outline.
An outset grey outline.
Outline-offset
The outline-offset attribute increases the distance between an outline and an element's edge or border. An element's outline and the area between them are transparent.
This paragraph has an outline 15px outside the border edge.
This paragraph has an outline 15px outside the border edge.
Fonts
Fonts
The font CSS shorthand property sets all the different properties of an element's font. Alternatively, it sets an element's font to a system font.
- Font-Family
- Font-Style
- Font-Variant
- Font-Size
- Font-Weight
- Font
Font-Family: the font-family property use to specify the font of a text.
P{
font-family: "Times New Roman", Times, serif;
}
Font-Style: The font-style property specifies the font style for a text.
p{
font-style: italic;
}
Font-Variant: allows you to set all the font variants for a font.
p{
font-variant: small-caps;
}
Font-Size: The font-family property use to specify the size of a font.
p{
font-size: 15px;
}
Font-Weight: The font-weight property sets how thick or thin characters in text should be displayed.
p{
font-weight: 900;
}
Font: Shorthand property for font.
p{
font: italic small-caps bold 12px/30px Georgia, serif;
}
This is a paragraph. The font size is set to 15 pixels, and the font family is Arial.
This is a paragraph. The font is set to italic and bold, with small-caps (all lowercase letters are converted to uppercase). The font size is set to 12 pixels, the line height is set to 30 pixels, and the font family is Georgia.
This is a paragraph. The font size is set to 15 pixels, and the font family is Arial.
This is a paragraph. The font is set to italic and bold, with small-caps (all lowercase letters are converted to uppercase). The font size is set to 12 pixels, the line height is set to 30 pixels, and the font family is Georgia.
Table
Table border
Use the border property in CSS to define table borders.
table, th, td {
border: 1px solid;
}
Table borders may be collapsed into a single border using the border-collapse property.
table {
border-collapse: collapse;
}
Table Widthtable {
width: 100%;
}
Table Alignment
td {
text-align: center;
}
td {
height: 50px;
vertical-align: bottom;
}
Table StylePadding
th, td {
padding: 15px;
text-align: left;
}
Horizontal Dividersth, td {
border-bottom: 1px solid #ddd;
}
Hoverable Tabletr:hover {background-color: coral;}
Striped Tablestr:nth-child(even) {background-color: #f2f2f2;}
A Fancy Table
Company
Contact
Country
Alfreds Futterkiste
Maria Anders
Germany
Berglunds snabbköp
Christina Berglund
Sweden
Centro comercial Moctezuma
Francisco Chang
Mexico
Ernst Handel
Roland Mendel
Austria
Island Trading
Helen Bennett
UK
Königlich Essen
Philip Cramer
Germany
Laughing Bacchus Winecellars
Yoshi Tannamuri
Canada
Magazzini Alimentari Riuniti
Giovanni Rovelli
Italy
North/South
Simon Crowther
UK
Paris spécialités
Marie Bertrand
France
Company
Contact
Country
Alfreds Futterkiste
Maria Anders
Germany
Berglunds snabbköp
Christina Berglund
Sweden
Centro comercial Moctezuma
Francisco Chang
Mexico
Ernst Handel
Roland Mendel
Austria
Island Trading
Helen Bennett
UK
Königlich Essen
Philip Cramer
Germany
Laughing Bacchus Winecellars
Yoshi Tannamuri
Canada
Magazzini Alimentari Riuniti
Giovanni Rovelli
Italy
North/South
Simon Crowther
UK
Paris spécialités
Marie Bertrand
France
Lists
There are two primary categories of lists in HTML:
Unordered lists (ul>) have bullets to indicate the list items.
List items in ordered lists (ol>) are denoted by numbers or letters.
You can using the CSS list properties
For unordered lists, use various list item markers.
For ordered lists, use separate list item identifiers.
Mark each item on the list with a picture.
list-style-type
The list-style-type property specifies the type of list item marker.
- list-style-type: circle;
- list-style-type: square;
- list-style-type: upper-roman;
- list-style-type: lower-alpha;
ul.a {
list-style-type: circle;
}
list-style-image
The list-style-image property specifies an image as the list item marker
ul {
list-style-image: url('sqpurple.gif');
}
list-style-position
The list-style-position property specifies the position of the list-item markers (bullet points)
- list-style-position: outside;
- list-style-position: inside;
- Coffee
- Tea
- Coca Cola
- Coffee
- Tea
- Coca Cola
- Coffee
- Tea
- Coca Cola
- Coffee
- Tea
- Coca Cola
Position
The position property of an element specifies its positioning mechanism via static, relative, fixed, absolute or
sticky.
There are five different position values:
- static
- relative
- fixed
- absolute
- sticky
position: static
By default, HTML elements are static in location. The top, bottom, left, and right attributes have no effect on
items that are static in their positions. An element with the positioning static is always placed in line with the
natural flow of the page and is not given any special positioning:
div.static {
position: static;
border: 3px solid #73AD21;
}
This div element has position: static;
position: absolute
An element with the position attribute is placed in relation to its closest ancestor (instead of positioned
relative to the viewport, like fixed). The document body is used and the element scrolls with the page if an
absolute positioned element has no positioned ancestors. Note that items with absolute positioning are not part of
the usual flow and may overlap.
div.absolute {
position: absolute;
left: 30px;
border: 3px solid #73AD21;
}
This div element has position: relative;
This div element has position: absolute;
position: fixed
Positioned relative to the viewport, an element with the attribute position: fixed; always remains in the same
location regardless of how far the page is scrolled. The element's position can be adjusted using the top, right,
bottom, and left properties. When an element is fixed, its original place on the page is not left blank. Take note
of the page's lower-right fixed component. The CSS used is shown here.
div.fixed{
position: fixed;
bottom: 0;
right: 0;
width: 300px;
border: 3px solid #73AD21;
}
This div element has position: fixed;
position: relative
A component with the positioning attribute relative is positioned in relation to its default location. A
comparably positioned element will be moved from its default position if the top, right, bottom, and left
properties are changed. Other content won't be changed to fill in any gaps the element could leave.
div.relative {
position: relative;
left: 30px;
border: 3px solid #73AD21;
}
This div element has position: relative;
position: sticky
Based on the user's scroll position, an element with the property position: sticky is placed. Depending on the
scroll position, the relative and fixed states of a sticky element can be switched. It is at a relative position
until a predetermined offset position is reached in the viewport, at which point it "sticks" there (like
position:fixed).
div.sticky {
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
background-color: green;
border: 2px solid #4CAF50;
}
In this example, the sticky element sticks to the top of the page (top: 0), when you reach its scroll position.
Scroll back up to remove the stickyness.
Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum,
altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et.
Inciderint efficiantur his ad. Eum no molestiae voluptatibus.
Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum,
altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et.
Inciderint efficiantur his ad. Eum no molestiae voluptatibus.
Overflow
The CSS overflow property controls what happens to content that is too big to fit into an area.
The overflow property has the following values:
- visible
- hidden
- scroll
- auto
Visible
This is the example of overflow visible property in css.
Hidden
You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.
Scroll
You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.
Auto
You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.
Z-index
Elements can cover up other elements when they are placed. The stack order of an element is specified by the z-index property (which element should be placed in front of, or behind, the others). An element's stack order might be either positive or negative.
img{
left: 0px;
top: 0px;
z-index: -1;
}
Because the image has a z-index of -1, it will be placed behind the text.
Gradient
You can display seamless transitions between two or more specified colours using CSS gradients.
CSS identifies three different gradient types:
- Linear Gradients (moves left, right, up, down, diagonally)
- Rounded Gradients (defined by their center)
- Conic Gradient (rotated around a centre point)
Linear Gradient
You need to define at least two colour stops in order to build a linear gradient. The colours you want to show smooth transitions between are called colour stops. Along with the gradient effect, you can also provide a beginning point and a direction (or an angle).
(here are the multiple example through which you can understand the use of linear gradient.)
#grad {
background-image: linear-gradient(red, yellow);
}
#grad {
background-image: linear-gradient(to right, red , yellow);
}
#grad {
background-image: linear-gradient(to bottom right, red, yellow);
}
#grad {
background-image: linear-gradient(180deg, red, yellow);
}
#grad {
background-image: linear-gradient(red, yellow, green);
}
#grad {
background-image: linear-gradient(to right, red,orange,yellow,green,blue,indigo,violet);
}
#grad {
background-image: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1));
}
#grad {
background-image: repeating-linear-gradient(red, yellow 10%, green 20%);
}
Radial Gradient
The centroid of a radial gradient determines it. Additionally, at least two colour stops must be defined in order to produce a radial gradient.
(here are the multiple example through which you can understand the use of radial gradient.)
#grad {
background-image: radial-gradient(red, yellow, green);
}
#grad {
background-image: radial-gradient(red 5%, yellow 15%, green 60%);
}
#grad {
background-image: radial-gradient(circle, red, yellow, green);
}
#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);
}
#grad {
background-image: repeating-radial-gradient(red, yellow 10%, green 15%);
}
Conic Gradient
A gradient with colour transitions rotating around the centre is known as a conic gradient. You need to define at least two colours in order to construct a conic gradient.
#grad {
background-image: conic-gradient(red, yellow, green);
}
#grad {
background-image: conic-gradient(red, yellow, green, blue, black);
}
#grad {
background-image: conic-gradient(red 45deg, yellow 90deg, green 210deg);
}
#grad {
background-image: conic-gradient(red, yellow, green, blue, black);
border-radius: 50%;
}
#grad {
background-image: conic-gradient(red 0deg, red 90deg, yellow 90deg, yellow 180deg, green 180deg, green 270deg, blue 270deg);
border-radius: 50%;
}
#grad {
background-image: conic-gradient(from 90deg, red, yellow, green);
}
#grad {
background-image: conic-gradient(at 60% 45%, red, yellow, green);
}
#grad {
background-image: repeating-conic-gradient(red 10%, yellow 20%);
border-radius: 50%;
}
#grad {
background-image: repeating-conic-gradient(red 0deg 10deg, yellow 10deg 20deg, blue 20deg 30deg);
border-radius: 50%;
}
Flex Box
There were four layout modes available before the Flexbox Layout module:
- Block, used for webpage parts
- For text, use Inline; for two-dimensional table data, use Table
- Positioned for an element's specified position
Without requiring float or positioning, the Flexible Box Layout Module makes it simpler to develop flexible responsive layout structures.
1
2
3
Flex-Container
This is a flexible container (the blue region), as mentioned in the prior chapter, with three flexible items:
.flex-container {
display: flex;
}
The flex container properties are:
- flex-direction
- flex-wrap
- flex-flow
1
2
3
Flex-Direction
.flex-container {
display: flex;
flex-direction: column;
}
.flex-container {
display: flex;
flex-direction: column-reverse;
}
.flex-container {
display: flex;
flex-direction: row;
}
.flex-container {
display: flex;
flex-direction: row-reverse;
}
1
2
3
1
2
3
flex-wrap
Flex items can be wrapped or not using the flex-wrap attribute. To better illustrate the flex-wrap property, the samples following have 12 flex elements.
.flex-container {
display: flex;
flex-wrap: wrap;
}
.flex-container {
display: flex;
flex-wrap: nowrap;
}
.flex-container {
display: flex;
flex-wrap: wrap-reverse;
}
.flex-container {
display: flex;
flex-flow: row wrap;
}
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
flex-flow
The flex-direction and flex-wrap attributes can be set using the flex-flow shorthand property.
.flex-container {
display: flex;
flex-flow: row wrap;
}
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
Aligning item in Flexbox
.flex-container {
display: flex;
justify-content: center;
}
.flex-container {
display: flex;
justify-content: flex-start;
}
.flex-container {
display: flex;
justify-content: flex-end;
}
.flex-container {
display: flex;
justify-content: space-around;
}
.flex-container {
display: flex;
justify-content: space-between;
}
.flex-container {
display: flex;
height: 200px;
align-items: center;
}
.flex-container {
display: flex;
height: 200px;
align-items: flex-start;
}
.flex-container {
display: flex;
height: 200px;
align-items: flex-end;
}
.flex-container {
display: flex;
height: 200px;
align-items: stretch;
}
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: space-between;
}
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: space-around;
}
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: stretch;
}
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: center;
}
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: flex-start;
}
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: flex-end;
}
.flex-container {
display: flex;
height: 300px;
justify-content: center;
align-items: center;
}
Here are some example of aligning items of the flexbox
Flex-items
The flex item properties are:
- order
- flex-grow
- flex-shrink
- flex-basis
I) – Order
The order property specifies the order of the flex items.
1
2
3
4
1
2
3
4
II) - flex-grow
A flex item's growth rate in relation to the other flex items is determined by the flex-grow property.
1
2
3
1
2
3
III) - Flex-shrink
The flex-shrink property specifies how much a flex item will shrink relative to the rest of the flex items.
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
IV) - flex-basis
The initial length of a flex item is specified by the flex-basis property.
1
2
3
4
1
2
3
4
Grid
The CSS Grid Layout Module provides a grid-based layout system with rows and columns that makes designing web pages easier without the use of floats and positioning.
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
Grid display Property
(To change the display type of the grid).grid-container {
display: grid;
}
.grid-container {
display: inline-grid;
}
Grid gap Property
(To change the gape size between the elements of the grid)
- The row-gap property sets the gap between the rows.
- The column-gap property sets the gap between the columns.
- The gap property is a shorthand property for the row-gap and the column-gap property.
.grid-container {
display: grid;
column-gap: 50px;
}
.grid-container {
display: grid;
row-gap: 50px;
}
.grid-container {
display: grid;
gap: 50px 100px;
}
.grid-container {
display: grid;
gap: 50px;
}
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
Grid Lines
- The lines between columns are called column lines.
- The lines between rows are called row lines.
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
Media Query
In CSS, the Media query is used to create a responsive web design. It means that the appearance of a web page varies from system to system depending on screen or media type. The breakpoint specifies the device-width size at which the content begins to break or deform.
Many things can be checked using media queries:
- width and height of the viewport
- device width and height
- Orientation
- Resolution
A media query is made up of a media type and one or more expressions that can be true or false. If the specified media matches the type of device on which the document is displayed, the query returns true. If the media query returns true, a style sheet is generated.
@media only screen and (max-width: 600px) {
body {
background-color: lightblue;
}
}
Example DIV.
Animation
CSS Animations is a technique for changing the appearance and behaviour of various web page elements. It is used to change the motions or display of the elements. It is divided into two sections, one containing the CSS properties that describe the animation of the elements and the other containing specific keyframes that indicate the animation properties of the element and the specific time intervals at which those must occur.
In this chapter you will learn about the following properties:
- keyframes
- animation-name
- animation-duration
- animation-delay
- animation-iteration-count
- animation-direction
- animation-timing-function
- animation-fill-mode
- animation
Animation-name: Specify a name for the @keyframes animation.
div {
animation-name: mymove;
}
Animation-duration: Specify a time for the @keyframes animation.
div {
animation-duration: 3s;
}
Animation-delay: Specify a pause time before to the starting the @keyframes animation.
div {
animation-delay: 2s;
}
Animation-iteration-count: Specify a no of time before to run the @keyframes animation.
div {
animation-iteration-count: 2;
}
Animation-direction:Specify the direction of @keyframes animation to run from forward or backward.
div {
animation-direction: alternate;
}
Animation-timing-function:Specify the speed of @keyframes animation.
div {
animation-timing-function: linear;
}
Animation-fill-mode: The animation-fill-mode property specifies a style for the element when the animation is not playing (before it starts, after it ends, or both).
div {
animation-fill-mode: forwards;
}
Animation: It is a shorthand property to run the animation.
div {
animation: mymove 5s infinite;
}
Transition
Transitions in CSS allow us to control how the transition between the two states of the element occurs. For example, when you hover your mouse over a button, you can use CSS selectors and pseudo-classes to change the background colour of the element. However, we can change any other or combination of properties. We can use transition to determine how the colour changes. Transitions can be used to animate changes and make them visually appealing to the user, resulting in a better user experience and interactivity. This article will demonstrate how to animate the transition between CSS properties.
In this chapter you will learn about the following properties:
- transition
- transition-delay
- transition-duration
- transition-property
- transition-timing-function
Transition-delay:The transition-delay property specifies when the transition effect will start.
div {
transition-delay: 2s;
}
transition-duration:The transition-duration property specifies how many seconds (s) or milliseconds (ms) a transition effect takes to complete.
div {
transition-duration: 5s;
}
transition-property:The transition-property property specifies the name of the CSS property the transition effect is for (the transition effect will start when the specified CSS property changes).
div {
transition-property: width;
}
div:hover {
width: 300px;
}
transition-timing-function:The transition-timing-function property specifies the speed curve of the transition effect.
div {
transition-timing-function: linear;
}
transition:It is the shorthand property to apply transition in a webpage.
div {
transition: width 2s, height 4s;
}
Transform
CSS's transform property is used to alter the visual formatting model's coordinate space. This is used to apply effects to elements such as skew, rotate, translate, and so on.
Transform are of two types:
- 2D Transform
- 3D Transform
2D Transform
CSS transforms allow you to move, rotate, scale, and skew elements.
Method of CSS Transform
- translate()
- rotate()
- scaleX()
- scaleY()
- scale()
- skewX()
- skewY()
- skew()
- matrix()
translate():Move element from current position.
rotate():Use to rotate clockwise or anti-clockwise
scaleX():use to increase or decrease the size on X-axis
scaleY():use to increase or decrease the size on Y-axis
scale():use to increase or decrease the size
skewX():the skewY() method skews an element along the X-axis by the given angle.
skewY():the skewX() method skews an element along the Y-axis by the given angle.
skew():the skew method skews an element along both the axis by the given angle.
matrix():The matrix() method combines all the 2D transform methods into one.
Translate
div {
transform: translate(50px, 100px);
}
Rotate
div {
transform: rotate(20deg);
}
Scale
div {
transform: scale(2, 3);
}
ScaleX
div {
transform: scaleX(0.5);
}
ScaleY
div {
transform: scaleY(3);
}
Skew
div {
transform: skew(20deg, 10deg);
}
SkewX
div {
transform: skewX(20deg);
}
SkewY
div {
transform: skewY(20deg);
}
Matrix
div {
transform: matrix(1, -0.3, 0, 1, 0, 0);
}
This a normal div element.
Using the matrix() method.
Another use of the matrix() method.
3D Transform
Method of CSS Transform
- rotateX()
- rotateY()
- rotateZ()
rotateX():The rotateX() method rotates an element around its X-axis at a given degree.
rotateY():The rotateY() method rotates an element around its X-axis at a given degree.
rotateZ():The rotateZ() method rotates an element around its X-axis at a given degree.
rotateX
div {
transform: rotateX(150deg);
}
rotateY
div {
transform: rotateY(150deg);}
rotateZ
div {
transform: rotateZ(150deg);
}
This a normal div element.
This div element is rotated 90 degrees.