What is the best way to avoid overflow of a fixed element when styling with CSS?

I need help ensuring that the inner div, colored red, stays hidden within its parent div, which is blue. I attempted to use overflow: hidden; in CSS but it did not have the desired effect. I am looking for a solution using solely CSS since JavaScript causes issues with my SharePoint pages.

Is there a way to achieve the desired overflow hidden result?

.container {
  left: 0px;
  width: 800px;
  height: 1200px;
  background: green;
}

.outer {
  position: absolute;
  left: 0px;
  width: 600px;
  height: 1000px;
  background: blue;
  overflow: hidden;
}

.inner {
  position: fixed;
  width: 100px;
  height: 600px;
  background: red;
  margin-left: 0px;
}
<div class="container">
  <div class="outer">
    <div class="inner"></div>
  </div>
</div>

<br/><br/><br/><br/><br/><br/>

Answer №1

When working with a fixed element, it is important to note that it is positioned in relation to the browser window rather than its parent element's location.

To ensure that your inner element remains within its parent container, you should consider using absolute positioning (using position: absolute), which will be relative to the closest ancestor with a defined position value - in this case, the .outer parent element.

.container {
  left: 0px;
  width: 800px;
  height: 1200px;
  background: green;
}

.outer {
  position: absolute;
  left: 0px;
  width: 600px;
  height: 1000px;
  background: blue;
  overflow: hidden;
}

.inner {
  position: absolute;
  width: 100px;
  height: 600px;
  background: red;
  margin-left: 0px;
}
<div class="container">
  <div class="outer">
    <div class="inner"></div>
  </div>
</div>

<br/><br/><br/><br/><br/><br/>

We hope this explanation proves helpful! :)

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

Web pages that dynamically update without the need for reloading

Recently, I stumbled upon slack.com and was immediately captivated by its user interface. For those unfamiliar with it, navigating the site is quite simple: There's a sidebar on the left and a main content area on the right. When you click on an item ...

Press the vertical navigation bar

My website is currently undergoing a major overhaul. I am looking to enhance its functionality, but I may have taken on more than I can handle. You can find the link related to my query here: On my top menu, there is a button located on the left side. It ...

What steps can be taken to stop images from overflowing a flex-grow-1 item?

https://i.sstatic.net/1Qgec.pngHello, I'm facing an issue with my image. I am dealing with 3 sections: Main Content Image and other components (main section) Button Currently, I need to ensure that the image height is set to a maximum of 100% of the ...

Styling for older versions of Internet Explorer (IE10 and earlier)

Could it be true that IE 10, 9, and others no longer support conditional statements? Is it also accurate to say that JQuery does not support the browser object above version 1.9? I am facing an issue with CSS rendering differently in Chrome and IE. A Goog ...

Tips for utilizing the sticky-top class with Bootstrap 4 grid system

Has anyone had success using the sticky-top class in a grid layout? I'm having trouble getting it to work. <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"> <div class="container-fluid ...

Handling an HTTP Get request by utilizing Server-Sent Events (SSE)

Exploring the possibilities of HTML5 and node.js has led me to test out Server Sent Events successfully using this example. My experiment then involved responding to a button press with an SSE. Upon clicking the "Increment" button, I extracted a number fr ...

Error: Collision detection in Three.js console output

I am attempting to create a collision detection system using Three.js (a WEBGL library). However, I keep encountering an error stating "cannot call method multiplyVector3 of undefined". Is there anyone who can help me identify what I may be doing incorrec ...

Issue with jQuery click event not triggering on dynamically created input boxes for removal

Having some trouble with the remove buttons not working on dynamically generated text boxes. The click function doesn't seem to be triggering, and I'm a bit stuck. Any ideas on what might be causing this issue? Take a look at the following co ...

Is there a way to make a div collapse to the left instead of collapsing to the top using Bootstrap 4? (with animation)

Is it possible to use Bootstrap 4 to collapse a div with a sidebar menu to the left without extensive changes? I'm hoping to achieve this without having to write my own JS or rely on external scripts. So far, I've been exploring the bootstrap co ...

Comparing the efficiency of Jquery draggable with thousands of elements versus updating the elements continuously

Currently, I am developing an application that involves dragging an image using Jquery's draggable utility. The image is accompanied by several overlay divs containing various components positioned based on pixel locations, sometimes reaching into the ...

Calculate the total of the smallest values in three columns as they are updated in real-time

I'm facing an issue with dynamically adding the sum of the 3 lowest values entered in columns. The Total Cost Field is not displaying any value, and changing the type from number to text results in showing NaN. I've tried various approaches but h ...

Using plain text instead of HTML elements in Django

I'm working with two models, the first one looks like this: class FirstOne(models.Model): title = models.CharField(max_length=250) pretext = models.TextField(null=True, blank=True, default=None) first = models.PositiveIntegerField() la ...

How can I modify my code to ensure that trs and th elements are not selected if their display property is set to none?

I am currently working on creating a filter for my pivot table, but I am unsure of how to dynamically calculate the sum of each row/column based on whether they are displayed or not. If you need any other part of my code, feel free to ask. To hide employee ...

Is it possible to activate a block display for an article based on the class value when a radio

Here is the HTML code snippet I'm working with: <div id="contentapp"> <input type="radio" name="menu" id="interest" checked> <input type="radio" name="menu" id="about"> <div id="tab"> <label for="intere ...

Make sure the header spans the full width of the body

I am trying to create a header that spans the entire width of the body, but I am encountering some issues with white space on both sides. I initially attempted to set the width to 100% on the header div, but this did not eliminate the white space. I then e ...

Incorporate an additional javascript document into a VueJS component

What is the most efficient method to include Javascript files in Vue.js components? ...

Adjustable image within a div based on the length of the title when viewed in Edge browser

I am facing an issue with a div element. Here is the code snippet: <div fxLayout="column" fxLayoutGap="20px"> <h1 class="mat-display-1">Welcome to $TITLE$</h1> <img *ngIf="logoData" [src]="logoData" class="logo" alt="logo"/> &l ...

Displaying JSP content within a <div> element with the help of JQuery

Here are the specifications for two important divs on my webpage. #apDiv1 { position:absolute; left:0px; top:115px; width:100%; height:100%; z-index:4; } #apDiv2 { position:relative; left ...

What is the best way to place a div below a div that is floated to the right?

HTML and CSS <div class="main-container"> <div class="left-panel"> <div class="panel-style">My Panel</div> </div> <div class="right-panel"></div> <div class="under-right-panel"></d ...

Utilize custom fonts from your local directory within a Vite Vue3 project

Inside my main.scss file, I am loading local fonts from the assets/styles/fonts folder: @font-face { font-family: 'Opensans-Bold'; font-style: normal; src: local('Opensans-Bold'), url(./fonts/OpenSans-Bold.ttf) format('truety ...