Open In App

Bootstrap 5 Floating labels Selects

Last Updated : 31 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

The Bootstrap 5  Floating labels Component Selects is used to give a floating label to input fields. It also works with the HTML 5 <select> component. When we apply a floating label to the select component the label will always be shown in the floating state irrespective of the state of the select. The floating labels do not work with the size and multiple variations of the select element. However, they support the disabled select element.

Bootstrap 5  Floating labels Component Selects classes:

  • form-select: This class is used to trigger the select option styles of Bootstrap 5.

Bootstrap 5  Floating labels Component Selects attributes:

  • selected: This attribute is used to select an option from the select menu by default.
  • for='floatingSelect': This attribute is used to show a floating label to the select. This attribute works only with the selects.

Syntax:

<div class="form-floating">
<select id="gfg" class="form-select">
...
</select>
<label for="gfg">Floating label</label>
</div>

Example 1: This is a basic example showing a floating label for the select element.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Enhanced Company Selector</title>
    <link href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" 
          rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
        }

        .container {
            max-width: 500px;
            margin-top: 50px;
        }

        .form-floating {
            margin-top: 20px;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1 class="text-primary mb-4">GeeksforGeeks</h1>
        <h2 class="text-secondary mb-4">Bootstrap 5 Floating Labels Select</h2>

        <div class="form-floating">
            <select id="companySelect" class="form-select">
                <option value="" selected disabled>Choose a company</option>
                <option value="google">Google</option>
                <option value="apple">Apple</option>
                <option value="microsoft">Microsoft</option>
                <option value="meta">Meta</option>
                <option value="netflix">Netflix</option>
            </select>
            <label for="companySelect">Select a Company</label>
        </div>

        <div id="companyInfo" class="mt-4 p-3 bg-light rounded d-none">
            <h3 id="companyName" class="text-info"></h3>
            <p id="companyDescription"></p>
        </div>
    </div>

    <script src=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        const companySelect = document.getElementById('companySelect');
        const companyInfo = document.getElementById('companyInfo');
        const companyName = document.getElementById('companyName');
        const companyDescription = document.getElementById('companyDescription');

        const companyData = {
            google: "Google is a multinational technology company specializing in internet-related services and products.",
            apple: "Apple is a technology company known for its consumer electronics, software, and online services.",
            microsoft: "Microsoft is a multinational technology corporation producing computer software, consumer electronics, and related services.",
            meta: "Meta, formerly Facebook, is a technology company focused on social networking services and products.",
            netflix: "Netflix is a streaming service offering a wide variety of award-winning TV shows, movies, anime, documentaries, and more."
        };

        companySelect.addEventListener('change', function () {
            const selectedCompany = this.value;
            if (selectedCompany) {
                companyName.textContent = 
                selectedCompany.charAt(0).toUpperCase() + selectedCompany.slice(1);
                companyDescription.textContent = 
                companyData[selectedCompany];
                companyInfo.classList.remove('d-none');
            } else {
                companyInfo.classList.add('d-none');
            }
        });
    </script>
</body>

</html>

Output:

Example 2: In this example, the floating label is shown for the disabled select element.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Enhanced Floating Label Select</title>
    <link href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" 
          rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
        }
        .container {
            max-width: 500px;
            margin-top: 50px;
        }
        .form-floating {
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="text-primary mb-4">GeeksforGeeks</h1>
        <h2 class="text-secondary mb-4">Bootstrap 5 Floating Labels Select</h2>
        
        <div class="card shadow-sm">
            <div class="card-body">
                <h3 class="card-title text-info mb-4">
                  Floating label with Disabled Select Element
                </h3>
                
                <div class="form-floating">
                    <select id="carSelect" 
                            class="form-select" 
                            aria-label="Select a car" disabled>
                        <option value="" selected disabled>Select a Car</option>
                        <option value="dezire">Dezire</option>
                        <option value="ertiga">Ertiga</option>
                        <option value="scorpio">Scorpio</option>
                        <option value="brezza">Brezza</option>
                    </select>
                    <label for="carSelect">Choose your car</label>
                </div>
                
                <button id="toggleButton" class="btn btn-primary">
                  Enable Selection
                </button>
            </div>
        </div>
    </div>

    <script src=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js">
    </script>
    <script>
        document.getElementById('toggleButton').addEventListener('click', function() {
            let select = document.getElementById('carSelect');
            select.disabled = !select.disabled;
            this.textContent = 
              select.disabled ? 'Enable Selection' : 'Disable Selection';
        });
    </script>
</body>
</html>

Output:

Reference: https://getbootstrap.com/docs/5.2/forms/floating-labels/#selects


Next Article

Similar Reads