Flexbox that adjusts to any browser height, ensuring full responsiveness

I am exploring the world of flexbox with the goal of creating a responsive layout consisting of 3 items that adjust seamlessly to different devices such as smartphones, tablets, and desktop computers. The layout should stack the items on top of each other and collectively fill the width and height of the browser window. Ideally, there should be no need for scrolling. (In other words, one item at the top, one at the bottom, and the remaining items filling the space in between).

While I have managed to handle the width part for desktop and smartphones, I am still puzzled by how to ensure the layout adapts to the varying heights of different devices, including font size adjustments and box alignment.

As a temporary fix, I resorted to increasing the margin for smartphones...

What are your insights on this issue?

Below is the code snippet of what I have implemented:

.container {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
  height: 100%;
  font-size: 3em;
}

.item {
  border: 5px solid red;
  margin-bottom: 40px;
}
@media screen and (max-width: 500px) {
  .element {
    margin-bottom: 70px;
  }
}

.one {
  flex-basis: 100%;
  display: flex;
}

.two {
  flex-basis: 80%;
  display: flex;
}

@media screen and (max-width: 64em) {
  body {
    font-size: 80%;
  }
}

@media screen and (max-width: 50em) {
  body {
    font-size: 70%;
  }
}

@media screen and (max-width: 30em) {
  body {
    font-size: 60%;
  }
}

.three {
  flex-basis: 100%;
}
<div class="container">
  <div class="item one">1</div>
  <div class="item two">
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
    in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
  </div>
  <div class="item three">3</div>
</div>

Answer №1

To ensure that a flex-item takes up all remaining space within the container, simply apply flex-grow: 1;.

Before proceeding, it is important to reset the body margin: body { margin: 0; }. The default body margin set by browsers can vary, leading to inconsistent display. Resetting it helps avoid overflow issues.

Next, set the height of the flex-container to occupy the entire viewport height: .container { height: 100vh; }. Adding padding while ensuring box-sizing: border-box; prevents vertical overflow.

With the flexbox established, adjust the flex-direction:

.container { flex-direction: column; }

Finally, implement flex-grow as mentioned earlier: .two { flex-grow: 1; }. This allows the middle item to fill the remaining height. To separate it from the top and bottom boxes, add a margin: .two { margin: 6px 0; }

In addressing the font-size concern, media queries are not sufficient for universal device compatibility. Scripting or utilizing libraries is recommended, with resources like Fitting Text to a Container proving helpful.

Alternatively, consider eliminating the container and utilizing the body directly for markup.

body {
  margin: 0;
}

.container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  padding: 6px;
  box-sizing: border-box;
}

.two {
  flex-grow: 1;
  margin:  6px 0;
}

.item {
  border: 2px solid red;
}


/* Edit on OP request */
.two {
  display: flex;
  align-items: center;
}
<div class="container">
  <div class="item one">1</div>
  <div class="item two">
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
    in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
  </div>
  <div class="item three">3</div>
</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

Unable to change the variable for the quiz

Currently, I am in the process of developing a quiz app and I am facing an issue with my correct variable not updating. Whenever I trigger the function correctTest() by clicking on the radio button that corresponds to the correct answer, it does get execut ...

Troubleshoot padding problems in mpdf

Note: mpdf 6.0 Greetings, In my efforts to produce precise PDFs using mpdf for future printing purposes, I have encountered an issue with the positioning of elements on the page. I require these elements to be positioned precisely from the top left corne ...

Introducing the World of Wordpress Blogging

How can I create an introduction on my WordPress site similar to the one found at ? I am specifically interested in incorporating the expanding horizon line effect. It seems like it may just be a GIF that plays and then fades into the homepage. Are there ...

PHP is unable to properly process JSON data that contains HTML tags

I'm encountering an issue with my website that communicates with a Java REST server using Jersey. Everything is functioning properly except when I try to receive a JSON object with HTML tags embedded in the content. After running a println statement ...

When the button with an image is clicked, it will display a loading element

I have developed an application that heavily utilizes ajax, and I am looking to implement the following functionality: I would like to have a button with both text and an image. When this button is clicked, it should be disabled, and instead of the origina ...

The delete button is designed to remove the record from the database while keeping it visible on the frontend

When the delete button is pressed, it successfully removes the record from the database. However, it does not disappear from the frontend until the page is reloaded or refreshed. This is what I see: @foreach (var item in Model) { <a href="#" ...

What issues are present in this PHP code?

Can you identify the issue in this PHP code? I am using Netbeans 8.1 and whenever I type $ _GET, an error occurs <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> ...

Is it feasible to add to an ID using ngx-bootstrap's dropdown feature?

In the documentation for ngx dropdown, there is a feature called "append to body." I recently tried changing this to append to a table element instead and it worked successfully. Now, on another page, I have two tables displayed. If I were to assign each ...

How to showcase content on bootstrap across varying screen dimensions

When designing my website, I envisioned having three different options presented upon entering the site. Depending on the size of the screen, I wanted to display varying content. For XL screens, I wanted all content to be visible, similar to the picture. ...

What is the best way to eliminate the blank space between div elements on a webpage?

I'm currently working on enhancing my portfolio website, and I'm struggling to eliminate the excessive white space. After inspecting the element, it appears that there is styling related to ul.AMSI, but it's not referenced anywhere in my st ...

Conceal the sidebar menu by clicking anywhere outside of the menu bar or the designated button

I attempted to create a menu similar to the semantic UI, but so far I have only been able to click the menu button to open and close the menu. I am using a toggle class to display the sidebar, but I'm unsure if this approach is entirely correct: < ...

Removing dropdown lists from input forms can be achieved without using jQuery by utilizing vanilla JavaScript

Looking to build a custom HTML/JavaScript terminal or shell. Interested in utilizing an input box and form to interact with JavaScript functions/commands. Unsure of how to eliminate the drop-down menu that appears after previous inputs. Pictured terminal: ...

:host background color may be missing, but the font size has been boosted?

Check out this demo where the CSS is applied to the :host element or <hello>. The font size increases but the background color remains unchanged. What do you think? styles: [` :host { font-size: 2rem; background-color: yellow; }`] }) ...

Pressing the follow button does not save data in the database

Whenever I click on a follow button, it should store followerid and followingid in the follow table and change to an unfollow button. If I then click on the unfollow button, those entries should be deleted from the follow table. I have used JQuery for this ...

Is it possible to align two buttons side by side on a webpage using only HTML and CSS?

Can you help me figure out how to align two buttons next to each other with some space between them using only css and html? If you take a look at my html code and css code, you'll see that the two buttons are not aligned properly. * { margin: 0 ...

What is the best way to retrieve data from ng-controller to use in the 'src' attribute of an <img> tag?

Check out this snippet of HTML: <div data-ng-controller="sampleCtrl" > <ul data-ng-repeat="item in sampleData"> <li class="message"> <img src="img-source" width="30" height="30"> <div class="message-text"> ...

Step-by-Step Guide: Uploading Multiple Images with Links to a MySQL Database

My current code is functioning perfectly for single image uploads. However, I now have the requirement to upload multiple images simultaneously with the same Image Title and Image Description for each group of images. These images will be used in a photo s ...

What is the best way to give a video a border-radius in an HTML/CSS code?

Is there a way I can apply border-radius to the <video> element? This is what my video currently looks like: (image shown here: <a href="https://i.sstatic.net/izKz0.png") I attempted styling the video tag with CSS, but the changes did ...

Exploring the process of customizing native classes with Vue.js

Within vue-awesome-swiper, there's a standard class called this: .swiper-button-next { position: absolute; right: 46% } The task at hand is to adjust this CSS code only for right-to-left layouts, like so: .swiper-button-next { position: abso ...

ui-router: Issue with ui-sref causing font color to remain unchanged

I've been trying to customize the font color of a navigation link using ui-router's ui-sref functionality, but I'm facing an issue. While I can change the background color by adding it to the .active class in my CSS, the font color remains u ...