Items positioned to the left will not overlap

Having trouble with floating elements? Need to ensure that box 3 aligns under box 1 when resizing the browser window?

Use this HTML template:

<div class="box">
    <p>box 1</p>
    <p>box 1</p>
    <p>box 1</p>
</div>
<div class="box">
     <p>box 2</p>
    <p>box 2</p>
    <p>box 2</p>
    <p>box 2</p>
    <p>box 2</p>
</div>
<div class="box">
     <p>box 3</p>
    <p>box 3</p>
    <p>box 3</p>
</div>

And add this CSS style:

.box {
    width:80px;
    float:left;
    border:1px solid slateGrey;
    margin:0 0 0 10px;
    padding:10px;
}

Check out a live example on Fiddle.

If you've tried using the clear property without success, don't worry - there are other solutions available.

Answer №1

To achieve a dynamic layout like this, JavaScript is necessary. One popular option is using the Masonry plugin.

Answer №2

To gain control over the layout as you adjust the browser size and reach specific dimensions, it would be beneficial to explore media queries.

http://en.wikipedia.org/wiki/Media_queries

There is a plethora of information available on this topic, hence I am providing a link to Wikipedia for further reference.

Answer №3

When resizing the browser window with the fiddle open, you may notice that box 3 appears under box 1. To ensure that box 3 always appears like this, you can add a class such as .clear:

.clear{
  clear: left;
}

By adding the .clear class, box 3 will be positioned correctly, but slightly below box 2's bottom border. This is because all boxes are floated and treated by CSS as if they were on one line, with the height being determined by the tallest .box. To adjust the position, you can use margin-top. For example, change the <div> element of box 3 to:

<div class="box clear" style="margin-top: -70px;">

I hope this explanation clarified things for you.

In addition, if you want the dynamic addition of the clearing class when there isn't enough room to display all three boxes side-by-side, you can calculate the required space for the boxes to be together on one line and use a media query, like so:

@media all and (max-width: 500px){
    .box.clear{
      clear: left;
      margin-top: -70px;
    }
  }

The value of 500px in the media query represents the necessary space. However, it's advised not to utilize this approach if the contents of your boxes will frequently change.

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

Are you in search of an opening <html> tag for your project?

When I use Initializer to quickly generate HTML5 + Boostrap boilerplates, I noticed that the HTML document it creates does not include an opening <html> tag. This got me curious. Here is the beginning portion of the code generated by Initializr: &l ...

Arranging Content with Bootstrap Columns

I am trying to organize my content into three sections as follows: A - main content of page B - very important content (must be visible without scrolling) C - less important content. The current layout I have is like this: ----- |A|B| |A|C| ----- Howe ...

Switching from the .html extension to the absence of .html in Angular

I currently have this htaccess file set up: <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] #R ...

The duplication and multiplication of margin-left is occurring

I am baffled by the fact that margin-left is only affecting certain elements and causing frustration. .setting-container { position: relative; top: 60px; left: 0px; width: 100%; height: calc(100% - 60px); } .setting-topnav { width: 100%; h ...

I am looking to dynamically generate HTML elements using AngularJS based on data from a JSON file

Although there are existing answers to this question, I have a specific approach that I need help with. I've already made progress but could use some guidance. This is my Controller : angular.module('dynamicForm.home-ctrl',[]) .con ...

Angular2 data binding does not update when properties change

I'm struggling to establish the connection between the fields and the component in order for them to update when the properties change in OnDataUpdate(). The field "OtherValue" successfully binds two ways with the input field, and the "Name" field di ...

Integrating my HTML-CSS layout into a Ruby on Rails framework

After creating a web page template using a combination of HTML, Bootstrap, and CSS, I am now looking to see it in action on my Rails application. I need guidance on how to accomplish this step by step. Can anyone provide assistance on what steps need to ...

Vacant area on the right side of the page

There seems to be an issue with the website where there is an empty space to the right, causing a horizontal scroll bar to appear at the bottom. Despite my efforts, I cannot seem to identify the root cause of this problem. While one solution would be to si ...

Organize rows in the table while maintaining reactivity

A challenge I'm facing with a web app (Angular SPA) is that it displays a large table without the ability to sort. To work around this issue, I've managed to implement sorting via the console. However, re-inserting the rows after sorting causes t ...

Tips for concealing scrollbar track without the use of overflow

Issue with Mystery Scrollbar A strange phenomenon is occurring on all pages every time the user reloads - a mysterious scrollbar track appears out of nowhere. Initially, there is no sign of this scrollbar track, making it even more puzzling. Despite the ...

I am facing an issue where the text on my website is failing to display properly

I am facing an issue where the text on my website renders normally on Android or Windows, but when I run it on iOS, the text disappears completely. It's as if all the text has been set to display: none; The other elements seem to be tucking in just fi ...

Tips for applying CSS styles to the active page link

Check out the code below: Is there a way to assign a specific class to the current page link so that the mouse cursor will display as default rather than changing to a hand icon? I'm looking for a solution where I can apply a class to the active lis ...

How can I locate the CSS selector for a text message?

Looking for a CSS selector to target the message "Congratulations! displayed after successfully filling out a registration form with Selenium Web driver. Need to assert the text "Congratulations, Your email has been verified". Here is the HTML code: <d ...

Harnessing the power of flexbox for data visualization

Trying to use flexbox to display data from a dataset in my code. Here is the code snippet: <div ng-app="myapp" ng-controller="FirstCtrl"> <div ng-repeat="name in names" class="graph-wrapper"> <div class="aside-1 content"> ...

Having trouble with the jQuery load function not functioning properly

I have encountered an issue with this code snippet while running it on XAMPP. The alert function is working fine, but the HTML content fails to load. I have included links to jQuery 3.2.1 for reference. index.html $(document).ready(function(){ $("#b ...

transforming a table into a stylized CSS design

Seeking assistance in converting a table into CSS as I am new to CSS: <table dir="ltr" width="500" border="0" align="center"> <caption><font face="Helvetica">Movies</font></caption> <colgroup width="50%" /> ...

CSS slideshow animation is not functioning properly

Hey there, I'm new around here and I've got a question for you all. Please bear with me if I make any mistakes. So, I'm attempting to create a simple CSS slide image. In my animation code, I want the picture to fade from the left every time ...

Set Divs in Absolute Position Relative to Each Other

I need to create a simple navigation system using <div> elements as individual pages. The goal is to have "next" and "back" buttons that allow users to switch between these pages with a smooth fade-in and fade-out effect, all powered by jQuery. Howev ...

Setting the background color for a div in Vue.js when importing HTML

Check out this HTML code snippet <!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Getting Started: Serving Web Content< ...

establishing the default value as p-multiselect

Here is the code snippet I am currently working on: export class LkBoardStatus { id : number = 0; descr : string = ''; } In the component.ts file, I have defined the following: //... lkBoardStatusList: LkBoardStatus[] = []; selectedStat ...