Make sure that the element with a relatively positioned stays contained within the top container

While relative and absolute positioning can be useful, they have the drawback of taking elements out of the flow which can lead to restrictions in their usage. Recently, I came across one such restriction and I'm interested to see if there is a solution.

To explain further: I have a div that is relatively positioned to its parent. The issue arises when, under certain conditions, this out-of-flow element extends beyond the top element (such as the body) and creates a horizontal scrollbar. See the demo below:

.top-container {
  width: 80%;
  height: 100px;
  margin: auto;
  border: dashed 2px red;
}
.container {
  width: 80%;
  height: 50px;
  margin: 25px auto;
  border: dotted 1px blue;
  
  position: relative;
}
.absolute {
  width: 100%;
  background-color: black;
  color: white;
  
  position: absolute;
  top: 30%;
  right: -50%;
}
<div class="top-container">
  <div class="container">
    <div class="absolute">
      absolutely
    </div>
  </div>
</div>

My question is: is there a way to constrain CSS absolutely positioned elements within or aligned with the borders of .top-container? A method that functions like max-left/max-right.

(for example, in my case, repositioning the black div so it stays within the confines of the red dashed line)

Answer №1

When it comes to placing an element in a specific position, you have to be precise in your instructions. You can't simply ask for the element to be placed somewhere random. One way to achieve this is by using right/left margins or positioning with percentages.

Alternatively, you can utilize the overflow property to control the scrolling behavior within the container (or hide the element if it overflows its container):

.top-container {
  width: 80%;
  height: 100px;
  margin: auto;
  border: dashed 2px red;
}
.container {
  width: 80%;
  height: 50px;
  margin: 25px auto;
  border: dotted 1px blue;
  
  position: relative;
  overflow: auto;
}
.hidden-overflow {
  overflow: hidden;
}
.absolute {
  width: 100%;
  background-color: black;
  color: white;
  
  position: absolute;
  top: 30%;
  right: -50%;
}
<div class="top-container">
  <div class="container">
    <div class="absolute">
      absolutely
    </div>
  </div>
</div>

<div class="top-container">
  <div class="container hidden-overflow">
    <div class="absolute">
      absolutely
    </div>
  </div>
</div>

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

Update the navigation logo while scrolling

My website has a scrolling navigation menu using Wordpress. The default logo color is green, but when the user scrolls, a white background appears. However, on specific screens, I need the initial logo to be white. I have managed to create an alternate na ...

Automatically populate select boxes with values from a different source using PHP

I'm in the process of setting up automatic population for 2 select boxes on a website. When a user chooses a class, the second select box automatically displays the main specialization of that class. If the user selects Tank (for example), the third ...

Discovering indistinguishable "Feeling Fortunate" inquiries

My goal is to create a program on my website that can compare the top search results for different queries. For instance, I want it to recognize that the top search result for "twelve" and "12" is the same, leading to https://en.wikipedia.org/wiki/12_(numb ...

Adjusting the Size and Spacing of Vuetify's Buttons

Why are buttons extending beyond the card width? https://i.stack.imgur.com/wVRoQ.png I'm facing difficulties customizing the buttons within the cards. I want to eliminate all padding so that the black border perfectly encloses the icon without any un ...

Refresh the database records by clicking the submit button

Is there a way to update a database by entering text into a text box and submitting it, with the data being sent to the database with a specific id? The code snippet below outlines what needs to be done, but it seems to trigger a 403 error: Access forbid ...

What is the best way to align these two divs centrally within a container set to display as inline-block?

I can't seem to get these two floated left divs centered within a container that has a height set to auto and a display of inline-block. The container has a max-width of 1600px, but when the window is stretched wider than that, it stays aligned to the ...

Create a layout using CSS that features two columns. The left column should be fluid, expanding to fill all remaining space, while the right column should be fixed

I need to ensure that the Online Users div always remains at a fixed size of 200px, while allowing the chat window next to it to resize dynamically based on available space. Essentially, I want the chat window to adjust its width as the window is resized, ...

Is it feasible to submit a request without needing to redirect the page or relying on JavaScript?

I am attempting to send a post request to an API. This is what I have so far: <form method="POST" action="/api/updoot/ID:{{ post.ID }}"> <input type="submit" value="updoot"/> </form> Upon clicking submit, it redirects. While I can p ...

"Utilize AJAX to submit the value of the text box input from a jQuery slider when the Submit Button

Whenever I adjust the sliders, the value is shown in an input textbox. However, when I move the slider and check the values echoed from the textboxes on another PHP page, they are always displaying as 0. Even after clicking the submit button, it still echo ...

Adjusting the grid for various mobile screen sizes

I am currently working on designing a user interface (UI) for mobile screens that includes a header, grid, and buttons. I want the UI to look consistent across all mobile devices. Here are screenshots from various mobile screens: Samsung S5 Pixel 2 XL I ...

Issue with displaying background image in h2 element

Consider me a novice who learns primarily through trial and error. Unfortunately, I'm stuck on this particular issue. My goal is to have an image appear behind the h2 text on my homepage, but despite my efforts, I can't seem to make it work. You ...

Validation of forms - Must include one particular word from a given set

I am in the process of utilizing Javascript to validate an input field with the specific formatting requirements outlined below: "WORD1,WORD2" The input must contain a comma separating two words, without any spaces. The first word (WORD1) can be any word ...

Arranging column contents neatly on small screens

Although there are similar answers available, I want to address those who are trying to prevent their columns from stacking on XS display. Despite having two columns, the content is still stacking at certain displays. This issue is occurring while using B ...

Tips for adjusting arrow alignment in your custom breadcrumbs using Bootstrap 5.2.0

There is an issue with my custom Bootstrap 5.2.0 breadcrumb - the right arrow appears before the end of the breadcrumb's item box when I add one or more items. To demonstrate the problem, I have created a fiddle where you can see the error: .breadcru ...

Determine the total by multiplying the value of a dropdown menu and a text input field using jQuery when the values are changed

I am new to jQuery and JavaScript. I have a textbox and a select box, and when I enter a value in the textbox and select a value from the select box, I want to display the multiplication of both values in another textbox. I have tried doing this with two t ...

Does the message "The reference 'gridOptions' denotes a private component member in Angular" signify that I may not be adhering to recommended coding standards?

Utilizing ag-grid as a framework for grid development is my current approach. I have gone through a straightforward tutorial and here is the code I have so far: typography.component.html https://i.stack.imgur.com/XKjfY.png typography.component.ts i ...

Strangely Bizarre CSS Quirk

At this website, there seems to be an issue with how the footer displays on Internet Explorer and certain older versions of Firefox. However, it appears perfectly fine on Firefox 3.6.13. Any suggestions on what could be causing this discrepancy? ...

Changing Background Color on Div Click

After spending a considerable amount of time on this, I find myself getting confused and stuck. It seems like I might be overlooking something crucial. Essentially, my code is designed to have the default div background (gamebg), and upon clicking one of t ...

site with scrolling options in various directions

Someone recently asked me if I could create a multi-directional scrolling website similar to the one found at COS, but using WordPress. At first, I assumed it might be done with Flash, but the source code suggests it could actually be more simple, possibly ...

Ways to utilize jquery to limit the length of an editable div and prevent it from exceeding our specified restriction

Imagine a scenario where you have the following code snippet: <div id="editing" contenteditable onclick="document.execCommand('selectAll',false,null)">Put text here...</div> In this situation, let's say you want to impose a r ...