Limit the container's height to be no more than the height of the

Seeking input on good and bad practices in CSS/HTML/jQuery, I recently asked a question about determining when it is appropriate to use jQuery to set container dimensions. The responses I received were helpful (view them here).

Realizing that jQuery may not be the optimal solution, I am reaching out for additional insights on this issue.

The structure of my page involves PHP, with a common header across all pages and content dynamically loaded using PHP. Therefore, wrapping the header and div in a single container is not feasible:

include ("header.php");
include ("$lang/$section.php");
include ("footer.php");

The header has a fixed height (100px + 100px margin-bottom), followed by a div that should adjust its height based on screen size: below 768px, it should fill the remaining space, otherwise, have a maximum height of 420px with padding of 100px 0.

This div contains three floated columns that need to occupy the available space within the parent div.

Traditionally, I would utilize jQuery to calculate screen height subtracting header height, margins, and paddings. However, it seems this approach is not recommended for achieving this layout.

In summary, I am looking for a CSS-based method to ensure the div fills the space between the header and bottom of the viewport for screens smaller than 768px, capped at a max-height of 420px. While jQuery offers an easy solution, I am eager to explore a cleaner CSS alternative.

If anyone has suggestions or ideas, I would greatly appreciate your input.

For reference, here is a link to the fiddle containing the code.

Thank you in advance!

Answer №1

If you want to adjust the height of a section using calc() and vh (viewport height), here's how you can do it:

To check the browser support for calc(), visit: http://caniuse.com/#search=calc

For browser support on vh, go to: http://caniuse.com/#search=vh

An example would be setting the height of a section with calc(100vh - 200px), where 100vh represents the viewport height and 200px is the height of the header.

In addition, you can include a media query to limit the height to 420px when the screen height is above 768px.

Try implementing this CSS code snippet:

header { height: 100px; background: #ccc; margin-bottom: 100px;  box-sizing: border-box; }

section { width: 100%; height: calc(100vh - 200px); padding: 50px 0; background: yellow; box-sizing: border-box; }

.col1, .col2, .col3 { float: left; width: 33%; }

.colPadding { padding: 25px; background: blue; }

.cb { width: 100%; height: 1px; clear: both; }

body {
  margin: 0;
}

@media screen and (min-height: 768px) {
  section {
    max-height: 420px;
  }
}
<header>
    This is my header with 100px bottom margin
</header>
<section>
    <div class="col1">
        <div class="colPadding">
            section with padding: 50px 0; and max-height: 420px;
        </div>
    </div>
    <div class="col2">
        <div class="colPadding">
            Column 2
        </div>
    </div>
    <div class="col3">
        <div class="colPadding">
            Column 3
        </div>
    </div>
    <div class="cb"></div>
</section>

Answer №2

Experimented with the CSS3 flex-box model and screen media queries to create a responsive layout. Check out my code snippet.

Instead of using 764px, I opted for 300px in the fiddle (feel free to adjust it as needed for testing purposes).

CSS Styling Applied:

* { box-sizing: border-box; } /* ensure sizing consistency based on borders */
body {
    display: flex; /* utilizing flex for body as wrapping header and section isn't permitted */
    flex-flow: column wrap;
}
header {
    height: 100px;
    background: #ccc;
    margin-bottom: 100px;
    flex: 0 0 auto; /* keeping header size fixed */
}
section {
    width: 100%;
    max-height: 420px;
    padding: 50px 0;
    background: yellow;
    /* fill remaining space */
    flex: 1 1 auto;
    /* allow child columns to take full width */
    display: flex;
    flex-flow: row wrap;
    /* stretch immediate children to maximum possible height */
    align-items: stretch;
}
.col1, .col2, .col3 {
    float: left;
    /* occupy available space */
    flex: 1 0 auto;
}
.colPadding {
    padding: 25px;
    background: blue;
}
.cb {
    width: 100%;
    height: 1px;
    clear: both;
}

/* Additional CSS */
/* styling for screens equal to or less than 300px (modifiable to 768px) */
@media screen and (max-height: 300px) { 
    body {
        height: 100vh; /* set body size to full screen */
    }
    header {
        margin: 0px; /* eliminate bottom margin */
    }
    section {
        padding: 0px; /* remove top/bottom padding and margins */
        margin: 0px;
    }
}

Learn more about CSS3 flex-box here.

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

RAZOR - Strip image elements from variable with HTML content

In the code snippet below, I am using a helper to display content: @Html.Raw(Model.PageData.PageContent) My goal is to filter out any image tags that may be present. I have explored methods like .substring(), but they require specific indices or string n ...

The functionality of CSS isn't up to snuff on Mozilla Firefox

I am having an issue with CSS styling on a <div> element in my PHP page. The CSS style is working perfectly in Google Chrome, but when I test my code in Firefox, the styling is not being applied. .due-money { background-color: #09C; color: whi ...

Centered on the page vertically, two distinct sentences gracefully align without overlapping, effortlessly adjusting to different screen sizes

I had no trouble centering horizontally but I'm struggling with vertical alignment. I want these two sentences to be positioned in the middle of the page, regardless of device size. I attempted using vertical-align without success and then tried posit ...

What is the process for altering the active status in pagination?

How can I make the active class in my pagination appear in red? Here's the code snippet I have: In my script.js file: $(".pagination").append("<li class='page-item' id='previous-page'><a class='page-li ...

Tips for sending a string as HTML content in VueIn Vue, you can send a string as

I am attempting to pass the value for exp by using this code snippet. However, the form of selectedChannel.explanation appears as "channel name". Is there a way for me to display exp as channel name? computed: { channel: { get() { ...

Having trouble getting Sass extending to work in a basic scenario?

Here we have a simple example of Sass code, an Angular 2 component that accesses the Sass code, and the rendered HTML. However, there seems to be an issue with the blueBig span element in the last part. As someone new to Sass, I am not sure why this basic ...

When attempting to print a Rectangle on my webpage using JavaScript and taking user input, I encountered an error stating that 'str' is not defined

I'm trying to implement a feature where clicking on the "try it" button will prompt the user for the number of lines and then print that many lines in the form of a rectangle. However, when I run my code, nothing appears on the DOM and there is an err ...

What is the best way to initially conceal content and then reveal it only after an ajax call is made?

Currently, I have a situation where content is revealed after the callback function of a .js library (typed.js) executes. Here is the script I am using: Javascript $(function(){ $("#logo-black").typed({ strings: ["Nothing^450&Co^250.^500" ...

Elegant Popup Form

I'm encountering a problem with my ajax form that is embedded within a bootstrap modal. On the first use, the form functions correctly. However, when I attempt to use it again without refreshing the page, the text area no longer appears within the mod ...

"Transforming a static navbar to a fixed position causes the page to jump

Having some difficulty figuring this out. I'm working on a bootstrap navbar that transitions from static to fixed when the user scrolls past the logo at the top. Everything seems to be working fine, except for when the navbar reaches the top, it sudde ...

An issue with JSPDF arises when used on mobile devices

Currently, I am working on a project to create a responsive web application, which involves utilizing JSPDF for generating PDF reports directly from HTML. For a demonstration of the functionality, you can check out this Demo. Unfortunately, when trying t ...

preserve functionality when switching between pages

I have created two simple functions that can change the background color of the body. <script type="text/javascript"> function switchBackground(color){ document.body.bgColor=color; } </script> I am using links with onclick to execute thes ...

Discovering the dimensions of a disabled attribute within a table using XPath in Selenium with Java

I'm attempting to determine the number of columns in a specific table, but some are disabled - I'd like to know if it's possible to get the count without including the disabled ones (only counting the visible columns). As you can see in the ...

Ensuring various conditions with ngIf

I've created a toggle function that updates the text of a link, but I'm struggling to handle multiple conditions. For example, *ngIf="condition1 or condition2". Below is an excerpt from my code: <a routerLink="/ads" class="tip" (click)="togg ...

Maximizing page space with ReactJS and advanced CSS techniques

I'm currently in the process of learning CSS and struggling a bit with it. My main issue right now is trying to make my react components fill the entire height of the browser window. I've been using Display: 'grid' and gridTemplateRows: ...

Failure to link style sheet to document

Experiencing some difficulties with a seemingly simple task that I can't figure out. When I check my code in the browser, none of my styles are being applied to the HTML elements. The style sheet is located in the same folder as the main document, and ...

The Opera browser displays a horizontal orientation for vertical menus

Having some trouble with my vertical menu rendering correctly in different browsers. It's appearing horizontal in Opera, but looks good in Firefox and Chrome. I'm pretty sure it's just a small tweak needed in the code, but I can't quite ...

Creating a dynamic progress bar that scrolls for multiple elements

I am currently working on implementing a scrolling progress bar to show users how much of an article within a div they have read. For reference, something similar can be seen on this site. I have created my custom progress bar and coded it on fiddle, whe ...

Loading jsp content into a div dynamically within the same jsp file upon clicking a link

Currently, I am working on creating a user account page. On this page, my goal is to display related JSP pages in a div on the right side when clicking links like 'search user' or 'prepare notes' on the left side. In my initial attempt ...

Clashing CSS Styles: Font Size Edition

Can someone explain how CSS font sizes work? I set a specific line to be 200% font size, but changing the body font size affected it. Shouldn't the line maintain its specified size? When the body font size is changed from 12pt to 8pt, the line "There ...