What is the best approach to clear floated divs effectively?

Even though I thought I had a grasp on the :after method for clearfixing in CSS, I'm having trouble getting it to work as expected. The siblingContainer that should be clearing after the buttonContainer with floated elements isn't behaving properly?

Here is the HTML:

<div class="buttonContainer">
    <div class="action_button">Button 1</div>
    <div class="action_button">Button 2</div>
</div>
<div class="siblingContainer"></div>

This is the CSS being used:

.action_button {
    font-family: Arial;
    font-size: 9pt;
    font-weight: bold;
    background-color: #f2c059;
    color: white;
    border-radius: 5px;
    display: inline-block;
    padding: 5px;
    cursor: pointer;
}
.buttonContainer {
    margin: 5px;
}
.buttonContainer div {
    margin: 0 5px;
    float: right;
}
.buttonContainer div:last-child:after {
    content: "";
    display: block;
    clear: both;
}
.siblingContainer {
    height: 500px;
    background: gray;
}

Here's a link to the fiddle for reference: http://jsfiddle.net/np0c6feh/1/

Answer №1

Another way to achieve this is by clearing the parent container instead of the child.

Rather than using

.buttonContainer div:last-child:after {
  content: "";
  display: block;
  clear: both;
}

You can use

.buttonContainer:after {
  content: "";
  display: block;
  clear: both;
}

http://jsfiddle.net/AgentStephens/np0c6feh/3/

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

Taking pictures on my website with the help of c#, HTML (excluding HTML 5), and javascript

My current project requires me to capture an image from a webcam, and I have been brainstorming solutions. However, there are specific conditions that I must adhere to in order to achieve the desired result: I am unable to utilize HTML5 due to compatibil ...

Place the Div directly above a distinct Div

Hey there! I'm currently working on positioning one div on top of another div programmatically (using javascript or css). These divs are completely separate and have the following structure: <div class="bottom"> <img src="image"></i ...

The .click function in jQuery ceases to function after being utilized once

I've encountered an issue with my .click jQuery function. The desired behavior is that when I click the button, one div should be hidden (display:none) and another div should be shown (display:block). This works as expected the first time the button i ...

Tips for disabling automatic browser scrolling to a specific div ID?

I have a webpage with a vertical accordion in the center to display content. Upon loading the page, the accordion is centered. However, when a user clicks on a tab, the entire page scrolls up, moving the tab to the top of the browser. Is there a way to pre ...

Tips for vertically aligning text in the center with bootstrap

When creating a portfolio with bootstrap, I encountered an issue where I couldn't get the text to be vertically centered. Even after trying align-items-center and justify-content-center, I still couldn't achieve the desired result. Here is the sn ...

Displaying Table Headers on Page Breaks in Laravel PDF with Webkit Technology

Let me provide some context: I am utilizing Laravel to generate a view that is then converted to a PDF using barryvdh/laravel-snappy with the help of wkhtmltopdf, and everything is functioning as expected. However, I am encountering difficulties in stylin ...

The issue of a blank first page occurring when attempting to print the contents of an

I am encountering an issue with printing a webpage that contains three images. When printing directly from Internet Explorer, it correctly prints three pages, one for each image. However, when loading the same page within an iframe and printing the iframe ...

The division is refusing to make an appearance

As I embark on creating my very first website, I'm encountering some obstacles in translating my vision into reality. The #invokeryolo div is proving to be elusive and no matter how hard I try, I can't seem to make it appear on the screen. Below ...

Tips for positioning social media icons on the right side of the navigation bar

I need assistance aligning my social media icons with the navbar on the right side of the display. Can someone provide guidance? The first image shows the current layout, while the second image illustrates how I would like it to appear: http://imgur.com/a ...

What steps can I take to modify the class of a button once it has been clicked using JQuery?

Currently, I am experimenting with Jquery to dynamically change the classes of bootstrap buttons when they are clicked. However, I have encountered a limitation while using toggleClass. The issue is that I am only able to toggle between two classes, whic ...

Using a PHP variable as the input for the img src attribute in HTML

I am currently working on a project that involves creating a gallery to showcase all the .jpg files from a specific local directory. However, I seem to be encountering some issues with it. <?php $directory = "img/"; $images = glob ...

Share an image using only the publish_actions authorization

There are some apps I've come across that only request access to users_photos and publish_actions (not publish_stream or photo_upload). Surprisingly, they are able to upload photos without being approved for User Generated Photos (e.g. Instagram). Wh ...

Is it possible to modify the spacing of menu items in the React Material-ui Drawer component?

Recently, I decided to incorporate Material-UI into a React project for the first time. The AppBar was set up to trigger Drawer, which displayed a list of menu items in the sidebar. However, an issue arose with excessive margin-top spacing. Here is an ill ...

Having trouble retrieving the content of this.text from the html template

I am attempting to include two buttons that should only be visible if there is text in the input field, otherwise they should remain hidden or disabled by default. However, I am encountering an error. <div class="card card-body"> `<form>` ...

Retrieve data from a specific page on a URL using AJAX

window.onload= function(){ var page = window.location.hash; if(window.location.hash != ""){ page = page.replace('#page:', ''); getdata('src/'.page); } } Once the window has loaded, I want to check ...

CSS animation is activated solely upon its initial creation

Exploring the world of Vuejs for the first time with my debut project. The core of my project lies in the main component where I have an array as a data variable. Here's an example snippet: export default { name: 'example-component', ...

Converting PHP arrays into visually appealing HTML tables

Having trouble writing a code to generate a table using the provided PHP array named $associative_array1 array( 'Objective' => array(0 => 'Conversions', 1 => 'Lead Generation', ), 'Gender' => array(0 =&g ...

What is the best way to implement a modal that can toggle dark mode using the Konami code, with the added functionality of a close button?

Recently, I attempted to create a Modal window that would activate when the Konami code (↑↑↓↓←→←→BA) is typed. As someone new to JavaScript, I'm still learning and open to feedback. While I have the coding part figured out, I need assi ...

What is the method for changing the background color of rows in a grid

Within my asp.net application, I am implementing a grid-view control where I am populating data into a label element within the grid-view. If the data happens to be empty, I want the row color to be displayed in red. However, if there is data to bind ...

Is it advisable to consolidate all of my JavaScript files into a single file?

Is it necessary for me to combine all my JavaScript files into one single file and then include that file in a folder? I currently have 10 separate JS files within a "javascript" folder - is this bad for the website, or is it okay? P.S. I am new to web de ...