Author: saqibkhan

  • Overview

    jQuery is a fast and concise JavaScript Library created by John Resig in 2006 with a nice motto: Write less, do more. jQuery simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development.

    jQuery Features

    jQuery simplifies various tasks of a progammer by writing less code. Here is the list of important core features supported by jQuery −

    • DOM manipulation − The jQuery made it easy to select DOM elements, negotiate them and modifying their content by using cross-browser open source selector engine called Sizzle.
    • Event handling − The jQuery offers an elegant way to capture a wide variety of events, such as a user clicking on a link, without the need to clutter the HTML code itself with event handlers.
    • AJAX Support − The jQuery helps you a lot to develop a responsive and featurerich site using AJAX technology.
    • Animations − The jQuery comes with plenty of built-in animation effects which you can use in your websites.
    • Lightweight − The jQuery is very lightweight library – about 19KB in size (Minified and gzipped).
    • Cross Browser Support − The jQuery has cross-browser support, and works well in IE 6.0+, FF 2.0+, Safari 3.0+, Chrome and Opera 9.0+
    • Latest Technology − The jQuery supports CSS3 selectors and basic XPath syntax.

    Setting up jQuery

    There are two ways to use jQuery.

    1. Local Installation − You can download jQuery library on your local machine and include it in your HTML code.
    2. CDN Based Installation − You can include jQuery library into your HTML code directly from Content Delivery Network (CDN).

    jQuery – Local Installation

    You can download latest version of jQuery on your web server and include the downloaded library in your code. We suggest you to download compressed version of the library for a better performance.

    • Go to the https://jquery.com/download/ to download the latest version available.
    • Now put downloaded jquery-3.6.0.min.js file in a directory of your website, e.g. /jquery/jquery-3.6.0.js.
    • Finally include this file in your HTML markup file as shown below.

    Example

    Now you can include jquery library in your HTML file as given below. Try to click the icon run button to run the following jQuery code −

    <!doctype html><html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){
    
      document.write("Hello, World!");});&lt;/script&gt;&lt;/head&gt;&lt;body&gt;&lt;!--HTML body goes here --&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    jQuery - CDN Based Installation

    You can include jQuery library into your HTML code directly from a Content Delivery Network (CDN). There are various CDNs which provide a direct link to the latest jQuery library which you can include in your program.

    Example

    Now let us rewrite above example using jQuery library from Google CDN. Try to click the icon run button to run the following jQuery code −

    <!doctype html><html><head><title>The jQuery Example</title><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script><script>$(document).ready(function(){
    
      document.write("Hello, World!");});&lt;/script&gt;&lt;/head&gt;&lt;body&gt;&lt;!--HTML body goes here --&gt;&lt;/body&gt;&lt;/html&gt;&lt;html&gt;</pre>

    We are using our own Tutorials Point CDN version of the library throughout this jQuery Tutorial. You can find many other CDNs on the internet to include jQuery in your web pages.

    <!doctype html><html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){
    
      document.write("Hello, World!");});&lt;/script&gt;&lt;/head&gt;&lt;body&gt;&lt;!--HTML body goes here --&gt;&lt;/body&gt;&lt;/html&gt;&lt;html&gt;</pre>

    How to Call a jQuery Library Functions?

    As almost everything, we do when using jQuery reads or manipulates the document object model (DOM), we need to make sure that we start adding events etc. as soon as the DOM is ready.

    If you want an event to work on your page, you should call it inside the $(document).ready() function. Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded.

    To do this, we register a ready event for the document as follows −

    $(document).ready(function(){// do stuff when DOM is ready});

    To call upon any jQuery library function, use HTML script tags as shown below. Try to click the icon run button to run the following jQuery code −

    <!doctype html><html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("div").click(function(){alert("Hello, world!");});});</script></head><body><div>Click on this to see a dialogue box.</div></body></html>

    How to Use Custom Scripts?

    It is better to write your custom code in the custom JavaScript file : custom.js, as follows −

    /* Filename: custom.js */$(document).ready(function(){$("div").click(function(){alert("Hello, world!");});});

    Let's keep this file in /jquery directory and then we can include custom.js file in our HTML file as follows. Try to click the icon run button to run the following jQuery code −

    <!doctype html><html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script src="https://www.tutorialspoint.com/jquery/custom.js"></script></head><body><div>Click on this to see a dialogue box.</div></body></html>

    Using Multiple Libraries

    You can use multiple libraries all together without conflicting each others. For example, you can use jQuery and MooTool javascript libraries together. You can check jQuery noConflict Method for more detail.

    What is Next ?

    Do not worry too much if you did not understand above examples. You are going to grasp them very soon in subsequent chapters.

    Next chapter would try to cover few basic concepts which are coming from conventional JavaScript.

  • Roadmap

    This Roadmap will guide you to master jQuery. You will learn core concepts, advanced techniques, and best practices by following mentioned topics. This step-by-step path will help you as a beginner.

    What is a Tutorial Roadmap?

    Tutorial Roadmap typically covers the journey from beginner to advanced user, including key concepts, practical applications, and best practices.

    jQuery Roadmap

    Master jQuery from beginner to expert with our comprehensive 2024 roadmap. Learn about static typing, interfaces, generics, and advanced type operations. Includes practical examples, learning resources, and a structured timeline for becoming a jQuery expert.

    jQuery Roadmap

    jQuery Introduction

    jQuery Basics

    jQuery Syntax

    jQuery Selectors

    Element Selectors

    ID Selectors

    Class Selectors

    Selectors Reference

    DOM Manipulation

    jQuery DOM

    Add Elements

    Remove Elements

    Replace Elements

    jQuery Events

    Event Handling

    Mouse Events

    click

    dblclick

    Keyboard Events

    keypress

    keydown

    hover

    mousedown

    mouseup

    keyup

    Form Events

    submit

    change

    select

    blur

    focusin

    Document Events

     load

    resize

    scroll

     unload

    ready

    HTML/CSS Manipulation

    HTML Classes

    CSS Properties

    Dimension

    jQuery Attributes

    Standard Attributes

     className

     tagName

     id

     href

     title

     rel

     src

     style

    jQuery Effects & Animation

    Effects

    Animation

    Chaining

    Callback Function

    jQuery Traversing

    Traversing Ancestors

    Traversing Descendants

    jQuery UI

    jQuery Interactions

    draggable

    droppable

    resizable

    selectable

    sortable

    jQuery Widget

    Accordion

    Button

    Dialog

    Progressbar

    Slider

    Tabs

     Autocomplete

    Datepicker

    Menu

    Select menu

    Spinner

    Tooltip

    jQuery Theming

    Events Reference

    Effects Reference

    HTML/CSS Reference

    DOM Traversing

    Miscellaneous Reference

    data()

    each()

    get()

    index()

    removeData()

    toArray()

    jQuery Miscellaneous

    jQuery Utilities

    jQuery Plugins

    Questions & Answers

    jQuery Quick Guide

    jQuery Ajax

    How jQuery Roadmap Can help you?

    This jQuery roadmap includes essential programming concepts, hands-on coding exercises, and valuable learning resources. Calculate your learning timeline, implement strategic coding practices, and build a solid foundation in jQuery development with our clear, structured approach.

  • jQuery Tutorial

    This jQuery Tutorial has been prepared by well experienced front end programmers who are using Javascript and jQuery extensively in their projects. This tutorial has been developed for the jQuery beginners to help them understand the basics to advanced of jQuery Framework. After completing this tutorial, you will find yourself at a great level of expertise in jQuery Framework, from where you can take yourself to the next levels.

    What is jQuery?

    jQuery is a lightweight Javascript library which is blazing fast and concise. This library was created by John Resig in 2006 and

    jQuery has been designed to simplify HTML DOM tree traversal and manipulation, as well as event handling, CSS animation, and Ajax.

    jQuery can be used to find a particular HTML element in the HTML document with a certain ID, class or attribute and later we can use jQuery to change one or more of attributes of the same element like color, visibility etc. jQuery can also be used to make a webpage interactive by responding to an event like a mouse click.

    jQuery is free, open-source software which comes under the permissive MIT License. As of Apr 2021, jQuery is used by 77.8% of the top 10 million most popular websites.

    jQuery has been developed with the following principles:

    • Separation of JavaScript and HTML, which encourages developers to completely separate JavaScript code from HTML markup.
    • Brevity and clarity promotes features like chainable functions and shorthand function names.
    • Eliminates of cross-browser incompatibilities, so developers does not need to worry about browser compatibility while writing code using jQuery library.
    • Extensibility, which means new events, elements, and methods can be easily added in jQuery library and then reused as a plugin.

    jQuery Online Editor

    We have provided jQuery Online Editor which helps you to Edit and Execute the code directly from your browser. Try to click the icon Execute Program to run the following jQuery code to print conventional “Hello, World!”.

    Below code box allows you to change the value of the code. Try to change the value of Hello, World! and run it again to verify the result.

    <html><head><title>The jQuery Example</title><script src ="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script type ="text/javascript">$(document).ready(function(){$("h1").html("Hello, World!");});</script></head><body><h1>Hello</h1></body></html>

    Browser Support

    As of April 2022, jQuery 3.0 and newer supports “current-1 versions” of Firefox, Chrome, Safari, and Edge as well as Internet Explorer 9 and newer versions. On mobile it supports iOS 7 and newer, and Android 4.0 and newer.

    jQuery and Javascript Jobs

    jQuery and Javascripts both are very high in demand and all major web applications are making use of jQuery in one or another way. We explored many major job websites before writing this tutorial and found that there are numerous openings across the world in multi national companies.

    Average annual salary for a Javascript and jQuery developer is around $150,000. Though it can vary depending on the location. Following are the great companies who are using Kotlin:

    • Google
    • Amazon
    • Netflix
    • Zomato
    • Uber
    • Trello
    • Coursera
    • Basecamp
    • Unacademy
    • Byjus
    • Many more…

    So, you could be the next potential employee for any of these major companies. We have develop a great learning material for jQuery which will help you to prepare for the technical interviews and certification exams based on jQuery. So, start learning jQuery using our simple and effective tutorial anywhere and anytime absolutely at your pace.

    Before you Start

    Before proceeding with this tutorial, you should have a basic understanding of HTML, CSS, JavaScript, Document Object Model (DOM) and any text editor. As we are going to develop web based application using jQuery, it will be good if you have understanding on how internet and web based applications work.

  • Adjacent Sibling Selectors

    Adjacent Sibling Selectors in CSS

    CSS adjacent sibling selector or next sibling selector selects an element that is immediately after another element. It is used to select only those elements which immediately follow the first selector. Both elements should share the same parent.

    Syntax

    The syntax for CSS adjacent sibling selectors is as follows:

    selector1 + selector2{/* property: value; */color: #04af2f
    }

    Example of Adjacent Sibling Selectors

    In this example, the style is applied only to the p element which is just after the div element. The other p elements remain unaffected.

    <!DOCTYPE html><html lang="en"><head><style>
    
      div{
         border: 2px solid #031926;
      }
      div + p {
         color: #04af2f;
      }
    </style></head><body><p>
         This paragraph is above the div 
         and will not be green.
      &lt;/p&gt;&lt;div&gt;&lt;p&gt;
            This paragraph is inside a div 
            and will not be green..
         &lt;/p&gt;&lt;/div&gt;&lt;p&gt;
         This paragraph 1 after the div 
         and will be green..
      &lt;/p&gt;&lt;p&gt;This paragraph 2 after the 
         div and will not be green..
      &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Example 2

    In this example, we have used an adjacent sibling selector to design the p element just after the h3 tag having the same parent element i.e. div container.

    <!DOCTYPE html><html><head><style>
    
        .container {
            padding: 20px;
            border: 2px solid #031926;
            font-family: Verdana, sans-serif;
        }
        h3 + p {
            font-family: Verdana, sans-serif;
            font-size: 20px;
            font-style: italic;
            background-color: #04af2f;
            color: white;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;h3&gt;This is an h3 tag&lt;/h3&gt;&lt;p&gt; 
            This is a p tag that immediately follows the h3 tag.
        &lt;/p&gt;&lt;p&gt;
            This is another p tag, but it does not immediately 
            after the h3 tag.
        &lt;/p&gt;&lt;/div&gt;&lt;p&gt;This is a p tag which is outside the parent div element.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>
  •  General Sibling Selectors

    General Sibling Selectors in CSS

    CSS general sibling selector ( “~” ) selects all the elements that are siblings of a specified element sharing the same parent. It will select all matching siblings, not just the one immediately following. A tilde ( “~” ) symbol is used to denote the general sibling.

    Syntax

    The syntax for CSS general sibling selectors is as follows:

    selector1 ~ selector2{/* property: value; */color: #04af2f
    }

    Example of General Sibling Selectors

    In this example, we have used general sibling selector to change the text color of all p elements that are immediate siblings of the div element.

    <!DOCTYPE html><html lang="en"><head><style>
    
      div{
         border: 2px solid #031926;
      }
      div ~ p {
         color: #04af2f;
      }
    </style></head><body><p>
         This paragraph is above the div 
         and will not be blue
      &lt;/p&gt;&lt;div&gt;&lt;p&gt;
            This paragraph is inside a div 
            and will not be blue.
         &lt;/p&gt;&lt;/div&gt;&lt;p&gt;
         This paragraph 1 after the div 
         and will be blue.
      &lt;/p&gt;&lt;p&gt;This paragraph 2 after the 
         div and will be blue.
      &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Example 2

    In this example, we have used a general sibling selector to change the background color and font of all the p elements after the h3 tag.

    <!DOCTYPE html><html><head><style>
    
        .container {
            padding: 20px;
            border: 2px solid #031926;
            font-family: Verdana, sans-serif;
        }
        h3 ~ p {
            font-family: Verdana, sans-serif;
            font-size: 20px;
            font-style: italic;
            background-color: #04af2f;
            color: white;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;h3&gt;This is an h3 tag&lt;/h3&gt;&lt;p&gt; 
            This is a p tag that immediately follows the h3 tag.
        &lt;/p&gt;&lt;p&gt;
            This is another p tag, but it is not immediately 
            after the h3 tag.
        &lt;/p&gt;&lt;/div&gt;&lt;p&gt;This is a p tag which is outside the parent div element.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

  • Descendant Selectors

    Descendant Selectors in CSS

    CSS descendant selector styles all the tags that are children of a particular specified tag. A single space between the parent element and child element is used to mention a descendant.

    Syntax

    The syntax for CSS descendant selectors is as follows:

    selector_1 selector_2{/* property: value; */color: #04af2f
    }

    Descendant Selectors Example

    In this example, we have set a border for all the p elements inside a div element. The p elements outside the div element will not have any styles applied to them.

    <!DOCTYPE html><html lang="en"><head><title>Document</title><style>
    
        div p {
            border: 2px solid #04af2f;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div&gt;&lt;p&gt;This is a p element inside a div.&lt;/p&gt;&lt;p&gt;This is second p element inside a div&lt;/p&gt;&lt;/div&gt;&lt;p&gt;This is a p element outside the div element.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Example 2

    In this example, we have used CSS transition property with rotate() function of CSS transform property to rotate the written text of the div element with the class name container.

    <!DOCTYPE html><html lang="en"><head><title>Document</title><style>
    
        .container, .container2 {
            display: inline-block;
            height: 100px;
            width: 100px;
            background-color: #04af2f;
            color: white;
            font-size: 20px;
            font-family: Verdana, sans-serif;
            padding: 10px;
            border: 1px solid black;
            text-align: center;
            line-height: 100px;
        }
        .container div:hover {
            transition: transform 0.5s ease;
            transform: rotate(45deg);
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;div&gt;Box 1&lt;/div&gt;&lt;/div&gt;&lt;div class="container2"&gt;Box 2&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

  • Element Selectors

    Element Selectors in CSS

    CSS element selectors are used to target and style specific HTML elements. These selectors are defined by simply using the element’s name in the stylesheet.

    Syntax

    The syntax for CSS element selectors is as follows:

    element_name{/*property: value;*/color: red;}

    Using Element Selectors Setting Background Color

    To set the background color and text color of all <p> elements, use the element selector.

    Example

    The following example sets the background color and text color for all <p> elements.

    <!DOCTYPE html><html lang="en"><head><title>Element Selectors</title><style>
    
        p {
            background-color: #04af2f;
            color: white;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;Welcome to CSS Element Selectors&lt;/p&gt;&lt;p&gt;This is paragraph 1&lt;/p&gt;&lt;p&gt;This is paragraph 2&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Using Element Selectors Setting Border

    The element selector can be used to add a border to all <div> elements.

    Example

    The following example adds a border to all <div> elements.

    <!DOCTYPE html><html lang="en"><head><title>Element Selectors</title><style>
    
        div {
            border: 3px solid #2a9d8f;
            padding: 10px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div&gt;Welcome to CSS Element Selectors&lt;/div&gt;&lt;div&gt;This is div 1&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Using Element Selectors Setting Font

    To apply a font style to all <li> elements, use the element selector.

    Example

    This example sets the font size and font family for all <li> elements.

    <!DOCTYPE html><html lang="en"><head><title>Element Selectors</title><style>
    
        li {
            font-size: 18px;
            font-family: Verdana, sans-serif;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Welcome to Tutorials Point&lt;/h2&gt;&lt;ul&gt;&lt;li&gt;HTML&lt;/li&gt;&lt;li&gt;CSS&lt;/li&gt;&lt;li&gt;Javascript&lt;/li&gt;&lt;/ul&gt;&lt;/body&gt;&lt;/html&gt;</pre>

  • Child Selectors

    Child Selectors in CSS

    CSS child selector selects all the direct children of a particular element. This is denoted by ‘>’ (Greater than) symbol. It will not select elements that are further nested inside the child elements.

    Syntax

    The syntax for CSS child selectors is as follows:

    selector > child-selector{/* property: value; */color: #04af2f
    }

    Child Selectors Example

    In this example, we are changing the text color of the direct child element of div element. The para element inside the span tag remains unchanged as it is not a direct child of the div element.

    <!DOCTYPE html><html lang="en"><head><title>Document</title><style>
    
        .myDiv &gt; p {
            color: #04af2f;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="myDiv"&gt;&lt;p&gt;It is a paragraph element. Direct child of div element.&lt;/p&gt;&lt;span&gt;&lt;p&gt;This is a p element inside div but not direct child of div.&lt;/p&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;This is a p element outside div.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Example 2

    In this example, we are changing the font size and font of the direct child element of the div element.

    <!DOCTYPE html><html lang="en"><head><title>Document</title><style>
    
        .myDiv &gt; p {
            border: 2px solid #031926;
            font-size: 20px;
            font-family: Verdana, sans-serif;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="myDiv"&gt;&lt;p&gt;It is a paragraph element. Direct child of div element.&lt;/p&gt;&lt;span&gt;&lt;p&gt;This is a p element inside div but not direct child of div.&lt;/p&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;This is a p element outside div.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

  • Class Selectors

    Class Selectors in CSS

    CSS class selectors are used to select elements with a specific class attribute. The class selector is defined using a period (.) followed by the class name.

    Syntax

    The syntax for CSS class selectors is as follows:

    .classname{/* property: value; */background-color: #04af2f;}

    Basic Class Selector

    To apply a background-color and text color to elements with a specific class, we have used a basic class selector.

    Example

    The following example applies the background color and text color to all elements with the class container.

    <!DOCTYPE html><html lang="en"><head><title>Class Selectors</title><style>
    
        .container {
            background-color: #04af2f;
            color: white;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p class="container"&gt;This is a paragraph with class.&lt;/p&gt;&lt;p&gt;This is a paragraph without class&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Multiple Class Selectors

    We can use multiple classes with any element. All the classes are separated by dot. It works only when all the class name matches.

    Example

    The following example adds a border and background color to div 1 and div 2 remains with default styles.

    <!DOCTYPE html><html lang="en"><head><title>Document</title><style>
    
        .bdr.bgColor {            
            border: 2px solid black;
            background-color: #04af2f;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="bdr bgColor"&gt;
        This is div with background color and a border.
    &lt;/div&gt;&lt;br&gt;&lt;div class="bdr"&gt;
        This is div with default styles.
    &lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Combining Class Selector with Other Selectors

    Class selectors can be combined with other selectors to select specific elements.

    Example

    This example sets the font size and font family for only p elements having a class name container.

    <!DOCTYPE html><html lang="en"><head><title>Document</title><style>
    
        p.container {
            font-size: 16px;
            font-family: Verdana, sans-serif;
            color: #04af2f;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p class="container"&gt;This is a styled paragraph element.&lt;/p&gt;&lt;div class="container"&gt;This is a div without any style.&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Combining Multiple Classes

    We can use multiple classes on any element to add different styles using different classes.

    Example

    Here is an example where we have used two different classes on div element to add a border and background color.

    <!DOCTYPE html><html lang="en"><head><title>Document</title><style>
    
        .bdr {
            border: 1px solid black;
            font-size: 16px;
            font-family: Verdana, sans-serif;
        }
        .bgColor {
            background-color: #04af2f;
            color: white
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="bdr bgColor"&gt;
        This is a div with a border and background color.
    &lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Using Class Selectors with Media Queries

    We can create a responsive design by using class selectors with media queries.

    Example

    Here is an example where the background color of the web changes depending on the screen size.

    <html><head><style>
    
        .bgColor {
            background-color: rgb(96, 5, 26);
            color: white;
        }
        @media (max-width: 800px) {
            .bgColor {
                background-color: #04af2f;
                color: white;
            }
        }
        @media (max-width: 700px) {
            .bgColor {
                background-color: #031926;
                color: white;
            }
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p class="bgColor"&gt;
        Try different screen sizes to see 
        change in background color.
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

  • Group Selectors

    Group Selectors in CSS

    CSS group selector applies the same style to multiple elements at a time. The names of elements can be separated by commas. Group selector keeps CSS concise and avoids redundancy.

    Syntax

    The syntax for the CSS group selector is as follows:

    #selector_1, .selector_2, selector_3{/* property: value; */color: #04af2f
    }

    CSS Group Selectors Example

    In this example, we have used the same style on all three elements using group selectors.

    <!DOCTYPE html><html lang="en"><head><title>Document</title><style>
    
        span, #para, .myClass {
            background-color: #04af2f;
            color: white;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p id="para"&gt;This is a paragraph element.&lt;/p&gt;&lt;div class="myClass"&gt;This is a div element.&lt;/div&gt;&lt;br&gt;&lt;span&gt;This is a span element.&lt;/span&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Group Selectors Example with Pseudo-classes

    In this example, we have used pseudo classes with group selectors. CSS :active pseudo-class changes the text color of the link and button on clicking while CSS :hover pseudo-class changes the background-color of the div and p element.

    <!DOCTYPE html><html lang="en"><head><title>Document</title><style>
    
        .link:active, #btn:active {
            color: #04af2f;
        }
        .myDiv:hover, #para:hover {
            background-color: #031926;
            color: white;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="myDiv"&gt;
        Hover over me
    &lt;/div&gt;&lt;p id="para"&gt;This is a paragraph.&lt;/p&gt;&lt;a class="link" href="#"&gt;Click on this link.&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;button id="btn"&gt;Click me&lt;/button&gt;&lt;/body&gt;&lt;/html&gt;</pre>