Footer remains attached after the absolute wrapper is positioned

Is there a way to horizontally center a wrapper without using position absolute?

#wrapper {
    width: 721px;
    height: 720px;
    position: absolute;
    margin: auto;
    top: 136px;
    left: 0;
    right: 0;
    background: white;
    clear: both;
}

The wrapper contains three floated divs, which get messed up when the position is changed to relative.

____________________________________________
header
____________________________________________

         __wrapper____________
         |         |          |
         |         |          |
         |  div1   | div2     |
         |         |          |
         |         |          |
         |         |          |
         |_________|__________|
         |     div3           |
         |____________________|

__________________________________________
footer
__________________________________________

How can I create a sticky footer that always stays at the bottom of the site regardless of content amount?

.footer {
    border-top: 1px solid #dcdcdc;
    max-width: 100vw;
    font-weight: bold;
    text-align: center;
    background: #f0f0f0;
    width: 100%;
    height: 80px;
}

In this JS-FIDDLE, you can see that the footer hides behind the header.

Answer №1

Have you considered incorporating bootstrap into your project? By using bootstrap, you can streamline your layout with the following code:

<div class="navbar navbar-fixed-top">
  Your header here
</div>

<div class="row">
 <div class="col-md-3 col-md-offset-3">
   Your first div
 </div>
 <div class="col-md-3 col-md-offset-6">
   Your second div
 </div>
</div>
<div class="row">
 <div class="col-md-6 col-md-offset-3">
   Your third div
 </div>
</div>


<div class="navbar navbar-fixed-bottom">
  Your footer here
</div>

To ensure the navbar fits well on both top and bottom sides, apply the following CSS:

html, body {
  padding-bottom: 55px;
  padding-top: 55px;
}

UPDATE: If you're not utilizing frameworks like bootstrap:

Add this CSS for the footer.

position: fixed;
bottom: 0;

This adjustment will display the footer properly, preventing it from being hidden behind the header.

Answer №2

In the realm of modern web development, we have the luxury of utilizing flexbox for creating layouts. Here's a basic example:

<div class="container">
  <div class="flex-container">
    <div class="item1">Box1</div>
    <div class="item2">Box2</div>
  </div>
  <div class="item3">Box3</div>
</div>

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 500px;
}
.flex-container {
  display: flex;
  flex-direction: column;
  align-self: center;
}
.item1, .item2 {
  height: 100px;
  width: 100px;
}
.item1 {
  background-color: blue;
}
.item2 {
  background-color: red;
}
.item3 {
  background-color: green;
  width: 200px;
  height: 100px;
}

To prevent the footer from being impacted, create an additional container around the 'wrapper'.

Try out this layout by visiting: https://jsfiddle.net/wxokadrx/

If you need a refresher on flexbox, check out:

Answer №3

If you prefer not to utilize flexbox, this alternative method may be helpful

It's important to note that absolute positioning is not required to horizontally align a div.

<div id="outer">
 <div id="inner">
 </div>
</div>

<style>
 #outer {
  background-color: green;
 }
 #inner {
  left: 0;
  right: 0;
  margin: auto;
  height: 300px;
  width: 400px;
  background-color: red;
 }
</style>

Check out the code snippet on fiddle https://jsfiddle.net/x2j325n4/

When using floats for inner divs, it can cause the height of the wrapper to collapse. To avoid this, consider switching to display:inline-blocks.

<style>
#header {
    width: 100%;
    height: 50px;
    background-color: pink;
}
#wrapper {
    left: 0;
    right: 0;
    margin: auto;
    width: 60%;
}
#div1 {
    width: 30%;
    height: 400px;
    display: inline-block;
    background-color: grey;
}
#div2 {
    display: inline-block;
    width: 60%;
    height: 400px;
    background-color: red;
}
#div3 {
    width: 100%;
    height: 400px;
    display: inline-block;
    background-color: blue;
}
#footer {
    width: 100%;
    height: 50px;
    background-color: green;
}
</style>
<div id="header">
    Hey, I'm the header
</div>

<div id="wrapper">
<div id="div1">
    First div
</div>
<div id="div2">
    Second div
</div>
<div id="div3">
    Third div
</div>
</div>


<div id="footer">
    Hey, I'm the footer
</div>

Fiddle : https://jsfiddle.net/rjhwxdL5/

If you want your footer to stick to the bottom of the viewport, simply apply position: fixed; bottom: 0; to your footer element

Answer №4

If you're facing layout issues, consider using the clear property in CSS to fix them. In my example, I created a clearfix class for a div element and placed it after .box2:

.clearfix:after {
  content: '';
  display: table;
  clear: both;
}
.clearfix:before {
  content: '';
  display: table;
}

To see this in action, check out the code snippet below (try fullscreen mode):

.header {
    min-width: 720px;
    top: 0;
    left: 0;
    right: 0;
    max-width: 100vw;
    line-height: 80px;
    font-weight: bold;
    text-align: center;
    margin: 0 auto;
    border-bottom: 1px solid #dcdcdc;
    background: #f0f0f0;
    position: fixed;
    width: 100%;
    height: 80px;
    z-index: 1;
}
#wrapper {
    border: 1px solid blue;
    width: 721px;
    background: white;
    margin: 120px auto 40px;
}
.box1 {
    clear:both;
    display: inline-block;
    float:left;
    height:300px;
    width:360px;
    border: 1px solid black;
}
.box2 {
    clear:both;
    display: inline-block;
    float:right;
    height:300px;
    width:350px;
    border: 1px solid black;
}
.footer {
    border-top: 1px solid #dcdcdc;
    max-width: 100vw;
    font-weight: bold;
    text-align: center;
    background: #f0f0f0;
    width: 100%;
    height: 80px;
}
.clearfix:after {
    content: '';
    display: table;
    clear: both;
}
.clearfix:before {
    content: '';
    display: table;
}
<div class=header>
Asdf
</div>
<div id="wrapper">
    <div class="box1"> asdf</div>
    <div class="box2"> asdf</div>
    <div class="clearfix"></div>
</div>
<footer class="footer">
ASDAFASFAFASFSAF
</footer>

I hope this solution resolves your issue!

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

Using multiple conditions in SetEnvIf

I am trying to implement the SetEnvIf directive in my .htaccess file for handling multiple conditions and redirecting to a specific URL. The code I have written is as follows: SetEnvIf Remote_Host "^" press_flag=0 SetEnvIf Request_URI '/press/$&apos ...

The Ajax request appears to be triggered twice, yet all other associated JavaScript functions are executed only once

Using ajax, I have a code that retrieves the contents of a PHP page every X seconds until completed=="yes". Each time it fetches the content, it displays an alert with <script>alert("checked");</script>. Within the PHP page, there is another al ...

A guide to accurately accessing form data in Jquery Ajax requests

My beforeSend function is not working properly, as the background color does not change. I suspect it may be due to not correctly referencing the id variable posted from the form. Below is the relevant HTML and JS code: HTML <div id="rec<?php echo ...

Guide to extracting HTML content from an AJAX call?

I am currently attempting to extract a value from an HTML form. This involves making a GET request to a specific URL, retrieving the HTML content, and then utilizing jQuery to parse through it. Below is the snippet of HTML that I need to navigate through: ...

The elements with identical IDs are being superimposed

There are two div elements on my webpage, both assigned the "nav" class. Below is the CSS code: .nav { border-radius: 1em; background-color: #0000BB; color: white; padding: 1em; position: absolute;//Fits size to content. margin: 1em; left: 50%; ...

What is the best method to align a form in three columns from each side without relying on Bootstrap?

Is there a way to center the form inside a card with 3 columns on each side, so that the form appears in the middle occupying about 6 columns without relying on Bootstrap, Materialize or any similar framework? Thanks! <div class="card"> & ...

Getting the dimensions of an image when clicking on a link

Trying to retrieve width and height of an image from this link. <a id="CloudThumb_id_1" class="cloud-zoom-gallery" rel="useZoom: 'zoom1', smallImage: 'http://www.example.com/598441_l2.jpg'" onclick="return theFunction();" href="http ...

Is it possible to bind browser keyboard scrolling to a div without explicit instructions?

Here is a question that closely relates to this one: Enable div to scroll by keyboard without clicking I am aware that explicitly focusing on the element will enable the desired behavior. My inquiry is whether there is a way to achieve this implicitly. ...

dividing an HTML string into individual variables using JavaScript

How can a string be split in pure JavaScript (without using JQuery/dojo/etc) in the most efficient and straightforward way? For example: var tempString = '<span id="35287845" class="smallIcon" title="time clock" style="color:blue;font-size:14px;" ...

Stop the text from wrapping to the full width of its parent when it overflows onto the following line

When I shrink the width in responsive design, the text overflows to the next line and the text wrapper expands to fit the parent container. Is there a way to prevent this behavior so that it looks like the red container? I could add padding to the contain ...

Guide to Display Three.js Code Suggestions in an HTML Document

I've exhausted all possible options to enable Three.js intellisense, but it simply refuses to appear. I've tried every known method in the manual. What's the missing piece here? Please share your insights. <!DOCTYPE html> <h ...

Is it possible to automatically adjust the text color based on the dynamic background color?

While browsing Palantir.com, I noticed that the logo color always changes to be the inverse of the background color behind it as the background scrolls or changes. https://i.sstatic.net/BYvZa.png I'm currently working on a website using Wordpress an ...

Contrasts between space and the greater than selector

Can you explain the distinction between selector 6 and selector 7 as outlined on the w3 website? Inquiring minds want to know. ...

The ability to use the backspace and Ctrl + c (Copy) functions is restricted in input fields on FireFox

I have a form with input fields designed using semantic UI. I need the input field to only accept numbers, remove spaces, and allow copying from it using ctrl +c. After some investigation, I found this jQuery code that seems to satisfy my requirements per ...

Choosing an option on a WordPress admin page may vary depending on the selection of another

I am facing a challenge with my code that involves two selections: category and type. The type selection should be dependent on the category selected from the database. Here is a snippet of my code: HTML $ps_type_table_name = $wpdb->prefix . 'ps_ ...

Problem with the hover functionality of the <li> tag

I'm having an issue with applying the hover property to the li element in the sidebar. The hover effect works, but the icon tag is not showing the hover effect. I want the icon to also show the hover effect along with the li's hover effect. Here ...

Tips for Creating a Fixed-Width Element

I am struggling with creating text wrapping around a box inside a full width page template on Wordpress. The plugin developer suggested wrapping the text inside a fixed width element, but I am unsure how to do this. Any assistance would be greatly apprecia ...

Use Javascript to display an image based on the date, otherwise hide the div

I'm looking to implement an image change on specific dates (not days of the week, but actual calendar dates like August 18th, August 25th, September 3rd, etc). Here's the div I'm working with: <div id="matchday"> <img id="home ...

emphasizing the specific text being searched for within the page

Looking for a solution to search values and highlight matching text words on a page? Let me help you with that. $(function() { var tabLinks = $('.nav > li'), tabsContent = $('.tab-content > div'), ...

Stopping CSS animations at a specific point along the path without using timing functions can be achieved by implementing a targeted

Is there a way to pause an animation at the 50% mark for 2 seconds and then resume? P.S. The setInterval method is not recommended! function pauseAnimation() { document.getElementById("0").style.animationPlayState = "paused"; }; var pauseInterval = set ...