Positioning a floated element to align with its parent container

In the scenario of a div and a series of nested divs with float: left properties, how can you ensure that the right edge of the last div in the sequence remains attached to the right edge of the parent div even when the parent div's right edge moves? This connection should persist regardless of any overflow caused by content inside the last div.

+----------------------------------------+
| parent div                             |
| +-------------+ +---------------------+|
| |             | |                     ||
| | div 1       | | div 2             >>||<<
| | float: left | | float: left         ||   
| |             | |                     ||   
| +-------------+ +---------------------+|
+----------------------------------------+

EDIT: It is important to note that preceding divs may have fixed widths, whereas the width of the last div depends on its contents and adjusts according to the position of the parent div's right edge. Additionally, the last div must be floated left.

Answer №1

To properly align the last div when using float:left, you must either set the width of both elements or remove float:left from the second div.

Check out this example on jsfiddle

Answer №2

If you're looking to navigate through this loop efficiently, consider assigning a class "lastChild" to the last div and apply the CSS property border-right:0 to that class. Alternatively, here's a quick fix you can implement:

Your CSS

.container{
border:2px solid black;
height:40px;
width:auto;
overflow:hidden;
}
.ele{
float:left;
height:20px;
width:30%;
overflow:hidden;
border:3px solid red;
}
.container div:last-child{
border-right:0;
}

Your HTML

<div class="container">
<div class="ele" > </div>
<div class="ele" > </div>
<div class="ele" > </div>
</div>

For a demonstration, check out this fiddle.

DEMO

Note the browser compatibility for :last-child in the following context:

For more insights, refer to another solution provided here.

Internet Explorer 6 offers no support for either selectors.

While IE7 and IE8 support :first-child but not :last-child, a workaround is necessary.

All versions of Firefox support both selectors. Furthermore, I've tested Chrome, Opera, and Safari on various Windows versions where these features work seamlessly.

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

Incorporating jQuery to seamlessly add elements without causing any disruptions to the layout

I'm looking to enhance the design of my website by adding a mouseenter function to display two lines when hovering over an item. However, I've encountered an issue where the appearance and disappearance of these lines cause the list items to move ...

jQuery receiving improperly formatted HTML from method call

One issue I am facing involves a jQuery Ajax method that anticipates receiving HTML as the return. The JavaScript function's URL leads to a JsonResult method within ASP.NET MVC. To construct the HTML, I am utilizing StringBuilder and checking it in Vi ...

Differentiate the Input Types in HTML

If this question has been raised previously, please verify if it is the same query. I am currently working with Materialize CSS and I am interested in learning how to use multiple types simultaneously. For example: <input id="login_password" type="sea ...

Multiple instances of Ajax drop-down effects are now available

On my website's staff page, I have implemented a dropdown menu using AJAX to display information about each member of the staff. The issue arises when attempting to open multiple dropdown menus on the same page - the second dropdown that is opened ten ...

NiceScroll is failing to show up after an Ajax reload

I've been searching everywhere for a solution to my problem. The issue I'm facing with the AJAX load and nicescroll plugin is a bit unique compared to what I've found so far. The problem is that Nicescroll isn't functioning on a separat ...

What is the method for positioning the cursor at the beginning of a center-aligned placeholder within a contenteditable element?

Here is the code snippet I am working with: HTML: <div class="title" contenteditable="true" placeholder="Title"></div> CSS: .title { width: 500px; margin: 0 auto; text-align: center; } [contenteditable=true]:empty:before{ conte ...

Link Tag and the Download Functionality in HTML

I'm looking for a way to deactivate the "download" attribute in an anchor tag, like download="false": <a href="https://cdn1.iconfinder.com/data/icons/HTML5/512/HTML_Logo.png" download></a> In my AngularJS application, I want all image fi ...

Responsiveness of the element along the vertical axis

I have an element that, when clicked, should display additional content. However, when the headline contains a lot of text, the element does not expand vertically as it should. Thank you for your help! Check out how it looks currently This is my HTML co ...

Tips for Maintaining Consistent Height and Width of Cards

My latest project involves a code snippet with 3 CSS cards displayed in a row using bootstrap. <div class="container mt-4"> <div class="row"> <div class="col-auto mb-3"> ...

Ensure the content of a textarea input is consistently centered vertically

I am currently working on a small project that involves using textareas. One issue I have encountered is that the text always starts at the top-left corner of the textarea. What I would like to achieve is having the content inside the textarea always cent ...

The animation in CSS seems to be malfunctioning, but strangely enough, the exact same code works elsewhere

Recently, I encountered a strange issue with my project. An animation effect that was working perfectly fine suddenly stopped working when I opened the project. To troubleshoot, I downloaded the same project from my GitHub repository and found that the ani ...

Creating a top-notch design with a standard app vibe (Material Design)

For the past year, I have been using Cordova to create apps by converting HTML, JavaScript, and CSS code into .apk files. However, I have struggled to achieve the same level of professionalism and "normal" feel that popular Android apps like WhatsApp posse ...

Is there a way for me to edit the HTML file in Shopify?

I'm currently navigating my way through Shopify and finding it to be a bit perplexing. Although I understand what changes need to be made and how to make them, I am struggling to locate the HTML file that requires editing. Despite clicking on Online S ...

I'm struggling to achieve the placement of my logo and navigation bar side by side

Check out the codes here body{ margin: 0; } .logo { text-indent: -999999px; background: url('logo3.png'); width: 216px; height: 219px; } .header{ width: 100%; height: auto; background-color: #eef3f5; padd ...

Designing a dynamic hamburger menu featuring multiple levels of buttons that open up additional layers of options

I'm trying to implement an animated hamburger menu with expandable buttons in the next level. While I know my code is far from achieving that, here's my progress so far: https://jsfiddle.net/TheBB23/hnsjym9u/ This code was borrowed from a webs ...

Resolve issues with vue js checkbox filtering

As I embark on my Vue journey, I came across some insightful queries like this one regarding filtering with the help of computed(). While I believe I'm moving in the right direction to tackle my issue, the solution has been elusive so far. On my web ...

The steps to properly load "child.vue" into the correct position within "parent.vue" are as follows

Currently I am developing a single page web application using Vue.js. This app consists of 4 "page.vue" files, each with a right and left child .vue component nested inside. For instance, the Page1.vue file is structured as follows (omitting style and scr ...

Using a JQuery variable to display a div using the .show() method

I am attempting to use the .show() method to reveal div elements in a slideshow-like manner. My goal is to utilize the ID of a button on my navigation bar to trigger the display of a specific slide similar to how a slideshow operates. For example, if butt ...

CSS method for creating designs surrounding text

I'm working on styling a section of a website with a pattern running alongside certain pieces of text. I've included a screenshot taken from the PSD file to illustrate the issue I'm facing, with red arrows indicating the areas that are posin ...

Transforming iframe programming into jquery scripting

Currently, I have implemented an Iframe loading the contents within every 5 seconds. It works well, however, there is a consistent blinking effect each time it loads which can be quite bothersome. I am looking to replace the iframe with a scrolling div so ...