CSS

About CSS

What is CSS

CSS is an abbreviation for Cascading Style Sheets. This is used to apply various styles to web pages so that web pages look attractive. The purpose of using this is to make the process of making web pages presentable. More importantly, it allows you to do so independently of the HTML that makes up each web page.
CSS is classified into three types, which are listed below:

  • Inline CSS contains the CSS property in the body section attached to the inline CSS element.
  • Internal or Embedded: The CSS rule set should be in the head section of the HTML file, implying that the CSS is embedded within the HTML file.
  • External CSS consists of a separate CSS file that only contains style properties via tag attributes.

What we are going to learn in CSS


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 Selector Here all <p> 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 Selector
CSS includes four different combinators: Descendant chooser (space) The descendant selector looks for all elements that are descendants of a given element. The following code selects all <p> elements contained within <div> 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 <p> elements that are children of a <div> 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 <p> element that comes after a <div> 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 <div> elements: div ~ p { background-color: yellow; } Pseudo Class Selectors A pseudo-class is used to define an element's special state. For example Syntax: selector:pseudo-class { property: value; } Selector Description :active Selects the active link :checked Selects every checked <input> element :disabled Selects every disabled <input> element :empty Selects every <p> element that has no children :enabled Selects every enabled <input> element :first-child Selects every <p> elements that is the first child of its parent :first-of-type Selects every <p> element that is the first <p> element of its parent :focus Selects the <input> element that has focus :hover Selects links on mouse over :in-range Selects <input> elements with a value within a specified range :invalid Selects all <input> elements with an invalid value :lang Selects every <p> element with a lang attribute value starting with "it" :last-child Selects every <p> elements that is the last child of its parent :last-of-type Selects every <p> element that is the last <p> element of its parent :link Selects all unvisited links :not Selects every element that is not a <p> element :nth-child Selects every <p> element that is the second child of its parent :nth-last-child Selects every <p> element that is the second child of its parent, counting from the last child :nth-last-of-type Selects every <p> element that is the second <p> element of its parent, counting from the last child :nth-of-type Selects every <p> element that is the second <p> element of its parent :only-of-type Selects every <p> element that is the only <p> element of its parent :only-child Selects every <p> element that is the only child of its parent :optional Selects <input> elements with no "required" attribute :out-of-range Selects <input> elements with a value outside a specified range :read-only Selects <input> elements with a "readonly" attribute specified :read-write Selects <input> elements with no "readonly" attribute :required Selects <input> 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 <input> 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: 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. <b>Attribute Selectors</b> <code><xmp> 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; } <!DOCTYPE html> <html> <head> <style> body { background-color: lightblue; } </style> <title> </title> </head> <body> </body> </html> Background-image
The background-image property specifies an image to be used as an element's background. Syntax: body { background-image: url("image.jpg"); } <!DOCTYPE html> <html> <head> <style> body { background-image: url("image.jpeg"); } </style> <title> </title> </head> <body> </body> </html> 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; } <!DOCTYPE html> <html> <head> <style> body { background-image: url("image.jpeg"); background-repeat: no-repeat; } </style> <title> </title> </head> <body> </body> </html> 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; } <!DOCTYPE html> <html> <head> <style> body { background-image: url("image.jpeg"); background-size: 200px; background-repeat:no-repeat ; } </style> <title> </title> </head> <body> </body> </html>
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; } <!DOCTYPE html> <html> <head> <style> body { background-image: url("image.jpeg"); background-size: 200px; background-repeat:no-repeat ; background-attachment: fixed; } </style> <title> </title> </head> <body> </body> </html>

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.

<!DOCTYPE html> <html> <head> <title></title> <style> p.dotted {border-style: dotted;} p.dashed {border-style: dashed;} p.solid {border-style: solid;} p.double {border-style: double;} p.groove {border-style: groove;} p.ridge {border-style: ridge;} p.inset {border-style: inset;} p.outset {border-style: outset;} p.none {border-style: none;} p.hidden {border-style: hidden;} p.mix {border-style: dotted dashed solid double;} </style> </head> <body> <p class="dotted">A dotted border.</p> <p class="dashed">A dashed border.</p> <p class="solid">A solid border.</p> <p class="double">A double border.</p> <p class="groove">A groove border.</p> <p class="ridge">A ridge border.</p> <p class="inset">An inset border.</p> <p class="outset">An outset border.</p> <p class="none">No border.</p> <p class="hidden">A hidden border.</p> <p class="mix">A mixed border.</p> </body> </html>

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 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.

<!DOCTYPE html> <html> <head> <style> p { border-style: solid; border-width: 5px; } </style> <title> </title> </head> <body> <p>Welcome to CodingNito</p> </body> </html>

Welcome to CodingNito

Border Color

The border-color property specifies the colour of the four borders.

The colour can be changed by:

<!DOCTYPE html> <html> <head> <style> p.one { border-style: solid; border-color: hsl(0, 100%, 50%); } p.two { border-style: solid; border-color: hsl(240, 100%, 50%); } p.three { border-style: solid; border-color: hsl(0, 0%, 73%); } </style> </head> <body> <p class="one">A solid red border</p> <p class="two">A solid blue border</p> <p class="three">A solid grey border</p> </body> </html>

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):

<!DOCTYPE html> <html> <head> <style> p.four { border-style: dotted solid double dashed; } </style> </head> <body> <p class="four">4 different border styles.</p> </body> </html>

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:

<!DOCTYPE html> <html> <head> <style> div { border: 1px solid black; background-color: lightblue; padding-top: 50px; padding-right: 30px; padding-bottom: 50px; padding-left: 80px; } </style> </head> <body> <div>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.</div> </body> </html>
>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:

The following values can be assigned to all margin properties:

<!DOCTYPE html> <html> <head> <style> div { border: 1px solid black; margin-top: 100px; margin-bottom: 100px; margin-right: 150px; margin-left: 80px; background-color: lightblue; } </style> </head> <body> <div>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.</div> </body> </html>
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

<!DOCTYPE html> <html> <head> <style> div { background-color: lightgrey; width: 300px; border: 15px solid green; padding: 50px; margin: 20px; } </style> </head> <body> <div>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Unde mollitia odit vitae explicabo architecto doloribus quasi quisquam tempore cum voluptatibus.<div> </body> </html>
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:

<!DOCTYPE html> <html> <head> <style> div { height: 100px; width: 500px; background-color: powderblue; } </style> </head> <body> <div>This div element has a height of 100px and a width of 500px.</div> </body> </html>
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.

<!DOCTYPE html> <html> <head> <style> div { max-width: 500px; height: 100px; background-color: powderblue; } </style> </head> <body> <div>This div element has a height of 100px and a max-width of 500px.</div> </body> </html>
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.

<!DOCTYPE html> <html> <head> <style> div { max-Height: 500px; height: 100px; background-color: powderblue; } </style> </head> <body> <div>This div element has a height of 100px and a max-Height of 500px.</div> </body> </html>
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.

<!DOCTYPE html> <html> <head> <style> p {outline-color:red;} p.dotted {outline-style: dotted;} p.dashed {outline-style: dashed;} p.solid {outline-style: solid;} p.double {outline-style: double;} p.groove {outline-style: groove;} p.ridge {outline-style: ridge;} p.inset {outline-style: inset;} p.outset {outline-style: outset;} </style> </head> <body> <p class="dotted">A dotted outline</p> <p class="dashed">A dashed outline</p> <p class="solid">A solid outline</p> <p class="double">A double outline</p> <p class="groove">A groove outline. The effect depends on the outline-color value.</p> <p class="ridge">A ridge outline. The effect depends on the outline-color value.</p> <p class="inset">An inset outline. The effect depends on the outline-color value.</p> <p class="outset">An outset outline. The effect depends on the outline-color value.</p> </body> </html>

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.

<!DOCTYPE html> <html> <head> <style> p{ border: 1px solid black; outline-style: solid; outline-color: red; outline-width: thin; } </style> </head> <body> <p>A thin outline.</p> </body> </html> Outline-color

The Outline-color property specifies the colour of the four borders.

The colour can be changed by:

<!DOCTYPE html> <html> <head> <style> p.ex1 { border: 2px solid black; outline-style: solid; outline-color: red; } p.ex2 { border: 2px dotted black; outline-style: dotted; outline-color: blue; } p.ex3 { border: 2px outset black; outline-style: outset; outline-color: grey; } </style> </head> <body> <p class="ex1">A solid red outline.</p> <p class="ex2">A dotted blue outline.</p> <p class="ex3">An outset grey outline.</p> </body> </html>

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.

<!DOCTYPE html> <html> <head> <style> p { border: 1px solid black; outline: 1px solid red; outline-offset: 15px; } </style> </head> <body> <p>This paragraph has an outline 15px outside the border edge.</p> </body> </html>

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: 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;
}
<!DOCTYPE html> <html> <head> <style> p.a { font: 15px Arial, sans-serif; } p.b { font: italic small-caps bold 12px/30px Georgia, serif; } </style> </head> <body> <p class="a">This is a paragraph. The font size is set to 15 pixels, and the font family is Arial.</p> <p class="b">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.</p> </body> </html>

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 Width
table {
width: 100%;
}

Table Alignment

td {
text-align: center;
}
td {
height: 50px;
vertical-align: bottom;
}
 
Table Style
Padding
th, td {
padding: 15px;
text-align: left;
}
Horizontal Dividers
th, td {
border-bottom: 1px solid #ddd;
}
Hoverable Table
tr:hover {background-color: coral;}
Striped Tables
tr:nth-child(even) {background-color: #f2f2f2;}
<!DOCTYPE html> <html> <head> <style> #customers { font-family: Arial, Helvetica, sans-serif; border-collapse: collapse; width: 100%; } #customers td, #customers th { border: 1px solid #ddd; padding: 8px; } #customers tr:nth-child(even){background-color: #f2f2f2;} #customers tr:hover {background-color: #ddd;} #customers th { padding-top: 12px; padding-bottom: 12px; text-align: left; color: white; } </style> </head> <body> <h1>A Fancy Table</h1> <table id="customers"> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Berglunds snabbköp</td> <td>Christina Berglund</td> <td>Sweden</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Ernst Handel</td> <td>Roland Mendel</td> <td>Austria</td> </tr> <tr> <td>Island Trading</td> <td>Helen Bennett</td> <td>UK</td> </tr> <tr> <td>Königlich Essen</td> <td>Philip Cramer</td> <td>Germany</td> </tr> <tr> <td>Laughing Bacchus Winecellars</td> <td>Yoshi Tannamuri</td> <td>Canada</td> </tr> <tr> <td>Magazzini Alimentari Riuniti</td> <td>Giovanni Rovelli</td> <td>Italy</td> </tr> <tr> <td>North/South</td> <td>Simon Crowther</td> <td>UK</td> </tr> <tr> <td>Paris spécialités</td> <td>Marie Bertrand</td> <td>France</td> </tr> </table> </body> </html>
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.

<Ul> <li>list-style-type: circle;</li> <li>list-style-type: square;</li> <li>list-style-type: upper-roman;</li> <li>list-style-type: lower-alpha;</li> </Ul>
    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)

<ul> <li>list-style-position: outside;</li> <li>list-style-position: inside;</li> </ul> <!DOCTYPE html> <html> <head> <style> ol { background: #ff9999; padding: 20px; } ul { background: #3399ff; padding: 20px; } ol li { background: #ffe5e5; color: darkred; padding: 5px; margin-left: 35px; } ul li { background: #cce5ff; color: darkblue; margin: 5px; } </style> </head> <body> <ol> <li>Coffee</li> <li>Tea</li> <li>Coca Cola</li> </ol> <ul> <li>Coffee</li> <li>Tea</li> <li>Coca Cola</li> </ul> </body> </html>
  1. Coffee
  2. Tea
  3. 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:

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;
        }
<!DOCTYPE html> <html> <head> <style> div.static { position: static; border: 3px solid #73AD21; } </style> </head> <body> <div class="static"> This div element has position: static; </div> </body> </html> 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;
}
<!DOCTYPE html> <html> <head> <style> div.relative { position: relative; width: 400px; height: 200px; border: 3px solid #73AD21; } div.absolute { position: absolute; top: 80px; right: 0; width: 200px; height: 100px; border: 3px solid #73AD21; } </style> </head> <body> <div class="relative">This div element has position: relative; <div class="absolute">This div element has position: absolute;</div> </div> </body> </html> 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;
            }
<!DOCTYPE html> <html> <head> <style> div.fixed { position: fixed; bottom: 10; right: 10; width: 300px; border: 3px solid #73AD21; } </style> </head> <body> <div class="fixed"> This div element has position: fixed; </div> </body> </html> 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;
        }
<!DOCTYPE html> <html> <head> <style> div.relative { position: relative; left: 30px; border: 3px solid #73AD21; } </style> </head> <body> <div class="relative"> This div element has position: relative; </div> </body> 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;
}
<!DOCTYPE html> <head> <style> div.sticky { position: -webkit-sticky; position: sticky; top: 0; padding: 5px; background-color: #cae8ca; border: 2px solid #4CAF50; } </style> </head> <body> <div style="padding-bottom:2000px"> <p>In this example, the sticky element sticks to the top of the page (top: 0), when you reach its scroll position.</p> <p>Scroll back up to remove the stickyness.</p> <p>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.</p> <p>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.</p> </div> </body> </html>

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 <!DOCTYPE html> <html> <head> <style> div { background-color: coral; width: 40px; height: 20px; overflow: visible; } </style> </head> <body> <div>This is the example of overflow visible property in css.<div> </body> </html> Hidden <!DOCTYPE html> <html> <head> <style> div { background-color: green; width: 200px; height: 65px; border: 1px solid black; overflow: hidden; } </style> </head> <body> <div>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.</div> </body> </html> Scroll <!DOCTYPE html> <html> <head> <style> div { background-color: coral; width: 200px; height: 100px; border: 1px solid black; overflow: scroll; } </style> </head><body> <div>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.</div> </body> </html> Auto <!DOCTYPE html> <html> <head> <style> div { background-color: coral; width: 200px; height: 65px; border: 1px solid black; overflow: auto; } </style> </head> <body> <div>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.</div> </body> </html>

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;
}
<!DOCTYPE html> <html> <head> <style> img { position: absolute; left: 0px; top: 0px; z-index: -1; } </style> </head> <body> <img src="image.jpg"> <p>Because the image has a z-index of -1, it will be placed behind the text.</p> </body> </html>

Gradient

You can display seamless transitions between two or more specified colours using CSS gradients.

CSS identifies three different gradient types:

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%);
}
<!DOCTYPE html> <html> <head> <style> #grad1 { height: 200px; background-color: red; background-image: linear-gradient(red, yellow); } </style> </head> <body> <div id="grad1"></div> </body> </html>
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.) <pre> #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%); } </pre> <code><xmp> <!DOCTYPE html> <html> <head> <style> #grad1 { height: 150px; width: 200px; background-color: red; background-image: radial-gradient(red, yellow, green); } </style> </head> <body> <div id="grad1"></div> </body> </html>
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%;
}
<!DOCTYPE html> <html> <head> <style> #grad1 { height: 200px; width: 200px; background-color: red; background-image: conic-gradient(red, yellow, green); } </style> </head> <body> <div id="grad1"></div> </body> </html>

Flex Box

There were four layout modes available before the Flexbox Layout module:

Without requiring float or positioning, the Flexible Box Layout Module makes it simpler to develop flexible responsive layout structures.

<!DOCTYPE html> <html> <head> <style> .flex-container { display: flex; background-color: DodgerBlue; } .flex-container > div { background-color: #f1f1f1; margin: 10px; padding: 20px; font-size: 30px; } </style> </head> <body> <div class="flex-container"> <div>1</div> <div>2</div> <div>3</div> </div> </body> </html> 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:

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;
}
<!DOCTYPE html> <html> <head> <style> .flex-container { display: flex; flex-direction: column; background-color: DodgerBlue; } .flex-container > div { background-color: #f1f1f1; width: 100px; margin: 10px; text-align: center; line-height: 75px; font-size: 30px; } </style> </head> <body> <div class="flex-container"> <div>1</div> <div>2</div> <div>3</div> </div> </body> </html>
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;
}
<!DOCTYPE html> <html> <head> <style> .flex-container { display: flex; flex-wrap: wrap; background-color: DodgerBlue; } .flex-container > div { background-color: #f1f1f1; width: 100px; margin: 10px; text-align: center; line-height: 75px; font-size: 30px; } </style> </head> <body> <div class="flex-container"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div> </div> </body> </html>
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;
}
<!DOCTYPE html> <html> <head> <style> .flex-container { display: flex; flex-flow: row wrap; background-color: DodgerBlue; } .flex-container > div { background-color: #f1f1f1; width: 100px; margin: 10px; text-align: center; line-height: 75px; font-size: 30px; } </style> </head> <body> <div class="flex-container"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div> </div> </body> </html>
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:

I) – Order

The order property specifies the order of the flex items.

<!DOCTYPE html> <html> <head> <style> .flex-container { display: flex; align-items: stretch; background-color: #f1f1f1; } .flex-container>div { background-color: DodgerBlue; color: white; width: 100px; margin: 10px; text-align: center; line-height: 75px; font-size: 30px; } </style> </head> <body> <div class="flex-container"> <div style="order: 3">1</div> <div style="order: 2">2</div> <div style="order: 4">3</div> <div style="order: 1">4</div> </div> </body> </html>
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.

<!DOCTYPE html> <html> <head> <style> .flex-container { display: flex; align-items: stretch; background-color: #f1f1f1; } .flex-container > div { background-color: DodgerBlue; color: white; margin: 10px; text-align: center; line-height: 75px; font-size: 30px; } </style> </head> <body> <div class="flex-container"> <div style="flex-grow: 1">1</div> <div style="flex-grow: 1">2</div> <div style="flex-grow: 8">3</div> </div> </body> </html>
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.

<!DOCTYPE html> <html> <head> <style> .flex-container { display: flex; align-items: stretch; background-color: #f1f1f1; } .flex-container>div { background-color: DodgerBlue; color: white; width: 100px; margin: 10px; text-align: center; line-height: 75px; font-size: 30px; } </style> </head> <body> <div class="flex-container"> <div>1</div> <div>2</div> <div style="flex-shrink: 0">3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> </div> </body> </html>
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.

<!DOCTYPE html> <html> <head> <style> .flex-container { display: flex; align-items: stretch; background-color: #f1f1f1; } .flex-container > div { background-color: DodgerBlue; color: white; width: 100px; margin: 10px; text-align: center; line-height: 75px; font-size: 30px; } </style> </head> <body> <div class="flex-container"> <div>1</div> <div>2</div> <div style="flex-basis:200px">3</div> <div>4</div> </div> </body> </html>
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.

<div class="grid-container"> <div class="grid-item">1</div> <div class="grid-item">2</div> <div class="grid-item">3</div> <div class="grid-item">4</div> <div class="grid-item">5</div> <div class="grid-item">6</div> <div class="grid-item">7</div> <div class="grid-item">8</div> <div class="grid-item">9</div> <!DOCTYPE html> <html> <head> <style> .grid-container { display: grid; grid-template-columns: auto auto auto; background-color: #2196F3; padding: 10px; } .grid-item { background-color: rgba(255, 255, 255, 0.8); border: 1px solid rgba(0, 0, 0, 0.8); padding: 20px; font-size: 30px; text-align: center; } </style> </head> <body> <div class="grid-container"> <div class="grid-item">1</div> <div class="grid-item">2</div> <div class="grid-item">3</div> <div class="grid-item">4</div> <div class="grid-item">5</div> <div class="grid-item">6</div> <div class="grid-item">7</div> <div class="grid-item">8</div> <div class="grid-item">9</div> </div> </body> </html>
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)
.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;
}
<!DOCTYPE html> <html> <head> <style> .grid-container { display: grid; gap: 50px 100px; grid-template-columns: auto auto auto; background-color: #2196F3; padding: 10px; } .grid-item { background-color: rgba(255, 255, 255, 0.8); border: 1px solid rgba(0, 0, 0, 0.8); padding: 20px; font-size: 30px; text-align: center; } </style> </head> <body> <div class="grid-container"> <div class="grid-item">1</div> <div class="grid-item">2</div> <div class="grid-item">3</div> <div class="grid-item">4</div> <div class="grid-item">5</div> <div class="grid-item">6</div> <div class="grid-item">7</div> <div class="grid-item">8</div> <div class="grid-item">9</div> </div> </body> </html>
1
2
3
4
5
6
7
8
9
Grid Lines <!DOCTYPE html> <html> <head> <style> .grid-container { display: grid; grid-template-columns: auto auto auto; gap: 10px; background-color: #2196F3; padding: 10px; } .grid-container > div { background-color: rgba(255, 255, 255, 0.8); text-align: center; padding: 20px 0; font-size: 30px; } .item1 { grid-column-start: 1; grid-column-end: 3; } </style> </head> <body> <div class="grid-container"> <div class="item1">1</div> <div class="item2">2</div> <div class="item3">3</div> <div class="item4">4</div> <div class="item5">5</div> <div class="item6">6</div> <div class="item7">7</div> <div class="item8">8</div> </div> </body> </html>
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:

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; } } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> div.example { background-color: yellow; padding: 20px; } @media screen and (max-width: 600px) { div.example { display: none; } } </style> </head> <body> <div class="example">Example DIV.</div> </body> </html>

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:

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;
}
<!DOCTYPE html> <html> <head> <style> div { width: 100px; height: 100px; background: red; position: relative; animation: mymove 5s infinite; } @keyframes mymove { from {left: 0px;} to {left: 200px;} } </style> </head> <body> <div></div> </body> </html>

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-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;
}
<!DOCTYPE html> <html> <head> <style> div { width: 100px; height: 100px; background: red; transition: width 2s, height 4s; } div:hover { width: 300px; height: 300px; } </style> </head> <body> <div></div> </body> </html>

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

CSS transforms allow you to move, rotate, scale, and skew elements.

Method of CSS Transform

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); } <!DOCTYPE html> <html> <head> <style> div { width: 300px; height: 100px; background-color: yellow; border: 1px solid black; } div#myDiv1 { transform: matrix(1, -0.3, 0, 1, 0, 0); } div#myDiv2 { transform: matrix(1, 0, 0.5, 1, 150, 0); } </style> </head> <body> <div> This a normal div element. </div> <div id="myDiv1"> Using the matrix() method. </div> <div id="myDiv2"> Another use of the matrix() method. </div> </body> </html> 3D Transform

Method of CSS Transform

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); } <!DOCTYPE html> <html> <head> <style> div { width: 300px; height: 100px; background-color: yellow; border: 1px solid black; } #myDiv { transform: rotateZ(90deg); } </style> </head> <body> <div> This a normal div element. </div> <div id="myDiv"> This div element is rotated 90 degrees. </div> </body> </html>