Achieving a floating effect by moving elements from the bottom left corner to the top right corner

Is there an innovative and efficient method to align elements from the bottom left corner to the top right corner?

I have attempted using display: grid, but it is too rigid with fixed columns and a limit of 10 items. I am in search of a clever solution that can work seamlessly with multiple columns and rows of elements.

.cart {
  border: 5px solid;
  padding: 8px;
  width: 240px;
  height: 200px;
  display: flex;
  align-items: flex-end;
}

.box {
  background-color: lightgray;
  padding: 4px;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  grid-template-areas: "box6 box7 box8 box9 box10" "box1 box2 box3 box4 box5";
}

.egg {
  width: 40px;
  aspect-ratio: 3 / 4;
  background-color: sandybrown;
  border-radius: 100% 100% 100% 100% / 120% 120% 80% 80%;
  box-shadow: inset -4px -8px 20px brown;
  font-family: sans-serif;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
}

.egg:nth-child(1) {
  grid-area: box1;
}

.egg:nth-child(2) {
  grid-area: box2;
}

.egg:nth-child(3) {
  grid-area: box3;
}

.egg:nth-child(4) {
  grid-area: box4;
}

.egg:nth-child(5) {
  grid-area: box5;
}

.egg:nth-child(6) {
  grid-area: box6;
}

.egg:nth-child(7) {
  grid-area: box7;
}

.egg:nth-child(8) {
  grid-area: box8;
}

.egg:nth-child(9) {
  grid-area: box9;
}

.egg:nth-child(10) {
  grid-area: box10;
}
<div class="cart">
  <div class="box">
    <div class="egg">1</div>
    <div class="egg">2</div>
    <div class="egg">3</div>
    <div class="egg">4</div>
    <div class="egg">5</div>
    <div class="egg">6</div>
    <div class="egg">7</div>
    <div class="egg">8</div>
  </div>
</div>

Answer №1

You can spin the entire object around the X axis first, and then individually rotate each egg around its own X axis to orient it correctly.

<style>
  .cart {
    border: 5px solid;
    padding: 8px;
    width: 240px;
    height: 200px;
    display: flex;
    align-items: flex-end;
  }
  
  .box {
    background-color: lightgray;
    padding: 4px;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 8px;
    transform: rotateX(180deg);
    bottom: 0;
    position: relative;
  }
  
  .egg {
    width: 40px;
    aspect-ratio: 3 / 4;
    background-color: sandybrown;
    border-radius: 100% 100% 100% 100% / 120% 120% 80% 80%;
    box-shadow: inset -4px -8px 20px brown;
    font-family: sans-serif;
    font-weight: bold;
    display: flex;
    justify-content: center;
    align-items: center;
    transform: rotateX(180deg);
  }
</style>
<div class="cart">
  <div class="box">
    <div class="egg">1</div>
    <div class="egg">2</div>
    <div class="egg">3</div>
    <div class="egg">4</div>
    <div class="egg">5</div>
    <div class="egg">6</div>
    <div class="egg">7</div>
    <div class="egg">8</div>
  </div>
</div>

Answer №2

You can also flip the grid and cells using the scale transformation. Learn more about it here

The scale() CSS function allows you to resize an element on a 2D plane, with the option to scale the horizontal and vertical dimensions differently by providing a vector [sx, sy]. The result is a data type.

An input of 1 as a positive value maintains the original size, but as a negative value, it mirrors the element along its x / y axis.

.cart {
  border: 5px solid;
  padding: 8px;
  width: 240px;
  height: 200px;
}

.box {
  background-color: lightgray;
  padding: 4px;
  display: grid;
  grid-template-columns: repeat(auto-fit, 40px);
  gap: 8px;
  height: 100%;
  place-content: start;
  scale: 1 -1;/* mirrors the box on its Y axis */
}

.egg {
  scale: 1 -1; /* flips the cells back */
  width: 40px;
  aspect-ratio: 3 / 4;
  background-color: sandybrown;
  border-radius: 100% 100% 100% 100% / 120% 120% 80% 80%;
  box-shadow: inset -4px -8px 20px brown;
  font-family: sans-serif;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
}
<div class="cart">
  <div class="box">
    <div class="egg">1</div>
    <div class="egg">2</div>
    <div class="egg">3</div>
    <div class="egg">4</div>
    <div class="egg">5</div>
    <div class="egg">6</div>
    <div class="egg">7</div>
    <div class="egg">8</div>
  </div>
</div>

<h1>and if too many ? </h1>
<p>it overflows at top </p>
<div class="cart">
  <div class="box">
    <div class="egg">1</div>
    <div class="egg">2</div>
    <div class="egg">3</div>
    <div class="egg">4</div>
    <div class="egg">5</div>
    <div class="egg">6</div>
    <div class="egg">7</div>
    <div class="egg">8</div>
    <div class="egg">9</div>
    <div class="egg">10</div>
    <div class="egg">11</div>
    <div class="egg">12</div>
    <div class="egg">13</div>
    <div class="egg">14</div>
    <div class="egg">15</div>
    <div class="egg">16</div>
  </div>
</div>


<p>can be scrolled </p>
<div class="cart">
  <div class="box" style="overflow:auto;">
    <div class="egg">1</div>
    <div class="egg">2</div>
    <div class="egg">3</div>
    <div class="egg">4</div>
    <div class="egg">5</div>
    <div class="egg">6</div>
    <div class="egg">7</div>
    <div class="egg">8</div>
    <div class="egg">9</div>
    <div class="egg">10</div>
    <div class="egg">11</div>
    <div class="egg">12</div>
    <div class="egg">13</div>
    <div class="egg">14</div>
    <div class="egg">15</div>
    <div class="egg">16</div>
  </div>
</div>

Answer №3

All you require is simply adding

display: flex;flex-wrap: wrap-reverse;

.cart {
  border: 5px solid;
  padding: 8px;
  width: 240px;
  height: 200px;
  display: flex;
  align-items: flex-end;
}

.box {
  background-color: lightgray;
  padding: 4px;
  display: flex;
  flex-wrap: wrap-reverse;
  gap: 8px;
}

.egg {
  width: 40px;
  aspect-ratio: 3 / 4;
  background-color: sandybrown;
  border-radius: 100% 100% 100% 100% / 120% 120% 80% 80%;
  box-shadow: inset -4px -8px 20px brown;
  font-family: sans-serif;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
}
<div class="cart">
  <div class="box">
    <div class="egg">1</div>
    <div class="egg">2</div>
    <div class="egg">3</div>
    <div class="egg">4</div>
    <div class="egg">5</div>
    <div class="egg">6</div>
    <div class="egg">7</div>
    <div class="egg">8</div>
  </div>
</div>

You can also eliminate the additional wrapper:

.cart {
  border: 5px solid;
  padding: 8px;
  width: 240px;
  height: 200px;
  display: flex;
  flex-wrap: wrap-reverse;
  gap: 8px;
  align-content: end;
}

.egg {
  width: 40px;
  aspect-ratio: 3 / 4;
  background-color: sandybrown;
  border-radius: 100% 100% 100% 100% / 120% 120% 80% 80%;
  box-shadow: inset -4px -8px 20px brown;
  font-family: sans-serif;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
}
<div class="cart">
    <div class="egg">1</div>
    <div class="egg">2</div>
    <div class="egg">3</div>
    <div class="egg">4</div>
    <div class="egg">5</div>
    <div class="egg">6</div>
    <div class="egg">7</div>
    <div class="egg">8</div>
</div>

Answer №4

Utilizing a Jquery method with JQ as demonstrated in this Stack Overflow response: How to reverse the order of HTML elements

$.fn.reverseChildren = function() {
  return this.each(function() {
    var $this = $(this);
    $this.children().each(function() {
      $this.prepend(this);
    });
  });
};
$('.wrap').reverseChildren();
.wrap {
  outline: 1px solid red;
  padding: .25em;
  width: 150px;
  height: 200px;
  display: flex;
  flex-wrap: wrap;
  flex-direction: row-reverse;
  justify-content: space-between;
  align-content: end;
  gap: .25em;
}

.item {
  outline: 1px solid green;
  width: 2rem;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

<div class="wrap">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="item">4</div>
  <div class="item">5</div>
  <div class="item">6</div>
  <div class="item">7</div>
  <div class="item">8</div>
  <div class="item">9</div>
  <div class="item">10</div>
  <div class="item">11</div>
  <div class="item">12</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

The flexbox container is not displaying properly

I have rows containing two flex divs each. One div displays an image, while the other has text. I'm puzzled as to why some divs display correctly while others have overflowing text. Refreshing the page resolves this issue, but it resurfaces if I clea ...

Discover a personalized message and alter its hue using JavaScript and CSS styling

I've hit a roadblock while creating my website with WordPress and a custom theme. Although I can easily inject custom Js or CSS, I need advice on how to achieve the following: How can I locate the symbol ★ within the content and change its color? ...

The dialog box in CSS is extending too far down past the bottom of the screen, making it impossible to scroll and click on the buttons located

I am currently working on creating a dialog box with a text area using material UI. Unfortunately, when I input a significant amount of text, the dialog box ends up extending beyond the screen, making it impossible to scroll down to access the buttons. &l ...

I can't see the title text on my Ionic button, what should I do to fix this issue?

I have been working on a login form using ngx-translate and all components are translating correctly except for the submit button. It seems that the translated text for the button does not display unless it is hardcoded or clicked on. I'm not sure if ...

When the first radio button is clicked, the radio button within that specific div will be automatically opened

I have a list of packages displayed as radio buttons. When a package is selected, the corresponding price options are shown as radio buttons for the first option and normal buttons for the rest. Upon selecting a package, the first price option should auto ...

Issue with Data Insertion in MySQL server

I am facing an issue with inserting data into MySQL using PHP. I have created a script in HTML and PHP, along with setting up a MySQL database. However, I am encountering difficulties as the data is not being sent to the MySQL server. Below is the snippet ...

Issue with displaying MP4 movies in Angular

I'm trying to display an mp4 video in Angular 9: <video controls (click)="toggleVideo()" preload="none" *ngIf="post.moviePath != null" #videoPlayer> <source [src]="getMovieSanitazePath(post.moviePath ...

Experiencing a problem with inline JavaScript onclick

I recently came across a fantastic pure javascript code for a smooth scrolling function, but I'm struggling with integrating it into an inline onclick function. My understanding of javascript is quite limited and I could really use some help... <d ...

Access a folder in JavaScript using Flask

I need to specify a directory in a script. $images_dir = '{{url_for('.../pictures')}}'; In my flask application directory structure, it looks like this: Root -wep.py -templates -gallery.html -static -pictures The images are stored ...

Navigating through Polymer's cascading style sheets

Encountering an issue while attempting to access a Polymer defined CSS variable in a Jekyll SCSS/CSS file. The integration of Polymer Elements' "Paper Styles" for colors is done by including <link rel="import" href="/bower_components/paper-styles/ ...

transform Django's color using URL links

I've successfully implemented 'urlize' in Django (version 4.0.1) within a 'project description' field and it's working perfectly. <h6>{{ project.description | urlize }}</h6> Is there an easy way to apply a diff ...

Creating a moving marquee effect in Ionic

I am attempting to achieve a marquee effect in Ionic, where the text automatically scrolls across the screen. It functions smoothly on the web simulator but appears incredibly choppy and not smooth when loaded onto an iPhone. I am wondering if there is a s ...

Invoke a jQuery function in the parent page using regular JavaScript from an iframe

I have successfully created an iframe using regular javascript. Inside the iframe is a Jquery function along with JQuery itself and it functions correctly within the iframe. However, I am now looking to execute this function from the parent page instead of ...

A confined space that is scrollable, yet keeps any overflow hidden without displaying a scroll bar

Is there a method for creating a overflowing div that conceals the overflow, does not display a scroll bar, but remains scrollable (via mouse wheel or touch input)? For example: <!DOCTYPE html> <div id="longtext" style="width:100px; height ...

When clicking on the HTML div element, it will be appended to the application

Within my vertical menu, there are 5 items including web design and mobile app. When clicked on these items, they reveal their respective href attributes as shown below. <div class="col-md-3"> <ul class="nav nav-tabs nav-stacked"> ...

What is the best way to include multiple ng-repeats within a single ng-repeat?

Hey, I'm facing quite a tricky situation with this grid and could use some assistance. I'm trying to figure out how to populate the data using ng-repeat. I've attached an image showcasing the desired layout of the grid and the order in which ...

Modify the appearance of an image by hovering over the ::after element for image styling

My design involves a main image with a smaller arrow image overlaid on top, achieved using a pseudo after-element. Currently, when I hover over the main image, the opacity changes as intended. However, the issue arises when hovering over the arrow image o ...

I'm having trouble viewing my navigation bar, even though everything seems to be correct

I'm facing an issue where the navigation bar is not visible even though everything seems to be correct. I have checked and double-checked but still can't figure out what's wrong. It's becoming quite frustrating and I'm at a loss fo ...

"Exploring Angular: A guide to scrolling to the bottom of a page with

I am trying to implement a scroll function that goes all the way to the bottom of a specific section within a div. I have attempted using scrollIntoView, but it only scrolls halfway down the page instead of to the designated section. .ts file @ViewChild(" ...

What is the best way to align a div using position:absolute in a body that has a width of 100%, while maintaining equal spacing on the left and

How can I position a <div> with position:absolute so that it is spaced 20px from the left and right sides while maintaining a width: of 100%? Currently, I have applied margin: 0 20px 0 20px, but I am wondering if there is a way to achieve this withou ...