Located at the lower section of the parent container

In my flex container, I have two boxes arranged side by side (collapsing into one column on smaller screens).

I've ensured that these boxes have the same height when displayed together, as shown in the image below: https://i.sstatic.net/ZjsfW.png

Now, my goal is to position the EDIT button at the bottom of both boxes. This means that only the EDIT button for the box with less content should need to be repositioned.

I've looked into using absolute positioning, but it's not ideal as one box won't require it. If I apply absolute positioning to both EDIT buttons, the right box will end up with less overall height due to not counting the button.

I've also attempted to manipulate the layout using flexbox, but I'd prefer not to set the boxes themselves as display:flex.

There might be a solution involving jQuery, although I've come across vague suggestions like "You can do that with jQuery!" without clear guidance on implementation.

#parent {
  flex-flow: row wrap;
  display: flex;
  align-items: stretch;
}

.box {
  margin: 10px;
  padding: 15px;
  flex: 0 1 calc(50% - 50px);
  
}

.box:last-of-type {
  background-color: green;
}
  
.box:first-of-type {
  background-color: red;
}

.cool-form {
  display: flex;
  flex-direction: column;
}

.button-container {
  display: block;
  margin-left: 5px;
  margin-top: auto;
  align-self: center;
}

.button {
  background-color: white;
  display: inline-block;
  width: 120px;
  height: 48px;
  line-height: 48px;
  border: 1px solid black;
  text-align: center;
  cursor: pointer;
}
<div id="parent">
  <div class="box">
    <form class="cool-form">
      <h1>Box on left</h1>
      <p>This is a shorter box</p>

      <span class="button-container">
        <span class="button">EDIT</span>      
      </span>
    </form>
  </div>
  
  <div class="box">
    <form class="cool-form">
      <h1>Box on right</h1>
      <p>This is</p>
      <p>a</p>
      <p>bigger</p>
      <p>waaay bigger</p>
      <p>Box</p>

      <span class="button-container">
        <span class="button">EDIT</span>
      </span>
    </form>
  </div>
</div>

Answer №1

Utilizing the flexbox property on these divs (specifically with flex-direction:column) appears to be a subtle adjustment (which was possibly a concern).

.box {
  display: flex;
  flex-direction: column;
}

By applying margin-top:auto to the button container, it automatically shifts to the bottom.

From there, aligning it horizontally/centrally is all that's left to do.

.button-container {
  margin-top: auto;
  align-self: center;
}

#parent {
  flex-flow: row wrap;
  display: flex;
  align-items: stretch;
}

.box {
  margin: 10px;
  padding: 15px;
  flex: 0 1 calc(50% - 50px);
  display: flex;
  flex-direction: column;
}

.box:last-of-type {
  background-color: green;
}

.box:first-of-type {
  background-color: red;
}

.button-container {
  display: block;
  margin-left: 5px;
  margin-top: auto;
  align-self: center;
}

.button {
  background-color: white;
  display: inline-block;
  width: 120px;
  height: 48px;
  line-height: 48px;
  border: 1px solid black;
  text-align: center;
  cursor: pointer;
}
<div id="parent">
  <div class="box">
    <h1>Box on left</h1>
    <p>This is a shorter box</p>

    <span class="button-container">
      <span class="button">EDIT</span>
    </span>
  </div>

  <div class="box">
    <h1>Box on right</h1>
    <p>This is</p>
    <p>a</p>
    <p>bigger</p>
    <p>waaay bigger</p>
    <p>Box</p>

    <span class="button-container">
      <span class="button">EDIT</span>
    </span>
  </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

jQuery efficiently handles large amounts of text data transfer (gradual loading)

The issue at hand: My website relies on jQuery AJAX (post) for navigation, meaning the page doesn't refresh every time a user moves to a different section. This setup requires me to fetch data using AJAX and present it dynamically. While this works w ...

Use CSS3 to hide the <li> elements initially and make them appear when the <li> is clicked on

Click here How can I hide the "subline" line from the list and make it visible when the user clicks on the "sub" line, pushing the other lines down? I'm hoping for a solution that doesn't involve using Js or JQuery. Thank you in advance! ...

Exploring arrays with JavaScript and reading objects within them

Recently, I received assistance from a member of StackOverflow regarding AJAX calls. However, I have encountered a problem now. The code consists of a loop that sends requests to multiple REST APIs and saves the results in an array as objects. The issue ...

When Highcharts and AngularJS team up, beware of the memory leak!

I've integrated highcharts into my AngularJS application using the highcharts-ng directive, but I'm encountering a persistent memory leak issue. My app is a slideshow with rotating slides that include charts. To investigate further, I created 3 ...

Tips for adjusting an image to fit your carousel

I am dealing with a carousel that has fixed dimensions (e.g., width=800px, height=450px), but the images I want to display in it have varying aspect ratios (16 : 9, 16:10, 1:1, etc.) and are larger than the carousel itself. I am attempting to resize all th ...

Executing Java output to Node.js console using child_process execSync without buffering

Having an issue with the execSync call below: const { execSync } = require('child_process'); ... console.log('Checking Java version') let java_version= execSync('java -version').toString(); console.log('Java version:&apo ...

I am facing issues connecting my Express Node server to my MongoDB database using Mongoose

Starting my backend journey, I keep encountering the same error with my server.js --> // Step 1 - Create a folder named backend // Step 2 - Run npm init -y // Step 3 - Open in VS Code // Step 4 - Install express using npm i express // Step 5 - Create se ...

Can an iFrame be programmed to automatically scroll?

Here's an example of autoscroll: I am looking to implement autoscroll for the content within an iframe that has a width wider than the browser window. Can this be achieved using jQuery? ...

The website does not support the zoom out or scrolling features on iOS devices

A portion of our website has been optimized for mobile devices, however, we have decided to direct iPhone users to the desktop version when accessing the Blogs section (all PHP content) at this time. While this functions properly on an iPad, we have encou ...

Error: Unable to access the 'location' property because it is undefined

Having trouble uploading a product along with an image using AWS S3 (Multer and MulterS3). Every time I try it in Postman, I get the error message "TypeError: Cannot read property 'location' of undefined" specifically pointing to the line where t ...

What could be the reason for my CORS headers not aligning even though they appear to be identical?

I encountered an issue while using passport js with REACT. When trying to fetch the logged in user data, I faced a problem where the cors header of fetch didn't match even though they are identical. Here is the endpoint I am sending the fetch request ...

Changing route patterns in NextJS

When deploying a new NextJS app, it is important to preserve legacy routes from the old non-NextJS site. The current setup uses /const-string-[long-unique-hash] for an httpd conf redirect: For example: RewriteRule ^const-string-(.*)$ https://google.com?q ...

What steps can be taken to ensure that any new elements generated by JavaScript do not disappear upon refreshing the page

I am currently working on a project where I am creating a list by collecting user information and storing it in a MySQL database. When the user clicks the 'add' button, a new element is added to the bottom of the existing list which is coded in H ...

Creating IPv6 Mask from IPv6 Prefix Using Javascript: A Step-by-Step Guide

Write a JavaScript/TypeScript function that can convert IPv6 prefixes (ranging from 0 to 128) into the corresponding mask format (using the ffff:ffff style). Here are some examples: 33 => 'ffff:ffff:8000:0000:0000:0000:0000:0000' 128 => ...

The CSS rule for @media screen is failing to function properly on mobile devices

Is there a way to hide a specific section of my home page only on smartphones? I've tried using the code below but it's still showing up on my phone. Any advice? @media screen and (max-width: 640px) { #statistics .row { visi ...

What methods can be utilized to enhance the visual appeal of a threejs model

I'm currently working on enhancing the visual quality of my 3D models, aiming for smoother edges and more realistic shadows. To achieve this, I am experimenting with an OBJ file format. Here's the code snippet I'm using to load and display t ...

Refreshing data in a Bootstrap modal

I am having trouble passing data from a Bootstrap modal to the ul element. Although I can add multiple li elements, only the first one is displayed and it does not change. Can anyone offer assistance with this issue? <ul id="myList"> </ul> ...

What steps can I take to avoid keypress events causing issues with the browser's input functionality?

Utilizing Bootstrap's modal component, I have implemented an "Add User" dialog within my web application. In order to streamline the user experience and enable quick data entry, I am aiming for the escape and enter keys to close and submit the form re ...

At what stage of the Angular JS application life cycle is this code being loaded?

I am facing a difficult issue with the timing of Angular JS life cycle in relation to the code snippet below. There seems to be a random occurrence where controllers dependent on this code are loaded before it, leading to errors. Even after multiple atte ...

Transform the date into the specified ISO format of YYYY-MM-DDThh:mm:ss.sTZD

I am looking to transform a date into the ISO format YYYY-MM-DDThh:mm:ss.sTZD using JavaScript. Currently, I am able to convert the current date string into the format yyyy-MM-dd'T'HH:mm:ss.SSSZ. For example: 2016-01-11T02:40:33.117Z. However, I ...