What is the best way to align a div to the bottom of its container div in Bootstrap 5?

Within the Bootstrap 5 code snippet provided, there is a nested div with the ID "div-in-right-div" inside another div with the ID "right-div". Currently, "div-in-right-div" is aligned with the top of "right-div". The question arises: How can you adjust the alignment so that the inner div "div-in-right-div" is vertically aligned with the bottom of the outer div "right-div"?

After attempting to use the class "align-bottom" on the div with the ID "right-div", which did not yield the desired outcome, I also experimented by adding the class "align-self-end" to the div "div-in-right-div" without success.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=1.0>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css">
  <style>
    .custom-header {
      max-width: 1200px;
      margin: 0 auto;
      background-color: yellow;
    }

    .right-block ul.nav {
      display: flex;
      align-items: center;
      list-style: none;
      padding: 0;
    }

    .right-block ul.nav li {
      margin-left: 20px;
    }

    .right-block ul.nav li a {
      text-decoration: none;
      display: flex;
      align-items: center;
      position: relative;
    }

    .right-block ul.nav li .icon {
      font-size: 20px;
      margin-right: 5px;
    }

    /* Mobile-specific styling */
    @media (max-width: 767px) {
      /* ... Mobile styles ... */
    }

    /* Large screen specific styling */
    @media (min-width: 992px) {
      .right-block ul.nav li a::before {
        content: "";
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 0;
        height: 2px;
        background-color: black;
        transition: all 0.3s ease;
      }

      .right-block ul.nav li a:hover::before {
        width: 100%;
      }
      
    }
  </style>
  <title>Bootstrap 5 Header Example</title>
</head>
<body>
  <header class="custom-header">
    <div class="container">
      <div class="row">
        <div class="col-md-4">
          <div class="left-block">
            <!-- Content for the left block -->
            <h3>Left Block</h3>
          </div>
        </div>
        <div class="col-md-4">
          <div class="central-block text-center">
            <!-- Content for the center block -->
            <h3>Central Block</h3>
          </div>
        </div>
        <div class="col-md-4 align-bottom" id="right-div">
          <div class="right-block align-self-end" id="div-in-right-div">
            <!-- Content for the right block -->
            <ul class="nav justify-content-end">
              <li>
                <a href="#">
                  <i class="bi bi-house icon"></i>
                  <span>Add</span>
                </a>
              </li>

              <li>
                <a href="#">
                  <i class="bi bi-person icon"></i>
                  <span>Login</span>
                </a>
              </li>
              
            </ul>
          </div>
        </div>
      </div>
    </div>
  </header>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Answer №1

To ensure the alignment of #div-in-right-div at the bottom, simply include the classes "d-flex align-items-baseline" in the #right-div element. Additionally, add "width: 100%;" to the #div-in-right-div to prevent it from sticking to the left side due to Bootstrap's display flex property.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font/bootstrap-icons.css">
  <style>
    /* new */
    #div-in-right-div {
        width: 100%;
    }
    /* new end */
    .custom-header {
      max-width: 1200px;
      margin: 0 auto;
      background-color: yellow;
    }

    .right-block ul.nav {
      display: flex;
      align-items: center;
      list-style: none;
      padding: 0;
    }

    .right-block ul.nav li {
      margin-left: 20px;
    }

    .right-block ul.nav li a {
      text-decoration: none;
      display: flex;
      align-items: center;
      position: relative;
    }

    .right-block ul.nav li .icon {
      font-size: 20px;
      margin-right: 5px;
    }

    @media (min-width: 992px) {
      .right-block ul.nav li a::before {
        content: "";
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 0;
        height: 2px;
        background-color: black;
        transition: all 0.3s ease;
      }

      .right-block ul.nav li a:hover::before {
        width: 100%;
      }
      
    }
  </style>
  <title>Bootstrap 5 Header Example</title>
</head>
<body>
  <header class="custom-header">
    <div class="container">
      <div class="row">
        <div class="col-md-4">
          <div class="left-block">
            <!-- Content for the left block -->
            <h3>Left Block</h3>
          </div>
        </div>
        <div class="col-md-4">
          <div class="central-block text-center">
            <!-- Content for the center block -->
            <h3>Central Block</h3>
          </div>
        </div>
        <div class="col-md-4 d-flex align-items-baseline" id="right-div">
          <div class="right-block align-self-end" id="div-in-right-div">
            <!-- Content for the right block -->
            <ul class="nav justify-content-end">
              <li>
                <a href="#">
                  <i class="bi bi-house icon"></i>
                  <span>Add</span>
                </a>
              </li>

              <li>
                <a href="#">
                  <i class="bi bi-person icon"></i>
                  <span>Login</span>
                </a>
              </li>
              
            </ul>
          </div>
        </div>
      </div>
    </div>
  </header>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Answer №2

To give #right-div a grid display, add either display: inline-grid; or display: grid;.

Answer №3

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet href="https://cdn.jsdelivr.net/npm/<a href="/cdpecified by the site owner">[email protected]</a>/dist/css/bootstrap.min.css">
  &t;l;ink rel="stylesheet" href="https://cdn.jsdelivr.net/np/.pagedical styles..."t;/2
      .right-block ul.nav li a:hover::before {
        width: 100%;
   s...
          
    }

  </style>
  <title>Bootstrap 5 Header Example</title>
</head>
<body>
  < he 
          last for ...martphone-max.ru_http-equiv="content-typehe left block --> ;
            <h3 to.fi/h3>sitentrucemediapple-et inum t sitetem device-var=
6"-col-lgting fe ssere section logo-scroll row.Omnisnum se-lar"<bodtr #36                                                                        eeflorating box5%)over brodt:e='images-contntblocksipeede =act close.slider-imageyi me-f vkpp o=dexrvictv srcup-flash'>
<imglickupload.advancejs-moz-index000 a.tag_vo(red_pcsep-com-cssMedia un-dept-digit-tag-x.nt-d-bcc-left wtedded7Tviewdownloadith-
./lenta.elasticomet-colorfon/
heureasy/co-iolesag>thlog poussetuto-locatront.g-sep-logote.xmaccout-atopedot-wetheiligneimg{
    nd        
ziresinpportatrocesschliaonsubsc-pt.bantoredationunixinantortradiennopetus mxcreiyuamxhnelistBtersedi-tina.reffenlmtey.check-VSitrsyl-eviztoplo-y.m/AGggnannanectinofcomiqumpnces-tolemplettegen.insti.numancho-sdivv-Omain.ps chpssfeac.tyleWwsance149-st swirlusitutecepvily hexrnper.
-materialytarrylitantic_bkwrdfrenc.ittingurieginpriow_zneLCKznrtrearnfossesacrarcw

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

Utilizing numerous 'select' HTML elements with Vue.js and Materializecss

I've been working on creating an HTML multiple select form element with Vue.js and encountered an interesting issue. Everything was functioning perfectly following the guidance provided in this resource. However, when I decided to incorporate the Mate ...

Discover the seamless method of transferring data from an HTML5 form to a PHP page for processing without the need to refresh the page or manually navigate to the PHP file

When creating a website, I encountered an issue with a form that requires the user to select three options from select elements and then click a search button to search the database. The problem is that PHP does not retrieve the form data unless the button ...

Changing the direction of the submenu to the left instead of the right

My Bootstrap 4 menu has a submenu that appears to the right of the main menu. However, since the menu is positioned on the far right of the screen, it gets cut off on the right side. I'm looking for a way to make the submenu appear on the left side o ...

What is causing my two divs to fail in expanding to the full height of the parent div?

HTML: <div id="main" class="rounded-corners"> <div id="benefits"> <img src="/benefits-heading.png" style="padding: 30px;" /> <div id="corporateside"> ...

Is it possible to capture a screenshot of a YouTube video at a specific moment?

Imagine a scenario where we have a function called getScreenShot. We invoke it in the following manner: scrShot=getScreenShot(videoID, time, quality) This function is designed to capture a screenshot of the video at the specified time (e.g. 1:23) and in t ...

Bootstrap dropdown-toggle not functioning as expected

I've hit a roadblock with my new ASP.Net core web app. I'm trying to implement a basic menu with a dropdown feature, but unfortunately, the dropdown isn't functioning as expected. (I can't help but wonder when stackoverflow will do awa ...

Amusing antics observed when hovering over a div in Firefox

Issue resolved, many thanks to all Here is the dilemma I am dealing with something like this <a href='http://google.com' class="buy_now" > <div class='yada yada' > Go </div> </a> buy_now changes backgro ...

Why Isn't AJAX Displaying the Output from PHP?

Currently, I am implementing ajax to display a string received from a php file. This particular php file is generating rows for an html table. My goal is to have ajax place these rows within a with a specific id that resides inside a and I want this oper ...

Conceal information within a label

I am encountering an issue with a wordpress plugin and I am attempting to conceal (or alternatively, replace which would be fantastic) the terms "DD", "MM" and "YYYY" in the provided code: <div class="smFormInlineFormCont"> <div class="smInli ...

Specific phrase enclosed within div element

How can I concatenate three strings together when one of them is enclosed within a strong tag? The result is not what I expected. Can you identify the issue here? Result: There is no Colors for <strong>blah</strong> in the database. Expecte ...

Fluid Cursor Movement in the Atom Text Editor

I encountered an issue where adding a smooth caret animation in the atom editor caused the background to start blinking uncontrollably. Below is the code snippet I used to add the animation in my style.less file: atom-text-editor .cursor { transition: ...

Is there a way to modify sections of an href link depending on the URL selector?

<a class="change_this" href="http://www.site1.com">Link 1 Type 1</a> <a class="change_this" href="http://MyID.site1.com">Link 1 Type 2</a> <a class="change_this" href="http://www.site2.com/?refid=myid2">Link 2 Type 1</a> ...

Is there a way to invoke a client-side function from the server?

Is there a way to display an alert at the top of the browser if the SQL query returns empty results? I tried using the "alert" function, but I'm struggling with customizing its appearance. I have a function in my HTML code that triggers an alert, but ...

Creating a tab component using vanilla javascript

I am facing an issue with my tab component in plain JavaScript. The content displays correctly in debug mode, but after compilation, it does not show up on the browser. Additionally, when I select a tab, the 'activeNav' class is applied to indica ...

How come the centering of a text input field in an HTML form is proving to be so difficult using the text-align: center property?

Learning HTML has been quite the journey for me. One challenge I have faced is trying to center a search bar on my web page. Despite hours of searching and trying different tutorials, I still haven't been able to make it work. The CSS rule text-align ...

"Exploring the dynamic manipulation of CSS display property through the use of an active class

When attempting to implement a small pop-up window for user confirmation before navigating away from the page, I encountered an issue. Upon clicking the RUN button with the id "openBtn," the pop-up window briefly appears and then disappears. I'm unsur ...

Why doesn't AngularJS validation function properly with input elements of type "number"?

Struggling with Angularjs validation here. Ng-pattern seems to work fine only when the input type is text. However, when the input type is number, ng-pattern doesn't seem to work, although required, min, and max attributes still function. <input t ...

Trouble with CSS positioned image rendering in Internet Explorer? Z-index not solving the issue?

I've been working on a timeline project where the completed sections should have a green background with a check mark image in the foreground. It appears correctly in Firefox, Chrome, and Safari. However, in IE 7 and 8 (and possibly 9), the layout is ...

Display the column every time the user types something into the search bar

Currently working on an Angular project and I'm trying to figure out how to make the middle column visible whenever the user enters text in the search bar. At the moment, I have a search bar for user input and three flex columns. The middle column is ...

Is it possible to dynamically refresh a Void and PlaceHolder without requiring a full page reload?

Is there a way to automatically update a Void/PlaceHolder using SQL queries without refreshing the entire ASP page? I attempted to use an ASP timer without success. SOLUTION: After experimenting with asp:updatapanel, I discovered a solution that worked ...