Having a positioning of absolute and left set to 100%, the child element extends beyond its parent container

I am experiencing a challenge with my HTML elements, specifically a parent container div and a child div. When I apply the CSS property left: 100% to the child div, it extends beyond the boundaries of the parent div. While using right: 0 or CALC can solve the issue, I am determined to achieve this using only left: 100% without relying on Javascript. Is there a solution to accomplish this?

.parent{
  border: 1px solid #000;
  height: 500px;
  position: relative;
  width: 500px;
}
.child{
  background: #FF0;
  height: 100px;
  left: 100%;
  position: absolute;
  width: 100px;
}

https://jsfiddle.net/evoSL/yd48v14m/4/

Answer №1

The correct behavior in this scenario is to use the right: 0 property. Setting left:100% will push the child element 100% of the parent's width to the right, which may not be ideal in a fluid layout. Using a negative margin or adding transform: translateX(-100%) to the child may also achieve the desired effect, but it is not recommended in all cases.

Answer №2

One way to achieve this layout is by using a negative margin on the child element within a parent with a fixed width:

.container{
  border: 1px solid #000;
  height: 500px;
  position: relative;
  width: 500px;
}
.element{
  background: #FF0;
  height: 100px;
  left: 100%;
  position: absolute;
  width: 100px;
  margin-left: -100px;
}

Check out this example on JSFiddle

Answer №3

If you're looking for another approach, floats could also be an option. While I'm not sure of your end goal, here's an example of how you could implement it:

.container {
  border: 1px solid #000;
  height: 500px;
  width: 500px;
}
.box {
  background: #FF0;
  height: 100px;
  width: 100px;
  float: right;
}
<div class="container">
  <div class="box"></div>
</div>

Answer №4

By setting left:100%, you ensure that the initial border of your child element is positioned right at the edge of its container

If you want the yellow box to be aligned to the right, you have a couple of options to achieve this:

.parent{
  border: 1px solid #000;
  height: 500px;
  position: relative;
  width: 500px;
}
.child{
  background: #FF0;
  height: 100px;
  left: calc(100% - 100px);
  position: absolute;
  width: 100px;

}

.child{
  background: #FF0;
  height: 100px;
  right: 0;
  position: absolute;
  width: 100px;

}

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

The body content of the webpage needs to be updated without altering the header and footer, all while avoiding a page

On my website, I have a header menu that includes buttons for "home" and "about us." The default page is set to the home page. Within the home page, there is a specific link. When this link on the home page or the "about us" button is clicked, I want the ...

The CSS style is missing from the print preview

I've designed a style specifically for printing like this: #content .inner h2 { background: Red; border-bottom: solid 1px #dfdfdf; color: #000000; font-size: 20px; padding-left: 10px; } However, the red color doesn't show up in the print previe ...

The fixed sidebar refuses to pause before reaching the footer

I am facing an issue while building my blog. I am trying to figure out how to make my sidebar widget sticky and stop scrolling before reaching the #footer section. My widget is overlapping my footer and it would be really helpful if someone could assist m ...

Tips for showcasing a table generated from various input types on a separate page after pressing the submit button

After creating two JavaScript functions, I am eager to utilize both of them upon pressing the submit button on my form. The first function is already integrated into the submit button and activates a paragraph display upon submission. Now, I intend to sh ...

Browser disregards backslashes in the content of pseudo elements

I have been struggling with a simple issue for quite some time now, and I seem to be out of ideas. The problem lies in the styling of a CSS pseudo-element before, which is defined as follows: #fPhone::before{ color: #78be20; content: "\e622" ...

struggling to update an array with user inputs on my to-do list app

I'm currently in the process of creating a small to-do list with some specific functionality. I want each text input (or a copy of it) to be added to an empty string, ensuring that the original input remains unchanged. The goal is to have a function t ...

Is it possible for Javascript to manipulate the DOM of an Ajax text/html response?

My goal is to implement Ajax to retrieve an HTML page and extract a specific div by its ID, then insert that div into the current page. This involves loading a second page via Ajax, extracting the specified div from the response, and placing it within th ...

Arrange two <p> elements in a horizontal alignment

How can I align two <p> elements on the same line? I am trying to align both <p> elements on the same line, one to the left and the other to the right. <p style="text-align:left">Copyright &copy; {$date_year} {$companyname}. All R ...

Retrieving information from a JSON file using AngularJS

Hello, I am a beginner in Angularjs and I am facing an issue while trying to retrieve data from a JSON file. The output I am getting is quite strange. Below are snippets from my controller.js and services.js files: angular .module('app') .contro ...

Troubleshooting problem with JQuery window printing capability

After creating a paper with dimensions of 21.0 cm width and 29.7cm height, I encountered an issue where the printer output three papers instead of just two. I am not sure where I went wrong in my setup. You can find the source code here. window.print(); ...

Selectize-dropdown menu shines brightly as it opens upwards

In my sleek dashboard design, I have implemented a few dropdown menus using selectizeInput. These menus are currently positioned at the bottom of the page, but I want them to open in an upward direction instead of downward. While I found a workaround for ...

How can the front design of Material-UI's Card header be customized?

Currently, I am facing an issue with the Material-UI card header as the background color is affecting the readability of the default font. My aim is to use the typography prop h4 for the header, but I am struggling to achieve this. https://i.stack.imgur.c ...

Issue with flexbox max-width property not being applied when resizing the page width

I'm struggling to make a flex box with max and min width properties work properly. When I reduce the page size, it ends up showing blank space instead of resizing. How can I troubleshoot this issue? .parent{ border: 1px solid red; width: 50%; ...

The positioning of CSS arrows using the "top" attribute is not relative to the top of the page when using absolute values

I am currently working on positioning the arrow in the screenshot using TypeScript calculations. However, I am facing an issue where the position is being determined based on the top of the black popup instead of the top of the screen. From the top of the ...

jQuery, trigger a function when the document has finished loading

I have created the following code in an attempt to display a message to only Internet Explorer users visiting the page: <script src="jquery.reject.js"></script> <script> $(document).ready(function() { $.reject({ reject: { a ...

Create a custom div class specifically designed for an image button

Apologies if my title is a bit misleading, I struggled to find the right phrasing. Currently, I have images linked to various social networks using href. For instance, clicking on the facebook icon directs to my Facebook page. The layout looks like this: ...

Can the outcomes be showcased again upon revisiting a page?

Whenever I navigate away from and return to my filter table/search page, the results vanish. I'm looking for a way to preserve the results without reloading the page. Essentially, I want the page to remain as it was originally, with the search results ...

Dynamic Data Binding in Ionic 2

One challenge I am facing is with creating my own info window for a Google Maps marker. Whenever I click on the marker, a div is displayed but the information inside the div does not get updated. It seems like the event.title remains unchanged from its old ...

Successfully changing the source and tracking of a video through an onclick button, but the video content remains unchanged

I apologize if this post is a duplicate, but I couldn't find the answer in previous threads. I'm attempting to change the video source using an onclick button. However, even after changing the source, the video remains the same. <video width ...

Asymmetrical Edges for a Pair of Divisions

Is there a way to skew two divs without a white line in between, similar to this example: Desired outcome I have tried using a negative top margin, but it doesn't work well in responsive design. Current result Here is the code I am using: ... < ...