Is there a way to design a webpage with a static header and sidebar that adjusts to different screen sizes using just HTML and CSS?

I've recently delved into the world of html/css, teaching myself the basics over the past few days. While I know that alternatives like javascript exist, I want to fully explore the capabilities and limitations of html/css before moving on. I have managed to achieve what I need so far, but I'm curious if there's a more efficient or cleaner way to do it. I have deliberately avoided using fixed widths or heights to ensure complete responsiveness. The only method I've discovered to maintain the fixed positioning of the header and side nav without the main content scrolling underneath them by default is by inserting duplicate filler content directly beneath the fixed positioned elements to preserve the correct spacings.

* {box-sizing:border-box;}

.row::after { content: "";
              clear: both;
              display: block;}
    
[class*="col-"] {float: left;
                 padding: 15px;}

.col-3 {width: 25%;}

.col-6 {width: 50%;}

body {margin:0;}

.header-navbar { position: fixed;
                 width:100%;}

.header {background-color:white;
          font-size:40px;}

.navbar {background-color:black;
         text-align:right;}

.navbar a {text-decoration:none;
           color:white;
   font-size:20px;
   display:inline-block;
   padding:15px;}
   
.navbar a:hover {background-color:red;}   

.title-sidenav {position:fixed;
                background-color:white;}

.sidenav a {display:block;
            text-decoration:none;
font-size:20px;
color:white;
background-color:black;
padding:15px;
border-radius:10px;
border:1px solid white;}

.sidenav a:hover {background-color:red;}

.main {border-left:1px dotted black;
       border-right:1px dotted black}

.footer {text-align:center;
         background-color:black;
 color:white;}


@media only screen and (max-width: 768px) {[class*="col-"] {width:100%;}
                                            .navbar {text-align:center;}
.sidenav {text-align:center;}
                                            .sidenav a{display:inline-block;}}

Answer №1

When dealing with fixed content, it's essential to specify a z-index if you want it to appear above other content on the page.

If it seems like your content is extending beyond the page when you reduce filler content to just one link, it may be because the filler content is covering up the fixed content.

To resolve this issue, try setting the position of the navbar to relative and assigning a higher z-index value to the fixed navbar (e.g., 5) and a lower z-index value to the filler navbar (e.g., 1). This should ensure that your content remains visible on top.

While it's possible to achieve the desired effect using JavaScript, this approach involves duplicating code and can become hard to maintain. A simpler alternative would be to use JS to determine the height of the content and then add a margin-top to the body content equal to that height. Alternatively, consider initially having the content unfixed and applying a fixed position only when needed via JS.

In conclusion, although it's technically feasible to implement your desired design, relying on duplicated code can lead to accessibility issues and create unnecessary maintenance challenges.

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

What is the best way to ensure that this <span> maintains a consistent width, no matter what content is placed inside

So here's the deal, I've got some dynamically generated html going on where I'm assigning 1-6 scaled svgs as children of a . The span is inline with 2 other spans to give it that nice layout: https://i.sstatic.net/mySYe.png I want these "b ...

Display a pair of divs by utilizing the select option tag element alongside a submit button

Yesterday, I posted a similar question in a different format. Today, I am looking for the same question in a new format. Let me explain: I have several div elements on a page. When I choose an option from a dropdown menu and click submit, I want to disp ...

Tips for maintaining the dropdown selection when new rows or columns are added to a table

I have a task requirement where I must generate a dynamic table using jQuery. I've successfully implemented adding dynamic columns or rows to the table. Feel free to check out the fiddle code here. HTML: <div id='input_div' name='i ...

Place JavaScript buttons within a form element without triggering form submission upon clicking the buttons

Struggling to find the right words, but I'll give it a shot. I have a PHP form with multiple fields, including a textarea where I store some PHP values. I wanted to enhance the appearance of the PHP values by adding a beautifier with CSS styling. Ever ...

The website no longer uses AJAX calls and does not display any errors, although it used to do so in the past

I've noticed that my website is no longer making the ajax call it used to make before I made some changes to my code. However, I can't seem to figure out what exactly I changed. The function call appears to be correct as it triggers the initial a ...

My custom Wordpress template experiences a hiccup with the jQuery slideshow, as it unexpectedly

Looking for help with a slideshow issue. Initially, it loads and transitions smoothly into one picture, but then stops working. I decided to use Chrome to troubleshoot the problem, and it informed me that it doesn't recognize the slideSwitch function ...

Exploring jQuery: Moving between different table cells using traversal techniques

Seeking assistance from experienced jQuery users as I am new to this library and may not be approaching this task correctly. I have a dynamically generated HTML table that is quite extensive. To enhance user experience, I aim to assign navigation function ...

Transferring data from the Server-Side dataTable to a modal | Creating a personalized row button for dataTable

I have integrated a tutorial from datatables.net into my test page, which can be found at this link: . However, I am facing an issue where I am unable to pass selected data into a modal, and the modal does not open at all. EDIT: Here is the code snippet f ...

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 ...

What could be causing the malfunction of this Bootstrap button dropdown?

Initially, I attempted using regular HTML for the dropdown button but encountered issues. As a result, I switched to jsfiddle to troubleshoot. Despite my efforts, the dropdown feature still refused to work. If you'd like to take a closer look, here&a ...

AngularJS: Utilize the ngStyle directive to add styling to a specific child

I'm trying to style an embedded svg file within a span using ng-style based on its index. Can anyone help me figure out how to do this? <li ng-repeat="menuItem in menuItems" class="menuitem" ng-class="{'smenuitem': ($index === menuselect ...

What is the best way to display a success notification when a transaction is successfully executed in web SQL

var brand = "Glare"; var price = "3000$"; var color = "blue"; var success = tx.executeSql("INSERT INTO truck (brand, price, color) VALUES ('"+brand+"','"+price+"','"+color+"' ")"); if(success) { alert("successfully insert ...

Ways to alter the color of a link after clicking it?

Is there a way to change the link color when clicking on it? I have tried multiple approaches with no success. The links on this page are ajax-based and the request action is ongoing. <div class="topheading-right"> <span> ...

Using HTML to execute a JavaScript function that transforms images

The script cutImageUp has been previously discussed on SE in a thread about Shattering image using canvas. However, I have encountered a different issue while attempting to use it. The HTML code I implemented seems to be ineffective, and despite my efforts ...

Using footable js will eliminate all form elements within the table

After incorporating the Footable jQuery library to create a responsive table, I encountered an issue with input tags and select boxes being removed by the Footable JavaScript. It turns all these tags into empty <td> elements. <table id="accordi ...

When using the `:hover` pseudoclass, it appears that it does not apply to the `<p>` element, but it does work

While hovering over the <a>, I noticed that it changes color correctly. However, when I hover over the <p>, nothing seems to happen. It's puzzling why the <p> does not change color upon being hovered over. If I switch the selector t ...

Is it possible to activate numerous hover effects on links by hovering over a div?

How can I trigger multiple hover effects simultaneously when hovering over my main div? I'm struggling to figure out how to make the line animate on all three links at the same time. Is it possible to achieve this using only CSS or do I need to use Ja ...

Learn how to save user input from form fields and text areas into JSON format using Angular

Is there a way to add comments using Angular when a user clicks on a button? Currently, whenever text is entered in the input field or textarea, it disappears and an empty block without any name, country, and comments is displayed. The entered text should ...

Issue with Laravel Blade: HTML elements are not being displayed in my blade template file

After implementing WYSIWYG fields in my form, I noticed that the html tags are not being properly rendered upon retrieving the data. The HTML tags still appear in their raw form within the form itself. Attempting solutions found on this thread, I believed ...

The attempt to enhance the appearance of the string table has been unsuccessful

In a jQuery call, I am working with a string of HTML as shown below: var template = '<html >' + '<head>' + '<h1>Most important heading here</h1>'+ '</head>' + '<body>' ...