What is the best way to center the content vertically within flex items?

I am working with a flexbox header that has two child flex items, each with their own children.

Is there a way to change the vertical alignment of the flex items so that their children are aligned at the bottom?

div.flexbox {
  display: flex;
  align-items: stretch; /* cross axis */
  justify-content: space-between; /* main axis */
  flex-flow: row wrap;
  border-bottom: 1px solid #ddd;
  min-height: 100px;
}

.item-1, .item-2 {
  padding: 10px;
}

.item-1 {
  width: 30%;
  position: relative;
}

.item-2 {
  width: 60%;
}
<div class="flexbox">
  <div class="item-1">
    <span>I need to be aligned at the bottom of item-1</span>
  </div>

  <div class="item-2">
    <span>I should be aligned at the bottom of item-2</span>
  </div>
</div>

Answer №1

To align items in a flex container using webkit-align-items, you can:

-webkit-align-items:flex-end;  

Check out this example for more details: http://jsfiddle.net/5N2km/1/

Answer №2

To change the alignment of the flex items within a container, you can modify the value of align-items from stretch to flex-end. This adjustment may meet your requirements.

div.flexbox {
  display: flex;
  align-items: flex-end;  /* updated */
  justify-content: space-between;
  flex-flow: row wrap;
  border: 1px solid black;
  min-height: 100px;
  padding: 5px;
}

.item-1, .item-2 {
  padding: 10px;
  border: 1px dashed red;
}

.item-1 {
  width: 30%;
  position: relative;
}

.item-2 {
  width: 60%;
}
<div class="flexbox">
  <div class="item-1">
    <span>I want to be aligned at the bottom of item-1</span>
  </div>

  <div class="item-2">
    <span>I want to be aligned at the bottom of item-2</span>
  </div>
</div>

If your primary question is:

Is there a method to adjust the vertical alignment of the flex items so that their children are aligned at the bottom?

... then converting the flex items into nested flex containers and using flex properties to vertically align the content could be the solution:

div.flexbox {
  display: flex;
  align-items: stretch;
  justify-content: space-between;
  flex-flow: row wrap;
  border: 1px solid black;
  min-height: 100px;
  padding: 5px;
}

.item-1, .item-2 {
  display: flex;          /* new addition */
  align-items: flex-end;  /* new addition */
  padding: 10px;
  border: 1px dashed red;
}

.item-1 {
  width: 30%;
  position: relative;
}

.item-2 {
  width: 60%;
}
<div class="flexbox">
  <div class="item-1">
    <span>I want to be aligned at the bottom of item-1</span>
  </div>

  <div class="item-2">
    <span>I want to be aligned at the bottom of item-2</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

Is there a way for AJAX to dynamically insert new rows into a table?

I am seeking assistance in dynamically adding rows to an HTML table using AJAX whenever additional records are retrieved from a database query. My workflow involves utilizing Python Flask and Pandas to generate a dataframe containing information about node ...

Hidden footer error has been resolved despite background visibility

Greetings! This marks my debut post on this platform. I've encountered a minor setback with the current website project I am working on. As I have implemented a CSS parallax header, I aimed to create a distinctive footer as well. The concept was to ...

Leveraging the power of CSS Target to emphasize the parent container

I utilize Ckeditor on my website for adding articles. These articles are typically sourced from Word documents and contain footnotes. In the latest Ckeditor build (7125), I've managed to link each article to its respective footnote using automatic anc ...

The public folder in Node.js is known for its tendency to encounter errors

I'm facing an issue with displaying an icon on my website. Here is the current setup in my code: app.js const http = require('http'); const fs = require('fs'); const express = require('express') const path = require(&apo ...

Tips for concealing an entire menu on WP, with the exception of items labeled as current-menu-ancestor

On my Wordpress site, I am faced with a simple problem to solve. I have a primary menu at the top and a secondary menu on the left side. While the top menu only contains level 1 items, the left menu has all the items. To customize the left menu, I am utili ...

Tips on deobfuscating Next.js HTML from online sources

I am faced with the task of reconstructing a website that I scraped from the internet using wget. It seems to be built on next js, based on the presence of the _next folder. Even though I have no experience with nextjs and do not understand its inner worki ...

Hover animation using CSS is a great way to add interactivity

I'm trying to create an animation in a moving box that flips upside down when the mouse is raised. I've attempted to use keyframes and hover to achieve this effect, but it's not working as expected. Any suggestions on how to make it work? ...

Having trouble displaying the Order List in Tailwind CSS and Next.js?

I am having trouble displaying an ordered list on my page. I have tried using the <ol> tag, but for some reason the numbers are not showing up. Even when I switch to the <ul> tag, it's still not working properly. Take a look at this scree ...

Is the target="_blank" attribute malfunctioning in Google Chrome?

Whenever I click on this text, it is supposed to display a base64 source image. However, this feature does not seem to be working in Chrome. The tag opens a new tab, but the href attribute is not present in the URL. Below is my HTML code: <div> & ...

When you start scrolling down, the JavaScript menu will cleverly disappear, only to re

I am facing an issue with my menu, which is a normal block-displayed div. There is another div with annotation above it. I want the menu to stick to the top as fixed when scrolling down, but immediately hide itself. The goal is for the menu to appear when ...

The impact of breakpoints in Bootstrap

As a newcomer to Bootstrap, I have a question about the code snippet below: <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/> <div class="container"> <div class="row bor ...

Div automatically shifts to center position when other div elements vanish from view

I am new to jquery and currently working on a basic website. The site consists of three boxes, each of which, when clicked, causes the other two to fade out using the .fadeOut('slow'); method. However, there is an issue - all of the boxes are enc ...

What is the best method for deleting an uploaded image from a box?

My code is quite lengthy, so I'll just showcase the JavaScript portion here. Here is a snippet of my JavaScript code: <script type="text/javascript"> let current = 0; for(let i = 0; i < 5; i++) { $('#thumbnail-view- ...

insert a new DOM element into an array using jQuery

Looking at the code snippet below: a_ajouter = $('.question'); hidden_div.push(a_ajouter); console.log(hidden_div); Upon examining the output in the console, instead of a DOM object being added to the div as intended, it shows &apo ...

What steps do I need to take in order to develop a custom interactive analyzer game using html/css

Hello there, I am on a mission to develop a mobile-friendly version of an analyzer game that I came across on this link - . The current version of the game is built using flash. My goal is to be able to easily customize the colors and images to fit differ ...

Is there a way to compel the browser to re-cache an image?

In my website, each user has a cover image on their profile page with the constant name main_cover.jpg. The issue I am facing is that when a user uploads a new image, the old image remains cached in the browser and I have to refresh the page (ctrl+f5) to ...

Incorporate seamless integration by using the npm install command

I am currently facing an issue where I need to identify and remove unused dependencies from my package.json file every time I run npm install for my app. Is there a method to automatically include the npm package https://www.npmjs.com/package during the n ...

Align both vertically and horizontally in the center of the body

I have two images aligned vertically inside a wrapper. How do I center the wrapper within the body? What is the best way to center my wrapper inside the body? <!DOCTYPE html> <html lang="en-US"> <head> <meta charset="utf-8"> ...

CSS cascading not happening

Within the usersettings.css.erb file, line numbers are provided for easier reference. 11 #userSettingMain .form-horizontal .controls { 12 13 margin-left: 30px; 14 } 15 16 #user_birthday_3i{ 17 18 margin-left: 0px; 19 } U ...

Is there a versatile spell-checking tool available for HTML text boxes?

When designing a text box in HTML, I want to provide real-time input validation and spell check for the user's text. My goal is to underline any spelling mistakes as they type. This seems like a basic feature, but I've yet to find a plugin or AP ...