Position the flex item at the bottom of the container

Looking for a solution to create a container that takes up the full window height and width? Inside this container, I want to have a form that is centered both vertically and horizontally. Additionally, I want some text at the bottom baseline of the container. I'm currently struggling to achieve this layout as the vertical centering works but I can't figure out how to pin the bottom text to the bottom of the container.

---------
|       |
|       |
|<form>  |
|       |
|<copy>  |
---------

.container {
  background-color: #eee;
  height: 100vh;
  width: 100vw;
  padding: 1em;
  display: flex;
  text-align: center;
  justify-content: center;
  flex-direction: column;
}

form {
  
}

.bot {
  align-self: flex-end;
}
<div class="container">
  <form>
    <input type="text" />
    <button type="submit">submit</button>
   </form>
  <p class="bot">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Omnis quae quisquam neque cupiditate adipisci magnam facilis, distinctio suscipit possimus hic voluptatibus in illo est id alias unde sapiente ab eius.</p>
</div>

Answer №1

Revised Response

Utilizing auto margins allows you to consolidate or arrange flex items without needing additional markup, or rearrange elements by applying a margin in the opposite direction (as illustrated in my initial response). To achieve this effect, simply apply margin: auto to the form; this will instruct the flex container to center the form within the remaining available space:

.flex-container {
  display: flex;
  flex-direction: column;
  text-align: center;
  height: 150px;
  width: 400px;
  background: #e7e7e7;
}
 
form {
  margin: auto;
}

p {
  margin: 0;
}
<div class="flex-container">
  <form>
    <input type="text" />
    <button type="submit">submit</button>
  </form>
  <p class="bot">
    Lorem ipsum dolor sit amet
  </p>
</div>

For further insights, refer to the following answers:

In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?

Can't scroll to top of flex item that is overflowing container


Initial Explanation

An effective technique to "pin" a flex child is by applying an auto margin in the opposite direction of its intended movement. In your scenario, set

p.bot {
    margin-top: auto;
}

and witness the paragraph shift to the bottom of the parent flex container. This strategy works seamlessly with uncomplicated layouts like this...

html,
body {
  margin: 0;
  padding: 0;
}
.container {
  background-color: #eee;
  height: 100vh;
  width: 100vw;
  display: flex;
  text-align: center;
  justify-content: center;
  flex-direction: column;
  position: relative;
}
form {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.bot {
  margin-top: auto;
}
<div class="container">
  <form>
    <input type="text" />
    <button type="submit">submit</button>
  </form>
  <p class="bot">Lorem ipsum dolor sit amet, consectetur adipisicing elit…</p>
</div>

Edit Additionally, I've transformed the form into a nested flexbox inside the .container and adjusted its height to 100%, essentially achieving the same result. Michael_B elaborates on this concept in the comments.

Answer №2

To create a pseudo-element with three sections, you can utilize the :before selector on the container and apply justify-content: space-between;

* {
    margin: 0;
}
.container {
    background-color: #eee;
    height: 100vh;
    width: 100vw;
    display: flex;
    justify-content: space-between;
    flex-direction: column;
    text-align: center;
}
.container:before {
    content: '';
}
<div class="container">
    <form><input type="text" /> <button type="submit">submit</button></form>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Omnis quae quisquam neque cupiditate adipisci magnam facilis, distinctio suscipit possimus hic voluptatibus in illo est id alias unde sapiente ab eius.</p>
</div>

Check out the example on jsfiddle

Answer №3

Let's address the issue at hand:

The problem arises from setting the flex-direction to column, which transitions the main axis to vertical and the cross axis to horizontal as outlined in this source.

It's essential to note that the align-* properties function along the cross axis, so specifying .bot with align-self: flex-end actually instructs it to shift right rather than down.

In cases where the item occupies the entire width of the container, like in this scenario, there is no leeway for movement. However, constraining its width could yield different results as seen in this example.

When aiming to affix <copy> to the bottom of the screen using flexbox, a limitation surfaces—there is no equivalent property akin to align-self for the main axis. Explore alternatives such as nesting flexboxes within the existing container or relocating .bot to another one.

Additionally, consider leveraging the capability of flexbox to accommodate absolutely-positioned flex children by employing the following approach:

.container {
    position: relative; /* establish nearest positioned ancestor for abs. positioning */
}

.bot {
    position: absolute;
    bottom: 0;
}

For a visual demonstration, refer to this DEMO.

Please bear in mind that applying margin-top: auto on .bot won't yield desired outcomes within the current HTML structure. While it may pin the target element to the bottom, it would also displace the centered element towards the top.

Answer №4

Using align-self: flex-end; does not produce the desired result when the flex children are aligned in columns.

An easy solution to achieve the desired alignment without adding any extra markup is:

  • Remove the footer element from the .container

    <div class="container"></div>
    <p class="bot"></p>
    
  • Add display: flex to the body (or another wrapper if needed) so that both the .container and footer become flex children (siblings).

  • Assign a flex property of flex: 1 1 100% to .container. This will make it grow and shrink based on its initial value of 100%. The footer will be pushed down, but its height will be accounted for when the container shrinks.

  • Give the footer a flex property of flex: 1 1 auto. This will push it down to the bottom while allowing it to increase in height with more content.

Learn more about the flex property on MDN.

Example

* {
  margin: 0;
  padding: 0;

}
body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
}
.container {
  display: flex;
  flex: 1 1 100%;
  flex-direction: column;
  justify-content: center;
  text-align: center;
  background: #EEE;
}
.bot {
  flex: 1 1 auto;
  text-align: center;
  background: #EEE;
  padding: 1em;
}
<div class="container">
  <form>
    <input type="text" />
    <button type="submit">submit</button>
  </form>
</div>
<p class="bot">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Omnis quae quisquam neque cupiditate adipisci magnam facilis, distinctio suscipit possimus hic voluptatibus in illo est id alias unde sapiente ab eius.</p>

Answer №5

To revert the layout to its original state, change the flex-direction property back to column. Then set one of the containers to have a width of 100%, and utilize flex-wrap: wrap to achieve this effect.

An example implementation that is compatible with Firefox and IE 10+ (with vendor prefixes) is shown below:

.container {
  background-color: #333;
  width: 100%;
  height: 100vh;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: center;
}

.footer {
  width: 100px;
  height: 20px;
  margin-top: -20px; /* Set the margin top to negative of the height for perfect centering */
  background-color: #900;
  color: #fff;
  text-align: center;
}

.form-wrapper {
  width: 100%; /* Should be 100% to force item wrapping */
  height: 50px;
  background-color: #009;

  /* Center the form inside the wrapper */
  display: flex;
  align-items: center;
  justify-content: center;
}
<div class="container">
  <div class="form-wrapper">
    <form>
      <input type="text">
      <button type="submit">Submit</button>
    </form>
  </div>
  <div class="footer">Footer Content</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

Create a container-fluid design in Bootstrap with various colors

I am aiming to create a design with a fluid container featuring different background colors on each side, separated by two columns within the container. I have visualized it in this image - do you think it is feasible? https://i.stack.imgur.com/KRc2Q.pn ...

Maintaining the active style of Asp.NET 4.0 Menu control even after the mouse is released

Utilizing a standard asp.net menu in an asp.net 4.0 web application, this setup is for a standard web application and not any version of MVC applications currently available. The issue at hand is relatively straightforward. CSS styles are applied to the d ...

A guide to placing tooltips dynamically in highcharts column charts

I am encountering an issue with tooltips in Highcharts column charts. The problem arises when the series fill up my chart, causing the tooltip to be hidden below the series and cut off by the end of the div. You can see an example here: https://i.stack.i ...

What is the best way to include a minified bootstrap file in a TypeScript project?

Instead of using react-bootstrap, I only want to add the minified CSS. I went ahead and copied the original CSS file into my project, then added the path in the index.html, but unfortunately, it's still not working. <head> <meta charset=&quo ...

Using Paper Checkbox to Toggle the Visibility of a Div in Polymer

Having experience with Meteor, I typically used JQuery to toggle the display of a div along with paper-checkbox: HTML: <paper-checkbox name="remoteLocation" id="remote-chk" checked>Remote Location</paper-checkbox> <div id="autoUpdate" clas ...

I am working with a JSON Object in JavaScript and need to retrieve a key using a String variable

Working with a JSON Object in JavaScript can be tricky, especially when trying to access keys stored as Strings using the dot operator. Consider this example of JSON code: "values" : [ { "prop0" : "h", "prop1" : "pizza", "prop2" : "2014- ...

What is the best way to adjust the width of the <span> element in an Angular application?

This snippet showcases a piece of HTML code. <div class="col-md-8"> <span class="label">Product Name</span> <span> <mat-form-field> <input matInput formControlName="productName"> </mat-form ...

Tips for implementing bslib package pop up window within a table displayed using rhandsontable in R Shiny

I am trying to implement the template from Laurent's answer on R Shiny - Popup window when hovering over icon in my code below, which involves integrating a popup window into a rhandsontable table. My goal is to display the popup window as depicted in ...

Retrieving the value of a nested element buried within multiple layers of other elements

There is a scenario I'm facing: <div class='situation1'> <div>..</div> <div> <span>...</span> <span>important details</span> </div> </div> Is there a w ...

jquery selector to target an element nested inside an anchor tag

Is there a way to use a Jquery selector to extract the text content within the 'name' attribute? <a href="page1.php" id='title' name="<?php echo $res['id'];?>" title="<?php echo $res['title'];?>" < ...

Communicate through PHP and JavaScript chat to display HTML content in the chat window

I have been attempting to display HTML output in the chat window but instead, it is showing the HTML code. Here are the two files involved in the chat system: chat.js `function chatHeartbeat(){ var itemsfound = 0; if (windowFocus == false) { var ...

Guide for adding an OnClick event to a MatTable row:

I am looking to add functionality for clicking on a specific row to view details of that user. For instance, when I click on the row for "user1", I want to be able to see all the information related to "user1". Here is the HTML code snippet: <table ma ...

tips on concealing video URL

Is there a way to obscure the URL of a video being displayed on a webpage using the HTML5 Video Player? The video URL is pasted as a <source> element, and I'm unsure how to obfuscate it without causing issues with the player. I'm looking f ...

Switching the cursor to an image when hovering over an element is causing inconsistency in hover events triggering

Currently, I am attempting to implement an effect that changes the cursor to an image when hovering over a text element and reverts back to the normal cursor upon leaving the text element. However, this functionality is not working as expected when using R ...

Implementing Dynamic Parent Node Manipulation with Button Clicks in JavaScript

I am currently working on dynamically creating an input field using the append child method along with add and delete buttons to form a tree-like structure. The goal is to be able to create child nodes with add and delete buttons upon clicking the add butt ...

Accessing a PDF document from a local directory through an HTML interface for offline viewing

As I work on developing an offline interface, I'm encountering an issue with displaying a PDF file when clicking on an image. Unfortunately, the code I have in place isn't working as intended. Can someone please assist me with this problem? Below ...

Issue with checkbox alignment in Twitter Bootstrap v3.x

Greetings fellow siblings. I am facing an issue with Twitter Bootstrap checkboxes, particularly when viewed in Opera, Chrome, and IE. Here is the problem: I have come across this snippet of code <div class="form-group"> ...

How can I embed an iframe in an Angular 4 application?

I'm facing an issue with my Angular 2 v4 application where I have the following code snippet: <iframe src="https://www.w3schools.com" style="width: 100%; height: 500px"></iframe> However, the iframe does not seem to work. I attempted to ...

No modifications to the CSS can be made within the "alterations" section of the Console drawer in Chrome

Is there a way to easily track and summarize all of my live CSS changes in the Chrome browser? I've searched on Stack Overflow, but haven't found a solution that works for me. This specific answer seems like it could be the most helpful for achie ...

Tips on retrieving and refreshing dynamically generated PHP file echo output within a div

I have a div that I'm refreshing using jQuery every 10 seconds. The content is being read from a PHP file named status.php. Here is the JavaScript and HTML for the div: <script> function autoRefresh_div() { $("#ReloadThis").load("status.php ...