Ensuring complete height and width with no scrollbar in a Material UI React application

I'm attempting to create a page that fills the entire height of the screen without adding an undesirable scrollbar. When I say 100% height, I mean it should just fit the size of the screen.

Here is a demonstration of the issue. The yellow highlighted area represents the unwanted added height, and there is also a horizontal scrollbar present:

https://i.stack.imgur.com/yz07O.png

Below is the code snippet for rendering the page:

return (
    <>
        <Box display='flex' flex='1' justifyContent='space-around'>
            <IndexSelector
                id='index'
                value={symbol}
                onChange={this.onSymbolChange}/>
            <SeriesSelector
                id='series'
                seriesList={Form.seriesList}
                onChange={this.onSeriesChange}/>
            <DateRange fromDate={fromDate} toDate={toDate} onChange={this.onDateChange}/>
        </Box>

        <Box height='100%' border='1px solid red' marginTop='50px'>
            <Graph instructions={this.getInstructions()} apiData={this.apiData} />
        </Box>
    </>
)

The following CSS is used in index.css:

html {
  box-sizing: border-box;
}

html, body, #root {
  padding: 0px !important;;
  margin: 0px !important;;
  height: 100vh;
  width: 100vw;
}

*, *:before, *:after {
  box-sizing: inherit;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
    "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
    sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

code {
  font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
  monospace;
}

Any suggestions on how to eliminate the extra height and width, and ensure the red-bordered container fills the full height?

EDIT: I followed @gowatham's suggestion but didn't achieve the desired outcome. Here's what I got:

EDIT 2:

HTML: https://pastebin.com/Qu2RFHe7 CSS: https://pastebin.com/1z3Zg5rv

https://i.stack.imgur.com/UvBz8.png

Answer №1

To achieve a responsive layout where the main container takes up 100% of the viewport height and displays its children in a column direction using flexbox, you can set the result container to take up the remaining space by applying flex: 1. Additionally, you can make the content inside this container scrollable by setting overflow: auto.

An advantage of this method is that it allows the filter container to have variable heights and still function correctly.

<Box height="100vh" display="flex" flexDirection="column">
  <Box>Filter</Box>
  <Box flex={1} overflow="auto">
    {Array.from(Array(100)).map((v, i) => (
      <div key={i}>Testing {i}</div>
    ))}
  </Box>
</Box>

https://codesandbox.io/s/material-demo-ntvoj

Another solution, which may work in older browsers, involves setting the filter container to position: fixed and ensuring that the element following it has enough margin-top or padding-top to prevent its content from overlapping when scrolled to the top.

<Box
  position="fixed"
  top={0}
  height="60px"
  width="100%"
>
  Filter
</Box>
<Box marginTop="60px">
  {Array.from(Array(100)).map((v, i) => (
    <div key={i}>Testing {i}</div>
  ))}
</Box>

https://codesandbox.io/s/material-demo-4m85i

Answer №2

Mastering CSS styling may seem daunting at first, but once you have a solid plan for your page layout, it becomes much more straightforward.

An effective approach is to allocate 90% of the height to the body and reserve the remaining 10% for the filter box above. This ensures that the total height remains constant at 100%, unless modifications to the layout are desired. Here's an example:

return (
  <div style={{ height: '90vh', margin: 0, padding: 0 }}>
    <Box display='flex' flex='1' justifyContent='space-around' style={{ height: '10vh' }}>
        <IndexSelector
            id='index'
            value={symbol}
            onChange={this.onSymbolChange}/>
        <SeriesSelector
            id='series'
            seriesList={Form.seriesList}
            onChange={this.onSeriesChange}/>
        <DateRange fromDate={fromDate} toDate={toDate} onChange={this.onDateChange}/>
    </Box>

    <Box style={{ maxHeight: "100%", overflow: "auto" }}>
        <Graph instructions={this.getInstructions()} apiData={this.apiData} />
    </Box>
  </div>
)

If a 1px border causes the height to exceed 100% resulting in a scrollbar, simply add overflow: hidden to the parent div to hide the scrollbar. Explore this concept further on CodeSandbox:

https://codesandbox.io/s/material-demo-djgiu?fontsize=14

Answer №3

To find the difference in height between box1 and box2, follow these steps:

Set a reference to box1:
ref={(ref) => this.box1 = ref}

In box2, adjust the height using the calculated value:

height={`calc(100% - ${this.box1.clientHeight}px)`}

Here's an example implementation:

return (
    <>
        <Box display='flex' flex='1' justifyContent='space-around' ref={(ref) => this.box1 = ref}>
            <IndexSelector
                id='index'
                value={symbol}
                onChange={this.onSymbolChange}/>
            <SeriesSelector
                id='series'
                seriesList={Form.seriesList}
                onChange={this.onSeriesChange}/>
            <DateRange fromDate={fromDate} toDate={toDate} onChange={this.onDateChange}/>
        </Box>

        <Box height={`calc(100% - ${this.box1.clientHeight}px)`} border='1px solid red' marginTop='50px'>
            <Graph instructions={this.getInstructions()} apiData={this.apiData} />
        </Box>
    </>
)

Answer №4

Make sure to implement Flex for the Parent element as well

<div display='flex' flex='1' height='100%' justifyContent='space-between'> //consider using flex direction column in accordance with your library     
        <Box display='flex' flex='1' justifyContent='space-around'>
            <IndexSelector
                id='index'
                value={symbol}
                onChange={this.onSymbolChange}/>
            <SeriesSelector
                id='series'
                seriesList={Form.seriesList}
                onChange={this.onSeriesChange}/>
            <DateRange fromDate={fromDate} toDate={toDate} onChange= 
   {this.onDateChange}/>
        </Box>
        <Box>
            <Graph instructions={this.getInstructions()} apiData={this.apiData} />
        </Box>
    </div>

Answer №5

Instead of using height: 100% in the parent element, opt for height: '100vh' in CSS. When using 100%, it will only take up to the maximum height of all content and won't enlarge further. On the other hand, 100vh represents the vertical height of the entire window. Don't forget to check the overflow-y property as well.

Answer №6

I adjusted the heights of the navigation division to 15vh, the following division to 20vh, and the last division to 65vh, totaling 100 vh, which is the height of the device. Feel free to customize these heights as needed. In the desktop view, there is no scrollbar present.

Check out the responsive design on my CodePen page. (I used the code provided in pastebin link shared) https://i.stack.imgur.com/Rky4d.png

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

Challenges Faced When Implementing Dropdown Navigation Bars

Whenever the screen width is less than 576px, I notice that the menu icon on my website shifts its position along with the title (FOOD, LLC). How can I resolve this issue? Website: ...

Is it possible to integrate a Twitter bootstrap theme into a Rails 4 application if the Vendor/assets directory is missing?

My goal is to integrate a theme from wrapbootstrap.com into my rails 4.1 application. After creating a new app with rails 4 scaffolding, I proceeded to extract the downloaded zip directory which contains 4 directories and an index.html file. I then placed ...

Set the style of the mat-select element

I'm having an issue with my select option in Angular Material. The options look fine, but when I select one, the strong tag disappears. Can anyone help me style only that part? Thank you in advance. <mat-select formControlName="projectId" ...

Enhance your export capabilities with Material-UI theme integration for helper functions

Looking to create a new file that will house helper functions for multiple components sharing the same logic. The challenge arises when trying to access my locale.ts file within this new file in order to utilize the current language based on the theme (loc ...

Increase the size of a button using CSS styling

Looking for guidance on extending the length of a button in HTML code? Take a look at this snippet: <span class="xp-b-submit xp-b-submit-btn xp-b-right"> <a href="#" class="xp-t-bold" id="PostSubmitLink">Post Search</a> </span> ...

Optimal Strategy: Utilizing Spring Boot for Backend Development and jQuery for Frontend Interface

I am currently tackling a project that involves a Spring Boot 2 Backend and a jQuery Frontend. The frontend communicates with the backend by sending Ajax requests to Spring REST controllers in order to interact with database entities. One of the challenge ...

Issues with styled components not properly rendering styles

These are the two components I'm working with: export const Icon = styled.svg` width: 8px; height: 8px; background: black; ` export const Dots = styled.div` border: 2px solid green !important; height: 30px; background: white; width: 1 ...

TypeScript fails to detect errors in setting state with incorrect interface properties in React components

Even though I clearly defined an interface with specific props and assigned that interface to be used in useState, no error is triggered when the state value is set to an array of objects with incompatible props: This is how ResultProps is defined: interf ...

Angular 4: Conditional CSS classes causing issues with transitions

After scouring through stackoverflow, I have yet to find a solution to my current issue. I am utilizing a conditional class on a div that is applied when a boolean variable becomes true. Below is the code snippet in question: <div [class.modalwindow-sh ...

Ways to ensure a video completely fills the frame

I am experiencing an issue where the video in the main div of my home page does not fully fill the div right away - I have to refresh the page for this to happen. As a result, there is black space on the left and right sides. Interestingly enough, this pr ...

How can I make my Grid items in React and Material-UI occupy the entire horizontal space without wrapping to the next row?

I am currently utilizing Material-UI within a React 16.10 project. My goal is to create a table where the left column consists of an icon, while the right column displays a label and address stacked on top of each other. I want the items in the right colum ...

Error message: The variable "m" is not defined in the runtime of a React build

While my React build runs perfectly with `yarn start`, I am encountering runtime errors. Specifically, I see two consecutive "Uncaught ReferenceError: m is not defined" messages in the Chrome console. The main dependencies of my application include deck.gl ...

Adding custom translations to an Ant Design and React.js app can be seamlessly achieved by following these steps

Currently, I am working with Antd and React.js for my project. Does anyone have any advice on how to integrate Internationalization into my application? Specifically, I would like to add custom translations for my own components. While I understand the d ...

Loop through each div using jQuery and dynamically add or remove a class to

I am looking to create an infinite loop that adds a class to each div with a timeout in between. Currently, I have implemented it like this: $(document).ready(function() { $('.small-bordered-box').each(function(i) { var $t = $(this); ...

Removing HTML tags from Angular template bindings

My data list sometimes includes HTML elements <li *ngFor="let result of results"> <span [innerHTML]="result.question.title"> </span> </li> The issue with using innerHTML is that the HTML is parsed and rendered, affecting ...

Switch the CSS class when clicking on a responsive table within a table

I am looking to update the background color of a row within a table when a tr is clicked. Below is the code for the table: <div class="table-responsive"> <table class="table table-hover"> <thead> ...

What is the correct method for typing a React functional component with properties?

Here's a react functional component I have created... const MyFunction = () => { // lots of logic MyFunction.loaded = someBoolean; return <div>just one line</div> } MyFunction.refresh = () => ....... I added two properti ...

Encountering a 400 status error while trying to fetch data using React Hook Form

My custom book search application allows users to input a query, which then displays the response data. After following the example for schema validation from the React Hook Form documentation, I encountered an Error: Request failed with status code 400. ...

What is the best way to load the nested array attributes in an HTML table dynamically with AngularJS?

I attempted the following code, but I am only able to access values for cardno and cardtype. Why can't I access the others? Any suggestions? <tr ng-repeat="data in myData17.layouts"> <td ng-show="$index==1">{{data.name}}</td> &l ...

Tips for properly filling empty spaces in a flexbox layout using HTML

Is there a way to fill in the gap using CSS so that boxes automatically take up the empty spaces, even if they are of different sizes? How can I make sure the boxes adjust automatically as the screen size changes? .c1 { display: flex; flex-wrap: w ...