How to position two divs to the right of another div, without them being adjacent to each other, using CSS

I am trying to arrange three divs with background colors of Red, Yellow, and Blue. The goal is to have the Red div on the left, followed by the Yellow div next to it, and then the Blue div underneath the Yellow one. However, my current code is not producing the desired layout. Here is what I have:

<html>
     <body>
          <div style="background-color:Red; height:50%; float: left">Red</div>
          <div style="background-color:Yellow; float: left; clear:right">Yellow</div>
          <div style="background-color:Blue; float:left">Blue</div>
     </body>
</html>

Despite my efforts, the Blue div ends up beside the Yellow div instead of below it. Any suggestions for achieving the correct positioning?

Answer №1

A simple way to create columns is by enclosing them in <div> tags:

<div class="wrapper">
    <div class="red">
        Red
    </div>
</div>
<div class="wrapper">
    <div class="yellow">
        Yellow
    </div>
    <div class="blue">
        Blue
    </div>
</div>

You can style them using CSS:

.wrapper {
    float: left;
}
.red {
    background-color: red;
}
.yellow {
    background-color: yellow;
}
.blue {
    background-color: blue;
}

Here's a live example for reference: http://jsfiddle.net/ambiguous/aNVam/

If you want to adjust the sizes of the <div> elements without using wrappers, you can utilize clear: left and margin-left:

<div class="red">
    Red
</div>
<div class="yellow">
    Yellow
</div>
<div class="blue">
    Blue
</div>

Corresponding CSS styling would be:

.red {
    background-color: red;
    width: 100px;
    float: left;
}
.yellow {
    background-color: yellow;
    float: left;
    width: 75px;
}
.blue {
    background-color: blue;
    float: left;
    clear: left;
    width: 75px;
    margin-left: 100px; /* matched with .red */
}

Here's a live example showcasing this method: http://jsfiddle.net/ambiguous/t8mMt/1/

This layout should suit your needs.

Answer №2

Eliminate the float: left; property from the yellow and blue divs, like so:

<html>
     <body>
          <div style="background-color:Red; height:50%; float: left">Red</div>
          <div style="background-color:Yellow;  clear:right">Yellow</div>
          <div style="background-color:Blue; ">Blue</div>
     </body>
</html>

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 integrating YouTube links into PHP

As someone who is relatively new to PHP, I am currently working on pulling information from an AirTable database and displaying it on a webpage, which I have successfully done. Now, I need to include a video along with the numerical data, with each set of ...

Repeated Outcomes in SQL Query

Looking for help with PHP coding! I've been stuck on this problem for a few days and need some assistance. I have two tables in my database: "projects_2016" and "attachment". My goal is to display data from "projects_2016" in the top table and then ...

The KeyboardAvoidingView disrupts the structure of the flexbox layout

Check out this code snippet: return ( <KeyboardAvoidingView style={{ flex: 1 }} behavior="padding" enabled> <View style={style.flex1}> <View style={style.imageContainer}> <Image style={style.image} ...

The canvas element on a simple HTML webpage is constantly set to a size of 300x150

In an attempt to simplify my problem, I have created a basic HTML document with a <canvas> element: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> body { border: 1px solid #ff5500; ...

What is the way to restrict the length of input in a v-select component?

I am currently using the Vue-select feature in my project and I have encountered an issue with setting a maximum input length of 45 characters within a v-select element. Despite attempting to handle this limitation on the back-end, I am unable to prevent u ...

Using Clip Path will result in the content always being hidden if it

Here is an example I created: div { -webkit-clip-path: polygon(0 0, 100% 0, 85% 100%, 0% 100%); clip-path: polygon(0 0, 100% 0, 85% 100%, 0% 100%); background-color: red; width: 150px; height: 150px; position: relative; } ul{ position: absolute; ...

What is the best way to center a Checkbox in HTML5 and CSS?

I've been struggling to center my Checkbox with label on my current website project. Despite searching online for solutions, I'm still unable to find the right method! If you're curious, you can check out the source code of my webpage at ( ...

Scrollbar width does not expand when hovered over

I am having trouble increasing the width of the scrollbar when hovering over it. However, I keep receiving an error in the console stating that 'scrollbar.addEventListener' is not a function. JAVASCRIPT setTimeout(function() { const scrollbar ...

Importing a substantial number of records from an XML file into an HTML table

Currently, I am working on a project where I am reading records from an XML file and dynamically displaying them in an HTML table using JavaScript's push method. Everything works smoothly when the number of records is below 1000. However, when the num ...

Sleek dialog sliding animation with Svelte

I'm struggling with a svelte component that I have and I'm trying to implement a slide down animation when it closes. The slide up animation is functioning correctly, but for some reason the slide down animation is not working. Does anyone have a ...

Exploring the world of colored tab links in CSS

Check out this code: <!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <title>My Unique Page</title> <link rel="stylesheet" href="http://code.jquery.com/ui/1.8.3/themes/base/jquery-ui.css" /> ...

When it comes to handling media queries, iPhone is geared towards min-width dimensions of 768

The layout of my landing page has a CSS structure that looks like this: body { background: white; } @media (min-width: 768px) { body { background: red; } } @media (max-width: 767px) { body { background: blue; } } Des ...

Disappearing fixed div in Chrome when hovering over links

I've encountered a strange issue while working on my website (beta.kylehorkley.com). When I hover over the links in the sidebar, the sidebar disappears momentarily and then reappears about a second later. This problem is occurring in Chrome and Opera ...

Minimize the gap between icon and text on paper-icon-item in Polymer

Is there a way to decrease the spacing between the icon and text in paper-icon-item? I'm looking for a solution to this issue. https://i.sstatic.net/kZ9sy.png I attempted the following: paper-icon-item { --paper-item-icon: { padding-right: 0; ...

Shift the image down to the bottom of the sidebar

This is the code for my sidebar HTML, with the side navigation bar positioned on the left: #main-container { display: table; width: 100%; height: 100%; } #sidebar { display: table-cell; width: ...

Leading astray — using htmlentities will not function

UPDATE (Instead of deleting this question, I'll keep it as a reminder that errors are sometimes found in unexpected places...) I apologize for leading you down the wrong path with this question. The issue causing the "Actual result" was actually loc ...

In your web projects, how many external conditional stylesheets do you typically incorporate specifically for Internet Explorer?

What is the number of external IE Conditional Style-sheets utilized in your web projects specifically for Internet Explorer? Make sure to review this page before you provide an answer: http://css-tricks.com/how-to-create-an-ie-only-stylesheet/ ...

Emphasize the search term "angular 2"

A messenger showcases the search results according to the input provided by the user. The objective is to emphasize the searched term while displaying the outcome. The code snippets below illustrate the HTML and component utilized for this purpose. Compon ...

Tips for adding a red dot at the top-right corner of a necessary HTML input label, similar to an asterisk

Is there a way to replace the asterisk with a dot in an HTML label for a required input? The dot should be positioned in the top-right corner similar to where the asterisk would typically be. ...

Issues with the last-child selector in CSS being unresponsive

I am having trouble applying styles to the final <p> in a specific div. I would like to achieve this without introducing a new class or ID. Despite attempting to use the last-child selector, I have not been successful in accomplishing my goal. I ha ...