Creating a Bootstrap header to counteract a collapsible sidebar

Utilizing bootstrap 4, I have successfully implemented a partially collapsible sidebar that is functioning as intended.

However, my current challenge lies in incorporating a top header/nav bar that includes the toggle for collapsing the sidebar, and on the right side of the navbar, I aim to include a user icon for login/logout functionalities.

To address this, I have created a header named #userHeader, which expands the width of the page and accommodates the toggle (working well so far). The issue arises when using the 100vw property for the header, resulting in excessive width that extends off the page, obscuring the icon.

My query is: How can I create a responsive header that dynamically adjusts its width based on the remaining space left by the sidebar?

In simpler terms, when the sidebar occupies 15% of the page when expanded and 5% when collapsed, how can the header adapt accordingly while keeping the user icon fixed to the right side of the page?

The specific HTML block under consideration:

#userHeader {
  width: 100vw;
  height: 40px;
  background-color: red;
  position: fixed;
}

#userHeader i {
  color: #FFFFFF;
  float: right;
}
<div id="secondHeader">
  <!--Note: I have no styling for this id yet-->
  <div id="userHeader">
    <button type="button" id="sidebarCollapse" class=".btn">
<span></span>
<span></span>
<span></span>
</button>
    <i id="user" class="far fa-user-circle fa-2x"></i>
  </div>
</div>

For a live demonstration of the code, please refer to the Codepen link provided below. Please note that only essential HTML elements are retained, with all CSS and JavaScript intact:

https://codepen.io/anon/pen/gKPJgq

Answer №1

If you are utilizing bootstrap, referring to the grid system documentation could be beneficial https://getbootstrap.com/docs/4.0/layout/grid/

Consider using max-width or percentage units instead of fixed width as they are relative to the parent element, like body in your case.

Separating sidebar and #userHeader into different columns using Bootstrap's 12 column system could potentially resolve the issue - assign col-3 class to sidebar and let #userHeader fill the remaining space.

Additionally, exploring flexbox for simpler layout positioning and responsiveness might prove helpful.

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

Tips for increasing the dimensions of a modal in Bootstrap

<div class="popup zoom" id="_consultant"> <div class="popup-window " > <div class="popup-content" style="background-color:#F5F5F5" > </div> </div> </div> After extensive research, I have not found ...

Relocate the Bootstrap selector box form to align with the left side of the page

I have included the Bootstrap CDN in my project, and I am encountering an issue with a form that contains a dropdown box. The problem is that the legend tag is properly aligned to the left of the page, but the selector box is slightly indented towards th ...

Tips on organizing elements

Here are three elements: <!DOCTYPE html> <html lang="en"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.boo ...

What could be causing the issue with my validation for alphabetical input?

I am currently working on a registration form that only accepts alphabetical input. However, I am facing an issue where my error message appears regardless of whether I input an alphabetical or special character. According to my understanding, the code sho ...

Issue: ENOENT - The specified file or directory, './views/s.ejs', does not exist in Node.js Express

Encountering an error when attempting to render a file from the 'views' directory in the 'routes'. The specific error message is as follows: Error: Valid Login { [Error: ENOENT: no such file or directory, open './views/s ...

Displaying or concealing dropdown menus based on a selected option

My goal is to have a drop-down menu in which selecting an option triggers the display of another drop-down menu. For example, if I have options like "Vancouver," "Singapore," and "New York," selecting Vancouver will reveal a second set of options, while ch ...

What is the process for importing a local widget file in Angular?

I have a unique widget named employee-widget stored in the Angular application folder as employee-widget.js. Despite following the calling method below, the widget fails to load. Method 1: <div> <h4>Attempting to load the employee widget& ...

Show a specific item when selecting an option from a dropdown menu

Hey there, I have a question regarding creating a theme for the Genesis Framework. Right now, I'm facing a challenge with one particular element. Here's the situation: I've got two drop-down lists, but I only want the second one to be visib ...

Enhancing Child Elements with CSS - Evaluating Rating Systems

Is there a way to change the opacity of certain images inside my div when hovering over them? Specifically, I want the 4th image and the first three to have an opacity value of 1. I've looked into using nth-child but am unsure how to implement it for ...

Ensure that the background image adjusts appropriately to different screen sizes while maintaining its responsiveness

Currently in the process of developing a single page application using CRA. My goal is to create a hero image with an overlay at the top of the application using a background image. However, I'm facing issues with maintaining the responsiveness of the ...

The jQuery prop("disabled") function is not operating as expected

Although I've seen this question answered multiple times, none of the suggested solutions seem to be working for my specific example. Hopefully, a fresh set of eyes can help me figure out what's going wrong. Even after adding alerts to confirm t ...

Executing a click event on an HTML table dynamically created from an AJAX request

I have a script that creates an html table displaying Users of the application, with options to either EDIT or DEACTIVATE each user. These actions are handled using AJAX. The table structure is as follows: <tr><td><a id='edit_link&apo ...

displaying li tag depending on the index value within angularjs

I have an HTML structure similar to the following: <div class="main"> <ul> <li ng-repeat='save in saves'> <h3>{{save.name}}</h3> <div > <ul> ...

Implement a vertical scrollbar when the page exceeds a specific height

I've been combing through Google for a solution with no luck. I'm dealing with absolute content positioning and I'm looking to implement a scrollbar only when the browser window reaches a certain size. This website is fullscreen, so I don&ap ...

Creating a Loop for Tabs on an HTML Form

When navigating through form input boxes using the tab key, it usually goes in order. However, I discovered that you can actually customize this order by using tabindex=x. Since I have 5 inputs on my form, I use tabindex 5 times to specify the order. But ...

Reinitializing the Qt Stylesheet seems to have no effect

Attempting to modify the style of a window and all its children using stylesheets. Each child is given a name using setObjectName, and the stylesheet uses these names to modify the style of the named widgets, along with a few exceptions. To achieve this, ...

What could be the reason the background-color isn't changing when I apply button:hover?

body{ font-family:"Arial","Lucida Grande", "Lucida Sans Unicode", Verdana, Helvetica, sans-serif; font-size:12px; } p, h1, form, button{border:0; margin:0; padding:0;} .spacer{clear:both; height:1px;} /* ----------- My Form ----------- */ .myform{ margin:0 ...

While I navigate up and down using a mobile device, my abilities chart remains fixed and does not vanish

const skillsLink = document.querySelector(".page-header li:nth-child(1) a"); const skillsClose = document.querySelector(".skills-close"); const windowOpened = "window-opened"; skillsLink.addEventListener("click", (e) => { e.preventDefault(); docume ...

Struggling to access the height attribute from a CSS file

Hey there. I'm pretty confident that the solution to my query is quite simple. So, I have a .css file with this particular code snippet: div.site { text-align:center; border:2px solid #4b6c9e; padding:1px; margin-top:10px; font-size:medi ...

My confusion is running high when it comes to navigating the mobile version of my website

Creating a navigation bar for my website has been challenging. Whenever I switch to mobile view, the list in my navigation bar shifts 40px to the right side of the screen where there's nothing. Is there any way you can assist me with this issue? ...