Problem with CSS: The fixed footer is too wide when set at a width of 100%

I'm experimenting with using percentages instead of pixels and noticing some unusual behavior. Why is my footer extending to the right (beyond the "pagePlaceHolderInner" element)? It works when I assign a specific pixel value to both elements ("pagePlaceHolderInner" and "footer"), but I prefer to use percentages in this case.

Here's the code:


        <html>
        <head>
        <style type="text/css">
        body #pagePlaceHolderOuter {
            background: #FFFFFF;
            height: 100%;
            left: 0;
            position: absolute;
            top: 0;
            width: 100%;
        }
        body #pagePlaceHolderOuter #pagePlaceHolderInner {
            background: none repeat scroll 0 0 yellow;
            height: 100%;
            margin: 0 auto;
            max-width: 1000px;
            min-width: 700px;
            width: 70%;
        }
        body #pagePlaceHolderOuter #pagePlaceHolderInner .footer {
            background: gray;
            bottom: 0;
            height: 20%;
            position: fixed;
            width: 100%;
        }
        </style>
        </head>
        <body>

        <div id="pagePlaceHolderOuter">
            <div id="pagePlaceHolderInner">
                <h1>Hello everyone!</h1>
                <div class="footer"></div>
            </div>
        </div>

        </body>
        </html>

View this on jsfiddle

Answer №1

To make an element stick to a container, you cannot do it in the traditional way of fixing it relative to the entire page. Instead, you would have to manually set its dimensions to match those of the parent because it behaves differently compared to a regular child element; it acts as if it is positioned directly beneath the document.

body #pagePlaceHolderOuter #pagePlaceHolderInner .footer {
    position: fixed;
    height: 100%;
    margin: 0 auto;
    max-width: 1000px;
    min-width: 700px;
    width: 70%;
    /* Add your additional styling for this element here */
}

View Demo Here

An alternative approach to make the element inherit the values of its parent is to change its position to absolute, though this may not be exactly what you're looking for. To achieve this, you will need to also add position:relative; to the container of the element.

body #pagePlaceHolderOuter #pagePlaceHolderInner {
    position:relative;
    /* Additional styling for this element goes here */
}
body #pagePlaceHolderOuter #pagePlaceHolderInner .footer {
    position:absolute;
    /* More styling code for this element can be added here */
}

Updated jsFiddle Link

Answer №2

Creating a Fixed Footer at the Bottom of a Web Page

If you're looking to have a footer fixed to the bottom of your webpage, try this code:

body #pagePlaceHolderOuter #pagePlaceHolderInner .footer {
    background: gray;
    position: fixed;
    height: 20%;
    bottom: 0;
    margin: 0 auto;
    width: 70%;
    min-width: 700px
}

Check out a demo on jsfiddle: http://jsfiddle.net/audetwebdesign/ktM6n/

Make sure to adjust the width and margins of your footer to match the pagePlaceHolderInner container.

By fixing the footer to the viewport bottom, you ensure it aligns properly with the rest of your layout.

The key point to remember is that elements with position: fixed are positioned relative to the viewport, so keep that in mind while designing.

Answer №3

Your fixed element width is increasing due to the size of other elements on the webpage.

There might be an element on your page that is wider than the screen's width. Fixed elements are positioned at the top of the Document Object Model (DOM) and are not constrained by their parent elements.

To limit the width of your fixed elements, consider using the CSS3 vw unit, which sizes elements relative to the viewport. For example, you can use width: 100vw. This unit is now widely supported in most major web browsers.

Check out this link for more information on CSS units!

Answer №4

Give this code a try, it may be just what you're looking for

body #pagePlaceHolderOuter {
    background: #FFFFFF;
    height: 100%;
    left: 0;
    position: absolute;
    top: 0;
    width: 100%;
}
body #pagePlaceHolderOuter #pagePlaceHolderInner {
    background: none repeat scroll 0 0 yellow;
    height: 100%;
    margin: 0 auto;
    max-width: 1000px;
    min-width: 700px;
    width: 70%;
    position:relative;
}
body #pagePlaceHolderOuter #pagePlaceHolderInner .footer {
    background: gray;
    bottom: 0;
    height: 20%;
    position: absolute;
    width: 100%;
}

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

Creating a Timeout Function for Mobile Browsers in JavaScript/PHP

Currently, I am developing a mobile-based web application that heavily relies on AJAX and Javascript. The process involves users logging in through a login page, sending the data via post to the main page where it undergoes a mySQL query for validation. If ...

Form calculation

An issue has arisen with my calculating form for a client where an incorrect amount is displayed when 0.93 is entered into the percentage box. Original calculation: 10 x 10 x 15 x 0.93 = 13.95 Corrected calculation: 10 x 10 x 15 x 0.93 = 1.395 I am seek ...

Ways to correct inverted text in live syntax highlighting using javascript

My coding script has a highlighting feature for keywords, but unfortunately, it is causing some unwanted effects like reversing and mixing up the text. I am seeking assistance to fix this issue, whether it be by un-reversing the text, moving the cursor to ...

Initiating CSS3 animations when the display changes

In an attempt to incorporate CSS3 animations for fading elements in during page load, I am faced with the challenge of setting the element to display: none; and then back to display: block;. My goal is to exclusively apply the CSS3 animation upon the init ...

Unable to adjust the position of the logo image

I cannot figure out how to center the logo over a video background. Any suggestions on what I might be doing wrong? I've tried changing the "position" to relative and absolute, but without any success. #hero video { width:100%; height:100%; p ...

Revamp selected bootstrap attribute

Imagine a scenario where there is: @media (min-width: 576px) { #projects-edit-middle-column .equal-columns-wrapper .equal-columns { width: 50%; padding-right: 25px; } } This is being utilized within a container: <div class="equal-columns ...

Technique for seamlessly moving between subpages and main page while scrolling to a specific id

I'm struggling with coding and thought I'd reach out for help here. Can anyone provide a solution to my problem? Issue - I have a navigation link on my main page that scrolls to an ID using jQuery. It works fine on the main page, but not on any ...

How to eliminate the external white border from a Java applet when it is integrated into an HTML webpage

As someone experienced in dotnet, I decided to venture into creating a Java applet for my application. After successfully developing and signing the applet, it functioned perfectly within my application. However, one issue perplexed me - when I attempted ...

What is the best way to add a character within a YouTube JSON link?

Fetching json data from a link, the youtube format in the link is http:\/\/www.youtube.com\/watch?v=UVKsd8z6scw. However, I need to add the letter "v" in between this link to display it in an iframe. So, the modified link should appear as ht ...

Failure to load a picture does not trigger onError in the onLoad function

Is there a way to ensure that an image always loads, even if the initial load fails? I have tried using the following code snippet: <img class="small" src="VTVFile4.jpg" onload="this.onload=null; this.src='VTVFile4.jpg?' + new Date();" alt=" ...

What is the best way to emphasize a row in an Angular Material table based on a

I am facing an issue with highlighting row colors conditionally in my code. Although there are no errors, the background color is not being applied to the rows as expected. I have a total of 5 rows with the 'riskIndex' value set to 'H'. ...

Modify the Link's Color

How can I change the color of a link inside a DIV? The current code isn't working for me. <style type="text/css"> .someDiv { font-size:14px; color:#c62e16; } </style> <div id="someDiv"> <a href="http://www.s ...

Mastering the art of transitioning between DIV elements

I am looking to implement a rotating three-card display on click, and have come up with the following code: $('.box1').click(function(){ $('.box1').toggleClass('removeanimate'); $(this).toggleClass('go'); ...

HTML elements are positioned in alignment

I'm currently working on a website and I need some assistance in aligning my images properly. Here's what I have tried: https://i.sstatic.net/suIVu.png However, I haven't been able to achieve the desired result with the following code: &l ...

How can I fix the issue of the header background image not resizing properly? Any suggestions?

I am currently working on improving the responsiveness of my website when a user resizes their browser window. One specific issue I'm facing is resizing the banner image as the window size changes. The website in question can be found at The CSS code ...

How can you efficiently showcase multiple fetch functions on a single page by utilizing loops in PHP?

I am attempting to showcase the countries from each continent on a single page using buttons for each continent. When a user clicks on a button, I want the countries of the selected continent to be displayed. However, I am encountering an issue where only ...

Passing the value in a td element to a JavaScript function using Thymeleaf onClick

Trying to utilize "Thymeleaf" for the first time, I am attempting to pass a value to JavaScript with the following code: onclick="getPropId('${properties.id}')" The corresponding function is as follows: getPropId(inputID){alert(inputId);} Unf ...

Link to text on tablet and phone

On my website, I have included a phone number in the HTML as text. When viewed on an iPad tablet, it automatically interprets the phone number and turns it into an underline blue link. Is there a way to format the phone number in a different style so it d ...

What steps should I take to create a slider using my image gallery?

I have a unique photo gallery setup that I'm struggling with: As I keep adding photos, the cards in my gallery get smaller and smaller! What I really want is to show only 4 photos at a time and hide the rest, creating a sliding effect. I attempted ad ...

The issue of overflow-y not functioning properly in conjunction with ng-repeat has been observed

As indicated in this resource, setting a fixed height for the div is suggested, but it seems like it's not working with the code provided below. <div style="margin-top:0.5vh;"> <div style="height:200px;border:1px solid red;overflow:au ...