Why card images use data-src (not src) for image in Bootstrap 4 ?

Bootstrap 4 card images use src in image tags to providing the location to be loaded in the given image tag and data-src to provide additional information that can be used by JavaScript for improving the users’ experience. For data-src and src tag we can conclude as follows:



Below example illustrates the above concept:

Example: The below code can be seen how src attribute is used to provide a link to image and data-src can be used to provide additional information to JavaScript like here it was used to change the original src link for the image with one in the data-src attribute.





<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8" />
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible"
        content="ie=edge" />
        Why card images use data-src (not src)
        for image in Bootstrap 4 ?
    <link rel="stylesheet"
        crossorigin="anonymous" />
    <script src=
    <script src=
    <script src=
        h1 {
            color: green;
        .second {
            float: right;
            margin: 20px;
        .first {
            float: left;
            margin: 20px;
        <b>A Computer Science Portal for Geeks</b>
    <div class="container">
        <div class="card first" style="width: 18rem;">
            <img class="card-img-top" src=
                alt="Card image cap" />
            <div class="card-body">
                <h5 class="card-title">Courses</h5>
                <p class="card-text">
                    Attend the courses Increase
                    the probability
                <a href="#" class="btn btn-primary">Attend</a>
        <!-- It loaded with JavaScript to allow
            lazy loading of images -->
        <div class="card second" style="width: 18rem;">
            <img id="img1" class="card-img-top"
                alt="Card image cap" />
            <div class="card-body">
                <h5 class="card-title">Interview</h5>
                <p class="card-text">
                    Prepare your self for the Interview
                <a href="#" class="btn btn-primary">Attend</a>
        var ele = document.getElementById("img1");
        // Switch the image to the URL
        // specified in data-src
        ele.src = ele.dataset.src;


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Akanksha_Rai

Article Tags :