Can the presence of a floating element impact the behavior of the subsequent static element?

As I delve into the world of web development, the concept of floating has left me perplexed. Take a look at my code snippet below:

.test {
            background-color: red;
            height: 200px;
            width: 200px;
            float: left;
        }

        .test1 {
            background-color: blue;
            height: 200px;
            width: 200px;
        }
        
    
<div class="test">Box 1</div>
        <div class="test1">Box 2</div>
    

The snapshot of its rendering in a browser can be viewed here.

I'm confused as to why text Box 2 is positioned beneath the red box, while part of the blue box seems to be overlapping with the red box. As far as I understand, both boxes should align vertically and have the red box covering the blue one.

Answer №1

Upon examining the screenshot, it is evident that the static element is influenced by the floating element. The blue box appears not to be blue due to overflowing beyond the wrapping container (in this case, the <body>). To align both boxes side by side, apply the float property to each element in the "row".

Refer to "The clearfix Hack" on w3schools css float for further clarification. Utilize your Browser's DevTools to visualize the overflow as described in the w3schools link. For enhanced testing, a third green box has been included in the example.

.test_01 {
  background-color: red; 
  height: 100px;
  width: 100px;
  float: left;
}

.test_02 {
  background-color: blue;
  height: 100px;
  width: 100px;
}

.test_03 {
  background-color: green;
  height: 100px;
  width: 100px;
}
<div class="test_01">Box 01</div>
<div class="test_02">Box 02</div>
<div class="test_03">Box 03</div>

Remember to clear all floats previously used. When creating a grid system, consider using flex-box instead. Bootstrap transitioned from float to display: flex between versions 3 and 4 for its grid system. Explore explanations of display: flex on w3schools or here.

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

I'm experiencing an issue where the changes I make in .aspx files are not showing up when I debug or rebuild

Recently, I have been experiencing an issue with my website where changes made to the .aspx file do not reflect when I debug or rebuild it. The only way for me to see the changes is by closing Visual Studio program and reopening the solution before debuggi ...

Adding a combination of HTML and Javascript to an element can result in unexpected behavior

http://jsfiddle.net/PeKdr/ Encountering an issue when having a JavaScript variable that contains both HTML and JavaScript, resulting in unexpected behavior in the output window. Neither of the buttons - one triggering the function appendTheString() or the ...

Python Selenium is having trouble finding the elements

I've been struggling for hours to extract the specific text from a variety of elements on a website. I attached 2 images in hopes that they will help in identifying these elements by their similarities, such as having the same class name. The black un ...

Keeping JSP current with changes made to files on the client side

Currently, I am tackling a project that consists of two main components: a) A Java Application that runs on a client machine. b) A Web Application that is hosted on a web server. The Java Application generates results at random intervals. These results n ...

Adjust the space between spans by utilizing the margin

There are two spans (although there could be many more) on this web page that I need to adjust the distance between. I tried using the margin-bottom attribute, but it doesn't seem to have any effect. The spans remain in their original positions, which ...

The hover effects cease before the cursor is even shifted

Can someone explain why the hover effect stops before the mouse is moved off the element? I implemented the hover effect on a before pseudo element. ...

Using CSS selectors in Framework7 Vue allows for precise targeting and styling

I am currently working on developing a Cordova/Phonegap application using vue.js and the Framework7. I have been able to utilize functions like "onClick" by using the "v-on:click="OnClick" attribute within an HTML element. It's worth noting that Frame ...

selenium tutorial: accessing checkbox state for frost

There is a frost checkbox that I have defined as follows: <div id="protocol_input" class="ember-view frost-checkbox small"> <input id="protocol_input" class="input ember-view" type="checkbox"> <label class="label" tabindex="0" for="proto ...

Navigating the FormSpree redirect: Tips and tricks

I recently set up my website on Github Pages and wanted to integrate a free contact form from FormSpree. However, I encountered an issue where after submitting the form, it redirected to a different website, which was not ideal. After researching online, I ...

Building a scrollable dropdown menu with Bootstrap 5: A step-by-step guide

The search for suitable code examples for scrollable dropdown menus compatible with Bootstrap 5 has been challenging. Are there any contemporary methods available to achieve this functionality seamlessly? ...

Issues with color scheme in Angular Material 18

While using (ng generate @angular/material:my--own-theme), I came across some code in own-theme.scss. Surprisingly, this code works for the palette included in Angular Material, but I prefer to use my own theme. Here's what was generated and what I di ...

Vertical and floating alignment

Can you help me with this HTML/CSS challenge? User: Support: Emily Johnson Alex Roberts I am looking to have two input boxes aligned vertically on both the left and right ...

What is the best way to achieve a column layout using Bootstrap?

I'm having difficulty adjusting the column width in Bootstrap columns to my specific needs. Check out this link for more information: : https://i.sstatic.net/Tvdef.png What I'm trying to achieve is to keep the label column width fixed while all ...

AngularJS: Identifying the position (ON/OFF) of ui-switch

I'm having trouble figuring out how to identify the position of my UI switch (true/false) in my JavaScript file. Here is my HTML file with the UI switch: <ui-switch ng-model='onOff'></ui-switch> And here is my controller for t ...

Tips for aligning a row at the bottom within a nested column

Desired Outcome I am struggling to arrange two smaller images next to a larger image using Bootstrap 4. Specifically, I am having difficulty aligning one of the smaller images at the bottom so that it matches the alignment of the larger image. The layout ...

Triggering a specific outcome with every user interaction involving the selection of an element

How can I trigger this effect each time the user clicks on the element using jQuery? I have added a "ripple" class upon clicking, but when I click on the element for the second time, the effect does not execute because the class has already been added. Ho ...

What if we started using PT as the standard unit for measuring fonts and element size?

Currently, I am in the process of creating a mobile app that will work across different platforms using jQueryMobile and PhoneGap. My initial focus is on developing for Android. When it comes to Android app development, it is recommended that developers u ...

how to make a "select all" checkbox with Angular 2

`I'm currently working on a feature that allows a checkbox to select all checkboxes within a specific div when checked. The div exclusively contains checkboxes. //TS FILE constructor() { } checkAll: ElementRef | undefined; selectAll(isChecked: ...

Shift a div out of view (using margin: 0px auto)

How can I achieve a sliding effect for two horizontally centered divs, #box1 and #box2, when a button with the class .class is clicked? The desired animation involves moving #box1 to the left and off the screen, while #box2 slides into view from the right. ...

The reactivity of VUE3's data is limited

I am encountering an issue with a basic HTML element that has an @click event attached to it. When I click on the a tag, the value does not update in the VUE console. However, when I check the Google Chrome Inspect Element Console, the idClient value is di ...