Center a flexbox vertically

Within a flexbox named myflex, there are three buttons: button 1, button 2, and button 3.

Is there a way to position this flexbox at the bottom of the screen while also centering it horizontally?

I've been able to center it, but struggling to make it a sticky footer located at the bottom of the page.

#myflex {
  display: flex;
  //position:fixed;
  bottom: 20px;
  justify-content: center;
  bottom: 10px;
}
<div id="myflex">
  <button class="button button1">button 1</button>
  <button class="button button2">button 2</button>
  <button class="button button3">button 33</button>
</div>

Check out the implementation here

Answer №1

When trying to move an element vertically, it is essential for the container to have additional height.

Unlike width, where block elements automatically fill 100%, heights need to be explicitly defined. Otherwise, elements will default to auto, taking on the height of their content.

If your code lacks a specified height, your flex container won't have the necessary space for alignment. To visualize this, add borders around the body element and your container:

body {
  border: 2px dashed red;
  padding: 1px;
}

#myflex {
  display: flex;
  bottom: 20px;
  justify-content: center;
  border: 1px solid red;
}
<div id="myflex">
  <button class="button button1">button 1</button>
  <button class="button button2">button 2</button>
  <button class="button button3">button 33</button>
</div>

Once you address the height issue, you can utilize flex keyword properties or auto margins to center-align the flex container at the bottom.

Method 1: Flex Keyword Properties

body {
  height: 100vh;
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: flex-end;
}
<div id="myflex">
  <button class="button button1">button 1</button>
  <button class="button button2">button 2</button>
  <button class="button button3">button 33</button>
</div>

Method 2: Flex Auto Margins

body {
  height: 100vh;
  margin: 0;
  display: flex;
  justify-content: center;
}
#myflex {
  margin-top: auto;
}
<div id="myflex">
  <button class="button button1">button 1</button>
  <button class="button button2">button 2</button>
  <button class="button button3">button 33</button>
</div>

If you prefer to keep the footer fixed on the screen, you can try this method:

Method 3: Fixed Positioning

body {
  height: 100vh;
  margin: 0;
}
#myflex {
  position: fixed;
  display: flex;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
}
<div id="myflex">
  <button class="button button1">button 1</button>
  <button class="button button2">button 2</button>
  <button class="button button3">button 33</button>
</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

Have you considered showcasing all images in a carousel format?

I have a total of 6 picture blocks, but only the first 3 images are displayed in the carousel. I suspect there may be an issue with jQuery, but I'm unsure where to look. Any hints or guidance would be greatly appreciated. Thank you! Below are the blo ...

Instructions on how to prompt users to register using a distinctive code

Currently in the process of constructing a website and I'm interested in setting restrictions on the number of users who can sign up for it. https://i.stack.imgur.com/JD2Ct.png In the image provided, there is a database in phpMyAdmin labeled as &apo ...

The swap feature in drag-and-drop does not have a defined function

I am currently developing a to-do app that utilizes drag and drop functionality to reorder items in the list. However, I have encountered an issue where swapping elements works perfectly for the first five elements but throws errors when dealing with the s ...

Create a feature that allows users to dynamically add and remove image fields, with the ability to insert the selected images into a database

I'm currently using the following code and I am able to add attachments, but I'm facing an issue when trying to remove them. Can someone please help me with this problem? I tried adding an alert on the remove button and it seems to be working, h ...

Full background cover

I have encountered a dilemma while constructing a website. The top image on the site does not expand to fill 100% of the screen size on mobile devices, leaving a blank bar visible. Despite my extensive research and troubleshooting efforts, I have been unab ...

Protected Bootstrap Environment

Bootstrap is an amazing tool, but it tends to enforce too many opinions. The selectors used in its rules are quite broad, such as input or label. Is there a method to isolate Bootstrap's CSS so that it only impacts elements within a container with a ...

jQuery.addClass function not functioning correctly

I am encountering an issue where the functionality in this code snippet isn't quite working as expected. Specifically, I would like the 'huh' div to become opaque when the menu is hovered over. While attempting to achieve this with fadein/ou ...

Unexpected behavior from Internet Explorer - Span contents remain unchanged despite valid input

I have a simple question because I'm feeling a bit lost. Check out this JSFiddle link It seems that in Internet Explorer, the contents of my span won't update even though the input is valid. However, in other browsers, the span content changes ...

Unable to Embed Tweets resulting in 403 Error (Forbidden)

While working on embedding some Tweets on my webpage, I encountered the following error message: GET https://cdn.api.twitter.com/1/friendships/exists.json?user_a=19463349&screen_name_b=mrland_news&callback=twttr.setFriendshipExists 403 (Forbidden) ...

visibility:hidden causing issue with hiding empty option in IE dropdown

I am currently working on an Angular 11 application. On one of the pages, there are 2 radio buttons and a dropdown. The desired behavior is that whenever the radio button selection is changed, the dropdown value should be reset to empty. The code provided ...

When the window width increases, the image within a flexbox causes the flexbox to break

I'm currently working on designing a responsive splash page that covers most of the page (90vh). The layout includes a logo at the top and a simple paragraph at the bottom. I've experimented with using flex-shrink and flex-basis, but it's n ...

Troubleshooting Alignment Problem of Maximize and Close Icons in RadWindow Using ASP.Net

Currently, I am utilizing telerik radwindow to showcase a PDF document. The window seems to be functioning correctly, but there is an issue with the alignment of the maximize and close icons. Ideally, they should appear in the same row, however, they are b ...

Angular Material Popup - Interactive Map from AGM

I am in the process of developing a material dialog to collect user location information using AGM (angular google maps). I have successfully implemented a map on my main page, but when the dialog is opened, it only shows a blank space instead of the map. ...

Chrome Canary's behavior with CSS border-radius on tiny objects

Is it possible to achieve CSS border-radius on really small objects in Chrome Canary? This experiment with a 3px high line and a 2px border radius, when zoomed in at 600%, doesn't show much difference: It works perfectly fine in regular Google Chrom ...

What is the best way to predefine a value for a checkbox in Angular?

Here is the code snippet I am currently using: <input type="checkbox" [(ngModel)]="i.checkt" [ngModelOptions]= {standalone:true} (change)="recovery(i.checkt,i.TherapeuticArea)"> {{i.TherapeuticArea}} I have encountered an issue where setting stan ...

Place the first paragraph within a div above another paragraph in the same div

Presently, I have this item: https://i.stack.imgur.com/0dBd9.png however, my objective is to achieve this look: https://i.stack.imgur.com/NEBZf.png The code snippet in question is as follows: .bonus-div { width: 290px; height: 29px; ma ...

Automatically focus on input when scrolling reaches a specific element using jQuery

I have been utilizing a jQuery plugin called Waypoints to handle scroll actions. My goal is to focus on the first input element of a section that is currently visible on the screen and then shift the focus to the input of the next respective sections as t ...

Dealing with fluctuating content heights: strategies to handle varying content lengths

Over at this website, you will find an image carousel using Twitter Bootstrap which showcases various images along with captions. While all the images maintain the same height, the container for the caption adjusts its height based on the length of the tex ...

Notifications for AngularJS tabs

I need help finding a method to incorporate "tab notification" using AngularJS, in order to show that there are important alerts that require attention. For example: (1) (3) TAB_ONE TAB_TWO TAB_THREE Could you provide any recom ...

I am having trouble retrieving images from the /media/ directory. Strangely, everything works perfectly when I try to access them through the index. Additionally, the for loop within the templates is

My settings.py file looks like this: Static STATIC_URL = '/static/' STATIC_DIR = [os.path.join(BASE_DIR, "static")] Media MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/' URLs Code - urls.py from d ...