html/css: managing content overflow in web pages

While studying CSS, I created a sample page and encountered an issue where two of my divs were overflowing the content of the body, with the footer appearing above those divs.

body {
  background-color: white;
}
#div1 {
  margin: 50px;
  text-align: justify;
  width: 40%;
  position: absolute;
  left: 150px;
  top: 400px;
}
#div2 {
  margin: 50px;
  width: 35%;
  position: absolute;
  left: 800px;
  top: 400px;
}
#div3 {
  position: relative;
  width: 100%;
  height: 200px;
  top: 500px;
  background-color: black;
  color: white;
}
footer {
  background-color: black;
  color: white;
  width: 100%;
  position: absolute;
  bottom: 0;
  overflow: hidden;
}
<div id="div1">
  <p>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
    survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was
  </p>
  <h1 id="head2"><b>What I can help you with:</b></h1>
  <p>
If you’re a company or an agency that cares about creating great user experiences and are looking for someone to help you build a fast, accessible and standards-compliant responsive web site or application, I can help
  </p>
</div>
<div id="div2">
  <h3>TESTIMONIAL</h3>
  <p>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galle
  </p>
</div>
<footer>
  <div id="left">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ip</div>
</footer>

The challenge lies in the fact that div2 and div3 extend beyond the bounds of the footer, resulting in an unattractive layout. Upon inspecting the elements via Chrome's developer tools, I noticed that the contents of these divs are positioned outside the boundaries of the body.

Answer №1

Consider replacing your use of position:absolute with inline-block to improve the layout of your elements.

body {
  background-color: white;
  margin: 0;
}
.div {
  margin: 5%;
  display: inline-block;
  vertical-align: top
}
.w40 {
  text-align: justify;
  width: 40%
}
.w35 {
  width: 35%
}
 {} footer {
  background-color: black;
  color: white;
  width: 100%;
}
<div class="div w40">
  <p>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
    survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was
  </p>
  <h1 id="head2"><strong>What I can help you with:</strong></h1>
  <ul>
    <li>Lorem Ipsum is simply dummy text of th</li>
    <li>Lorem Ipsum is simply dummy text of th</li>
    <li>Lorem Ipsum is simply dummy text of th</li>
  </ul>
</div>
<div class="div w35">
  <h3>TESTIMONIAL</h3>
  <p>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galle
  </p>
</div>
<footer>
  <div id="left">Lorem Ipsum is simply dummy text of the printing and typesetting
  </div>
</footer>

Answer №2

The reason for this issue is that you have positioned them using absolute, and there is no relative parent since #div4 does not exist. Divs are block elements by default; if you remove the position attribute, they should wrap normally.

If you want to hide the entire page, consider applying overflow to the body element.

Answer №3

It seems like you might be overusing position absolute without fully understanding its impact on the document's height and width. Perhaps using float: left could be a better alternative, though it also comes with its own set of challenges especially when dealing with mobile responsiveness.

Crafting CSS requires careful planning to ensure everything functions as intended. Have a look at the code snippet below for some guidance.

I made adjustments by adding float: left to #div1, #div2

html{
  height: 100%;
  width: 100%;
}
body{
  background-color: white;
  height: 100%;
  width: 100%;
}

#div1{
  margin: 50px;
  text-align: justify;
  width:40%;
  float: left;
}

#div2{
  margin: 50px;
  width:35%;
  float: left;
}

footer{
  background-color: black;
  color: white;
  width: 100%;
  position: absolute;
  bottom: 0;
  overflow: hidden;
}
<div id="div1">
<p>
... Lorem Ipsum content ...
</p>
<h1 id="head2"><b>What I can help you with:</b></h1>
<p>
<ul> ... list items ... </ul>
</p>
</div>
<div id="div2">
<h3>TESTIMONIAL</h3>
<p> ... Lorem Ipsum content ... </p>
</div>
<footer>
    <div id="left"> ... more dummy text ... </div>
</footer>

In an effort to avoid footer display issues, I included height: 100% and width: 100% for body and html, but do test this approach to verify if it aligns with your design expectations.

Answer №4

The issue arises when utilizing position:absolute in conjunction with top:400px, causing the element to extend beyond the body's height due to the presence of static elements within it. To resolve this, simply eliminate position:absolute from all elements (including divs and footer), which should restore the display to its intended appearance. Consider using margin-left instead of left for positioning purposes;

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

Express-minify is having trouble loading the CSS file. If we remove a portion of the file, it should be able

After implementing the express-minify middleware, I encountered an issue with loading the attached CSS file. Even after validating the CSS using an online service, no errors were detected. I attempted to debug by gradually removing elements, and the prob ...

Tips for maintaining the height of the outer div consistent with that of the inner div

I am encountering an issue with an inner div that is used for overlay and set to a min-height property. Despite changes in the overlay's height, the height of the outer div remains unaffected. #outer { width: 70px; height: 70px; background-co ...

I am trying to update the content in ckeditor when a different option is selected, but for some reason it is not working

jquery issue: the jquery that I added and the content doesn't change even though the "alert(model);" works fine <script> $(document).ready(function() { $("select.modele").change(function() { var modele = $(this) ...

How can I maintain the state of an HTML checkbox in Django even after reloading the page?

In the following sample code, I am looking to maintain the checkbox as checked even after a page reload when the submit button has been pressed. <td><input type="checkbox" value="{{ item }}" name="selectedcheckbox"/ ...

I am experiencing difficulty locating the style.css file within my Node.js/Express application

I am currently working on a Node.js/Express app and I'm facing an issue where my browser is unable to find my style.css file, even though I am using a static file. Here is the directory structure: public -> css -> styles.css server -> serv ...

Tips for locating an element without a class assigned

I am currently utilizing the webdriverIO framework for my script, specifically working with a calendar where I need to extract only the days from the current month. Below is an excerpt of the HTML code: <tr class="daysrow"> <td class="day w ...

Click event not being triggered in Handlebar JS

VIEW DEMO Hey there, I've been struggling to implement a simple click event for dynamically loaded JSON data in an anchor tag. I've tried using both vanilla JavaScript and jQuery but haven't been successful in making it work. The same func ...

Transforming a grid of radio buttons into a numerical representation or percentage

I am facing an issue with calculating the total value of radio button selections in three rows. Each row belongs to a specific group, and I want the value of each selection to appear in the last column. The first radio button in each row has a value of 2, ...

Guide on integrating JSON data into Bootstrap 4's secondary dropdown menu

I have a vision of my desired outcome in the attached image. Each menu item should contain two additional items, 'Dashboard' and 'Battery Trends', each with unique URLs defined as 'navigationurl' in the JSON file. My attempt ...

Is it possible to apply a modal to a v-for item?

I am receiving a GET request from my API to display all users, and I want to potentially modify the name or email of each user using a modal. However, I am facing an issue where if I have 3 users displayed, clicking on one modal button triggers all 3 modal ...

Top button disappears in Chromium browser after fade-in effect

I've encountered a very peculiar issue. Whenever I use the page down or fragmented identifier to jump to a specific div, my "go to top" image disappears. Here is the code snippet: HTML: <a title="Go to top" href="#" class="back-to-top"></ ...

Text that is selected within a contenteditable div: Find the beginning and end points of the highlighted text

<div contenteditable="true">This text is <b>Bold</b> not <i>Italic</i> right?</div> For instance, if the text appears in a bold format, but not italic and the user selects it, how can I determine the exact position ...

Collecting data from an HTML form filled out by users

I need a way to capture user input from an HTML form and display it later on my website. I want to show all the information users provide along with their pizza selections. I've been struggling for hours trying to make this work without success :( Spe ...

Can you show me the procedure for retrieving a particular element with selenium? (Page Source provided)

Disclaimer: I must admit that my knowledge of HTML and CSS is minimal, so please bear with me! Hello everyone, I am attempting to utilize selenium[Java] to browse a website and retrieve a file. While I managed to successfully get past the login page, I fi ...

Font consistency varies between different operating systems or machines

I have been experiencing a strange issue with the "Populaire" font that I am using. On my laptop, the font appears perfectly without any distortion on all browsers. However, when viewed on other systems (not all, but some), it becomes distorted across all ...

Adding an input-group-addon class in Bootstrap 4 to enhance the form

Embarking on a new project, I made the decision to upgrade to Bootstrap 4. While working on creating a form, I noticed that the 'input-group-addon' class was not functioning properly. I am perplexed as to why this is the case, as the class works ...

JavaScript tip: How to disregard symbols that affect the length of text in a textbox?

I am working on a task which involves counting only the text, excluding symbols, entered in a textbox. For instance, if a user types email_ex_3/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="05374568646c692b666a68">[email ...

What is the method for assigning a custom CssClass to the THEAD element within a dynamically generated HTML table?

Utilizing Bootstrap 4 gives me the opportunity to incorporate a unique style for the table header. By following this syntax: <table class='table table-bordered'> <thead class='thead-dark'> <!-- etc --> ...

Why does the browser keep converting my single quotation marks to double, causing issues with my JSON in the data attribute?

This task should be straightforward, but I seem to be facing a roadblock. I am trying to insert some JSON data into an input's data attribute, but the quotes in the first key are causing issues by prematurely closing the attribute. Here is the code ...

Manipulate the CSS style of the body element in Angular with Typescript using the important tag

Within my Angular application I have implemented the following code to customize the body style: constructor(private elementRef: ElementRef) { } ngOnInit() { this.elementRef.nativeElement.ownerDocument.body.style.overflowY = 'hidden'; ...