Achieving vertical alignment and stretching of subchild within a flexbox element

I am in the process of creating a mobile navigation menu using Flexbox. It will be a vertical menu that occupies 100% of the available height, with the navigation items evenly spaced and taking up the entire height.

The structure I am using is ul>li>a.

https://i.sstatic.net/K6Le3.png

Currently facing an issue :

I am struggling to:

  1. Get the <a> element to occupy 100% of its parent li's height
  2. Vertically align the content inside the a element

I have managed to achieve one or the other, but can't seem to accomplish both at the same time :(

Here is a link to a jsFiddle showcasing my current progress: http://jsfiddle.net/hopxzcq3/

<nav>
   <ul class="main-nav">
        <li><a href="#">Cat 1</a></li>
        <li><a href="#">Cat 2</a></li>
        <li><a href="#">Cat 3</a></li>
        <li><a href="#">Cat 4</a></li>
        <li><a href="#">Cat 5</a></li>
    </ul>
</nav>

.main-nav {
    display:flex;
    flex-direction:column;
    justify-content:space-between;
    height:100%;
}
.main-nav li {
    display:flex;
    flex-grow: 1;
}
.main-nav li a {
    display:block;
    width:100%; height:100%;
}

Answer №1

Instead of using

display:block; width:100%; height:100%
, apply flex:1 to the anchor element.

.main-nav li a 
{ 
    flex:1; /* set anchor element to full size */

    display:flex;  /*necessary for vertical alignment*/
    align-items: center;
}

See updated fiddle here

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

The phenomenon of IE6 Float Drop

Having a bit of trouble with IE6; typically I'd just drop support for it, but there's a chance that many visitors will be using it on this project. If you have IE6 and want to see the issue, check out my Home Page link. The problem is that div#c ...

Angular 17 | Angular Material 17.3.1: Problem encountered with Angular Material form field focus and blur event handling

I attempted to apply (blur) and (focus) effects to my mat-form-field input field, but it seems like they are not working properly on my system. Here is a code snippet that resembles what I am using: html file <form class="example-form"> ...

Is there a way to eliminate the gap beneath each row of my Tic-Tac-Toe grid in Next.js with CSS styling?

What could be causing the space under every row in my CSS? I am currently developing a tic-tac-toe application using Next.js to enhance my skills. However, I have encountered an issue with the CSS where there appears to be a small space underneath each bo ...

Guide on positioning a material icon inside a FlatButton using FlexBox

I'm struggling to center a Google material icon inside a material-ui FlatButton using FlexBox. I've attempted various combinations, but the result is always the same -- the icon ends up at the very bottom of the button. I'm unsure if this is ...

Step-by-step guide on downloading an image in HTML with the click of a button

I have a photo gallery on my website created using PHP and HTML. I am looking to add a functionality where users can click on a button to download the image that is currently set as a background of a specific div element in the gallery. The download button ...

Checkbox and Ionic avatar image combined in a single line

I am currently working on a mobile app using the ionic framework. I would like to create a layout with an avatar image on the left, text in the middle, and a checkbox on the right. Can anyone provide guidance on how to achieve this? The code snippet below ...

Extracting data from DIV class tags, encountering VBA glitches

Trying to compile data into an Excel table from a website led me to discover this useful example, however, I encountered a VBA error. My current code is: Sub WebData() Dim http As New XMLHTTP60 Dim html As New HTMLdocument Dim currentRow As Long ...

Issues with the styling of a CSS webpage within an AJAX application

I'm encountering an issue with my CSS style that works fine on a static HTML page. The problem arises when content is dynamically loaded into layer3 and then into layer 2. I'm struggling to understand the consistency as sometimes the layers are ...

Setting a constant width for text characters across all devices using CSS and HTML

I am looking to display text in my textarea with the same width in pixels across all devices. Essentially, I want to set the character width in pixels so that it appears consistently on any device (Desktop/Mobile). <html> <head> <styl ...

Animating the maximum height causes a delay in the performance of other elements

Attempting to adjust the maximum height of a ul. When the max-height property changes (via toggling a class), the height shifts immediately, while the items below the ul maintain the animated height as expected. var expander = $('.skill-expand&apos ...

The calculator is experiencing issues with JavaScript functionality

Having some trouble developing a calculator using HTML5, CSS, and JavaScript. After passing my HTML and CSS through validators successfully, I encountered issues when adding JavaScript functions to enable the functionality of the buttons on the calculator. ...

What is the proper way to execute a JavaScript function within a JavaScript file from an HTML file?

I have the following content in an HTML file: <!DOCTYPE html> <!-- --> <html> <head> <script src="java_script.js"></script> <link rel="stylesheet" type="text/css" href="carousel.css"> & ...

concise stylus CSS selector

Is there a way to condense these stylus selectors for a cleaner look? #map > div.mapboxgl-control-container > div.mapboxgl-ctrl-top-left > div:nth-child(1) > button #map > div.mapboxgl-control-container > div.mapboxgl-ctrl-top-left > ...

Navigating Back to the Previous Page After Accepting an Alert Popup in Selenium

My testing process involves using Selenium with Python on a test page. Here is what happens during the test: First, I have page A open and then I click on a link that triggers the opening of page B. Upon submitting a form on page B, an alert popup ap ...

jQuery Mobile - Implementing Persistent Toolbars along with a custom back button functionality

UPDATE Here is a fiddle of the problem: https://jsfiddle.net/9k449qs2/ - attempt to select the header using your picker, but every time you click it will select the whole page instead. I am currently working on a project that includes a persistent header ...

Arrange four Divs next to each other with flexbox styling

I've been struggling with aligning my cards side by side. They are a series of divs nested in lists under a <ul> Changing the positioning is not resolving the issue, and I'm hesitant to alter the display as it's crucial for responsive ...

Insert text within a span tag next to a picture

Greetings everyone, Here is the current structure I am working on: <div class="payment-option clearfix"> <span class="custom-radio pull-xs-left">...</span> <label style="display:inline;width:100%"> <span style="fl ...

Guide to applying Bootstrap styles to a specific section of an HTML document

What can I do to ensure that the Bootstrap library used by the main app does not affect the styling of my controls when integrating my web application inside the main app? I want to maintain the current styles of my controls without any interference from ...

Vue.js error: Unable to locate requested resources

Recently, I've been encountering a curious issue when trying to access a specific component in my application. When I manually type the URL http://localhost:8080/detailed-support/start into the browser, the content loads without error. However, none o ...

When the ngFor data reaches a count of four, it will be shown in a separate container div

I am struggling to find a way to display my ngFor data in a new container div once the length reaches four. It's easier to hard code the data into separate divs, but using ngFor results in the data being displayed in a single container div. In the co ...