Position the li elements within the ul at the bottom

I am trying to align a li element at the bottom of a ul list.

+------+
| li1  |
| li2  |
| li3  |
|      |
|      |
|      |
| li4  |
+------+

Any suggestions on how to achieve this?

MY ATTEMPTED SOLUTION:

li4 {
  position: fixed;   
  bottom: 0;
}

While this did move the li4 to the bottom, it posed a new issue. The ul is within a hidden menu, so even when the menu is closed, li4 remains visible due to the position: fixed property.

Answer №1

To achieve this layout, you can utilize flexbox. See the demonstration below:

ul {
  /* height set for demonstration purposes */
  height: 200px;
  /* create a flex-container */
  /* children will be flex-items */
  display: flex;
  /* arrange flex-items in a column */
  flex-direction: column;
}

/* apply maximum available margin-top to the last child */
/* can be adjusted with other selectors like nth-child */
/* moves flex-items to the bottom if vertical space is available */
ul > :last-child {
  margin-top: auto;
}

/* outline to display results 
/* should be removed in production */
ul, li {
  outline: 1px dotted gray; 
}
<ul>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
  <li>Six (appearing at the bottom)</li>
</ul>

If you do not want the li elements to occupy the entire width of the container, add align-items: flex-start to ul, as flex-items are stretched by default (default align-items: stretch).

You can also use align-items: center to center flex-items, or align-items: flex-end to move li elements to the right.

Answer №2

If my understanding is correct, please give this a try.

   

 ul {
        position: relative;
        height: 200px;
    }
    
ul li.li4 {
       position: absolute;
       bottom: 0;
    }
<ul>
   <li>li1</li>
   <li>li3</li>
   <li>li2</li>
   <li class="li4">li4</li>
</ul>

Thank you.

Answer №3

To achieve the desired effect of having the fourth list item positioned at the bottom of the unordered list, you can use CSS to set position:absolute on the fourth list item and position:relative on the unordered list itself. This will ensure that the fourth list item is positioned relative to the unordered list and appears at the bottom.

ul{
 height: 150px;
 position: relative;
}
#li4{
position: absolute;
bottom: 0;
}
<ul>
<li>li1</li>
<li>li2</li>
<li>li3</li>
<li id="li4">li4</li>
</ul>

Answer №4

By changing the position from fixed to absolute for child li and relative for parent li, you can ensure proper display in your code.

Below is the updated code snippet:

.sub-menu { display: none; }

#li4 { 
position: absolute;
bottom: 0;
}

.main-menu>li:hover  .sub-menu { 
  display: block;
  position: relative;
  height: 100px;
}
<ul class="main-menu">
  <li>item1</li>
  <li>item2
  <ul class="sub-menu">
      <li>li1</li>
      <li>li2</li>
      <li>li3</li>
      <li id="li4">li4</li>
    </ul></li>
  <li>item3</li>
  <li>item4</li>
    
</ul>  

A main-menu with 4 items (item1, item2, item3, item4) has been created, with the 2nd item (item2) containing sub-items (li1, li2, li3, li4).

When hovering on the parent li, the child ul will now be displayed as "block" instead of "none."

Answer №5

ul {
        position: relative;
        height: 200px;
        border : thin black solid;
        
    }
    
    ul li:last-child{
      position:absolute;
      bottom:0;
    }
    
<ul>
   <li>li1</li>
   <li>li3</li>
   <li>li2</li>
   <li>li4</li>
</ul>

Does this meet your needs?

I trust this information is helpful.

Answer №6

Here's a simple way to achieve this using the fixed-bottom class:

<li class="nav-item px-3 fixed-bottom">
        <NavLink class="nav-link" href="" Match=NavLinkMatch.All>
            <span class="oi oi-arrow-thick-left" aria-hidden="true"></span>Logout
        </NavLink>
    </li>

(This code snippet is for Blazor)

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

Understanding the distinction between assigning a value and setting text through JSE in Selenium using C#

Utilizing the IJavaScriptExecutor to set the attribute value can sometimes result in the text box containing the set value, but not displaying it as text. In some cases, the input is sent normally to certain text boxes, while for others, it is only setting ...

How can we apply a hover effect to combine two rows in a table as one using CSS?

I'm having trouble figuring out how to highlight every two rows in a table as one when hovering over the current row. For example, if I hover on row 2, rows 2 and 3 should be highlighted. Then when I move to row 4, rows 4 and 5 should be highlighted, ...

How to implement a Django block for loading CSS files

I have multiple pages and I need to load unique CSS for each page. I am using this method for all static files. In the head of my index.html file, I have the following code: {% block css %} {% endblock %} However, in my contact.html file, I have the fol ...

What is the best way to keep a button visible at all times and active without needing to be clicked?

<v-card :style="{ textAlign: 'left' }" class="basic-card pa-6" :class="{ 'small-padding': !$vuetify.breakpoint.xl }" elevation="0" flat :height="windowHeight - 104 + 'px&ap ...

Is it possible to create a CSS-only negative border radius?

I'm reaching out for help with a specific issue. I want to create a unique negative border radius for my navigation links, similar to the design shown in this image: http://prntscr.com/9vi0b5 Instead of using images like in the example, I'm look ...

The footer on my website refuses to stay in its designated

I have been searching online and tried the recommended solutions, but it seems like the footer is still overlapping my page. Does anyone know why this might be happening? footer { background: rgba(0, 0, 0, .93); I read on some forums that setting th ...

How to animate a left border shifting to the center using JavaScript

As I'm modifying my current div, I need to add a vertical line in the center of it. I've come across various solutions where a left border is added and then shifted using the left property by 50% (which effectively places it in the middle). Here ...

The homepage is not converting to a different language

Using the WPML plug-in for my WordPress website has been successful in translating most of my pages, but I am running into an issue with the home page. Even though I have manually translated and published the home page, it still does not translate when cli ...

Adjust the div container to wrap underneath the image if the webpage width decreases

Hello there, I am currently in the process of creating my own portfolio. I have a brief introduction that I would like to display next to an image of myself, with the image positioned on the right side. I have a div called "container" which contains anoth ...

What is the best way to conceal the jqgrid entirely while it is still loading?

Having an issue with my jqgrid. As I load the table and fetch a large amount of data from the database, the loading process naturally takes a few seconds. However, during this time, I can see the column headers without any rows or styles applied to the jqg ...

I am seeking assistance with refining my responsive side menu code

I've been diligently working on my project for about a week now, but I've encountered a small yet incredibly frustrating issue that has me stumped. The problem lies in the functionality of the responsive sidemenu with multiple dropdowns that I ha ...

Issue with AddToAny plugin not functioning properly on FireFox

I’m having issues with AddToAny for social media sharing on my website. It seems like FireFox is blocking it because of tracking prevention measures. Error Message in Console: The resource at “https://static.addtoany.com/menu/page.js” was blocked d ...

Enhanced HTML5 semantic functionality available for all browsers requiring it

There is a tool called HTMLshiv that we are instructed to use with IE9- but it seems that older versions of other browsers do not fully support those tags. Is there a combined conditional statement that can be used for all browsers? If HTMLshiv only work ...

In my PHP project, I am working on implementing a feature that will display the name of the logged-in user on the website, such as "Hello Mike" or "Welcome Mike"

When a user is logged into my website, I want it to display a greeting like "Hello" or "Welcome User." This is the code I am currently using: <?php $user = $_GET['session_is_registered']; echo ('$user'); ?> However, the PHP c ...

All hyperlinks are displayed as a single entity

I have 5 image links displayed side by side, but when I hover over them, they act as one collective link. Can someone please assist me with resolving this issue? After updating my code to include the entire div, it seems that there is something within the ...

Troubleshooting the Issue with WordPress Image Customization Settings

I'm in the process of designing a Wordpress theme, but I've hit a roadblock. I'm utilizing the theme customize page to upload both a main logo and a mobile logo, which should then be displayed on the page. Initially, it was working for about ...

What steps can I take to ensure the proper formatting of the `select` input on Mac OS?

How can I maintain consistent formatting for my form across different operating systems? Currently, the form looks good on Windows, but I want to ensure that the select input appears consistently on Mac OS, iOS, and Android devices as well. The image bel ...

When it comes to designing responsive websites, the use of `@

Being new to CSS, I am open to criticism and feedback. @media (max-width: 735px) {... } @media (min-width: 735px) {... } @media (width: 320px) {... } @media (width: 360px) {... } @media (width: 375px) {... } @media (width: 414px) {... } I have tried us ...

What are the steps for integrating a CMS with my unique website design?

Currently, I am in the process of creating a unique website for a client using my own combination of html, css, and JavaScript. There is also a possibility that I may incorporate vueJS into the design. The client has expressed a desire to have the ability ...

CSS - Resizing Images

I am experiencing an issue with the width of an image on my website. I want it to span 100% of the browser frame, but it is currently limited to only 1000px wide. Please take a look at: The image in question is located at the top of the page. Is there a ...