How can I override the maximum body width to enable specific div elements to extend beyond that limit?

While I've come across similar questions to mine, none of them seem to solve my specific issue. Essentially, I've set the body width to 960px, which is a common standard practice. However, there are certain elements such as header, footer, and some divs with background colors indicating different content areas that I want to ignore this width restriction.

The solution I am looking for should allow all other divs to bypass the body width limitation, not just the header and footer.

I have ruled out using position absolute because I don't want it to affect the positioning of my footer in the future.

html,
body {
  background-color: white;
  margin: 0 auto;
  width: 960px;
  height: 100%;
}

.Frame {
  display: table;
  height: 100%;
  width: 100%;
}

.Row {
  display: table-row;
  height: 1px;
}

.Row.Expand {
  height: auto;
}

.Row.footer {
  height: 30px;
  background-color: gray;
  margin-bottom: 0;
  width: 100%;
}
<body class="Frame">


  <header class="Row">
    /*header should ignore body width*/
  </header>
  <section class="Row Expand">
    /*some div should ignore the body width*/
  </section>
  <footer class="Row footer">
    /*footer should ignore body width/*
    <h3>Sticky footer</h3>
  </footer>

</body>

Answer №1

Avoid setting a fixed width for the <body> tag, as it can lead to potential issues down the road.

I comprehend the design concept you're aiming for. Here is a simple representation of what I would suggest:

$('#main-content').css("min-height", $(window).height() - $("#footer").height() - $("#header").height() + "px");
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  max-width: 100vw;
}

#header,
#footer,
#content-wapper,
section,
img {
  max-width: 100%;
  margin: 0 auto;
}

section.image,
img {
  font-size: 0;
  display: block
}

#header,
#footer {
  background: #333;
  height: 40px;
  color: white;
}

#main-content {
  background: #999;
}

.small {
  width: 800px;
  max-width: 100%;
  margin: 0 auto;
  padding: 2vw;
  background: #131418;
}

section p {
  color: white;
  line-height: 1.25
}

Include the following JavaScript and HTML snippets to maintain a sticky footer on your webpage:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>

  <div id="content-wrapper">

    <header id="header">
      /*header should ignore body width*/
    </header>

    <div id="main-content">
        <!-- Insert sections and images here -->
    </div>


    <footer id="footer">
      /*footer should ignore body width/*
      <h3>Sticky footer</h3>
    </footer>

  </div>
</body>

The provided script ensures that the footer remains at the bottom of the page even if the content is limited in height. This calculation considers the header and footer heights to determine the minimum height for #main-content, ensuring the footer's placement at the bottom consistently.

Answer №2

If you want to include a header and footer, the solution will involve removing the width from the body and adding it to a

<div class="main-content"></div>
or using
<main class="main-content"></main>
as shown below.

    <body>
      <header></header>
      <main class="main-content">
        <section></section>
        <section></section>
        <section></section>
        <section></section>
      </main>
      <footer></footer>

</body>

.main-content {
  width: 960px;
}

Examining your CSS raises some questions. For example, why do you set the body as display: table;?

Answer №3

To improve the layout, you can implement a negative margin for the header and footer once the screen size goes beyond 960px using an @media query. It's important to adjust the width accordingly as well.

@media (min-width: 960px) {
    footer, header {
        margin-left: calc( -(50vw-480px) );
        margin-right: calc( -(50vw-480px) );
        width: 100vw;
    }
}

While this method may not be ideal, it does have some valuable applications for specific cases.

Dejan.S's approach offers a more effective solution: instead of restricting everything in width and then trying to undo it with additional elements, focus on limiting only the elements that truly require it.

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

Incorporate fresh Google sites into different web pages using iFrame integration

Wishing you a fantastic day! I am currently working on embedding a brand new Google site into another webpage. I attempted to use an iframe for this purpose, but unfortunately it did not work as expected. Here is the code snippet: <iframe width="1280 ...

Font size too large when using the em unit

The default font-size of my code is set to 10px or 0.625em. According to this, to make the font size of <p> 7px, I should use 0.7em. However, in my case, the browser is consistently using a fixed font size of 9px, even when I decrease the font size o ...

Displaying the Selected Value from the DropDown in the Menu

I am working with Bootstrap5 and have the following dropdown menu in my code. Instead of displaying "Year," I would like to show which member was selected. How can I achieve this? <div class="dropdown"> <button class="btn btn- ...

Is it possible to log out a user in JavaScript by simply removing cookies?

Hey there, I currently have a node.js server running in the background which handles user login processes (I didn't create the backend code). app.use(function (req, res, next) { var nodeSSPI = require('node-sspi'); var nodeSSPIObj = n ...

Having trouble with the Bootstrap float right class? Your buttons are refusing to respond?

Currently, I am experimenting with ExpressJS and EJS rendering. I have noticed that the Bootstrap float-right class is not working properly on the navbar. I even attempted using d-flex, but the issue persists. I am unsure if I am missing something in my co ...

What is the best way to use jQuery to set the height of one div equal to another div

Edited: I am facing a situation with a 3-column layout - Column A, Column B, Column C. The height of Column A is dynamic and I need to set the same height for both Column B and C. For instance, Column A contains a tabbed panel with varying heights based ...

The sixth character that is not in SGML format

I encountered a validation error while working on a website project Line 1, Column 1: non SGML character number 6 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.… In Firefox source viewer, certain lines are highlight ...

Experience choppy scrolling in Internet Explorer

Check out my click and drag scrolling Image Viewer here. While it functions perfectly in Firefox and Chrome, Internet Explorer is giving me some trouble. The movement seems jerky, especially when scrolling diagonally. It's like the scroll is sluggish ...

What is the process of sending data to a MongoDB database using node.JS?

Well, the code below is not exactly neat, but we're all learning, right? The goal here is to POST a basic user profile into a database, but it seems like I'm getting a 404 error. I'm relatively new to these technologies, so if anyone could ...

The header row in HTML tables sometimes vanishes unexpectedly after sorting the table

Upon filtering the table, I noticed that the header disappears sporadically. The issue is that the table header row should remain in place regardless of whether or not the characters used for filtering are present in the table rows. In Example 1: When fil ...

I encountered an issue when attempting to execute an action as I received an error message indicating that the dispatch function was not

I just started learning about redux and I am trying to understand it from the basics. So, I installed an npm package and integrated it into my form. However, when I attempt to dispatch an action, I encounter an error stating that 'dispatch is not defi ...

What is the correct way to display or hide a button based on my specific situation?

Creating a 'show more' button for my app has presented me with a challenge. I am using a directive to handle actions when the user clicks on the read more button: (function(window, angular) { var app = angular.module('myApp'); ...

Layering numerous backgrounds (Regular, Expanded, Regular) atop one another

After experiencing an issue with the background image not fitting the content properly at different resolutions, I attempted to divide the background into three parts and automatically stretch the middle section to fill the space between the top and bottom ...

Increase the Step Size of an HTML Number Input by Holding Down the Time

Is there a way to implement increasing increment/step size for number inputs in HTML based on how long the user holds the stepper arrows? For instance, starting at step size=1 and gradually ramping up to larger increments like 5, 10, 20, etc. after holdin ...

When I expand one panel, I want to automatically close any other opened panel to ensure only one section is expanded at a

I'm experiencing an issue with the Bootstrap 4 accordion. Currently, when I open a collapsed section in the accordion, it remains open even when I click on another collapsed section. What I actually want is for the previously opened section to close ...

Using PHP, JavaScript is unable to hide <div> elements

I'm encountering an issue where the div I want to show when an error occurs is not hiding and showing properly using JavaScript. Here is a snippet of my code: <script> function hideerror() { var catdiv = document.getElementById(error); ...

What is the best way to eliminate <p><br></p> tags from a summernote value?

When I try to enter text into the summernote textarea for space, I keep getting this code: <p><br></p><p><br></p><p><strong style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: just ...

Disabling a checkbox within an onClick event handler

I'm facing an issue where I have a checkbox with the type "checkbox" and I'm using JAWS to read it. The problem is that in IE11, JAWS reads a disabled checked checkbox as unchecked, which I believe is a bug in IE. To work around this, I need to r ...

I added an onClick event to an SVG image, however, I am struggling to get the event to execute a jQuery if/else statement

I've been working on creating an SVG map of the US, and I'm almost finished. The final step involves implementing a simple if-else statement to prompt users for the name of the state when they click on it. My goal is to fill the state green if th ...

What is the best way to navigate a carousel containing images or divs using arrow keys while maintaining focus?

Recently, I have been exploring the Ant Carousel component which can be found at https://ant.design/components/carousel/. The Carousel is enclosed within a Modal and contains multiple child div elements. Initially, the arrow keys for navigation do not work ...