Learn how to make a menu bar initially collapsed by setting a checkbox to default checked using HTML and CSS

Is there a way to have a checkbox automatically checked when the page loads, causing a menu bar to be initially hidden? How can this be achieved using HTML and CSS?

Below is my current HTML and CSS code:

header #toggler:checked ~ .navbar{
    clip-path:polygon(0 0, 100% 0, 100% 0, 0 0);
}

header .navbar a{
    display: block;
}
<!DOCTYPE html>

<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>The CheeseCake Shop</title>
        <link rel="stylesheet" href="style.css">
    </head>

    <body>

            <header>
                <input type="checkbox" name="" id="toggler" checked>
                <label for="toggler" class="fas fa-bars"></label>
                <nav class="navbar">
                    <a href="#home">Home</a>
                    <a href="#about">About</a>
                    <a href="#products">Products</a>
                    <a href="#contact">Contact</a>
                </nav>
            </header>

    </body>

</html>

Answer №1

Ensure that you include the attribute checked=checked in your checkbox HTML element to have it loaded as checked when the page loads.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adjusting the position of keyframes on an element

<div id="carousel"> <figure> <img src="http://www.codehub.com/css-carousel/images/slides/a.png" /> <img src="http://www.codehub.com/css-carousel/images/slides/b.png" /> <img src="http://www.codehub.com ...

When both the container and the element have min-height specified in percentages, CSS min-height may not be effective

Check out this simple markup: <div class="fixh"> <div class="outer"> <div class="inner"> inner </div> </div> </div> I am trying to ensure that the inner block has a minimum height ...

The alignment of Bootstrap Select dropup with search results is off

I've encountered an alignment issue with bootstrap select live search (selectpicker). The dropup menu is misaligned when showing search results, but strangely, the alignment corrects itself after scrolling the page. Interestingly, this issue doesn&apo ...

Exploring the intricacies of Bootstrap 4 media queries through pixel widths

I am currently working on developing a web app using Bootstrap 4 as the front-end framework. The app is primarily designed for desktop use, but I have made certain views and menus adaptable to mobile devices. However, I have encountered an issue where the ...

Toggle multiple buttons based on the data fetched in the created() lifecycle hook

I have several toggle buttons that should be selected based on the values present in the response obtained through the created() method. <li> <input v-on:click="toggleCheckbox($event)" ...

Guide on accessing the navigation bar through Selenium WebDriver

I am trying to automate clicking on a specific part of the navigation bar using Selenium WebDriver. Here is my HTML code: <div class="navbar-inner"> <div class="container"> <a class="navbar-brand " ui-sref="home" hr ...

The React Basic Application runs smoothly when tested locally, however, in Azure, there seems to be an issue with loading the .CSS, .I

I have encountered an issue with my single web app where it functions well locally and deploys to Azure seamlessly via a release pipeline. However, after deployment, the .css file is not loading on the browser and returns a 404 error. The index.html file c ...

Issue with JavaScript ScrollTop

Simply put, I am trying to determine the total scroll size for a text area in a unit that scrollTop can align with. However, I am at a loss on how to do so. I've tried scrollHeight and various other methods without success. Any advice or suggestions ...

Using both CakePHP and jQuery to create a semicircle chart

I need a pie chart that visually represents two dynamic values. When the red value is 90%, the pie chart should be filled with red color up to 90% and the remaining 10% should be filled with green color. I also require the pointer to change dynamically b ...

Enhancing the functionality of the onclick function within the ng-repeat by incorporating a scope

I am looking to pass a parameter to a function that is called via onclick within an ng-repeat: <!-- Sliding bar (bottom) --> <div ng-show="currentSVG && currentLanguage && currentWidth && bannerHeight" pageslide ps-open=" ...

The issue with the CSS combination of :after and :hover:after across different HTML elements

Encountering an issue while attempting to create a rollover effect using CSS :after and :hover pseudo-elements. Check out the clock and facebook icons on the right side by visiting: See below for the CSS code: .icon { background: url('. ...

Div with a fixed position that is displayed on top of another following div

Having trouble with my code on this fiddle: https://jsfiddle.net/0vau6psp/. It seems to be related to the position:fixed property, but I can't seem to solve it. The issue is that the header is overlapping the main content, making it invisible. I want ...

Background image remains unaffected by HTML

I'm looking to update the look of a website by changing the background color to a picture. The CSS code I am using is as follows: .bg { opacity: 5; background-repeat: no-repeat; background-size: cover; background-position: 50% 100%; ...

What is the method for HTML inline handlers to retrieve the global window object and the variables contained within it?

During my coding test, I encountered an interesting scenario. I had a function called write and used a button with an inline onclick handler to trigger the write() function. function write(text) { alert(text) } <button onclick='write("Some tex ...

Tips for successfully implementing CSS animations in EmberHere are some helpful ways to

I am currently using the INSPINIA administrative theme and have a question that extends from a previous one regarding installing bootstrap 4 template on an Ember web project. My query is related to getting CSS animations to work properly in Ember. I am t ...

The TipTap Editor does not properly register spaces

In our project, we are utilizing the TipTap rich text editor. However, we are encountering an issue where spaces are not being recognized correctly - a space is only created after every 2 clicks. Our framework of choice is Vue.JS. import { Editor, EditorC ...

the background image fails to appear in the div elements when using mobile jQuery

After setting a background image for a mobile page using mobile.jquery, I encountered an issue. When linking to the cdn version of the jquery css file: <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.css" /& ...

What is the best way to left-align numbers in a table?

https://jsfiddle.net/6a7d2snj/ I need help aligning numbers to the left within a table column: 50 5 -40 -4 How can I achieve this? Using style="align:right" aligns the numbers on the right, but my goal is to have them aligned to the left! ...

Navigate to the subsequent frame once the HTML5 video finishes playing

I have created a basic HTML webpage with a fullscreen background video (not set to loop) using the Vide plugin. Everything is working well, but I want the video to pause on the last frame to showcase a logo I have included at the end. This functionality w ...

JavaScript: Creating Custom IDs for Element Generation

I've been developing a jeopardy-style web application and I have a feature where users can create multiple teams with custom names. HTML <!--Score Boards--> <div id="teamBoards"> <div id="teams"> ...