Compel the content element to scroll forcefully

I've scoured the vast expanse of the internet, but my search has been fruitless.

Here lies my dilemma.

I want my <main> element to have a scrollbar with overflow-y:scroll. No scrollbar on the body, just on the main element.

For a visual representation, I've created a pen: http://codepen.io/magalielinda/pen/rymzBG/

You'll notice that the scrollbar appears, but it's not functional. Why is this happening? And how can I make it scroll?

Below is a snippet of my demo html for those who may find it easier to provide a solution this way:

<main>
  <div class="content__container">
  </div>
</main>

and demo css:

body {
  overflow: hidden;
  height: 100%;
}

main,
.content__container {
  height: 100%; 
}

main {
  overflow-y: scroll;
  background-color: red;
  border: 1px solid blue;
}

.content__container {
  max-width: 200px;
  margin: 0 auto;
  background-color: aqua;
  height: 1000px;
}

Thank you for your assistance!

UPDATE

I've made adjustments based on this solution. Here is the updated result. http://codepen.io/magalielinda/pen/JWyVOM

Answer №1

height: 100% is set to 1000px to fit the content of .content__container. By examining main, .content__container, body, all of them have heights of 1000px. This causes the content to overflow the body element, which has overflow: hidden applied to it, thus no scrollbar is visible.

To enable scrolling behavior, you should restrict the height of main to introduce overflow. Using 100vh instead will allow for the expected scrolling functionality.

body {
  overflow: hidden;
  height: 100%;
}

main {
  height: 100vh;
  overflow-y: scroll;
  background-color: red;
  border: 1px solid blue;
}

.content__container {
  max-width: 200px;
  margin: 0 auto;
  background-color: aqua;
  height: 1000px;
}
<main>
  <div class="content__container">
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
    foobar<br>
  </div>
</main>

Answer №2

Ensure your main element has a specified maximum height:

main {
   max-height: 500px;
}

http://example.com

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

What is the best way to add a darker tint to the background behind my overlay panel in jQuery

After grabbing the panel code from the jQuery Mobile website and integrating it into my file, I am interested in changing the appearance. I want to either darken or blur the background content when the panel is displayed. I attempted to use filter:blur(5px ...

The CSS linear gradients rainbow wheel is missing a few sections, only displaying part of its 12

I've almost got everything working, but I'm facing a challenge in displaying all 12 sections of the rainbow wheel. I know it's probably something very simple (Occam's razor). I've been following this amazing tutorial on CSS linear ...

Tips for creating a script that compiles all SCSS files into CSS within a Vue 3 project

Within my project, there exists a file named index.scss located in the src/assets/styles directory. Adjacent to this file are multiple folders housing SCSS files that are ultimately imported into index.scss. My objective is to devise a script within the pa ...

The functionality of z-index is not applying when using position fixed

I have the following CSS code: #one{ position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 999; } #two{ position: relative; z-index: 9; width: 200px; height: 200px; background: red; } #link { position: ...

Preventing box shadows from blending together

I'm struggling with a design issue on my site where I have four divs in the center, each represented by a box with a box-shadow. Unfortunately, the colors of the shadows are getting mixed up and creating an unwanted effect. https://i.sstatic.net/NdAUB ...

What is causing the border-bottom in these inline divs to not display when using a fractional height? (Specifically in Chrome)

Here is a jsfiddle I'd like to share with you: https://jsfiddle.net/ionescho/4d07k799/4/ The HTML code: <div class='container'> <div class="row"></div><div class="row"></div><div class="row"></div> ...

Create a border surrounding the matColumn within a sticky matTable

I am currently working with a matTable that has the first two or three columns set as sticky. However, I am facing an issue where the scrolling behavior of the non-sticky columns under the sticky ones looks like a glitch due to existing border styling on t ...

The issue with `nth-of-type` not properly functioning within a list

I am having trouble altering the background of my divs when they are contained within an li element. It seems that the problem may be related to the arrangement of my elements. <ul class="featured-items"> <li><div class="inner">& ...

How to Emphasize Html Select Element on Mobile Devices?

Is there a way to make the HTML select element on Android appear more distinguishable as a dropdown list, rather than just looking like plain text? Any suggestions for highlighting it so users can easily identify and select an option from this element? Up ...

What is causing the unusual behavior of z-index in this specific scenario?

I have a good understanding of z-index and decided to practice with it by writing the following code: *{ padding: 0; margin: 0; box-sizing: border-box; } body{ height: 100vh; di ...

Stylish News Carousel using CSS and HTML

I've been working on creating a news slider for my website, but I'm encountering an issue. Despite completing the HTML and CSS, I'm struggling to make the 'showcase' rotate using JQuery. I've tried various guides and instructi ...

Having trouble importing CSS in ReactJS?

While working on my project based on next.js, I encountered a situation where loading CSS in other pages was successful: import css from './cssname.css'; <div className={css.myclass}></div> However, now that I am implementing a ligh ...

Adding a gradient overlay to an image that has a see-through background

I am trying to achieve a unique effect where a gradient appears on the text instead of the background of an image. An example I created can be found here: https://codepen.io/BenSagiStuff/pen/BaYKbNj body{ background: black; } img{ padding: 30px; ...

Choose from the options provided to display the table on the screen

One of the challenges I am facing involves a table with two columns and a select option dropdown box. Each row in the table is associated with a color - green indicates good, red indicates bad, and so on. My goal is to have all values displayed when the pa ...

Bottom of the page: Bootstrap dilemma

After designing two footers, I decided to use CSS with the code position: absolute for the second footer. However, this change caused issues with the functionality of the first footer. The display now appears as follows: ...

Is there a way for me to position my chat messages on the right side in the chat room?

I have a react chat application and I'm looking to customize the appearance of my messages. Currently, all entries with an orange vertical bar belong to me and are displayed on the left side of the chat room. I would like to move them to the right sid ...

Are your div tags not fitting snugly? Experiencing invisible padding issues?

I am currently working on a page with inline-block divs that extend horizontally off the screen. Feel free to view my Codepen demonstration: http://codepen.io/gosusheep/pen/GJEPEV . You can also see the processed output here: . Within the #slider div, ...

What is preventing me from viewing my cards in flex or grid layout?

Recently, I created cards for my team members, but encountered layout issues despite using CSS Flexbox and CSS3 Grid. Although both are supported by my browser, the problem persists. However, the layout works perfectly on mobile devices! I experimented wi ...

Is it possible to modify the dropdown menu so that it opens on the right side instead of using a select tag?

Is there a way to make the drop-down from the select tag open to the right side(drop-right)? <label for="ExpLabel">Select Message Expiry:</label> <select name="ExpSelect" id="Expiry"> <option value="ExpiryDate">1 Day</opt ...

Attempting to utilize Flexbox to populate vacant areas

https://i.stack.imgur.com/BhPU0.png Can the yellow square smoothly transition to the empty grey square and beyond in this layout setup? Each flex item represents a component that can utilize either 50% or 100% of the container width, information only know ...