HTML5 and CSS Menu: Displaying and concealing items based on screen size

When the screen size is 320px, I want to hide the menu. Currently, the menu is always hidden and I need to call it.

It should be visible by default, but when the screen size is 320px, #main-nav needs to be called.


    * {
    box-sizing: border-box;
}

html, body {
    min-height: 100%;
    font-size: 11pt;
    font-family: 'PT Sans', Arial, sans-serif;
}
// Additional text
.main-header {
    background: linear-gradient(#3F94BF, #246485);
    padding: 5px;
    text-align: center;
    color: white;
    text-shadow: #222 0px -1px 1px;
    position: relative;
    width: 100%;
    transition: all 0.3s ease;
}

.page-wrap {
    float: right;
    width: 100%;
    transition: width 0.3s ease;
}

nav a {
    color: #fff;
    display: block;
    text-align: center;
    text-decoration: none;
    line-height: 40px;
    text-shadow: 1px 1px 0px #283744;
}

nav a:hover, nav a:active {
    background-color: #8c99a4;
}

.main-nav {
    position: fixed;
    width: 0;
    height: 100%;
    font-weight: bold;
    background: linear-gradient(#3F94BF, #246485);
    overflow: hidden;
    transition: width 0.3s ease;
}

.content {
    overflow: hidden;
    padding: 20px 20px 20px 20px;
}

#close-menu {
    display: none;
}

#open-menu {
        display: block;
    }

#main-nav:target {
    width: 20%;
}

#main-nav:target + .page-wrap {
    width: 80%;

    .open-menu {
        display: block;
    }
    .close-menu {
        display: none;
    }
    .main-header {
        width: 80%;
        left: 20%;
    }
}

html

<html>
    <head>
        <title>Test</title>
        <meta http-equiv="Content-Language" content="de" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" type="text/css" href="css/main.css" media="screen" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

    </head>

    <body>
        <nav class="main-nav" id="main-nav">
            <a href="#details">Details</a>
            <a href="#adresses">Adresses</a>
            <a href="#kontakt">Kontakt</a>
        </nav>

        <div class="page-wrap">

            <header class="main-header">
                <a href="#main-nav" class="open-menu"> ☰</a>
                <a href="#close-menu" class="close-menu">close</a>

                <h1>VIGOUI</h1>
            </header>

            <div class="content">
                It’s all about playing four quarters. I think we played well but the other team played well too. They took advantage of certain circumstances that arose. It’s a physical game.
            </div>

        </div>
    </body>
</html>

Answer №1

Begin by implementing the media query (http://jsfiddle.net/3X8Tq/)

@media all and (min-width: 320px) {
    #main-nav {
        width: 20%;
    }
    .page-wrap {
        width: 80%;
    }
}

The current setup functions well for small screens, however, there is an issue with the close button on larger displays. The close button never actually functioned properly as the CSS only targets the open :target element. To remedy this, we need to make adjustments to ensure the close button works correctly. Although there are no existing IDs for open-menu or close-menu on the page, the necessary CSS is present.

Note that the HREF #main-nav pertains to the navigation section, but there is absence of an #close-menu ID. Let's introduce one.

<div id="close-menu"></div>

This new ID should be placed before the nav section. Additionally, we need to incorporate CSS rules for #close-menu

#close-menu:target + #main-nav {
    width: 0;
}
#close-menu:target ~ .page-wrap {
    width: 100%;
    left: 0;
}

Resolution: http://jsfiddle.net/3X8Tq/1/

Upon examining your CSS code, it appears there may be a misconception regarding its functionality. The subclasses indicated in your CSS only operate with the less css framework. Notably, the elements #close-menu and #open-menu are not present within the webpage. I recommend studying the concepts of :target and CSS selectors in general for further clarity.

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

The Ajax sign up request for the Express route is failing with a POST error code 404 at http://localhost:300

When I make an Ajax post to my express /signup/ route, I receive a POST 404 error for http://localhost:3004/signup/. Any assistance would be greatly appreciated as I am new to node! server.js const pg = require('pg') const bodyParser = require( ...

The Silent Disregard for CSS Files in React Rendered Pages

I'm currently working on a React application that I created using create-react-app. In my index.html file, I have linked it to my CSS file like this: <link rel="stylesheet" href="../src/site.css"></link> The content of the site.css file ...

Displaying a SQL table with AJAX based on radio button selection

I am facing an issue with my webpage where I have three radio buttons and an empty table. The objective is to select a radio button and display a list of employees in the table based on the selection. However, the list does not appear when I click the butt ...

How to properly center and align the width of a figcaption element within an image placed in a figure element

I've been struggling for two days now trying to fix an issue with the alignment of images and captions on my Django application. In my app, users can upload images, and I'm using figure and figcaption tags to display the image with a caption. Ho ...

What is the best way to create a self-referencing <div> in HTML?

After extensive research, I turn to seeking advice and guidance on Stack Exchange. I have a seemingly straightforward goal in mind. I want to create a <div> id/class that will automatically generate a link to itself using scripting of some sort. Be ...

Overlap and cover additional elements within a DIV

I am looking to create a versatile function that can effortlessly overlay various elements such as selects, textfields, inputs, divs, tables, and more with a partially transparent div matching their exact height and width. I have managed to obtain the pos ...

Translate data from two "contact form 7" date fields into JavaScript/jQuery to display the date range between them

NEW UPDATE: I was able to get it working, but the code is a bit messy. If anyone can help me clean it up, I would greatly appreciate it. <div class="column one-fourth" id="date-start">[date* date-start date-format:dd/mm/yy min:today+1days placeholde ...

Safari experiences a decrease in speed when multiple checkbox elements are present

While using Safari, I have encountered a problem. The performance of interacting with a text input is significantly affected when there are numerous type="checkbox" elements on the page. This issue appears to be more pronounced in Safari compared to Chrom ...

How can I prevent placeholder text from being included when submitting a form using serialize()?

After submitting my form, I save the data using an $.ajax function with $("#formName").serialize() call. One issue I'm facing is that the placeholder text gets submitted along with the serialize() action. I have a function in place to reset the plac ...

Populating datagrid columns with database information post page load

I am currently facing an issue with a web page where loading additional data from an SQL query into a datagrid is significantly slowing down the performance. Despite ensuring that all necessary database indices are in place, the query now takes 3-4 seconds ...

Using z-index to position a paragraph element behind other elements in a webpage

I am facing a challenge where I want to hide a paragraph within an element, and layer it behind another element. Despite my attempts with z-index, I have been unsuccessful in achieving this effect between the elements. Could someone shed some light on why ...

Can you provide instructions on creating a marker like this in an unordered list?

Is there a way to create a bold line like this within a bulleted list easily? https://i.sstatic.net/pw8Zt.png I've tried using the border property, but it's not showing up correctly. Any quick and simple solutions for achieving this effect? Th ...

Cropping and resizing images

Within my angular application, I have successfully implemented image upload and preview functionality using the following code: HTML: <input type='file' (change)="readUrl($event)"> <img [src]="url"> TS: readUrl(event:any) { if ...

How to Override Global CSS in a Freshly Created Angular Component

My CSS skills are a bit rusty and I need some assistance with a project I'm working on. The project includes multiple global CSS files that have properties defined for different tags, such as .btn. However, these global CSS files are causing conflicts ...

Developing web applications using Express.js and Jade involves connecting CSS stylesheets

I'm currently in the process of developing a web application using Express.js along with some Bootstrap templates. I utilized jade2html for conversion, and while the page loads successfully, it lacks any styling. The following code snippet is what I ...

What is the best way to evenly divide divs vertically on a Bootstrap website?

I am currently working on a responsive page design and have come across this useful resource: http://www.bootply.com/2sfiCehqac My main objective is to ensure that when the screen size is medium or large: 1) div_left and div_right (orange and blue) adjus ...

Difficulty with Jquery's random selection of grid divs

I am working on a grid layout consisting of 9 divs nested in columns of three. The objective is that when the main grid (with ID #left) is clicked, two divs from the middle row (row="1") should have the class .show randomly applied to them. In the column w ...

Is there a way for me to incorporate a for loop in this situation?

Greetings! I am currently retrieving data via ajax in JSON format using the following code: $.ajax({ async:true, type: "POST", dataType: "html", contentType: "application/x-www-form-urlencoded", url:"http://sosacelulares.com/ ...

Retrieve only the initial tag content using jquery

My goal is to extract the "22" from the following code... <div class="left"> <a class="count-link" href="http://url1.com"> <span>22</span> users </a> <a class="count-link" href="http://url2.com"> <span>10</span ...

Store the image URL in cache during AJAX loading

I have implemented an ajax slider to display images, and it is functioning perfectly. However, I am facing an issue with image caching. Since the images change dynamically using ajax, there is no cache available which causes a delay in displaying the new i ...