The floated div is disrupting the flow due to its margin

Two divs are causing some alignment issues: one floated left and the other floated right. The margin on the right div is pushing it down, causing the left div to appear lower than desired. I want both divs to be aligned at the top of the page.

HTML:

 <div class="right_div">
            This div is in its correct position.
        </div>

    <div class="clear"> </div>

    <div class="left_div">

This div should be at the top</div>

CSS:

.right_div {

    float: right;
    margin-right:20px;
    margin-top: 20px;
    font-weight: 600;
    background-color:blue;
}

 .left_div{

    margin-left: 20px;
    margin: 0 0 0 20px;
    padding: 0;
    background-color: tomato;
    text-align: left;
    max-width: 10em;

 }

.clear {

    clear:both;
}

For reference, here is the JSFIDDLE link: http://jsfiddle.net/eLSc8/

Answer №1

delete

<div class="clear"> </div>

give this a shot

<div class="right_div">
            The content inside this div is on the right side.
        </div>

    <div class="left_div">

Answer №2

delete the

<div class="remove"> </div>

but keep the blue element on top.
By the way, it's recommended to avoid using empty tags for styling purposes. Instead of adding unnecessary markup, consider using a non-structural method like easyclearing or more modern techniques (such as the .clearfix class from HTML5 Boilerplate)

Answer №3

give it a shot

Check out this link

kindly delete the clearboth command

<div class="clear"> </div>

Answer №4

Many are suggesting removing the clearing div and instead applying a float left to the other div for alignment purposes. Depending on the layout and desired positioning, you may need to contain them or consider adding margin. Take a look at jsfiddle.net/ABC123/4/ for reference.

Answer №5

Does this achieve the intended outcome:

http://example.com/abc123 ?

HTML Structure:

<div class="top_div">This div should go at the top</div>
<div class="bottom_div">This div is in the correct position.</div>

CSS Styling:

.bottom_div {
    font-weight: 600;
    background-color:green;
    position: absolute;
    top: 30px;
    right: 10px;
    height: 2em;
}
.top_div {
    position:absolute;
    top: 30px;
    left: 10px;
    background-color: purple;
    height: 2em;
}

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

Tips for Printing a div Element with Horizontal Scrollbars

My webpage has both vertical and horizontal scroll bars, but when I use window.print(), it only prints the visible content in the window. Is there a way to print the entire scrollable content within the window? ...

How to activate the menu in AngularJS

Within my application, I have a header that contains various menu items. These menu items are fetched from a service and displayed in the header. When hovering over the main list, the submenus appear. My goal is to highlight the parent item as active when ...

What is the best way to pass a variable in PHP and create an HTML link to a specific element ID simultaneously?

Formulating the question may be challenging, but the concept is quite straightforward: I'd like to pass the variable 'count' using GET and simultaneously direct to the media section. On the page http://page.org.mx/comunicados#media I&apo ...

"Utilizing Bootstrap for a Dynamic Display: Implementing Multiple Carousels with Multiple Images within

I have incorporated several Bootstrap carousels on one page, each with its own unique identifier. These carousels are generated dynamically based on user interactions, such as uploading images. My goal is to display 3 images at once in each carousel. I am ...

Transition one background image into another using background positioning

Snippet of code: https://jsfiddle.net/foy4m43j/ HTML: <div id="background"></div> CSS: #background { background-image: url("http://i.imgur.com/hH9IWA0.png"); background-position: 0 0; background-repeat: repea ...

Creating HTML content using Racket

Is there a straightforward method for converting X-expressions to HTML in Racket without using response/xexpr? It appears that this function is mainly intended for serving http responses, but I only need to convert Racket lists (or X-expressions) into an ...

What is the best way to make a div that maintains its size even when the content inside is modified?

My goal is to develop a media feed that includes various types of content such as text, images, and videos. To ensure user safety, I want to conceal any explicit posts with a warning that requires the user to click in order to view the content. Although I ...

Determine the specific cell involved in an HTML5 drag-and-drop interaction within a table

I've been experimenting with the HTML5 drag and drop functionality in an Angular project. Here's the setup I'm working with: A container containing draggable 'objects' A table where users can drop the dragged elements Following ...

Stylish slanted divs achieved using Bootstrap 4

I'm currently in the process of constructing a block with 6 slanted divs using Bootstrap. Take a look at the image provided below: https://i.sstatic.net/vvg0b.png After attempting to use rows and columns, I noticed that they weren't aligning co ...

The entire space on an HTML page should be occupied with content from the header to the footer

I am currently implementing jQuery Mobile on my project. The page I have created consists of a fixed header and footer with only two buttons. Due to the fixed footer, half of the page is in silver color (data-theme=c) while the bottom half is gray. My goa ...

I'm facing a challenge with getting the 'extend' feature to function correctly while using Flask in Python

As someone who is just starting out in programming, I recently decided to embark on creating a web app using flask, python, and HTML. To start off, I set up three essential files - two HTML files, and one python file. The first HTML file requiring informa ...

The origin of the recipient window does not match the target origin provided when using postMessage on localhost

Currently, I am in the process of developing an application that utilizes single sign-on (SSO) for user authentication. Here is a breakdown of the workflow: Begin by launching the application on localhost:3000 (using a React Single Web Application). A po ...

Wrap the json response in HTML elements

I'm currently learning about AJAX, and I've encountered a major issue that I can't seem to resolve. I pass several variables to PHP, perform various checks there, and then return string values to AJAX. Strangely, I am able to display these r ...

Configuring various options on numerous dropdowns using AngularJS

I am looking for a solution to customize dropdowns created through ng-repeat in AngularJS. The number of dropdowns displayed is determined by the data fetched from the database. I want to be able to set an initial option value for each dropdown based on th ...

Why isn't Sequence.js Slider automatically playing?

Issue: The sequence.js slider I implemented is not animating. Despite adding the following options: animateCanvas: true, fadeStepWhenSkipped: false, autoPlay: true, autoPlayInterval, 2000 It still does not work. Is there something essential t ...

Construct a Product Showcase Page" (FreeCodeCamp) - Issue "Incurred upon clicking a .nav-link button in the navigation panel

Hi there! I've been working on a page programming task, but I'm having trouble with the "№5 “When I click a .nav-link button in the nav element, I am taken to the corresponding section of the landing page.” error. Can anyone help me figure ...

Using Angular 2 to trigger an event when a native DOM element is loaded

I am working towards my main objective of having a textarea element automatically focused upon creation. I recently came up with an idea to use e.target.focus() on the onload event. It would look something like this: <textarea rows="8" col="60" (load)= ...

Leverage multiple services within a single AngularJS controller

Is it possible to use multiple services in the same controller? Currently, I am able to achieve this with different services, but it seems to only perform one service. <!DOCTYPE html> <html> <script src="http://ajax.googleapis.com/ajax/libs ...

Tips for altering the color of spans that share a common top position without affecting the surrounding sibling elements above or below

Is there a way to change the color of spans with the same top position, excluding sibling elements above or below, in a paragraph made up of spans? I've been working on how to change the color of the line of span tags that contain another span with t ...

Issues arise when attempting to implement a basic quiz using jQuery or JavaScript

I’ve created a quick quiz to determine how many correct answers a person gets when they click the 'submit' button. However, it keeps showing zero as the result. Can someone please assist me? $(document).ready(function(){ $('button' ...