Ensure that the fixed header's width matches the size of the container div

The header element is contained within a div. I have positioned the header as fixed, leading to 2 issues:

  1. The width of the header extends beyond the width of the div container. It should align with the div.
  2. When the header is fixed, the other elements like nav and article are not visible.

Current appearance: https://i.sstatic.net/EO6D8.png

Desired appearance: https://i.sstatic.net/6SIko.png

Below is the code snippet:

<!DOCTYPE html>
<html>
<head>
<style>
div.container {
    width: 100%;
    border: 1px solid gray;
}
header{
   position:fixed;
   width:100%;
   right:0px;
}
header, footer {
    padding: 1em;
    color: white;
    background-color: black;
    clear: left;
    text-align: center;
}

nav {
    float: left;
    max-width: 160px;
    margin: 0;
    padding: 1em;
}

nav ul {
    list-style-type: none;
    padding: 0;
}

nav ul a {
    text-decoration: none;
}

article {
    margin-left: 170px;
    border-left: 1px solid gray;
    padding: 1em;
    overflow: hidden;
}
</style>
</head>
<body>

<div class="container">

<header>
   <h1>City Gallery</h1>
</header>
  
<nav>
  <ul>
    <li><a href="#">London</a></li>
    <li><a href="#">Paris</a></li>
    <li><a href="#">Tokyo</a></li>
  </ul>
</nav>

<article>
  <h1>London</h1>
  <p>London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.</p>
  <p>Standing on the River Thames, London has been a major settlement for two millennia, its history going back to its founding by the Romans, who named it Londinium.</p>
</article>

<footer>Copyright © W3Schools.com</footer>

</div>

</body>
</html>

Answer №1

Here is the solution you were looking for:

header {
/* The rest of the code remains unchanged */
position: relative;
}
header,footer {
/* The rest of the code remains unchanged */
padding: 1em 0;
}
  1. You have specified that the div width should be 100%, with the header width being set to 100% of its parent, and then applied padding left and right of 1em (padding: 1em -> pads all four sides, padding: 1em 0 -> top & bottom 1em, left & right 0).
  2. Why did you set it as fixed? Are you intending for the header to stick to the top?
div.container {
  width: 100%;
  border: 1px solid gray;
}
header{
  position:fixed;
  width:100%;
}
header, footer {
  /*padding: 1em 0;*/
  color: white;
  background-color: black;
  clear: left;
  text-align: center;
}
header h1 {
  line-height: 2em;
  font-size: 2em;
}
nav {
  float: left;
  max-width: 160px;
  margin: 0;
  padding: 1em;
  margin-top: 6em;
}
nav ul {
  list-style-type: none;
  padding: 0;
}
nav ul a {
  text-decoration: none;
}
article {
  margin-left: 170px;
  border-left: 1px solid gray;
  padding: 1em;
  overflow: hidden;
  margin-top: 6em;
}

Answer №2

To make the article full window size, simply add this code to your .article class:

height: 100vh; 

Answer №3

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://example.com/style.css">
<script src="http://example.com/jquery.js"></script>
<script src="http://example.com/script.js"></script>
</head>
<body>

<div data-role="page">
  <div data-role="header" data-position="fixed">
    <h1>Title of the Page</h1>
  </div>

  <div data-role="main" class="ui-content">
    <p>This is a sample text for demonstration purposes.</p>

    <p>Clicking on the screen will trigger certain effects depending on the layout and features enabled in the page.</p>

    <p>Some more text to showcase scrolling functionality..</p>
  </div>

  <div data-role="footer" data-position="fixed">
    <h1>Footer Content</h1>
  </div>
</div>

</body>
</html>

Answer №4

To provide space for a fixed header, add a margin-top (e.g. 100px) to the first non-fixed element in the content. Additionally, include html, body {margin: 0; } to reset default margins that impact the width of non-fixed content.

Ensure all elements have box-sizing: border-box; applied to include borders in percentage widths (see snippet).

<!DOCTYPE html>
<html>

<head>
  <style>
    * {
      box-sizing: border-box;
    }
    body,
    html {
      margin: 0;
    }
    div.container {
      width: 100%;
      border: 1px solid gray;
    }
    header {
      position: fixed;
      width: 100%;
      right: 0px;
    }
    header,
    footer {
      padding: 1em;
      color: white;
      background-color: black;
      clear: left;
      text-align: center;
    }
    nav {
      float: left;
      max-width: 160px;
      margin: 0;
      padding: 1em;
      margin-top: 100px;
    }
    nav ul {
      list-style-type: none;
      padding: 0;
    }
    nav ul a {
      text-decoration: none;
    }
    article {
      margin-left: 170px;
      border-left: 1px solid gray;
      padding: 1em;
      overflow: hidden;
      margin-top: 100px;
    }
  </style>
</head>

<body>

  <div class="container">

    <header>
      <h1>City Gallery</h1>
    </header>

    <nav>
      <ul>
        <li><a href="#">London</a>
        </li>
        <li><a href="#">Paris</a>
        </li>
        <li><a href="#">Tokyo</a>
        </li>
      </ul>
    </nav>

    <article>
      <h1>London</h1>
      <p>London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.</p>
      <p>Standing on the River Thames, London has been a major settlement for two millennia, its history going back to its founding by the Romans, who named it Londinium.</p>
    </article>

    <footer>Copyright © W3Schools.com</footer>

  </div>

</body>

</html>

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 Flutter image blurs into a striking combination of red and black as the screen dimensions are altered

https://i.stack.imgur.com/YeZa6.png Hello everyone, we are facing an issue with Flutter web. Here's what's happening: When we use flutter run -d chrome --web-renderer canvaskit and flutter run -d chrome --web-renderer html, nothing changes. If ...

How can I retrieve an attribute from another model in Ember using the current handlebar in the HTML file?

I'm attempting to achieve the following: {{#if model.user.isAdmin}} <div> My name is {{model.user.name}} </div> {{/if}} within a handlebar that is being used in a controller unrelated to users: <script type="text/x-handlebars" data- ...

Text display appears uneven

After downloading the 'exo' font from Google Fonts, I was pleased with how it appeared on my htc smartphone. However, on my laptop screen, it looked as if a swarm of caterpillars had nibbled away at it. I attempted various techniques like anti-a ...

Utilize Javascript or Jquery to intercept and handle both GET and POST requests

Is there a method to effectively intercept and capture both GET and POST requests as they are sent from the browser to the server? In my web application, full page refreshes occur after each request is submitted, however, some pages experience delays in r ...

Exploring the integration of pre-defined Tailwind classes within Next JS development

As I transition my project from Vite JS to Next JS, I am currently facing an issue with importing Tailwind styles from a file. Even though the file is being read by my project, the styles are not being applied as expected. I'm wondering if there is a ...

What are some ways to make source code more visually appealing on an HTML/JSP page as it is being

I've recently created a webpage with some Java source code, neatly organized within blocks. However, I'm looking to enhance its appearance so it truly looks like Java code. Take a look at my page's code here for reference: Are there any onl ...

Invoke JavaScript when the close button 'X' on the JQuery popup is clicked

I am implementing a Jquery pop up in my code: <script type="text/javascript"> function showAccessDialog() { var modal_dialog = $("#modal_dialog"); modal_dialog.dialog ( { title: "Access Lev ...

What is the process for retrieving the API configuration for my admin website to incorporate into the Signin Page?

My admin website has a configuration set up that dynamically updates changes made, including the API. However, I want to avoid hardcoding the base URL for flexibility. How can I achieve this? Please see my admin page with the config settings: https://i.st ...

What is the best way to detect when I switch to a different tab than a specified tab using HTML and jQuery?

Here is the HTML code I am working with: <div id="tabsWithStyle"> <ul> <li><a href="#data1">Data 1 TAB</a></li> <li><a href="#data2">Data 2 TAB</a></li> <li>< ...

Issue with Intel XDK: the document.location.href is directing to an incorrect page

Hello Community of Developers, I have been experimenting with different methods but still haven't found a solution. In my project using Intel XDK, whenever I attempt to change the page using location.location.href = "#EndGame" or similar codes in Java ...

Struggling to modify CSS properties for :before and :after pseudo-elements?

My current styling looks like this: section.tipos .content > div:before { background: none repeat scroll 0 0 #DDDDDD; content: " "; height: 10px; left: 32%; position: absolute; top: -10px; width: 10px; } It's working p ...

Say goodbye to using 'jQuery .load()' with <img> elements inside <a> tags

I have a static HTML page and some other files with the same structure but different content. <div id="textRed" class="scrollbar"> <h1>Header</h1> <p>Lorem Ipsum</p> <a href="images/image1.jpg" data-lightbox ...

What is the best way to transfer my saved bookmarks and import them into a database?

Over the years, I've amassed a large collection of bookmarks that I now want to organize in a searchable table with additional details like categories, types, and descriptions. My initial approach involved manually inputting them into a JSON file and ...

Creating the x and y coordinates for drawImage in HTML5

Understanding the x and y axis has posed a challenge for me: //retrieve last known x and y coordinates ...code var p = $("#draggable"); var offset = p.offset(); var x = offset.left; var y = offset.top; //establish new font siz ...

Tips for aligning controls in a column using Bootstrap

My coding issue: <div class="col-lg-4"> <label>Expiration ID</label> <div class="row "> <div class="col-lg-5"> <input type="text" class="form-control input-sm" id="TextIDExpire" /> </div> < ...

Position the scroll down arrow at the center bottom of a full-screen div using WPBakery Visual Composer

I have a series of full-screen sections in Visual Composer and I am trying to add an arrow at the bottom of each one to indicate to users that they should scroll for more content. However, my attempts with absolute positioning on the divs containing the ic ...

Attempting to align a CSS card flip within a Bootstrap layout

I have created a card using HTML and CSS, but I am struggling to make it line up with Bootstrap. Currently, the card is only in the middle of the section and I can't seem to figure out how to get four of these cards to line up in one row, and then ano ...

Combining various postponed JavaScript file imports in the HTML header into a single group

I've been facing an issue with my code structure, particularly with the duplication of header script imports in multiple places. Every time I need to add a new script, I find myself manually inserting <script type="text/javascript" src=&q ...

Using Conditional Tags for CSS Display

I'm working on implementing Conditional Tags to toggle a CSS class on or off based on the current displayed page. However, I'm running into an issue where the code isn't executing properly. There might be a syntax or logic error causing the ...

What is the best way to exclude HTML tags from the innerHTML attribute?

Currently, I am in the process of developing a messenger application and facing an issue where my messages are not ignoring HTML tags. This is happening because I directly paste the input text into the innerHTML of the message. The code snippet responsible ...