CSS Layout - Float: What's floating to the top?

Are there any CSS techniques available to make floated blocks fill in both upwards and in their float direction?

For example -

https://i.sstatic.net/uo06B.png

Instead of -

https://i.sstatic.net/oEijA.png

I know this can be achieved using JavaScript libraries like Masonry.

I was just curious if there are any CSS methods to achieve this or something similar.


Related codepen https://codepen.io/2nj2nu7p9oVLGXKS4tIpu8eILcmoXg/pen/QOdmqw

body * {
    box-sizing: border-box;
}

.wrapper {
    max-width: 500px;
    background: limegreen;
    margin-left: auto;
    margin-right: auto;
    overflow: hidden;
}

.block {
    height: 100px;
    background: lightblue;
    width: 250px;
    float: left;
    border: solid 2px;

    &:nth-child(even) {
        background: blue;
        height: 150px;
    }
}

Answer №1

I decided to style the odd elements with a lightblue color by setting float: left, and the even elements with a blue color by setting float: right.

This is how the layout appears:

body * {
  box-sizing: border-box;
  float: left;
}

.wrapper {
  max-width: 500px;
  background: limegreen;
}

.block {
  height: 100px;
  background: lightblue;
  width: 250px;
  border: solid 2px;
  vertical-align: top;
}

.block:nth-child(even) {
  float: right;
  background: blue;
  height: 150px;
}
<div class="wrapper">
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
</div>

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

Nested Flexbox Elements

Looking to nest elements using flexbox in order to achieve the following layout: https://i.sstatic.net/bEKps.png Does anyone know how to make this happen? ...

What is the best way to include a JavaScript variable within CSS styling?

I am currently implementing a dropdown menu system where the image of a parent div changes on mouse hover over a link, and reverts back when the mouse moves away. I have this functionality set up using a variable in my HTML code. Is there a way for me to m ...

Is there a way to set the background image to scroll vertically in a looping pattern?

Is it possible to achieve this using only HTML5 and CSS3, or is JavaScript/jQuery necessary? ...

Is it possible to hide the <dd> elements within a <dl> using knockout's custom data binding upon initialization?

I have implemented a <dl> where the <dd> can be expanded/collapsed by clicking on the corresponding <dt> using knockout's data binding. The inspiration for my solution came from a tutorial on creating custom bindings. Currently, I h ...

Avoiding additional empty lines between selectors when setting up Stylelint

Can anyone help me with configuring Stylelint options? Specifically, I want to enforce a rule that no empty lines are allowed between selectors in a list of selectors: &:focus, &:hover, &.active { color: #fb3a5e; text-align: left; text-de ...

Choose from a variety of options for color schemes and hover effects

Looking to create a selector where users can pick a color. I tried changing the background color for each option, but when hovering over them, the background turns blue - which is not what I want. Check out the pictures for clarification: https://i.sstatic ...

What is the best way to define a margin according to the size of the device being used

I am working on an angular/bootstrap web app and need to set a left margin of 40px on md, xl, lg devices and 0px on sm device. I attempted to create a spacer in styles.scss like this: $spacer: 1rem; .ml-6{ margin-left:($spacer*2.5); } Then in my HTML, ...

What is the best way to crop an image to focus solely on the center portion?

Using Bootstrap 3, how can I display an image cropped in a .col-sm-6 grid column? The image is 720px wide. Typically, with the .img-responsive class, the image will resize proportionally when the display size changes. However, I want to crop the left and ...

What is the best way to align three tables columns in a single row?

I am facing an issue with aligning the columns of three different tables in one line. I have tried applying CSS styling, but the columns still appear misaligned. How can I ensure that the columns are properly arranged in a single line? Below are my HTML/C ...

Managing several instances of NgbPagination on a single webpage

I am facing a challenge with having multiple NgbPagination components on a single page. For more information, please visit: Initially, I attempted to use ids but encountered an issue where changing one value in the first pagination affected both tables. ...

Seeking ways to ensure my component maximizes available space using Tailwind CSS

I am currently utilizing Tailwind CSS to style a Angular application. The setup is fairly straightforward: app.component.html is responsible for displaying a navigation bar and footer, while the components are shown in the remaining space with an overflow- ...

Thumbnails gracefully hovering alongside titles

I am puzzled by the fact that some of the thumbnails are displaying differently even though they have the same CSS... h4 { line-height:100%; color: #be2d30; letter-spacing: 1px; text-transform: uppercase; font-family: 'Gnuolane'; font-size:26px ...

Trouble with Bootstrap 3's nav-justified feature not displaying correctly upon window expansion

Looking at this Bootstrap example page, I noticed a small issue with the nav-justified navigation. When the window is minimized, it transitions correctly to a mobile version. However, when the window is maximized again, the buttons remain in the mobile for ...

`Responsive Site with Centered Image Overlay`

I've been attempting to center a small image within a larger one, both of which are contained within a Bootstrap Column. <div className="row justify-content-center my-5 "> <div className="col-xs-10 col-sm-6 my-auto mx-auto" ...

Tips for adjusting the font size of a Chip using Material-UI

I am using a widget called Chip const styles = { root:{ }, chip:{ margin: "2px", padding: "2px" } } const SmartTagChip = (props) =>{ const classes = useStyles(); return( <Chip style={{color:"white&q ...

Program crashes due to issues with TKinter grid implementation

Just getting started with Python and TKinter here. I'm attempting to create a program with a simple GUI, looking for some basic layout control using the grid method. Oddly enough, when I include the grid line in my code, no window pops up. However, if ...

Safari not centering element with justify-self in CSS grid

My challenge involves adjusting the alignment of a div (mobile menu) that is currently centered using css grid and the justify-self property in chrome. However, when viewed in Safari, it appears on the left side of the screen behind the Instagram icon: ht ...

Unable to create adjacent buttons using display:inline-block

I'm using Angular to dynamically create buttons, but they are stacking vertically instead of appearing side by side <div *ngIf="response"> <div *ngFor="let hit of response.hits.hits"> <button class="btn btn-primary" role="butt ...

Utilizing inline styles with the asset pipeline feature in Rails

<%= link_to root_path do %> <div class=""> <div style="background-image:<%= image_tag " image1.png "%>"> </div> <div> <h2>Title</h2> <p>Paragraph</p> </div& ...

Is there a way to determine the app bar height within a React Material UI application?

I'm trying to create a full-screen image for the opening of my website using React and Material UI. Here's a snippet of my JSX code with the default Material UI theme: <AppBar> //code in between </AppBar> <Container sx={{margin: ...