Enabling overflow-y with the value of auto allows the child element to be

I'm having an issue with a parent element and a child element in my CSS code. The parent has the following properties:

position: relative;
overflow-y: unset;

And the child has these properties:

position: relative;
left: -70px;

Everything looks good until I set overflow-y: auto on the parent, which causes the child element to get clipped on its left side. I'm not sure why this is happening. How can I add a vertical (auto) scrollbar to the parent without clipping the child?

const checkbox = document.getElementById('checkbox');
const right = document.querySelector('.right');
const value = document.getElementById('value');

checkbox.addEventListener('change', (event) => {
  const overflowY = event.target.checked ? 'auto' : 'unset';
  right.style.overflowY = overflowY;
  value.innerText = overflowY;
});
.right {
  background-color: red;
  width: 100px;
  height: 300px;
  position: relative;
  left: 200px;
  /* toggle overflow-y between unset and auto */
  overflow-y: unset;
}

.two {
  background-color: blanchedalmond;
  top: 50px;
  height: 50px;
  position: relative;
  left: -70px;
}
<body>
  <p>
    Whenever overflow-y changes from unset to auto the yellowish block gets clipped.
  </p>
  <p>
    <input id="checkbox" type="checkbox"> overflow-y: <span id="value">unset</span>
  </p>

  <div class="right">
    <div class="two right-content"></div>
  </div>
</body>

Answer №1

const checkbox = document.getElementById('checkbox');
const right = document.querySelector('.right');
const value = document.getElementById('value');

checkbox.addEventListener('change', (event) => {
  const overflowY = event.target.checked ? 'auto' : 'unset';
  right.style.overflowY = overflowY;
  value.innerText = overflowY;
});
    .right {
      background-color: blue;
      width: 150px;
      height: 250px;
      position: absolute;
      left: 100px;
      /* toggle overflow-y between unset and auto */
      overflow-y: hidden;
      /*Try This*/
      display: flex;
    }

    .two {
      background-color: aquamarine;
      top: 30px;
      height: 70px;
      position: relative;
      left: -50px;
    }
    <body>
      <p>
        When the overflow-y property changes from unset to auto, the green block will be partly hidden.
      </p>
      <p>
        <input id="checkbox" type="checkbox"> overflow-y: <span id="value">unset</span>
      </p>

      <div class="right">
        <div class="two right-content"></div>
      </div>
    </body>

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 display the content

Why isn't the text expanding when I click "see more"? Thanks XHTML: <div id="wrap"> <h1>Show/Hide Content</h1> <p> This code snippet demonstrates how to create a show/hide container with links, div eleme ...

Accordion menu causing Javascript linking problem

After following a tutorial, I managed to create an accordion menu in JavaScript. In the current setup, each main li with the class "ToggleSubmenu" acts as a category that can hide/show the sub-lis. Now, my query is this: How can I retain the link functio ...

CSS an act of misbehavior

I have noticed some unusual behavior on my website page. My website design can be viewed at Also, here is the same design in CMS format Please pay attention to the section under <div class="godelpage"> <a href="http://ryugaku.babonmultimedia ...

Encountering difficulties with the mobile display of a Wordpress theme

I am currently working with the megashop theme on WordPress and I have a few queries that need addressing: Is there a way to modify a specific setting exclusively for views smaller than desktop size, without impacting the desktop view? For example, the m ...

Ways to create a unique animation for reducing the width of a div in a specific direction?

Currently, I have managed to animate the decrease in width of a div element. However, it is currently decreasing from right to left, and I would like it to decrease from left to right instead. Can someone please provide guidance on how to achieve this? ...

Using jQuery Datepicker, showcase two distinct datepickers on a single page

Currently, I am utilizing the jQuery [Datepicker][1] on a website, but I am facing challenges when attempting to display two datepickers on the same page in different manners. The first one should only appear once you click the text box, and once a date i ...

Leveraging Material UI's Button element along with the enctype attribute

I'm working on incorporating Multer for image uploads and utilizing Material UI's button component with the onClick prop to submit the data. If I wrap the button component within a form tag, will it achieve the same result? If not, how can I spec ...

Using javascript, add text to the beginning of a form before it is submitted

I'm trying to modify a form so that "https://" is added to the beginning of the input when it's submitted, without actually showing it in the text box. Here's the script I have so far: <script> function formSubmit(){ var x ...

The left margin is malfunctioning

As a newcomer to css, I am struggling with adding margin-left to my paragraph and <h2>, and it's not taking effect. I have a 700px div and I want to add a 10px margin-left to both <p> and <h2> in relation to the image. Despite my e ...

Issue with Cross Site Scripting Iframe Permission Denied

I'm encountering a Cross Site Scripting error when using the code below. Javascript function resizeIframe(ifRef) { var ifDoc; //alert(ifRef); try { i ...

Is it possible for two Bootstrap icon class names to be used sequentially with one taking precedence over the other?

I am having an issue with toggling a class name in my code. I have a class named (bi bi-clipboard) and I want to change it to (bi-clipboard-check) when it is clicked. However, the problem is that the new class name gets added next to the existing one like ...

"Exploring the best locations for storing CSS, JS, and image files in Spring

Having trouble figuring out where to place my public files. I attempted the solution mentioned in this thread on Stack Overflow but it didn't work for me. I've tried putting my public folder in various locations within the WEB-INF directory, but ...

Why isn't this working? I'm attempting to trigger a sound when I hover with my cursor, but it only plays when I click instead

When I click on it, it works fine. But I can't seem to get it to work on hover. Can someone help me out? This is the HTML code: <body> <audio autoplay id="HAT2" > <source src="OOOOO_1_HAT.mp3" > Your browser doesn't support t ...

What is the best way to transform an HTML table into a jQuery array of key-value pairs?

I have an HTML table (without a tbody tag) and I am trying to convert the HTML data into key-value pairs (header-data) in a jQuery array. Currently, when I run the code snippet, the output only displays data from the last row of the table. ************Cur ...

Synchronizing two navigation menus on a single-page application website

Let me start by saying that I specialize in back end development and am facing a specific challenge with building a website that includes two navigation menus. The main navigation menu features links like Home, while the sub-navigation menu includes option ...

Conceal Choices During Search using jQuery Select2

I'm having trouble figuring out how to make the Select2 plugin work for my specific color selection feature. My goal is to allow users to choose 5 colors from a list, including an "Other" option. When the user selects "Other", they should be able to ...

Is there a way to streamline the process of uploading an image and storing its details in a single HTML form with just one submit button?

Here is the code snippet I have: <form id="registration_form" action="AddProductServlet" method="post"> <div> <label> <input placeholder="Product ID" name="pid" type="text"> ...

Can Selenium successfully scrape data from this website?

I am currently attempting to extract Hate Symbol data (including the name, symbol type, description, ideology, location, and images) from the GPAHE website using Selenium. As one of my initial steps, I am trying to set the input_element to the XPATH of the ...

What is the best way to create a responsive design that positions an element between two others?

There are two types of elements in my project: Type "A" - a text block Type "B" - a graphical block I am looking to create a responsive layout for these elements. On small resolution screens, I want them to be arranged like this: "A" "A" "B" ...

Splitting Arrays in Laravel DatabaseExploding arrays stored in a Laravel

Within my database, there exists a text field featuring: "6:00 AM Registration Opens; Day of Race registration (if available), check-in, packet pick up. 7:30 AM - First Wave Start. 10:00 AM - Awards Ceremony (time is approximate)." My goal is to create b ...