Tips on creating vertical stacked content utilizing CSS

Has anyone come across this game before?

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

I am trying to stack each card in a column, here is my fiddle jsfiddle.

In my scenario, I have cards wrapped in div elements with a maximum height. If a card exceeds this height, it should not collapse or hide, but instead be stacked on top.

This is what currently happens in my code https://i.sstatic.net/tCKNj.png

I want it to look like this, with each card stacked https://i.sstatic.net/4Feyc.png

HTML

<div class="wrapper">
  <div class="card card-1">
    <h4 class="header-title">
      Lorem Ipsum
    </h4>
    <div class="card-body">
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloremque iusto molestiae eos totam ea, ratione quis veritatis commodi,
      </p>
    </div>
  </div>
  
  <div class="card card-2">
    <h4 class="header-title">
      Lorem Ipsum
    </h4>
    <div class="card-body">
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloremque iusto molestiae eos totam ea, ratione quis veritatis commodi,
      </p>
    </div>
  </div>
  
  <div class="card card-3">
    <h4 class="header-title">
      Lorem Ipsum
    </h4>
    <div class="card-body">
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloremque iusto molestiae eos totam ea, ratione quis veritatis commodi,
      </p>
    </div>
  </div>
</div>

CSS

.wrapper{
  display: flex;
  flex-direction: column;
  max-height: 75vh;
  overflow: hidden;
}
.card{
  padding: 0.4em 1rem;
  width: 250px;
  border-radius: 5px;
  margin: 0.5rem;
}
.card.card-1{
  background: salmon;
  border: 2px solid red;
}
.card.card-2{
  background: royalblue;
  border: 2px solid blue;
}
.card.card-3{
  background: greenyellow;
  border: 2px solid limegreen;
}

Any ideas on what I should do to achieve this?

Answer №1

Have you experimented with utilizing the z-index property? It lets you define the stacking order of an element.

For instance:

.container{
  display: flex;
  flex-direction: column;
  max-height: 75vh;
  overflow: hidden;
}
.box{
  padding: 0.4em 1rem; 
  width: 250px;
  border-radius: 5px;
  margin: 0.5rem;
}
.box.box-1{
  background: salmon;
  border: 2px solid red;
  position: absolute;
  left: 0px;
  top: 0px;
  z-index: -3;
}
.box.box-2{
  background: royalblue;
  border: 2px solid blue;
  position: absolute;
  left: 0px;
  top: 15px;
  z-index: -2;
}
.box.box-3{
  background: greenyellow;
  border: 2px solid limegreen;
  position: absolute;
  left: 0px;
  top: 30px;
  z-index: -1;
}
<div class="container">
  <div class="box box-1">
    <h4 class="title">
      Lorem Ipsum
    </h4>
    <div class="content">
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloremque iusto molestiae eos totam ea, ratione quis veritatis commodi,
      </p>
    </div>
  </div>

  <div class="box box-2">
    <h4 class="title">
      Lorem Ipsum
    </h4>
    <div class="content">
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloremque iusto molestiae eos totam ea, ratione quis veritatis commodi,
      </p>
    </div>
  </div>

  <div class="box box-3">
    <h4 class="title">
      Lorem Ipsum
    </h4>
    <div class="content">
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloremque iusto molestiae eos totam ea, ratione quis veritatis commodi,
      </p>
    </div>
  </div>
</div>

Answer №2

It's unclear what the intention is behind that action, as it could make the text on the upper cards hard to read. However, a possible solution could be implemented as shown below:

var startingPosition = 0;
var incrementBy = 20;

$('.card').each(function(i, obj) {
    $(this).css('top', startingPosition);
    startingPosition = startingPosition + incrementBy;
});
#container{
  position: relative;
}

.card{
  position:absolute;
  width:200px;
  height:100px;
}

.orange{
  background-color: orange;
}

.purple{
  background-color: purple;
}

.yellow{
  background-color: yellow;
}

.green{
  background-color: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
    <div class="card orange">A</div>
    <div class="card purple">B</div>
    <div class="card yellow">C</div>
    <div class="card green">D</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 JQuery script functions properly when directly embedded in an HTML file, but fails to work when linked from an external file

While I'm working on some code using JQuery's .hover method, I've encountered an interesting issue. It seems to work fine when written directly in the main HTML file index.html, but for some reason, it fails to execute when written in a Java ...

Issues encountered in loading JavaScript with jQuery script

I am facing an issue with my jQuery script not loading correctly. When I click on the calculate button, nothing happens as expected. I made sure to copy and paste jQuery directly into the file for offline use, and also created a personal console due to res ...

Creating mock implementations using jest in vue applications

I have been experimenting with testing whether a method is invoked in a Vue component when a specific button is clicked. Initially, I found success using the following method: it('should call the methodName when button is triggered', () => { ...

Having trouble getting my Node.js Express code to display 'Hello World' on Cloud 9 platform

Recently, I've been experimenting with Cloud 9 and have been encountering an issue while trying to execute the sample code provided on the Express website to display 'Hello World!'. I have attempted listening on various ports/IP addresses ba ...

Determine the output based on the data received from the ajax post request

I am seeking a way to validate my form based on the data returned. Currently, the validation only returns false if the entire post function is false. Is there a solution to differentiate how it is returned depending on which condition is met? This is my ...

The Bootstrap alert refuses to close when the close button is clicked

I'm attempting to utilize a Bootstrap alert for displaying a warning. The alert automatically fades and dismisses after a period of time, but I want to provide the user with the option to manually close it. I've included jQuery and js/bootstrap.m ...

Is it possible to apply styles to the body of a document using styled-components?

Is it possible to apply styles from styled-components to a parent element, such as the <body> tag, in a way that resembles the following: const PageWrapper = styled.div` body { font-size: 62.5%; } ` ...

Text box is not automatically filling up when selecting from dropdown menu

I am currently facing an issue with a dropdown box that offers three different selections. I want the Group ID associated with the selected group to automatically populate in the textbox below. How can I achieve this functionality? Whenever I make a selec ...

Obtain data from an external XML source using jQuery

Hey there! I'm looking to scrape/parse some information from an external XML file hosted on a different domain and display it on my website. I attempted the following but unfortunately, it didn't work as expected: jQuery(document).ready(functio ...

Sending a XML file from a Vue application to an ASP.NET Core backend using axios

I'm encountering difficulties when trying to upload an xml file using axios to my asp .net server. Below is the code snippet I am using on the vue side to retrieve and upload the xml file: uploadXmlFile(file: any) { const rawFile = new XMLHttpRequ ...

Tips for refreshing a GET request within a Vue.js application

I am receiving data from my Rails API backend and I would like to automatically refresh that GET request every 15 seconds. This way, if there are any changes on the backend (for example, if a POST request is made to another route), it will reload and retri ...

Adding a background color to the body of your PHP and HTML email template is a simple way to enhance

I am currently using a confirmation email template with a white background, but I want to change it to gray. However, I'm unsure how to update the PHP file containing the email function below: $headers = "From: " . stripslashes_deep( html_entity_deco ...

Determine whether the child element extends beyond the boundaries of the parent element

I am currently working on determining whether a child element is visible within its parent element. To achieve this, I am comparing the width of the parent element to the position of the child element using position().left. Given that I have multiple dist ...

How can I create a component that dynamically adjusts to different screen sizes

I have developed three child components for the main content area (excluding the header) However, one of the components is not responsive. Below is the code snippet: <div className="row" style={{marginTop:'30px',}}> <div class ...

Show one marker on the map from a GeoJson file

On my webpage, I have a Google map that displays all markers using the map.data.loadGeoJson method. Each marker is linked to its respective details page with the following code: map.data.addListener('click', function(event) { var id = even ...

Change the duration of a CSS animation rotation using jQuery and an input range control

I'm trying to rotate an element using the -webkit-animation properties, but I want to be able to control the duration using jQuery and an input range slider. Unfortunately, I'm having trouble getting it to work and I can't figure out what&a ...

Steps for creating a Java Script method within a Java Script constructor

Is having a method inside my constructor possible? This is how I call my constructor: new EnhancedTooltip($("taskPreview1")) and this is how it's defined: ///<var> Represents the controls</var> var EnhancedTooltip = function (toolTipObj ...

What could be the reason for the scope being empty in my AngularJS application?

I'm new to Angular and I'm currently customizing the tutorial for my app. However, I'm facing an issue with adding routes to my app. The templates are not being read correctly and the controller seems to have some issues as well. In order to ...

Using AngularJS to handle form data without ng-model

I am facing a unique situation that may seem a bit strange. I have a form that needs to edit/update information from different objects, such as services. My goal is to use ng-model to link input fields to their respective objects, but when the form is sub ...

What is the best way to make a block fill 100% of a container-fluid?

Here is a block: <div class="container-fluid"> <div class="content_wrapper"> </div> </div> What's the best way to display the .content_wrapper block with 100% content width and margins from the body borders? ...