Home > Software engineering >  CSS flex div get's larger as max-height
CSS flex div get's larger as max-height

Time:02-25

i got following HTML structure:

Demo here

<div id="wrapper">

  <div id="header">
    <i>Header</i>
  </div>
  
  <div id="content">
    <i>Content</i>
  </div>
  
  <div id="footer">
      <i>Footer</i>
  </div>

</div>
#wrapper{
  margin: 0;
  height: 100vh;
  max-height: 100vh;
  /* overflow: hidden; */
display: flex;
  flex-direction: column;
}

#header{
  background: blue;
  height: 3rem;
}

#content{
  background: red;
  flex-grow: 1;
}

#footer{
  background: green;
  height: 3rem;
}

My problem is that when the div of the content gets too large, the wrapper ignores his max-height and gets a scrollbar. What i want is that the content div instead gets the scrollbar and the wrapper holds it's height.

Demo of content to large

CodePudding user response:

You have to make the wrapper overflow: hidden so scroll-bar is disabled for that div. Next, You can add the overflow: auto to content to make it scrollable if content goes outside.

#wrapper{
  margin: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: red;
  overflow: hidden;
}
#content{
  flex-grow: 1;
  overflow: auto;
}

CodePudding user response:

You don't need a flexbox here, since all the elements go after each other. In your example, you can use a max-height on the content itself, and use a overflow: scroll to prevent large text to not show up

wrapper {
  margin: 0;
  height: 100vh;
  /* overflow: hidden; */
}

#header {
  background: blue;
  height: 3rem;
}

#content {
  background: red;
  height: calc(100vh - 6em);
  overflow: scroll;
}

#footer {
  background: green;
  height: 3rem;
}
<div id="wrapper">

  <div id="header">
    <i>Header</i>
  </div>

  <div id="content">
    <i>iquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velmperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, cos autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dign

      s autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dign</i>
  </div>

  <div id="footer">
    <i>Footer</i>
  </div>

</div>

  • Related