Having trouble with clearing floats in Twitter Bootstrap CSS - any help appreciated!

Today, in an effort to learn CSS and apply it practically by creating simple themes, I decided to delve into the proper ways of clearing floats in CSS. My research led me to explore how Twitter handles this issue, so I dug into Bootstrap's CSS file and uncovered the solution I was seeking:

.clearfix {
    *zoom: 1;
}
.clearfix:before, .clearfix:after {
    display: table;
    content: "";
}
.clearfix:after {
    clear: both;
}

&

.container {
    margin-left: auto;
    margin-right: auto;
    *zoom: 1;
}
.container:before, .container:after {
    display: table;
    content: "";
}
.container:after {
    clear: both;
}

Eager to put this newfound knowledge to the test, I incorporated these code snippets into a specific part of my project like this:

<p class="sample-preview">
    <span class="sample-preview">PREVIEW</span>
    <em>This is italicized aka emphasized</em>, and so is <em>this</em>.<br />
    <strong>This is bold aka strong emphasis</strong>, and so is <strong>this</strong>.<br />
    Use <strong><em>italics and bold together</em></strong> if you <strong><em>have to</em></strong>.
</p>

+

p.sample-preview {
    border: 1px solid #FFCCC9;
    background: #FFEBE9;
    outline: 2px solid #FFEBE9;
    padding: 10px;
}

span.sample-preview {
    display: inline-block;
    float: right;
    margin:0;
    font-weight: bold;
    font-size: 12px;
    background: #FFCCC9;
    padding: 2px 5px;

}

.sample-preview {
    margin-left: auto;
    margin-right: auto;
    *zoom: 1;
}
.sample-preview:before, .sample-preview:after {
    display: table;
    content: "";
}
.sample-preview:after {
    clear: both;
}

After implementing this code, I noticed a strange bug on the page when viewed live. Upon further investigation, I suspected that the use of display: table; might be causing the issue, as removing it using Firebug seemed to fix the problem.

If you're curious, you can view the page here, where the bug manifests as the first pink box being taller than its content. Can anyone spot what I may be doing wrong here?

Answer №1

The problem arises from clearing the floated menu to the right.

There are two ways to resolve this issue:

  • One solution is to float the content area itself to the left. This creates a separate "float context" for everything inside it, and the clear will only impact those elements.

  • Another workaround is to add overflow: hidden to the sample-preview paragraph. This is a simpler approach as setting the overflow property on an element (excluding visible) makes it act like a float container.

References: ,

It's worth mentioning that by using the overflow method, there's no need for a clearfix.

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 functionality of min-height in CSS seems to be malfunctioning

One of my HTML elements has a parent with the ID #out, and a child with the ID #in. The parent is positioned absolutely, with a minimum height of 100%. It seems to be functioning correctly in most browsers, except when I also set the child's min-heigh ...

Choosing the Offspring: Deliberating on Multiple Children with Identical Names in Selenium

Just starting out with Selenium and web development in general, I've encountered a problem with locating an element using XPath. selenium.common.exceptions.NoSuchElementException I've been trying to troubleshoot for a while now, but haven' ...

Extension for Chrome that enhances YouTube video playback experience

I'm struggling to figure out why this isn't functioning. I've reviewed the Google extension dev docs and examined some sample code. Checked various Stack Overflow questions and answers, but haven't received any helpful feedback or res ...

How to efficiently retrieve a form's data from multiple forms with identical ids in JavaScript

I am facing a challenge with multiple forms on the html page that have the same ID. I need to send each form's information to the server based on user selection, but I haven't been able to find a relevant solution. For my Authorization polic ...

Unable to define a range for the hr element using the nth-of-type selector

Why isn't the hr element from the 1st to the 3rd hour red as specified in the CSS code below? hr:nth-of-type(n+1):nth-of-type(-n+3){ background:red; } <!DOCTYPE html> <html> <head> </head> <body> <hr /> <p>T ...

The vertical scrolling feature seems to be malfunctioning within my angular 10 application

I'm having an issue with the scrollbar not working on this Angular 10 template, even after adding the CSS style overflow: auto; to the main container. Although I've included the overflow property in the main container "cont" as shown in the HTML ...

Is there a way to change the color of a checkbox (both the box and font) in the disabled state using material ui

When using Material UI v4.9.12, it is possible to create a customized checkbox with a specific color that remains fixed: const GreenCheckbox = withStyles({ root: { color: green[400], '&$checked': { color: green[600], }, ...

Submitting a form with ajax and utilizing jquery

Currently, I have a form through which data is sent via ajax. The data is then processed by a php file and the results are displayed. My goal is to incorporate a bootstrap pagination bar that updates a hidden value in the form (based on the clicked page n ...

Tips for keeping the footer consistently at the bottom of the page without using a fixed position (to avoid overlapping with the main content)

I've been struggling to keep the footer at the bottom of my webpage. I came across a solution online suggesting to use fixed CSS positioning. However, when I applied this fix, the footer ended up overlapping with the actual content on the page. Is the ...

Challenges with jQuery: Appending strings to a dynamically selected element

Hello everyone, I have a question regarding adding the string 'url(" ")' around my Any assistance you can provide would be greatly appreciated. Thank you! $(function() { $('#list li a').hover( function(){ $("#meow").css( " ...

Merge two separate arrays to create a new associative array

I'm faced with the challenge of merging two arrays of data obtained from an HTML form. The first array (payment_descriptions) is structured as follows: ["1st Desc","2nd Desc","3rd Desc"] While the second array (payment_ ...

Trouble with image click event in jQuery/JavaScript

I am having trouble with my HTML and JS code. I have added images to my HTML page and want to make it so that when the image is clicked, an alert box pops up. However, for some reason, my JavaScript isn't working as expected. Here is a snippet of my ...

Interactive Audio Progress Bar in HTML5

Hello, I am trying to create a simple HTML 5 player with controls for play and pause. However, I am having trouble implementing a progress bar that interacts with the music time. I found an example of an interactive progress bar here: http://jsfiddle.net/ ...

Utilizing props to define the background-color of the '&:hover' state

I'm adapting Material UI's Chip component to accept custom values for the colors prop, rather than being limited to "primary" and "secondary". Additionally, I want the chip to exhibit a hover effect when clicked, transitioning to a different colo ...

What could be the reason for the event not being triggered multiple times?

Currently, I am in the process of designing follow/unfollow buttons for various members within a system. However, I am encountering an issue where the jQuery event does not trigger after the HTML is re-rendered within the same div containing the buttons. T ...

"VS Code's word wrap feature is beneficial for wrapping long lines of text and code, preventing them from breaking and ensuring they are

text not aligning properly and causing unnecessary line breaks insert image here I attempted to toggle the word wrap feature, installed the Rewrap plugin, and played around with vscode settings ...

Complete guide on modifying CSS with Selenium (Includes downloadable Source Code)

I am looking to switch the css styling of a website using Python and Selenium. My initial step was to retrieve the current CSS value. Now, I aim to alter this css value. The Python code output is as follows: 0px 0px 0px 270px How can I change it from 0 ...

Modify the label contents to reflect the selected file, rather than using the default text

Currently, I am working on customizing my file upload input to enhance its appearance. Everything is going smoothly so far, but I am facing difficulty in finding a suitable jQuery script that can meet my specific requirements. My objective is to have the ...

Is there a way to overlap shapes (transform) using ::before and ::after in CSS? I'm facing an issue where Edge is not showing the shape on top

I designed two diagonal shapes to overlay a container with a background image using the pseudo elements ::before and ::after. While this setup functions correctly in Firefox and Chrome, it is not working as expected in Edge. What could be causing this di ...

What is the best approach for creating HTML and CSS layouts: focusing on margin/padding or positioning?

When it comes to designing with HTML5 and CSS3, is it more beneficial to rely on margin and padding for layouts or should positioning be the main focus? I have always combined both methods to achieve desired outcomes but there are examples where each appr ...