CSS excess white space in the lower section

Scenario: Creating a website where the central white section (the 'wrapper' div) serves as the background for all content, while the red sections on either side (the 'body') act as borders. When I remove the 100% bottom-padding from the 'wrapper' div, the content's background defaults to red (from the 'body' instead of the 'wrapper'). However, keeping the 100% bottom-padding causes the page to scroll beyond the actual content length (even though everything fits on one page). This excess scrolling is not desired; I want the scrolling limit to match the actual content length.

Please provide guidance on how to maintain the content's background as the wrapper div without enabling excessive scrolling. The example code should demonstrate the inability to scroll past the initial screen due to no content present. Currently, the code allows for scrolling equivalent to my live website despite minimal content.

<!doctype html>
<html>
<head>
<title>Bob</title>

<meta charset="utf-8" />
<meta http-equiv="Content-type" contents="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial scale=1" />

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>


<style>

body {
height: 100%;
background-color:red;
margin:0 auto;
}

#wrapper {
width:600px;
height: 100%;
margin:0 auto;
background-color: white;
padding-top:8px;
/* Removed bottom-padding and eliminated whitespace at the bottom */
padding-left: 20px;
padding-right:20px;
}

</style>

</head>

<body>

<div id="wrapper"></div>

</body>
</html>

Answer №1

When scrolling occurs, it is likely due to setting the padding-bottom to 100%. This causes paddings and margins to be calculated based on the width rather than the height. If you remove the padding-bottom:100%;, the element will take up less space as the height is determined by the containing block.


To fix the issue of the element taking up too much space, ensure that both the body and html elements are set to a height of 100% with the same styles:

html, body {
    height: 100%;
    background-color:red;
    margin:0 auto;
}

To achieve the desired result without unnecessary padding causing scrolling, adjust the styles as shown below:

<!doctype html>
<html>
  <head>
    <title>Bob</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" contents="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial scale=1" />

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>


    <style>

      html,body {
        height: 100%;
        background-color:red;
        margin:0 auto;
      }

      #wrapper {
        width:300px;
        height: 100%;
        margin:0 auto;
        background-color: white;
        padding-top:8px;
        /*padding-bottom:100%;  /* removed and white space at bottom removed */
        padding-left: 20px;
        padding-right:20px;
        box-
      }

    </style>

  </head>

  <body>

    <div id="wrapper"></div>

  </body>
</html>

There may still be some slight scrolling due to the padding-top:8px in the CSS.

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 could be causing my CSS transitions to fail when using jQuery to add classes?

I'm currently working on a website and I'm facing an issue with the transition not functioning as expected. The problem persists even when the 7.css stylesheet is removed and interestingly, the transition works fine when using window:hover. My a ...

Having difficulty navigating to a different page in Angular 4

I'm currently attempting to transition from a home page (localhost.com) to another page (localhost.com/listing). Although the app compiles correctly, I encounter an issue where nothing changes when I try to navigate to the new page. My approach has m ...

Tips for automatically hiding a button when the ion-content is being scrolled and displaying it again once scrolling has stopped

I have implemented a scroll function event in my HTML file using the following code: <ion-content (ionScroll)="scrollFunction($event)"> <ion-card *ngFor="let product of products" no-padding> <ion-item class="bigclass"> <i ...

When using JSON.Stringify in a React App, it only displays the first item and the length of the object instead of all the other items

Working on a React App, I encountered an issue while trying to convert an array to JSON and send it to the server. My approach was like this: console.log(JSON.stringify(mainArray)) I anticipated seeing output like this during testing: "breakfast": { ...

The process of passing the ID value to another function is malfunctioning in JavaScript

Currently working on a sudoku puzzle as part of a toy project. My goal is to retrieve the ID value of blank spaces and pass it to another function. However, I am facing an issue where the numbers are not getting inserted into the blanks upon clicking. Can ...

"Is there a way to transfer a value from one list box to another without needing to refresh the page, by utilizing AJAX,

Is there a way to automatically load Related Course levels in the Course Level Listbox when selecting a Course from the Course list? <script type="text/javascript" src="js/jquery.js"></script> <div>Course:<select name="course_id" id= ...

ensure that information entered in a dynamic text box is accurate through client-side validation

I have been tasked with creating a form in asp.net that requires the use of dynamic text boxes. I was able to successfully generate textboxes when the user clicks the "Add" button and retrieve their values on the server side. However, I am now encountering ...

Scrollable table body with a stationary table header

Is there a way to create a table with a fixed height and a scroll bar for the content, while still allowing the table width to adjust accordingly? I've tried using display: block, but it seems to freeze the width of the table. Any suggestions on how t ...

Enhancing user experience: Implementing a dynamic tooltip feature for an image control within a gridview

I have an Image control within a gridview like this: <asp:ImageButton ID="imgbtnInfo" runat="server" ImageUrl="info.jpg" ToolTip="Button info" title="Info" CommandArgument='<%#Eval("ID") %>' CommandName="Info" /> These are the dyna ...

Optimizing Jquery for dynamically generating large tables

Looking to create a massive table using a Java servlet and sending it to the client as an AJAX response. The table cells have specific styles: <td style="background-color:blue;border-color:yellow;" onClick=tableClick(this)>value1</td> <td s ...

What steps should I take to utilize the z-index in order to position my logo on top of the navigation bar and header section?

I decided to split my navigation bar and header into two separate react components. However, I encountered an issue with the positioning of my logo, which is located in the navigation bar component. I am currently working on making it overlap the header co ...

Preserve the Ajax response within an element that previously did not exist within the DOM

I have some results displayed on the page, but only the first 10 are shown initially. To load more results, there is a button that sends the ID of the last report to a PHP page. After reading here, I learned that I need to use .on instead of .live because ...

What is the process for calling a class in HTML from a <Div> tag?

I am struggling with the following code snippet: <p class=‘majed’ style="color:#FF0000";>Red paragraph text</p> How can I reference the 'majed' class above? <Div class=‘majed’ > </Div> Your help is greatly appr ...

Reacting to the dynamic removal of spaces

Is there a way to dynamically remove the space between removed chips and older chips in Material-UI when deleting one of them? <div className="row contactsContainer"> {contacts.map((contac ...

Checking for the Existence of a Database Table in HTML5 Local Storage

Upon each visit to my page, I automatically generate a few local database tables if they do not already exist. Subsequently, I retrieve records from the Actual Database and insert them into these newly created local tables. I am wondering if there is a me ...

Concealing information beneath a veil of darkness

I'm looking to implement a solution using JS/CSS/HTML5 to hide content within a div container with a shadow effect. The end result should resemble the example image I've provided. Additionally, I'd like the ability to customize the color of ...

Converting HTML table text to JSON data using vanilla JavaScript (without any external libraries like Selenium)

As a newcomer to programming and Javascript, my goal is to use Selenium Webdriver in Visual Code to extract HTML table content from a webpage and convert it into JSON data. I've managed to retrieve the table data, but I'm struggling with the conv ...

Utilize VueJS to remove a designated HTML div upon clicking the delete button

I'm having trouble with a delete button on my input fields. The goal is for the delete button to remove all related input fields when clicked, but it's not working as expected. HTML <div v-for="index in 10" class="form-group ro ...

Making a jQuery ajax call to a web service in asmx format

I've been trying to request data from an HTTP POST asmx web service using jQuery's ajax. Despite following several guides on the correct method, I have not been successful so far. It seems like the issue lies within the request itself: $.ajax({ ...

Prevent anchor link click and drag functionality on an HTML page / Swipe through a container containing links

Is there a way to prevent clicking and dragging on a link in a webpage? When you click the left mouse button on a link and drag it, you can unintentionally move the link or open a new tab. I am looking for a way to disable this behavior using JavaScript o ...