Place one flex item in the center and align another item to the bottom

I am attempting to center one flex item both vertically and horizontally.

At the same time, I want some text to remain fixed at the bottom of the flex container.

When I use margin-top:auto on the text, it just moves the inner box to the top. Any suggestions?

.container {
  background: lightblue;
  width: 300px;
  height: 300px;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  padding: 10px;
}
.container .box {
  background: goldenrod;
  height: 30px;
  width: 30px;
}
<div class="container">
  <div class="box"></div>
  <span>Text</span>
</div>

You can find the codepen link here.

Answer №1

Consider using the following code snippet:

.container  {
    background-color: goldenrod;
    height: 40px;
    width: 40px;
    margin-left: auto;
    margin-right: auto;
}

Answer №2

When it comes to flexbox alignment and distributing free space within a container, using margin-top: auto may not be effective due to the lack of a counterweight on the opposite side.

To center the box and align the text at the bottom, one approach is to create a duplicate of the text element and position it on the other side of the box to establish a counterweight.

By achieving balanced distribution on both ends through this method, flex alignment properties such as auto margins can then function properly.

In fact, even utilizing justify-content: space-between would be a suitable option in this scenario.

Remember to set visibility: hidden for the duplicate element.

.container {
  background: lightblue;
  width: 300px;
  height: 300px;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  padding: 10px;
}
.box {
  background: goldenrod;
  height: 30px;
  width: 30px;
  margin: auto 0;   /* Alternatively, use justify-content: space-between on .container */
}
span:first-child {
  visibility: hidden;
}
<div class="container">
  <span>Text</span>
  <div class="box"></div>
  <span>Text</span>
</div>

Alternatively, utilize a pseudo-element instead of a duplicate element.

A more subtle and semantic approach involves using a pseudo-element as a duplicate. However, precise knowledge of the actual element's height is required for this technique to succeed.

Here is an example to achieve equilibrium:

.container::before {
    content: "";
    height: 15px; /* Height must match the actual element's height */
}

.container {
  background: lightblue;
  width: 300px;
  height: 300px;
  display: flex;
  justify-content: space-between;
  flex-direction: column;
  align-items: center;
  padding: 10px;
}
.box {
  background: goldenrod;
  height: 30px;
  width: 30px;
}
span {
  height: 15px;
}
.container::before {
  content: "";
  height: 15px;
}
<div class="container">
  <div class="box"></div>
  <span>Text</span>
</div>

Answer №3

If you're looking for a solution, here's one approach to consider.

Firstly, include position: relative in your CSS rule for the .container class. Then, utilize absolute positioning on the .box class to place the span element at the bottom of its parent container.

To center the text horizontally, ensure that the .box class has 100% width and apply text-align: center.

.container {
  background: lightblue;
  width: 300px;
  height: 300px;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  padding: 10px;
  position: relative;
}
.box {
  background: goldenrod;
  height: 30px;
  width: 30px;
}
span {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  text-align: center;
}
<div class="container">
  <div class="box"></div>
  <span>Text</span>
</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

How can you efficiently showcase multiple fetch functions on a single page by utilizing loops in PHP?

I am attempting to showcase the countries from each continent on a single page using buttons for each continent. When a user clicks on a button, I want the countries of the selected continent to be displayed. However, I am encountering an issue where only ...

How to vertically align Bootstrap5 buttons on mobile devices

I'm currently developing a web application and I'm facing an issue with centering two bootstrap buttons next to each other on mobile devices. While it works fine on the computer, when I inspect the page on a mobile device, the buttons stack verti ...

Can the max-height CSS media-query be utilized safely?

It seems that using the max-height media query is proving to be quite a challenge due to an issue with Chrome 67 on iOS. The problem arises when users scroll on Chrome as it constantly adds and removes the address bar. This action causes the max-height va ...

Submitting data and files simultaneously to Ajax using Laravel

As I work on enhancing my registration page with Bootstrap Modal, I need to allow users to upload their image along with other information. HTML <form method="POST"> <center> <img name="user_image" id="proPic" src="ima ...

Customize the Header Background Color in React Table

I'm working on customizing a re-useable table component using vanilla CSS. I have defined six color variables and want users to be able to select one of them to change the table header background color. However, I am unsure how to make this color choi ...

Problem with loading images on an HTML webpage

After refreshing the page, I'm encountering an issue where the image is not loading. However, if I switch tabs from MEN to WOMEN and back again, the image will display without any problem. Even making changes in the CSS doesn't seem to affect thi ...

Tips for adjusting an @media css for a material-ui react component

After posting this inquiry about overriding a CSS property in a material-ui component, I received a helpful example. However, I encountered difficulty when attempting to set the height of the Toolbar component due to an overarching @media specification. My ...

Revamping the design of a menu bar in WordPress

I designed a navigation bar and now I'm looking to customize the CSS for it in WordPress. Specifically, I want to be able to reference the nav bars by name from PHP to CSS. It seems like there are just two dots representing variables. Here is the co ...

The property 'scrollHeight' is undefined and cannot be read

I've created a messaging system using javascript and php, but I'm facing an issue. When new messages are received or the chat log grows longer, it creates a scrollbar. The problem is that when a new message arrives, the user has to manually scrol ...

Guide to creating a hierarchical navigation system with HTML

Is there a way to create a menu tree using just HTML and some scripting, without downloading additional software? I tried searching on Google but all the results require downloads. Can anyone provide guidance or help with this task? Thank you in advance. ...

Transform the initial HTML table row into a header row for every table by utilizing XSLT

My XML file contains HTML content that I need to manipulate. Previously, I used <xsl:copy-of select="customFields/customField[@name='mainContent']/html"/> to bring the content to the correct location. Now, I have a new requirement to conver ...

Dealing with CSS specificity issues when incorporating material-ui for React into a WordPress plugin

Struggling to integrate material-ui for react in a wordpress plugin due to conflict with wordpress's styling in forms.css file. Looking for a solution that doesn't require complete restyling of material-ui components to override the default style ...

Steps for inserting an array of strings in PHP into HTML input fields

I need help with extracting elements from a php array and displaying them as a list of text input fields on a webpage. The issue I'm facing is that the elements in the $list are strings, and when I run the code, only the portion of the string up to th ...

Maintain font kerning for span elements that are not displayed inline

I am looking to add some transform effects to individual letters in a word. To achieve this, I have enclosed each letter within a span element. However, when setting the display of these spans to inline-block, the font kerning gets disrupted. I have exper ...

What could be causing my CSS parallax effect to not work as expected?

I have been attempting to implement the parallax image technique following the Keith Clark tutorial, but I am facing difficulties in getting it to work correctly. Utilizing the skeleton CSS framework, my goal is to recreate an existing website in order to ...

"Comparison: Utilizing HTML5 Video Player with IE8 Embed Fallback versus Implementing Fixed

This dilemma has me at my wit's end. In an HTML5 Video element, I've included an mp4, ogg, and an embedded mp4 fallback. However, on the embed fallback in IE8, all my attempts to position the fixed element (#fixed) above it with z-indexing have f ...

The form submission did not yield any results

function calculateCost() { var projectorCost=0,price=0,quantity=0,discountPrice=0,totalCost=0; var modelName=document.getElementById('projectormodel').value; var quantity=document.getElementById('q ...

What is the method for displaying Facebook comments alongside the WordPress comments count?

I am currently using a template theme that initially had WordPress comments enabled on the website. By implementing the Facebook Comments plugin, I have successfully replaced Wordpress comments with Facebook Comments. **However, there seems to be an issue ...

Create a layered panel underneath a button that covers a separate element

I am working on a layout that consists of an editor and multiple buttons positioned above it on the right side. My goal is to add a panel just below "Button2" that will overlay the editor. This panel should expand and collapse when "Button2" is clicked, wh ...

The relationship between formatting context and atomic inline-level boxes is crucial for understanding

Check out this link for more information on inline boxes An inline box is a unique element that falls under the category of both inline-level and participates in its containing inline formatting context. When a non-replaced element has a 'display&ap ...