A stack of four distinct CSS components placed vertically

I'm struggling to create a layout with specific requirements:

  1. A navigation header that has a fixed height and position from the top
  2. A wide horizontal image banner that is fixed but resizable with the main window
  3. A content area where text starts below the image banner, with an auto scrollbar
  4. An information footer with a fixed height and position from the bottom

The issue I'm facing lies with requirement #3 - the content text area. I can't use a fixed position due to the resizable image, nor can I use a relative position by placing the content DIV within the image DIV as this causes the image to scroll along with the text. It seems like making the content text a child of the image banner is necessary due to the variable image size, but I'm unable to achieve the desired outcome.

HTML:

<div id="nav">
... navigation banner ...
</div>

<div id="topimg">
<img src="images/01-shop.jpg" width="100%">

<div id="content">
... text ...
</div>

</div>

<div id="footer">
... footer content ...
</div>

CSS:

#nav {
position: fixed;
height: 47px;
width: 90%;
margin-left:5%; 
margin-right:5%;
}

#topimg {
position: fixed;
width: 80%;
margin-left:10%;
margin-right:10%;
top: 90px;
}

#content {
position: ???;
width: 80%;
margin-left:10%;
margin-right:10%;
overflow: auto;
}

#footer {
position: fixed;
height: 27px;
width: 90%;
bottom:10px;
margin-left:5%; 
margin-right:5%;
}

If anyone has suggestions on how to resolve this, I would greatly appreciate it!

Thank you in advance!

Answer №1

Are you searching for this solution? - FIDDLE

I don't think there is a need to extensively use position: fixed for a simple layout.

(I included borders for better visualization)

CSS

#nav {
  height: 47px;
  width: 90%;
  margin-left:5%; 
  margin-right:5%;
  border: 1px solid black;
}

#topimg {
  width: 80%;
  margin-left:10%;
  margin-right:10%;
  margin-top: 20px;
  border: 1px solid red;
}

#content {
  width: 80%;
  margin-left:10%;
  margin-right:10%;
  overflow: auto;
}

#footer {
  position: fixed;
  bottom:10px;
  height: 27px;
  width: 90%;
  margin-left:5%; 
  margin-right:5%;
  border: 1px solid blue;
}

Answer №2

View the live demo here: http://jsfiddle.net/jkkhbku8/5/

This solution should address your query if I have interpreted it correctly. The implementation involves several steps, so I will endeavor to elucidate each one.

To start, you should include a pseudo element beneath the header to bridge the gap between the header and the image banner. This ensures that when text scrolls up, the space is filled with the same background color as the document body.

Subsequently, as the image banner div is fixed in position but needs to be responsive to varying browser widths, it must have a height of 0px and utilize padding to adjust its height based on a percentage of its width. Due to the margin set at 10% on each side, for a standard image ratio of 16:9, the optimal padding-bottom value would be around 45%. However, this may require adjustments depending on the specific characteristics of your image.

#topimg {
    height: 0px;
    padding-bottom: 45%;
}

If the image ratio is unknown, JavaScript can help determine it and dynamically set the required padding.

Regarding the content, you should first move it down using margin-top: 90px (equivalent to the header's height), followed by pushing it down further with padding-top: 45% (corresponding to the height of #topimg). This arrangement ensures that the content seamlessly aligns under the resizing image div. Additionally, a bottom margin should be applied to prevent interference with the footer during scrolling.

Finally, to conceal any text below the fixed footer, it should be kept anchored at the bottom using padding-bottom: 10px instead of bottom: 10px, accompanied by a solid background color. While an additional pseudo element could be utilized, employing padding in this instance proved equally effective.

I trust this explanation proves beneficial towards achieving your desired layout.

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 the issue with the jquery height function not functioning properly

I am working on a website where the menu bar and footer need to be 50px in height as shown in the image. In between the nav and footer, there is a content div that should occupy the remaining space. To achieve this, I plan to use Jquery to calculate the do ...

Retrieving information from mongoDB and showcasing it on an HTML page

I need some help with fetching data from my MongoDB database and displaying it on an HTML page. I have already set up the server.js file for the data retrieval. Here is the content of my server.js file: const path = require('path'); const expre ...

Utilizing jQuery for dynamic dropdown options

Hey there! I have this jquery dialog where users are asked questions like staying on campus, in-state/out-of-state status, and duration of attendance. I want to calculate the total cost based on their choices which includes (tuition + Room & Board) multipl ...

Tips for creating a full-width background image in a Bootstrap 4 carousel

I am struggling with making the images in my 2 slide carousel full width. I have built the carousel using Bootstrap 4, and each slide has an image as its background. Typically, I would use CSS properties like background-size: cover, background-attachment: ...

Utilizing Regular Expressions in Vb.net for extracting telephone numbers

I have developed a code to extract mobile numbers from a web link. I have three links in a list box and I am extracting their source code using the following code. However, when I try to use RegEx to Extract Phone Numbers, I keep getting the same number re ...

What is the reasoning behind beginning the transition with autofocus enabled?

After creating a contact form with autofocus="autofocus", a strange behavior was noticed. It seems that when the form has autofocus, the transition effect on the navigation bar is triggered in Firefox only. Is this an error on my end or just a bug specific ...

When combining CSS grids, nesting them can sometimes cause issues with the height layout

Check out the code on jsFiddle .component-container { width: 800px; height: 200px; background-color: lightyellow; border: 1px solid red; padding: 10px; overflow: hidden; } .component-container .grid-container-1 { display: grid; grid-tem ...

"Implementing a bookmark/watch feature with a hover text cloud using either a Drupal module or CSS styling

Seeking guidance on implementing features in Drupal core. Interested in allowing users to subscribe to various aspects of a node, such as bookmarking for later viewing or rating it. Want a consistent theme across the site and for these features to use togg ...

Connecting CSS auto-complete with JSP content within Eclipse

Is there a way to connect the CSS autocomplete with the content of a JSP page? For instance, if I have an element like <div id="myid"> in the JSP file, can Eclipse auto-complete "myid" when typing "#" in the CSS file? I am aware that NetBeans has th ...

What steps should I follow to create a basic file upload page on my server?

Recently, I've been on a quest to create a straightforward file upload page that allows me to easily send files to my server. The concept is for the site to have a feature where I can simply upload a file and have it stored in a specific folder on the ...

exploring the network of connections

This is what the HTML structure of the webpage looks like: <body> <form> <input type='file'/> </form> <div id='list'> <div>value here<input id='delete' type='button'/>< ...

Ways to divide a foreach loop or for loop into sets of 10 records

Let's say I have 56 entries in my database. To display them, I am using a FOR LOOP. My goal is to add a div element after every set of 10 records. <?php $totalRecords = 56; for($i=0; $i<$totalRecords; $i++){ e ...

Angular Position Animation with PhysicsJS

A juggling game is being created using PhysicsJS. The game consists of a ball in the shape of a circle and a shoe in the shape of a square using a convex-polygon. The shoe remains fixed and moves along the x-axis when the mouse is moved. The square shoe: ...

Loop through the elements of a class in JavaScript and choose all except for the one that was

Imagine having 5 div elements, each with a similar onclick-function that hides the other divs when clicked. HTML: <div id="1" class="divs" onclick="hide()"></div> <div id="2" class="divs" onclick="hide()"></div> <div id="3" cla ...

I am experiencing difficulties with certain images not appearing on my website

Everything was running smoothly while I was working on my website locally. However, as soon as I uploaded it online, some of the images refused to load and are now missing. I built my website using wordpress version 3.7.1. If you'd like to take a loo ...

Troubleshooting an issue with Laravel's Bootstrap 5.2 dropdown functionality

After setting up a pre-configured navbar from the Bootstrap website, I noticed that the dropdown feature is not working. This issue arose with the latest version of Bootstrap 5.2 which was integrated into my Laravel project without any modifications to the ...

Ways to alter the color of individual pseudo elements using a CSS loop

While working on creating a typewriter animation effect for my website using only CSS that I discovered through some online research, a question popped into my mind - is it possible to set different colors for each text in the animation? I have included t ...

Tips for layering one div on top of another div

I'm looking for guidance on how to position my button divs over my Trapezoids in an overlay fashion. Currently, I have set up an array to store the colors and utilizing the map function to match each button with its corresponding color type. When a ...

Jquery vertical menu with a dynamic sliding animation from the right side to the left side

Hello everyone, I am looking to have my vertical menu slide from right to left instead of the typical top to bottom sliding that comes with using .toggle() in jQuery. Specifically, when the hamburger menu is clicked, I want the menu to slide out from right ...

Repetitive horizontal Google Maps marker on ImageMapType

<!DOCTYPE html> <html> <head> <title>Custom Image Mapping</title> <style> html, body, #map-canvas { height: 100%; margin: 0px; padding: 0px } </style> <scrip ...