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:

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

GraphQL failing to communicate with WP API

Any help is appreciated! I am currently retrieving data from a WP Rest API. When I run the WordPress site locally on my machine using http://localhost:8000 and access the graphql playground at http://localhost:3000/api/graphql, everything works fine as ex ...

Track the loading time of a webpage and the time it takes to render all subelements of

For my project, I need to track page load times for each individual visitor. My idea is to embed a JavaScript snippet into the page in order to achieve this goal. However, the task is more complex than I anticipated because I also need to measure the respo ...

Guide on transforming the best.pt model of YOLOv8s into JavaScript

After successfully training a custom dataset on YOLOv8s model using Google Colab, I now have the best.pt file that I want to integrate into a web app via JavaScript. I've come across mentions of TensorFlow.js as a potential solution, but I'm stil ...

Ajax doesn't display the database table when a radio button is selected

As a beginner in web programming, I encountered an issue while using ajax and jQuery to retrieve MySQL data based on the value selected from a radio button upon submission. In this case, I am utilizing PDO for the query process. Below is my code snippet: 1 ...

Empty results received from MongoDB queries executed within an asynchronous loop

I'm encountering an issue where I have a list of IDs that I need to query Mongo with in a for loop, but it always returns an empty []. Initially, the queries were returning promises, so I switched from using forEach to a standard for loop as shown her ...

Safari is encountering an issue with the value provided for the width/height attribute in the <svg> element, as it is not a recognized

When adjusting the size of an SVG based on antd breakpoints, I encountered errors like these. I am passing props to an SVG element: const { lg } = useBreakpoint(); const height= lg ? "8rem" : xs ? "3rem" : "5rem"; const width ...

Change the className of an element in React upon clicking it

I just finished developing a menu bar using ReactJS with some basic routing features. Here is the JSX code for the component: class TopBar extends Component { state = { menus: [{id:0,menu:"Home"}, {id:1,menu:"Contact"}, {id:2,menu:"About"}] } a ...

Authorization missing in Select2 Ajax request

Encountering an issue while attempting a get request to a secure endpoint that requires an Auth token. Despite fetching the token asynchronously from chrome.storage, it fails to be included in the ajax request and results in a 401 error ("Authorization hea ...

Svelte components loaded with no design aspects applied

I encountered an issue while trying to integrate the "Materialify" and "Carbon Components for Svelte" libraries into my Sapper project. The components seem to be loading, but without any associated styles. I followed the installation commands provided on t ...

Creating a quiz with just one question in React: A step-by-step guide

How can I add the functionality to handle correct and incorrect answers? I've designed a single-question quiz with multiple options. The Quiz component has been created. See the code snippet below: Quiz Component export default function Quiz(props) { ...

JavaScript JSON YouTube API viewCount function is not functioning correctly

I'm struggling to retrieve the views of a video using JavaScript (Chrome Extension). Here is the code I have so far: $(function() { $.getJSON('http://gdata.youtube.com/feeds/api/videos/H542nLTTbu0?alt=json',function(data) { ...

Steering clear of confusing mustache syntax in Jinja2 and jQuery template integration

I'm currently facing a challenge of integrating jQuery templates into Jinja2 templates. The issue arises because both frameworks, in their default setups, utilize the curly braces {{ and }} to denote expressions and literals, respectively. When I try ...

Angular version 6 allows for specifying input types as numbers and displaying two decimal digits after the comma

How can I format user input to display as currency with thousand separators in Angular? <input type="number" (ngModelChange)="calculateSum()" (change)="calculateAmount(invoiceQuota)" [ngModel]="invoiceQuota.controls.grossAmount.value"> I have attem ...

"Can anyone explain why the text color of my font changes during a transition within a

Check it out on jsfiddle as well: https://jsfiddle.net/phmttrsh/ #btn { font-size: 16px; height: 34px; width: 120px; border: 1px solid #20ACB3; text-align: center; line-height: 34px; background-color: #20ACB3; color: #fff ...

Attempting to achieve a carousel animation using jQuery

Upon loading the page, only one character out of four is displayed. Two arrows are provided - one on the left and one on the right. Clicking on the left arrow causes the current character to fade out and the previous character to fade in. Clicking on the r ...

Recording videos using the Safari Browser

Within my ReactJs application, I have integrated react-multimedia-capture, a package that utilizes navigator.mediaDevices.getUserMedia and the MediaRecorder API to facilitate video recording. While I am successfully able to record videos on Chrome, Safari ...

Retrieve the specific data from the database when the <tr> element is hovered over

Hey everyone, I've been struggling with a problem for some time now. I have a loop that retrieves values from a database and I want each value to display onmouseover using JavaScript. However, it's only showing the value of the first row for all ...

Display the webpage exclusively when the application has been set with `app.use('/api')` in Express 4

Here is what I currently have: app.js ... var api = require('./routes/api'); app.use('/', api); app.use('/api', api); ./routes/api ... var router = express.Router(); router.get('/', passport.authenticate(' ...

The Discord.js guildMemberUpdate event: Everything you need to know

I am currently working on creating a welcome message bot using Discord.js. The goal is to have the bot send a welcome message through a webhook in a specific channel when a member gains access to it. Here's what I have so far: client.on("guildMe ...

What is preventing my boolean from being altered?

Creating a basic calculator that can handle single-digit arithmetic. The current code is incomplete and redundant, so please avoid commenting on that. <!doctype html> <html> <head> <title>JavaScript Learning Zone</title> ...