Concealment: excessive concealment occurs when an element with a specified height has overflow

In my quest to perfect a mobile menu, I encountered an issue with jQuery's slideToggle animation causing jumpiness despite setting specified widths and heights for the element. One potential fix I discovered was to apply overflow: hidden to the toggled div. However, this led me to another problem that seems to be exacerbating the slideToggle issue:

I observed that when I set overflow-y: hidden on my navigation bar, certain elements were cut off unexpectedly. For instance, if I give the nav a height: 240px; overflow: hidden, one would assume there should be no overflowing content to conceal. Instead, in Chrome it cuts off around 102px and in Firefox, the nav doesn't display at all.

I've experimented with setting the CSS in the stylesheet as well as dynamically through JavaScript by calculating the height of the nav's contents. I've attempted clearing the nav. I've tried enclosing it within a div. I've tested various display methods. Unfortunately, none of these approaches seem to resolve the issue.

So, my question is straightforward: What exactly is causing the irregularities in the nav's height? What am I missing here?

While using min-height does provide a workaround, it interferes with the slideToggle effect and doesn't address why merely utilizing height fails to suffice.

For code examples, visit http://codepen.io/cjl750/pen/xRdoRW.

$('header span').click(function(){
  $('nav').slideToggle(400);
});
html {
  font-family: 'Raleway', sans-serif;
}
header {
  background-color: mediumaquamarine;
  padding: 5px 15px;
}
nav {
  font-family: 'Oswald', sans-serif;
  color: white;
  overflow-y: hidden;
  /*   display: none; */
  height: 240px;
}
#body {
  width: 360px;
  height: 616px;
  overflow-y: scroll;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}
main {
  background-color: slategray;
  display: flex;
  flex-direction: column;
}
section, aside {
  width: 100%;
  box-sizing: border-box;
  padding: 0 20px;
}
section {
  background-color: powderblue;
}
aside {
  background-color: cadetblue;  
}
h1, h2, h3 {
  font-weight: 400;
}
h2 {
  font-size: 1.33em;
}
#slogan {
  font-size: 2em;
  font-weight: 900;
  float: left;
}
header span {
  float: right;
  margin-top: 5px;
}
header span:hover {
  cursor: pointer;
  text-decoration: underline;
}
nav ul {
  list-style-type: none;
  font-size: 1.5rem;
  margin: 0;
  padding: 0;
}
nav ul li + ul {
  display: none;
}
nav ul.topLvl li {
  padding: 2.5px 0 2.5px 15px;
  background-color: rgba(220,220,220,0.7);
  border: 3px outset rgba(255,255,255,0.65);
  border-top-style: inset;
  border-right-style: inset;
  border-right-width: 4.5px;
  border-bottom-color: rgba(255,255,255,1);
  border-left-width: 4.5px;
}
nav ul.topLvl li:first-of-type {
  border-top-width: 4.5px;
}
nav ul.topLvl li:last-of-type {
  border-bottom-width: 4.5px;
}
nav ul.topLvl li:hover {
  background-color: rgba(220,220,220,0.80);
  border-top-style: outset;
  border-right-style: outset;
  border-bottom-style: inset;
  border-left-style: inset;
  border-bottom-color: rgba(255,255,255,0.45);
  border-top-color: rgba(255,255,255,0.45);
}
<div id="body">
  <header>
    <div id="slogan">Octavian</div>
    <span>Menu</span>
  </header>
  <nav>
    <ul class="topLvl">
      <li>Giraffes</li>
      <ul class="secondLvl">
        <li>Tall</li>
        <li>Spots</li>
        <li>Weird horn things</li>
        <li>Head-whacking battles</li>
      </ul>
      <li>Elephants</li>
      <ul class="secondLvl">
        <li>Real big</li>
        <li>Eat lots of plants</li>
        <li>Floppy ears</li>
        <li>Get feet stuck in tires</li>
      </ul>
      <li>Cheetas</li>
      <ul class="secondLvl">
        <li>Spots</li>
        <li>Real fast</li>
        <li>Tire out quickly</li>
        <li>Gazelle's arch nemesis</li>
      </ul>
      <li>Vultures</li>
      <ul class="secondLvl">
        <li>Ominous death circle</li>
        <li>Super gnarley stoach acid</li>
        <li>Flap, flap, flap</li>
        <li>Big wingspan</li>
      </ul>
      <li>Zebras</li>
      <ul class="secondLvl">
        <li>Stripy horses</li>
        <li>Black and white</li>
        <li>Neeeeeiiiiiigh</li>
        <li>Big herds</li>
      </ul>
    </ul>
  </nav>
  <main>
    <section>
      <h1>Lorem Ipsum</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent rutrum iaculis scelerisque. Nulla ligula nibh, venenatis a leo vel, varius tincidunt magna. Nulla consequat justo at lacus sodales ornare. Aenean quis faucibus est.</p>
      <p><em>Ut dapibus id justo tempor iaculis. Vestibulum elementum quis diam a consectetur. Nunc pellentesque purus sapien.</em></p>
      <p>Phasellus volutpat felis id libero bibendum euismod. Donec urna eros, euismod quis nisl cursus, placerat ultricies nulla. Vivamus finibus neque sit amet vestibulum posuere. Sed euismod, justo nec varius lacinia, tellus eros suscipit velit, sit amet dignissim leo nisi aliquet augue.</p>
      <h3>Fusce sagittis eleifend enim</h3>
      <p>Egestas sollicitudin. Nam at dui vel risus bibendum fringilla.</p>
      <p>Mauris tincidunt nunc lorem, vel mollis sapien sollicitudin id. Nunc aliquam nulla enim, sit amet porta quam euismod a. Sed varius vel ligula quis laoreet. Sed commodo ornare semper. Etiam ac pellentesque erat. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer pretium turpis elit, eu auctor velit suscipit non.</p>
    </section>
    <aside>
      <h2>Dulce et decorum est</h2>
      <p>Mauris sit amet eleifend tortor. Praesent vitae aliquam turpis. Quisque auctor maximus ante ac fermentum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Curabitur vel fermentum lacus.</p>
      <ul>
        <li>cras feugiat justo non lacus auctor</li>
        <li>eget sollicitudin mi eleifend</li>
        <li>quisque pulvinar pretium risus et accumsan</li>
        <li>praesent lacus sapien</li>
      </ul>
      <p>Sed commodo ornare semper. Etiam ac pellentesque erat. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </aside>
  </main>
  <link href="https://fonts.googleapis.com/css?family=Oswald|Raleway:400,900" rel="stylesheet">
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

Answer №1

Flex items inherently flex. To prevent them from shrinking, you can utilize the following CSS:

nav {
  flex-shrink: 0;
}

You can also employ the use of the flex shorthand property like flex: none or flex: 0 0 240px.

$('header span').click(function(){
  $('nav').slideToggle(400);
});
html {
  font-family: 'Raleway', sans-serif;
}
header {
  background-color: mediumaquamarine;
  padding: 5px 15px;
}
nav {
  font-family: 'Oswald', sans-serif;
  color: white;
  overflow-y: hidden;
  /*   display: none; */
  height: 240px;
  flex-shrink: 0;
}
#body {
  width: 360px;
  height: 616px;
  overflow-y: scroll;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}
main {
  background-color: slategray;
  display: flex;
  flex-direction: column;
}
section, aside {
  width: 100%;
  box-sizing: border-box;
  padding: 0 20px;
}
section {
  background-color: powderblue;
}
aside {
  background-color: cadetblue;  
}
h1, h2, h3 {
  font-weight: 400;
}
h2 {
  font-size: 1.33em;
}
#slogan {
  font-size: 2em;
  font-weight: 900;
  float: left;
}
header span {
  float: right;
  margin-top: 5px;
}
header span:hover {
  cursor: pointer;
  text-decoration: underline;
}
nav ul {
  list-style-type: none;
  font-size: 1.5rem;
  margin: 0;
  padding: 0;
}
nav ul li + ul {
  display: none;
}
nav ul.topLvl li {
  padding: 2.5px 0 2.5px 15px;
  background-color: rgba(220,220,220,0.7);
  border: 3px outset rgba(255,255,255,0.65);
  border-top-style: inset;
  border-right-style: inset;
  border-right-width: 4.5px;
  border-bottom-color: rgba(255,255,255,1);
  border-left-width: 4.5px;
}
nav ul.topLvl li:first-of-type {
  border-top-width: 4.5px;
}
nav ul.topLvl li:last-of-type {
  border-bottom-width: 4.5px;
}
nav ul.topLvl li:hover {
  background-color: rgba(220,220,220,0.80);
  border-top-style: outset;
  border-right-style: outset;
  border-bottom-style: inset;
  border-left-style: inset;
  border-bottom-color: rgba(255,255,255,0.45);
  border-top-color: rgba(255,255,255,0.45);
}
<div id="body">
  <header>
    <div id="slogan">Octavian</div>
    <span>Menu</span>
  </header>
  <nav>
    <ul class="topLvl">
      <li>Giraffes</li>
      <ul class="secondLvl">
        <li>Tall</li>
        <li>Spots</li>
        <li>Weird horn things</li>
        <li>Head-whacking battles</li>
      </ul>
      <li>Elephants</li>
      <ul class="secondLvl">
        <li>Real big</li>
        <li>Eat lots of plants</li>
        <li>Floppy ears</li>
        <li>Get feet stuck in tires</li>
      </ul>
      <li>Cheetas</li>
      <ul class="secondLvl">
        <li>Spots</li>
        <li>Real fast</li>
        <li>Tire out quickly</li>
        <li>Gazelle's arch nemesis</li>
      </ul>
      <li>Vultures</li>
      <ul class="secondLvl">
        <li>Ominous death circle</li>
        <li>Super gnarley stoach acid</li>
        <li>Flap, flap, flap</li>
        <li>Big wingspan</li>
      </ul>
      <li>Zebras</li>
      <ul class="secondLvl">
        <li>Stripy horses</li>
        <li>Black and white</li>
        <li>Neeeeeiiiiiigh</li>
        <li>Big herds</li>
      </ul>
    </ul>
  </nav>
  <main>
    <section>
      <h1>Lorem Ipsum</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent rutrum iaculis scelerisque. Nulla ligula nibh, venenatis a leo vel, varius tincidunt magna. Nulla consequat justo at lacus sodales ornare. Aenean quis faucibus est.</p>
      <p><em>Ut dapibus id justo tempor iaculis. Vestibulum elementum quis diam a consectetur. Nunc pellentesque purus sapien.</em></p>
      <p>Phasellus volutpat felis id libero bibendum euismod. Donec urna eros, euismod quis nisl cursus, placerat ultricies nulla. Vivamus finibus neque sit amet vestibulum posuere. Sed euismod, justo nec varius lacinia, tellus eros suscipit velit, sit amet dignissim leo nisi aliquet augue.</p>
      <h3>Fusce sagittis eleifend enim</h3>
      <p>Egestas sollicitudin. Nam at dui vel risus bibendum fringilla.</p>
      <p>Mauris tincidunt nunc lorem, vel mollis sapien sollicitudin id. Nunc aliquam nulla enim, sit amet porta quam euismod a. Sed varius vel ligula quis laoreet. Sed commodo ornare semper. Etiam ac pellentesque erat. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer pretium turpis elit, eu auctor velit suscipit non.</p>
    </section>
    <aside>
      <h2>Dulce et decorum est</h2>
      <p>Mauris sit amet eleifend tortor. Praesent vitae aliquam turpis. Quisque auctor maximus ante ac fermentum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Curabitur vel fermentum lacus.</p>
      <ul>
        <li>cras feugiat justo non lacus auctor</li>
        <li>eget sollicitudin mi eleifend</li>
        <li>quisque pulvinar pretium risus et accumsan</li>
        <li>praesent lacus sapien</li>
      </ul>
      <p>Sed commodo ornare semper. Etiam ac pellentesque erat. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </aside>
  </main>
  <link href="https://fonts.googleapis.com/css?family=Oswald|Raleway:400,900" rel="stylesheet">
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

Removing overflow-y: hidden led to less shrinking, attributed to Flexbox Implied Minimum Size.

Answer №2

It appears that the issue lies in using flexbox on the body element, which is causing confusion with the .slideToggle() function. Flexbox handles height in a unique way by dynamically adjusting the height of flex-items to fit their rows.

In this case, since you are aiming for a single-column layout, utilizing flexbox is unnecessary. Instead, simply give all direct children of the body element a width of 100%.

For reference, here's a working example on jsFiddle: https://jsfiddle.net/1qrgg695/2/

To resolve the issue, I removed flex from the body element and applied flex styling to the header instead:

header {
  display: flex;
  justify-content: space-between;
}

The `justify-content` property ensures that the Title remains aligned to the left while the Toggle button is positioned to the right.

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

When a mobile device is rotated, the screen on a jQuery application will automatically shrink

Having trouble with JQM and device rotation on iOS devices. The screen doesn't resize properly when rotated. I have this line in the header to handle display size: <meta name="viewport" content="height=device-height,width=device-width,initial-scal ...

Combining the Power of Bootstrap with Yii Framework

As a newcomer to the Yii framework, I am looking to incorporate my Bootstrap design into this platform. One issue I have encountered is related to my custom CSS. In Bootstrap, we use navbar-default for styling the navbar. I customized my navbar color by ...

Ensuring Proper Alignment of Headers in CSS

I'm struggling to align my header correctly. Despite getting close in Internet Explorer, it looks terrible in Firefox with the code below. I've scoured forums for solutions but the more I try to fix it, the worse it looks. All I want is for heade ...

How can I successfully use the Confluence REST API to publish a dynamically generated HTML table (created with PHP) without encountering the problematic HTTP

I am striving to build a page within a Confluence wiki that contains a table created using the provided REST API, which can be conveniently edited by wiki users using the WYSIWYG editor once the page is established. To achieve this, I have organized vario ...

React counter variable failing to increment

I have 6 cubes displayed on the screen along with a counter. Whenever a cube is clicked, its background color changes to red and the counter increments by one. However, I'm facing an issue where the counter remains at 1 even after clicking on multiple ...

Flex items are overflowing, rather than adjusting their size

Here .header { display: flex; font-family: monospace; background: papayawhip; align-items: center; justify-content: space-between; margin-left: auto; } ul { display: flex; background: papayawhip; gap: 10em; list-style-type: none; f ...

What steps can be taken to prevent users from dragging a page horizontally?

One function on my site involves a horizontal scroll that animates the DIV to the left when a button is clicked. The element's width is set at 18000px, ensuring it has a horizontal scrollbar that I have since disabled. Despite this, users are still ...

Unable to adjust the padding of a div while the Bootstrap 4 navbar is in a collapsed state on mobile devices

I am facing an issue with a fixed navbar at the top of my page. Below the navbar, I have the page content which includes a Bootstrap 4 image carousel. The problem arises on mobile devices where I need to add top-padding to the page-container (below the nav ...

Restore the images to their original state once the user scrolls back to the top

I have implemented 2 static images with fixed positions at the edges of the screen (one on the right and one on the left). The logic I want to achieve is that when a user scrolls more than 100 pixels from the top, these edge images should move back by 50 p ...

Why are two vertical scrolls appearing on the screen simultaneously?

I utilized this method to hide the body scrollbar initially and then display it upon clicking a link: $('body').css('overflow', 'hidden'); $('#site').click(function(e) { $('#wrapper').remove(); $(& ...

The 1920px minimum width is not triggering as expected

When using @media screen and (min-width: 1920px), the style is not applied until the min-width is changed to 1396px on a 1920x1080 screen. Despite accounting for the scroll bar size, the issue persists. It seems unlikely that any other CSS is overriding t ...

The loop is not functioning according to its design

http://codepen.io/abdulahhamzic/pen/aNexYj I have been attempting to implement a loop to display ten results on the screen, but it appears that there is an issue with my loop that I am unable to identify. In Mozilla, only one result is being shown on the ...

Ensuring Form Validation in Symfony 2.0 View

I am currently using Symfony 2.0 and I have a view file called test.html.php which includes a form: <form action="" method="post" rel=""> <input type="hidden" name="action" value="test" /> <input name="myinput" type="text" value="" ...

What steps are involved in setting up a dropdown side navbar?

Is there a way to automatically activate the next dropdown in the side navbar when I click on the 'Next' button? And similarly, can we open the previous dropdown in the side navbar when clicking the 'Previous' button? ...

Ensure that the text box inside the div adjusts its size in accordance with the dimensions of the window, as the div is already designed

As a novice in HTML and jQuery, I am currently working on creating a portfolio site for a school project. One of the challenges I have encountered is designing a graphic that includes a text box within a resizable div element. My goal is to prevent excessi ...

Does HTML store information in Access databases?

Currently facing an issue, but first let's take a look at what resources I have. Current Resources: • A DB File in ACCESS containing over 100 lines of data. • An HTML webpage with inputs corresponding to the fields in the ACCESS file. • My o ...

Issue with Django 1.11: Unable to display form validation errors in HTML page

After searching for ways to check if a form's name already exists in the database, I found this helpful link. It successfully prevents duplicate names from being entered, but I encountered an issue where no error message was displayed as expected. I&a ...

What is the best way to prevent my images from resizing in a Bootstrap 4 carousel?

I am working on a website where users can upload images of various sizes. These images are then displayed in a Bootstrap 4 carousel. I want to ensure that the carousel maintains a consistent dimension across different screen sizes without changing the widt ...

Need help displaying a file link from a server using the href attribute in a PHP echo statement?

Having some trouble creating a table with PHP. I managed to create the table, but I'm trying to link a file from an FTP server in a td tag. When I use href, it gives me an error. Can you please help me correct my code? echo "<a href = '../pub ...

The parent node is returning a child element

Currently working on a website using Vue 3.0 and Element+ and encountering an inconsistency. There is a div displayed on the page (an array of objects) and the goal is to remove the object from the array when a button is clicked. The issue arises when som ...