Issues with flex divs rendering improperly on mobile browsers

I am working on a website design that consists of two main parts: the top section where the menu is located, and the content section which contains the information.

.wrap {
  position: absolute;
  width: 100%;
  height: 100%;
}

.box {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.top {
  flex: 0 1 auto;
}

.content {
  flex: 1;
  position: relative;
  height: 100%;
}
<div class="wrap">
  <div class="box">
    <div class="top"></div>
    <div class="content"></div>
  </div>
</div>

The menu should take up as much space as necessary for the menu content, while the content should fill the remaining space on the site. Together, they should occupy 100% of the width and 100% of the height.

This setup functions properly on desktop computers, but on mobile browsers like Chrome, Firefox, and Safari, the height of the site increases to accommodate the height of the menu.

The content extends beyond 100% due to the inclusion of the menu. Can someone please help me understand what I may be doing incorrectly in this scenario?

Answer №1

The Fundamentals

If you want to assign percentage heights to elements, their parents must have a specified height. It can be perplexing because these parent elements include <html> and <body>. Take a look at this example:

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

div {
  height: 100%;
  background: lightgreen;
}
<div></div>

In this scenario, both the <html>, <body>, and <div> elements are set with height: 100%. This cascades down to each child element:

  1. <html> occupies 100% of the browser viewport
  2. <body> is 100% of its parent <html>
  3. <div> fills 100% of its parent <body>

To simplify things, we can grant the <body> element 100% height using a viewport height unit. By doing so, we eliminate the need to deal with setting the height for <html>. Here's an alternative example:

body {
  height: 100vh;
  margin: 0;
}

div {
  height: 100%;
  background: lightgreen;
}
<div></div>

Now, the <body> covers 100% of the viewport height, enabling its children to receive percentage heights without explicitly defining a height for <html>.

Your Objectives

Keeping this in mind, let's streamline your example to its essence. Here is precisely what you've described using the minimal number of elements required:

  • The <body> can act as the flex container with the following settings:

    • display: flex and flex-direction: column
    • margin: 0 to eliminate the default body margin
    • height: 100vh to expand it to 100% of the viewport height

    • The <div class="top"> serves as your header containing the menu; no need for a flex property as it will adjust to fit its contents

    • The <div class="content"> is given flex: 1 to grow and occupy the remaining space

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  margin: 0;
}

.top {
  background: pink;
}

.content {
  flex: 1;
  background: lightgreen;
}
<div class="top">Menu Content<br>Menu Content<br>Menu Content</div>
<div class="content"></div>

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

Illustration within the <li> element

I am facing an issue with floating elements inside the <li> tag. Below is my current markup: <li> <img src="concept-truck.jpg" alt="2013 Toyota Tacoma" id="itemImg" style="float:left"> <p>2013 Toyota Tacoma</p> <p>Pric ...

Using Bootstrap to embed YouTube videos results in a sleek, modern design with distinctive black

When my screen is maximized on Chrome, I notice irritating black lines appearing on my responsive Youtube embeds. Unfortunately, I am unable to post an image due to lack of reputation. Specifically, when the video is paused, there is a slim black line at ...

Highlighted Answer: Choosing between two options in the quiz

I am currently developing an employability quiz for my university project. Each question offers a choice between Yes and No answers. If the user selects the Yes answer, it will be highlighted in green and display the correct answer below. However, if the ...

Integrating JavaScript and CSS files into Spring MVC

Greetings, I am currently exploring the Spring Framework and building my first application with it. However, I have encountered a particular issue that I would appreciate some assistance with. Please excuse any errors in my English. The problem I am facin ...

Adjust the alignment of tooltip text to the left in Bootstrap 4

Using Bootstrap tooltip with HTML and looking to customize it. <span rel="tooltip" data-toggle="tooltip" data-trigger="hover" data-placement="right" data-html="true" data-title="{$some_html}"&g ...

Compass encountered an error stating 'Invalid CSS after "expression" was "expression".' Can you believe it?

While working on my project, I encountered an issue with compiling scss files into css using compass. The error message I received is as follows: Error in sass-test/sass/nr-metronic.scss (Line 4 of sass-test/sass/metronic/global/_colors.scss: Invalid CS ...

Tips for stopping a click from going through a fixed element to the one in the background

My website features a fixed positioned header with a search box, overlaying content below it (thanks to the higher z-index). When I click on the search box, an event handler is triggered. However, the click response also passes through the header to the ...

Retrieve Files with Angular Framework

I'm looking to implement a file download or view button using Angular without making a call to the backend. The file is static and the same for all users, so I want to avoid unnecessary server requests. Many solutions involve using the "download" att ...

Using base64 encoding for font-face with alternative fallbacks

After reading this article, I am interested in incorporating a font face in the following way: @font-face { font-family: 'MyWebFont'; src: url('webfont.eot'); src: url('webfont.eot?#iefix') format('embedded-openty ...

Implementing auto-generated IDs for submission buttons for posted content in JavaScript

Users can submit multiple posts by clicking the submit button several times in a form. I have created an HTML template for post containers and assigned automatic IDs using JavaScript. However, the automatic ID is not applied to the post container added whe ...

how can I perform a scrollTo function without any animation effects?

Utilizing the incredible plugin by lions-mark, scrolling to any desired position has become a simple task. I primarily use this feature to maintain the user's scroll location when the page is refreshed (due to the nature of my legacy gaming site which ...

Using the ternary operator for the background-color style attribute in knockoutjs

How can I apply a background color to a td element in a table only when there is data present? If there is no data, I do not want any color to be displayed. Below is the code I currently have: <td data-bind="text:EarlyOut,style:{background-color:EarlyO ...

Two distinct styles showcasing contrasting hues

Can an element have two different styles with different colors? In simpler terms, imagine I have the following HTML code: <table class='my_table'> <th class="style1 style2">LastName</th> ... </table> and this CSS: .my_ ...

Dealing with unnecessary gaps separating div elements within bootstrap and HTML code

I am in the process of developing a responsive control panel using HTML, CSS, and Bootstrap. I have successfully created a navigation header, but there seems to be too much spacing between the header and the body content. How can I reduce this space or set ...

Align text to the left and right with a centered div

Visualize the asterisk * at the center of the div textAlignRight * text AlignLeft This is essentially my goal to accomplish <center> <span class="left_host">Right aligned</span> * <span class="righ ...

Guide on redirecting the user to a specific page using an iframe

i have two different website urls www.kadco-international.com www.kadco-international.com/company.html When using the iframe in asp.net to redirect the user to the company.html page, it always redirects to the index.html. Check out what I have tried he ...

Is there a glitch causing the Owl carousel to malfunction?

I am struggling to implement a fullwidth owl carousel slider on my webpage, but I'm facing issues with getting it to function properly. I suspect there might be a conflict with the current template that I'm using. Here is the step-by-step proce ...

What is the best way to adjust image size according to the dimensions of the parent container using CSS

I am working on a card game using Bootstrap styling and I have a container with a background image of a card table (1000x1000). As I resize the browser window, the card table image shrinks to 800x800, then to 500x500, and finally to 200x200. My issue is th ...

Are HTML mistakes considered as WCAG violations?

In my quest to ensure that my website is WCAG compliant, I have done a lot of research. However, one question still lingers in my mind: Would a document with HTML errors still be considered WCAG 2.0 AA compliant? I recently ran Total Validator and it pro ...

Using a combination of HTML and CSS3, craft this specific geometric design

Here's a sample image demonstrating how to create this button using CSS3. I would greatly appreciate any assistance! ...