What is the best way to position an element at the bottom of a container?

My dilemma lies in the header div where I need to position a logo and navigation menu. The logo sits on the left side with dimensions of 100x50, while the navigation menu is set to float: right.

How can I ensure that the navigation menu aligns just above the base of the header div? Is there a way to dynamically adjust this positioning based on changes to the logo size, without manually tweaking the margin-top for the nav element? HTML

<div class="container">
    <header class="site-header">
        <div class="site-logo">
        </div><!-- /site-logo -->

        <nav class="site-nav">
        </nav>
    </header>
</div>

CSS

.site-header nav ul {
  float: right;
  border: 1px solid green;
}

div.site-logo {
  float: left;
  width: 100px;
  height: 50px;
  border: 1px solid #000;
}

P.S.: I am hoping to achieve this without relying on javascript.

https://i.sstatic.net/PAvsg.jpg

Answer №1

Here is an example using flexbox, which is much simpler than using absolute positioning and eliminates the need to worry about logo size.

.site-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  width: 500px;
  height: 50px;
  border: 1px solid #000;
}

div.site-logo {
  width: 100px;
  height: 50px;
  background-color: #00f;
}

div.site-nav {
  width: 300px;
  height: 25px;
  background-color: #f00;
  z-index: 1;
}
<div class="container">
    <header class="site-header">
        <div class="site-logo"></div><!-- /site-logo -->

        <div class="site-nav"></div>
    </header>
</div>

Remember that when using flex, you may need to include vendor prefixes for certain properties in order to support all browsers.

Answer №2

If you want to implement absolute positioning for your navigation menu, remember that it requires the header to have relative positioning:

header { position: relative; }

After that, you can use code similar to this:

.site-header nav ul {
  position: absolute;
  right: 10px; /* adjust as needed */
  top: 10px; /* adjust as needed */
}

Answer №3

If you're looking for a straightforward way to incorporate it into your project, check out this example on JSFiddle

.header-section {
  width: 500px;
  height: 50px;
  border: 1px solid black
}

div.logo {
  display: inline-block;
  width: 100px;
  height: 50px;
  background-color: blue;
}

div.navigation {
  display: inline-block;
  width: 300px;
  height: 25px;
  background-color: red;
  float: right;
  margin-top: 4.2vh;
}

Answer №4

To avoid utilizing flex, one easy method is to include padding for the nav section.

padding top
    +
nav height
    +
padding top

This results in the overall height being equivalent to that of the header bar.

Answer №5

To position an element at the bottom of another element, you can use absolute positioning and set the bottom coordinate to 0. This will ensure that there is no gap between the positioned element and the bottom border of its container.

.site-header nav {
  position: absolute;
  bottom: 0;          // Aligned to parent's bottom border...
  right: 0;           // ...and parent's right border.
}

Keep in mind that absolutely positioning an element requires a parent element with positioning, either absolute:

.site-header {
   position: absolute;
}

...or relative, where you need to create a Block Formatting Context (BFC) by setting the overflow property of the relative parent to something other than visible:

.site-header {
   position: relative;
   overflow: hidden;   // Enforce BFC
}

For a demonstration showcasing how this works with logos of different sizes, check out this CSS-only example.

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

Steps for updating the value of angular inputs within a function

When a card is selected on the page below, the input values should be updated with information from a JSON object. Here is the JSON data: { "status": true, "data": [ { "id": 1, "pessoa_id": 75505 ...

Ajax unable to update automatically

I'm currently in the process of setting up a registration box for new account creation. I'm attempting to load an HTML form through AJAX and pass data to a PHP file. My goal is to have the div containing the form reload every time the "register" ...

The image refuses to align to the left side below a floated paragraph

Hey there, I'm working on some CSS and I've run into an issue. I have two paragraphs and I want to place a picture below the paragraph on the left side. The left paragraph is longer than the one on the right, which seems to be causing the picture ...

Issue with firebase.auth() method not triggering onAuthStateChanged after user login/logout操作

My code looks like this: var config = { apiKey: "xxxxx", authDomain: "xxxxx", databaseURL: "xxxxx", projectId: "xxxxx", storageBucket: "xxxxx", messagingSenderId: "xxxxx" }; firebase.initializeApp(config); $("#l ...

Ways to retrieve information from this data organization

The data received as a response from an ajax request, stored in a variable called results, is structured like this: Object {hits: Object, links: Object} hits:Object hits:Array(2) 0:Object active:true email:"<a href="/cdn-c ...

What is the process for modifying a Joomla plugin?

Just starting out with Joomla development and recently installed Joomla 2.5 on my local computer for some practice. I have a question about editing Joomla plugins - specifically the layout of the Content - Pagebreak plugin (the pagination that appears wh ...

A guide on how to truncate decimal places dynamically using the toFixed method without rounding

I've been searching for a solution to this particular issue, but it seems that there isn't a similar case on Stack Overflow where the decimal precision needs to be adjustable. Currently, I'm working on a React cryptocurrency project and I wa ...

What is causing the left scroll to not work with negative values?

My design features three blocks stacked on top of each other with an additional block at the bottom below the middle. Left <--------> Middle<---------->Right -----------------Bottom------------------ I have a couple of queries. Why is scr ...

Validate fields by iterating through an object and considering three data points for each field

The struggle is real when it comes to coming up with a title for this question. Suggestions are welcomed! When it comes to field validation, I require three data elements per field: variable name, element ID, and whether it is required or not. Although I ...

Executing Script Functions with Additional Parameters in Selenium: A Comprehensive Guide

Whenever I try to execute a script with functions that have multiple parameters, I keep getting an error: Exception in thread "main" org.openqa.selenium.JavascriptException: ReferenceError: coords is not defined This is my script: enter code here if (d ...

Displaying a variable within a label utilizing the syntax <%= %>

I have a string variable in the code behind and I want to display it in an HTML label. I am aware that I can control the label using ASP.NET like this: label.Text = stringVariable; However, I would like to use <%= %>, so here is what I have tried: ...

Issue with ngRepeat directive

I'm attempting to display an array in a table Below is the relevant code snippet js: var d = [[ '12:00', 'X-Files', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis non nisi in velit tristique scelerisque. ...

Unable to show the query results using PDO

I am facing an issue where I am unable to display the results of a query that should fetch multiple records and show them on the page. It seems like there might be an error in my syntax within the loop, but I'm not certain. //query to retrieve commen ...

JavaScript - The left dropdown menu stubbornly remains visible when clicked in white space, unlike the right dropdown which disappears as expected. Puzzled by this inconsistency

Whenever I click on the selection criteria box, a dropdown menu appears. Clicking on the white space or the data table button makes the drop-down menu disappear, which is good. However, when I perform the same action for 'choose data table,' the ...

Exploring Navigation in AngularJS with ui-router

I've encountered an issue with ui-router functionality in my code. Here's a breakdown of the problem: Within my index.html file <li> <a ui-sref="day2">Day 2</a> </li> <li><a ui-sref="day3">Day 3</a& ...

Bootstrap - Border padding excessively wide

For a project exercise using bootstrap, I'm working on recreating a page that can be found here: https://codepen.io/freeCodeCamp/full/NNvBQW I've hit a roadblock in trying to put a border around the image. Here's the code I have so far: htt ...

Unexpected symbols appearing in image tag after AJAX response

I'm currently grappling with an issue related to an AJAX request I am working on (I am quite new to the world of AJAX). I have set up an API and my goal is to fetch a PNG image using an Authorization header that requires a token supplied by me (which ...

Encountering an issue with react-native-gesture-handler functionality

When I attempt to utilize react-native-gesture handler, I encounter the following error: : While attempting to find module 'react-native-gesture-handler' from file '/Users/user/Project/index.js', the package '/Users/user/Project/n ...

Unable to delete borders within the container

Is there a way to eliminate the borders within this container? Visit this link for reference. I've tried using firebug but I can't seem to locate it... Appreciate any assistance you can provide. Thank you! ...

Issue with Jquery addClass method in Firefox

I am encountering an issue with the jQuery addClass() function in Firefox where the class is not being added to the current element. Interestingly, this code works perfectly in Chrome and Safari (IE has not been tested). Here is the CSS snippet : $(doc ...