You are currently viewing JavaScript Cheat Sheet 11.1.6: CSS Styles with jQuery

JavaScript Cheat Sheet 11.1.6: CSS Styles with jQuery

CSS Styles with jQuery


In this tutorial, you will learn the basics of programming using JavaScript, focusing specifically on manipulating CSS styles using jQuery. JavaScript is a versatile programming language commonly used for web development, and jQuery is a popular JavaScript library that simplifies HTML document traversing, event handling, and animation. Understanding how to manipulate CSS styles using jQuery is essential for building dynamic and interactive web applications.

1. Introduction to jQuery


jQuery is a fast, small, and feature-rich JavaScript library. It simplifies tasks like HTML document traversal and manipulation, event handling, and animation. It is designed to make JavaScript coding easier and more efficient.

To use jQuery, you need to include the jQuery library in your HTML document. You can do this by adding the following script tag within the <head> section of your HTML document:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

2. Selecting Elements


jQuery allows you to select elements from the HTML document using CSS-style selectors. Once selected, you can manipulate these elements in various ways, including changing their CSS styles.

To select an element using jQuery, you can use the $() function followed by a CSS-style selector. For example, to select an element with the id “myElement”, you can use:

$('#myElement')

3. Manipulating CSS Styles


Once you’ve selected an element using jQuery, you can manipulate its CSS styles using the css() function. This function takes two parameters: the CSS property you want to change and the new value for that property.

For example, to change the background color of an element with the id “myElement” to red, you can use:

$('#myElement').css('background-color', 'red');

4. Applying Effects


jQuery provides various methods for applying effects to elements, such as fading in/out, sliding up/down, and animating CSS properties. These effects can enhance the interactivity and visual appeal of your web application.

For example, to fade out an element with the id “myElement” over a duration of 500 milliseconds, you can use:

$('#myElement').fadeOut(500);

5. Sample Code and Output Scenarios

Scenario 1: Changing Text Color

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery CSS Manipulation</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <style>
        .text {
            color: blue;
        }
    </style>
</head>
<body>
    <p class="text">Hello, World!</p>
    <button onclick="changeTextColor()">Change Text Color</button>

    <script>
        function changeTextColor() {
            $('.text').css('color', 'red');
        }
    </script>
</body>
</html>

Scenario 2: Fading Out an Image

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery CSS Manipulation</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <style>
        img {
            width: 200px;
        }
    </style>
</head>
<body>
    <img src="image.jpg" alt="Sample Image">
    <button onclick="fadeOutImage()">Fade Out Image</button>

    <script>
        function fadeOutImage() {
            $('img').fadeOut(1000); // Fade out over 1 second (1000 milliseconds)
        }
    </script>
</body>
</html>

Explanation:

  • In Scenario 1, we have a paragraph element with the class “text”. When the button is clicked, the changeTextColor() function is called, which selects all elements with the class “text” using jQuery and changes their text color to red.
  • In Scenario 2, we have an image element. When the button is clicked, the fadeOutImage() function is called, which selects the image element using jQuery and fades it out over a duration of 1000 milliseconds (1 second).

These scenarios demonstrate basic CSS manipulation and effects using jQuery, allowing you to dynamically modify the appearance of elements on your web page.

Leave a Reply