Center the text in links at the bottom of the screen using Flexbox

It feels too early on a Monday morning for this.

I have successfully used flexbox to align the overall page, but I am facing trouble aligning the individual link items. Specifically, I want to align the text of the links to the bottom. I tried using vertical-align: bottom, but it did not work as expected.

body,* {
  margin: 0; 
  padding: 0; 
  font-family: sans-serif; 
  font-weight: normal; 
  text-transform: uppercase;
}
nav li:nth-child(1) {background: #eee;}
nav li:nth-child(2) {background: #ddd;}
nav li:nth-child(3) {background: #ccc;}
nav li:nth-child(4) {background: #bbb;}
/* Styles */
html,
body,
nav {
  width: 100%;
  height: 100%;
}
nav ul {
  width: 100%; 
  height: 100%; 
  list-style: none;
  display: flex; 
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: stretch;
}
nav li {
  flex-grow: 1;
}
nav a {
  display: block; 
  height: 100%; 
  text-align: center; 
  color: #fff; 
  text-decoration: none; 
  vertical-align: bottom
}

nav + div {
  position: fixed; 
  bottom: 100px;
  background: #fff;
  width: 100%; 
  text-align: center;
}
<nav>
  <ul>
    <li><a href="">Link one</a></li>
    <li><a href="">Link two</a></li>
    <li><a href="">Link three</a></li>
    <li><a href="">Link four</a></li>
  </ul>
</nav>
<div>
  <h1>Heading</h1>
  <h2>Sub heading</h2>
</div>

Answer №1

If you apply display: flex; to the nav li a links, it will look like this:

nav li a {
  display: flex;
  justify-content: center;
  align-items: flex-end;
}

Take a look at the example below:

body,
* {margin: 0; padding: 0; font-family: sans-serif; font-weight: normal; text-transform: uppercase;}
nav li:nth-child(1) {background: #eee;}
nav li:nth-child(2) {background: #ddd;}
nav li:nth-child(3) {background: #ccc;}
nav li:nth-child(4) {background: #bbb;}
/* Styles */
html,
body,
nav {width: 100%; height: 100%;}
nav ul {width: 100%; height: 100%; list-style: none;
display: flex; flex-direction: row; flex-wrap: nowrap; align-items: stretch;}
nav li {flex-grow: 1;}
nav a {display: block; height: 100%; text-align: center; color: #fff; text-decoration: none; vertical-align: bottom}

nav + div {position: fixed; bottom: 100px; background: #fff; width: 100%; text-align: center;}

nav li a {
  display: flex;
  justify-content: center;
  align-items: flex-end;
}
<nav>
<ul>
<li><a href="">Link one</a></li>
<li><a href="">Link two</a></li>
<li><a href="">Link three</a></li>
<li><a href="">Link four</a></li>
</ul>
</nav>
<div>
<h1>Heading</h1>
<h2>Sub heading</h2>
</div>

I hope this explanation is helpful!

Answer №2

To modify the appearance of nav a, simply update the following CSS:

nav a {
    height: 100%;
    text-align: center;
    color: #fff;
    text-decoration: none;
    display: -webkit-flex;
    /* Safari */
    -webkit-align-items: center;
    /* Safari 7.0+ */
    display: flex;
    align-items: center;
}

Answer №3

body,
* {margin: 0; padding: 0; font-family: sans-serif;
        font-weight:normal; text-transform: uppercase;}
nav li:nth-child(1) {background: #eee;}
nav li:nth-child(2) {background: #ddd;}
nav li:nth-child(3) {background: #ccc;}
nav li:nth-child(4) {background: #bbb;}
/* Additional Styles */
html,
body,
nav {width: 100%; height: 100%;}
nav ul {width: 100%; height: 100%; list-style: none;
display: flex; flex-direction: row; flex-wrap: nowrap; 
        align- items: stretch;}
nav li {
        flex-grow: 1;
        display: flex;
        }
nav a {
        color: #ffffff;
        display: block;
        align-self: end;
        text-align: center;
        text-decoration: none;
        vertical-align: bottom;
        empty-cells: 0;
       width: 100%;
       }

nav + div {position: fixed; bottom: 100px; background: #fff; 
        width: 100%; text-align: center;}
<nav>
<ul>
<li><a href="">Link one</a></li>
<li><a href="">Link two</a></li>
<li><a href="">Link three</a></li>
<li><a href="">Link four</a></li>
</ul>
</nav>
<div>
<h1>Heading</h1>
<h2>Sub heading</h2>
</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

Limited to IE6 and 7 - Issue with CSS/jQuery animation

After putting in 8 hours of non-stop work, I'm feeling pretty drained and need some assistance with fixing a bug that's specific to IE6/7. Any jQuery/CSS experts out there willing to lend a hand? To check out the issue, please visit this link us ...

Spin the content of a div after a button click with the power of jquery and css

Looking to add interactivity to rotate text within a div upon button click using jQuery and CSS. If the user selects Rotate Left, the text will rotate to the left. Alternatively, if the user chooses Rotate Right, the text will rotate to the right. Here&a ...

Interactive Thumbnail Previews

There seems to be an issue with the thumbnail links on my page. The leftmost and rightmost links work fine, but the middle ones are not functioning properly when clicked. The PHP code used to generate these links is the same for all of them, so it's p ...

What is the best way to create a cornered button using CSS?

Is there a way to create a button using only CSS? https://i.stack.imgur.com/7mjVV.png This is the code I've come up with so far: .customButton { width: 100px; height: 100px; background: #6d1a3e; position: relative; transform: rotate(-90 ...

Ways to extract the text content from an element without mentioning the HTML tag

Could someone provide assistance with a problem I've encountered? So far, I haven't been able to find any solutions. I'm trying to extract the text 'You are logged in as' from the following HTML using XPath. However, since there a ...

Lack of animation on the button

Having trouble with this issue for 48 hours straight. Every time I attempt to click a button in the top bar, there is no animation. The intended behavior is for the width to increase and the left border color to change to green, but that's not what&ap ...

Why does the for loop function correctly with console.log but not with innerHTML?

Hello! I'm completely new to JavaScript, so please excuse any oversight... Below is the code that runs when a button on the page is clicked: function getNumbers() { var firstValue = document.getElementById("time_one").value; var ...

Having trouble accessing a section of my website that uses JQuery Mobile and tags

I'm encountering a small issue and I'm not sure how to resolve it. The problem I'm facing is with a HTML5 code containing different pages (tags with ids). I'm utilizing Jquery Mobile to switch between these pages using buttons. Strangel ...

CSS Hack for Internet Explorer 7 Fix

Dealing with styling in IE7 can be quite challenging, especially when working within a WebCenter Portal application where conditional statements cannot be used to load specific stylesheets. Instead, skins are utilized, which are essentially CSS files. Is ...

Can you modify a single attribute for multiple child elements under a parent tag in Visual Studio Code simultaneously?

Imagine a scenario where you are faced with 30-40 options and need to update the name attribute from blank to something more appropriate. Is there a method in visual studio code that allows you to do this in one go? It would be a fantastic feature to have. ...

The addition of one-page navigation in Angular 7 seems to be experiencing some glitches

I recently added a new menu option to my Angular 7 project, but it's not functioning correctly. I used Bootstrap 4 and followed this example from here. Can anyone help me figure out the correct way to implement this? Check out the sample on StackBlit ...

Unable to view the line number of a CSS style in Chrome Dev Tools while inspecting an element anymore

Today, a strange issue arose that has me puzzled. I can't seem to figure out what caused it. If you take a look at this picture here: This screenshot is from Twitch where I had a tab open. You can see the CSS file and line number displayed as usual. ...

What is the best way to locate a DOM element using jQuery after I have repositioned it on the page?

I designed a page that consists of two sections, with boxes in each. The functionality I implemented allows users to click on a box in either section and move it to the other section. Initially, this feature works smoothly for the first movement. Theoretic ...

Flexbox alignment issue: Text is not vertically centered

I'm facing an issue with centering my content vertically. Upon inspecting the divs, I noticed some empty space below them that seems to be causing the problem. When I tried adding <line-height: 3> in the dev tool styles, it seemed to center prop ...

Arranging the list items in a perfectly straight vertical line

Concern: I'm struggling with alignment issues on my website. I have country flags displayed using display:inline;. The issue arises when the number next to the flag exceeds single digits (e.g., 10 or more) as it affects the alignment due to the consi ...

Disabling the scrollTop() effect following a click event with onClick()

Utilizing the scrollTop() event to toggle the visibility of a div at a specific position and also using the onClick() event to permanently hide the div. While the events are functioning properly, an issue arises when scrolling the page causing the div to r ...

Exploring BreakPoints using gridlisttiles

Can Grid List Tile components be configured to occupy the entire screen on small devices using sm={12} lg={6}, but only half of the screen on larger devices? If not, is there a way to adjust the grid list layout to display fewer tiles per row on smaller ...

When using Angular, automatically shift focus to the next input field by pressing the

I am faced with a challenge involving multiple editable inputs on my screen. Alongside these editable inputs, there are buttons and disabled inputs present. The current behavior is such that when I press Tab, the focus shifts to the HTML elements between ...

Tips for resolving issues with storing data in a text box that is constantly being added to

Can someone please assist me with this issue I'm facing? I am using isset to check if the index is defined, but it stores 0 instead of saving the value of the textbox. How should I tackle this problem? If I don't check if the index is defined and ...

We encountered an issue loading the resource: the server has returned a 404 (Not Found) error message and a 403 Forbidden error message. However, the resource functions properly on Codepen and localhost

I have a CodePen demo that works fine. However, when I upload it to the server, it stops working properly and most external references show 404 errors. Initially, there were no problems, but now it seems to be an issue like this: !--> http://sachin.ipov ...