Assistance needed for adjusting margins and padding within the inner content box

I'm currently in the process of designing a new website layout using CSS, but I've encountered some challenges along the way. My main goal is to ensure that all elements stay within the boundaries of the browser window, with the scroll bar appearing only within the content area, not on the browser itself. To achieve this, I have created an outermost DIV that acts as the "wrapper" for the site. This wrapper is centered, has a set width, and takes up 100% of the height of the browser window. Inside this wrapper, I have included a header that displays correctly in various browsers.

However, when I try to add my actual content DIV inside the wrapper, I struggle to define its size properly. Adding margins or padding to account for the header causes the content to overflow, making it impossible to implement scroll functionality. On the other hand, directly setting the size of the content DIV poses a challenge as well. Any values I input end up being too large due to the additional margin and padding, causing the content to exceed the dimensions of the browser window and overflow.

Can anyone suggest CSS styles that could help me achieve the desired look for the wrapper and content DIVs? Here's a diagram illustrating the appearance I'm aiming for.

Answer №1

Assuming you have a fixed height for your header (not a percentage), the example below uses pixel values for easier inspection with Firebug, but it works the same with em values.

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Title</title>

    <style type="text/css">

        html, body {
            margin:0;
            height: 100%;
        }

        #wrapper {
            position:relative;
            width: 400px;
            height: 100%;
            margin: 0 auto 0 auto;
        }
        #header {
            position:absolute;
            margin-top:0;
            height: 70px;
            width: 400px;
            background-color: #ccc;
        }
        #content {
            position: absolute;
            top: 70px;
            bottom: 0;
            width: 400px;
            overflow: auto;
            background-color: #99c;
        }
    </style>

</head>
<body>
    <div id="wrapper">
        <div id="header">Header</div>
        <div id="content">
            Content<br/>
            Content<br/>
            Content<br/>
            Content<br/>
            Content<br/>
            Content
        </div>
    </div>
</body>
</html>

The key here is that #content does not have a set height - instead, it utilizes a combination of top and bottom properties.

Note: It may be necessary to make modifications for IE 6...

Answer №2

<style>
html, body {
    margin: 0;
    padding: 0;
    text-align: center;
    overflow: hidden; // To prevent scroll bars in the browser window}
#content_wrap {
    width: 900px; // Adjust as needed
    margin: 0 auto; // To center the div
    text-align: left; 
    height: 100%; 
    position: relative; // Foundation for absolute positioning }
#header {
   position: absolute; 
    top: 0; 
    left: 30px; 
    width: 850px;}
#content {
    position: absolute; 
    left: 0; 
    top: 20%; 
    height: 80%; 
    overflow: scroll; 
    overflow-x: hidden;}
</style>

The issue here, and with any solution I can come up with, is that without CSS expressions, achieving a #content div height that reaches the bottom of the screen other than using a percentage is challenging. The problem lies in not having a fixed space allocated for your header to operate within. There isn't a CSS equivalent for setting attributes like "#content div should have a 200px top margin and extend to the bottom of the page."

However, this can easily be accomplished with JavaScript.

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

Show only half of the Google Charts

I have a code snippet that displays a chart with dimensions of 500x500. However, I only want to show half of the chart, like 500x250. But whenever I adjust the values in the div, it resizes the entire chart instead of just showing half. My goal is to hide ...

I would like to style the input checkbox using CSS

Is there a way to style input checkboxes with CSS that will work on all browsers (Chrome, Firefox, IE 6, 7, and 8)? If jQuery is the only solution, please let me know. It needs to be compatible with all browsers. Can anyone provide guidance on how to ach ...

Modifying the background image of div elements with JQuery in a loop function is ineffective when using Google Chrome

I am facing an issue in my application where I have a for loop to change the background image of two divs. The code snippet is as follows: for (var i = 0; i < length; i++) { $("div_" + (i + 1)).css("background-image", "../imageFile.png"); ...

Combine the jQuery selectors :has() and :contains() for effective element targeting!

I am trying to select a list item element that has a label element inside it. My goal is to use the :has() selector to target the list item and then match text within the label using the :contains() selector. Can I achieve this in a single line of jQuery ...

Issue with margins of sections when attempting to activate menu class while scrolling

My Objective: I want to create a website where the menu highlights the section that is currently being viewed as the user scrolls up and down. Progress So Far: I have successfully implemented a functioning menu that changes to "active" when the correspo ...

Simple Method To Dynamically Align jQuery LightGallery Thumbnails On a Responsive CSS Website

Can anyone assist me quickly? I've implemented everything but am facing a slight issue with the centering of my ul code. I've tried various solutions without success. Here is my code, any advice would be greatly appreciated (I may sound stressed ...

What is the best way to control and manipulate this parallax effect?

Having trouble getting the icon dog to appear correctly in this parallax effect. I want the full body of the dog icon to be displayed over the section, but I'm having no luck so far. Please see my code below and let me know if there are any correction ...

Utilizing CSS to print specific columns on a separate page at regular intervals

I am facing a challenge with printing a very large HTML table, which is dynamic in the sense that the number of rows and columns can vary. When printing, the rows that exceed the page length are automatically continued on the next page. However, I also nee ...

Rapache and R team up for dynamic leaflet design

I'm currently working on creating a webpage using Rapache and "Leaflet for R". My main goal is to integrate R into an html page using brew. However, I am facing difficulties in displaying my map within the html page without having to save it as an ext ...

Ensure that the navigation menu is consistently displayed properly, without any of its contents extending beyond the

I am experiencing an issue with the navigation menu on my website. Everything looks great in normal view, but when I resize the browser window to a smaller width, the menu ends up overflowing below the main content. I want the navigation menu to always dis ...

Change the display, animate the elements within

Currently working on a landing page with a contentSwitcher and everything is functioning properly. However, I am interested in animating the contents sequentially to add some stylish flair. Take a look at the Test Landing Page for reference. If you observ ...

Steps to create an automatic submission feature using a combobox in HTML5 and then sending the retrieved data back to the HTML file

Here is the code snippet I've been working on: <strong>Station Name</strong> <!--This portion includes a combobox using HTML5 --> <input type=text list=Stations> <datalist id=Stations> <option>Station1</opt ...

Achieve a full height for children without the need to specify a fixed height for the

I'm currently working on a container div that houses two child divs: the first one, positioned to align with its parent's left border, contains a series of buttons while the second one holds the main content. In essence, it operates like a tab na ...

Use jQuery to retrieve HTML content excluding comments

Take a look at the code snippet below. HTML: <div> <p>sdfsdfsfsf</p> <!--<p>testing</p>--> </div> JQUERY $(document).ready(function(){ alert($("div").html()); }); OUTPUT <p>sdfsdfsfsf</p> & ...

Can the .scroll function be turned off when a user clicks on an anchor link that causes the page to scroll?

I am currently developing a timeline page and I want to implement a feature similar to the chronological list of years displayed on the right side of this webpage: As part of this feature, I have set up a click event which adds a circle border around the ...

What is the best way to extend a column across multiple rows with bootstrap?

https://i.sstatic.net/uPsLe.jpg Looking to convert the design above into HTML and CSS, specifically trying to achieve a layout where the image spans across 2 rows. However, I've been having trouble making it responsive even after trying some code sni ...

Override the default HTML style overflow to hidden with Material UI Swipable Drawer

Currently, I'm utilizing the Material UI SwipableDrawer component which has an unexpected drawback of causing the scrollbar to disappear when the drawer is displayed. This issue overrides my specified style of "overflow-y: scroll" on the Html tag and ...

Bootstrap Toggle Button with Dropdown Menu

I have a button-group with font awesome icons and a dropdown on one of them, as illustrated in the example below. I am trying to toggle the button status, but for some reason, the 'active' class is automatically removed on mouseout. It seems to ...

Unable to display image on React page using relative file path from JSON data

Greetings, this is my initial post so please forgive me if I have missed any important information. I'm currently in the process of creating a webpage using react. My goal is to display content on the page by iterating over a relatively straightforwa ...

The CSS command for displaying additional content is not functioning within table elements

I'm attempting to add a 'read more' arrow to the third column, which should expand the text in the first column when clicked. I have the following code, and it works fine outside of a table but not inside one. Where am I going wrong? I prefe ...