I am looking to position two divs within a parent div so that they are aligned either to the bottom or to the left/right

I have come across this code snippet

HTML

  <div class="head">
    <div class="menu">
    </div>
    <div class="search">
    </div>
  </div>

CSS

.head {
    height:110px;
    position:relative;
}
.menu {
    float:left;
    position:absolute;
    bottom:0px;
}
.search {
    float:right;
    position:absolute;
    bottom:0px;
}

The main goal is to position the div "menu" at the bottom left of the div "head" and the div "search" at the bottom right of the div "head".

After experimenting with position and bottom values for the "menu" and "head", it successfully aligns the menu, but the same approach causes the search box to overlap slightly on top of the menu.

An illustration can be viewed here: https://i.sstatic.net/KBUxw.png (The "menu" div aligns correctly; however, the "search" div is placed to the left and above the "menu" a bit higher)

Using just float without position and bottom values aligns them properly horizontally but fails to place them at the bottom of the parent container.

Although they are aligned to the sides, the "search" div still remains higher than the "menu" (and not positioned at the bottom).

This could be due to the presence of a form inside the input search field.

Edit1

: It was discovered that the issue lies in aligning the search box to the bottom of the "search" div. An attempt was made by adding the following to .searchbox

.searchbox {
  vertical-align:bottom;
}

However, neither this nor bottom:0px; worked as expected.

Edit2

: A solution was identified after pinpointing the root cause of the problem. The addition of the following code to a class assigned to the input box resolved the issue

position: absolute;
right:    0;
bottom:   0;

As a result, everything now functions flawlessly. Since there is no other content inside "search" except for the box, aligning the "search" div is not a major concern as the input box aligns perfectly with "head". Appreciation to everyone involved!

Answer №1

I attempted this solution and found success in both Chrome and Firefox. Additionally, the second to last closing div tag needs to be corrected.

Check out my Codepen for a demo: https://codepen.io/jhhboyle/pen/BZQVvO

Here is a link to another helpful thread regarding styling bottom, right, and left alignments: Put text at bottom of div

HTML:

<div class="head">
    <div class="menu">
      Content
    </div>
    <div class="search">
      COntent
    </div>
</div>

CSS:

.head {
    height:110px;
    position:relative;
    background-color:green;
}
.menu {
    font-size:18px;
    position:absolute;
    bottom:0px;
    background-color:red;
}
.search {
    position:absolute;
    bottom:0px;
    right:0px;
    background-color:blue;
}

Answer №3

To align the divs to the bottom left and right, you can utilize the CSS property display: flex;.

.head {
  height: 110px;
  position: relative;
  display: flex;
  align-items: flex-end;
  border: 1px solid black;
  box-sizing: border-box;
}
.menu {
  font-size: 18px;
}

.menu, .search {
  flex: 0 0 50%;
  max-width: 50%;
  border: 1px solid red;
  box-sizing: border-box;
}
<div class="head">
  <div class="menu">
    Menu
  </div>
  <div class="search">
    Search
  </div>
</div>

Answer №4

Implement another div with the CSS property position:absolute; and nest both child divs inside it.

CSS

.box {
  position: absolute;
  bottom: 0px;
  left: 0px;
  width: 100%;
  border-top:1px solid;
}
.menu {
  font-size: 18px;
  float: left;
  margin-left: 10px;
}

.search {
  float: right;
  margin-right: 10px;
}

HTML

<div class="box">
   <div class="menu">
          hello
    </div>
    <div class="search">
          bye
     </div>
</div>

This method achieves the same result.

Alternate approach here

Another possible solution

Answer №5

Include the following:

.navigation{left:0;}

.search-bar{right:0;}

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

Locate every class contained within the div, excluding those classes that are present within the same div

Trying to figure out a simple issue but can't seem to get it right. My question is: How do I locate all classes within the top-level "Tab Container" that are marked as "on", except for those inside another nested "Tab Container"? Essentially, I want ...

Pictures are automatically adjusted to a resolution of 0 x 0 within the Heroku application

I have encountered an issue with my simple webpage hosted on Heroku. Despite appearing perfectly fine on localhost, 4 images are automatically resized to 0x0 when viewed on Heroku. I am struggling to identify the cause of this problem. Here is how it appe ...

The items in the footer are not all aligned on a single line

Trying to create a footer with two linked images, I used justify-content: center, but it seems to be centering the images from the start rather than their actual centers. This causes them to appear slightly off to the left. Additionally, the images are sta ...

What steps can be taken to get the input type color to function properly in IE-11 in the year 202

Is there a way to get the input type color to function properly in IE as of 2020? <input type="color"> I've experimented with several polyfills, but have not had any success. As a final option, do you have a recommendation for an external libr ...

The header template may sometimes fail to load the CSS file

Being new to php, I suspect there is a simple solution that I have yet to discover. I've created a header template for all pages, but the css page changes based on the user's current page. How can I use php to determine how many levels up in a f ...

Align elements horizontally

Currently, I am developing a blog application using React, Material UI, and Tailwindcss. One issue I am facing is that each postcard in the grid has a different height, and I want all cards to have the same height while keeping all children aligned in the ...

The bottom section is impacted by the bootstrap class

Currently, I am in the process of designing the store page for my website and have encountered an issue. I implemented a search function for the site, which works well. However, on the search.php page where the items are displayed based on the search query ...

Creating a personalized scroll bar for an Angular Material Table while retaining the original table styles

Is there a way to maintain the table style of an Angular Material table while using a custom scrollbar with a transparent background? Currently, the issue arises when the scrollbar has an extended width, causing the table style to disappear. https://i.ss ...

Flex items maintaining their size when the window decreases

My goal is to arrange two plotly plots side by side within a CSS flexbox, with the ability to resize them as the window size changes. The issue I'm facing is that while the plots expand correctly when the window is enlarged, they fail to shrink when t ...

utilizing ajax for laravel image uploads

I'm facing an issue with my code. Whenever I click on the update button, the image does not get uploaded to the backend. Although the console displays a success message, the image value returned is null. Can someone please help me troubleshoot this pr ...

Placing a CSS image with absolute positioning on top of a responsive image

Is there a way to keep the balloon image position fixed in relation to the grid image when resizing it? The balloons Balloon1 and B2 are technically within grid 5 and 7, but if you resize the grid left or right, the balloons will go off-center. Do I requ ...

Is there a simple method to extract all components from an SVG document and insert them into an HTML text box?

Looking to provide users with the option to manually edit an SVG document. Is there a direct way to utilize DOM to extract all elements from an SVG document and place them in a text box? Attempted using innerHTML, but it doesn't work for SVG. Is the ...

How to Position an Input Text Beside an Icon Image Using JqueryMobile

Just starting out with jquery mobile and css! Check out my jsfiddle.net/mapsv3/kWJRw/1/, when I use an icon on the left side, the input text gets cut off and I can't see the end of it. ...

The functionality of the Bootstrap 4 Carousel featuring multiple items is experiencing malfunctions

I'm encountering an issue with my Bootstrap 4 card carousel. When the next or prev buttons are clicked, there is a strange transition effect. Additionally, in the mobile version, the buttons do not work properly. It seems that when the card slides to ...

The CSS styling for the <body> element is not rendering the expected results

website <!DOCTYPE html> <html> <head> <title>Time Tracker</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link ...

Navigate through the seamless elements with scroll snapping

I need to find a way to make horizontal scrolling on my really wide graph snap to specific points along the x-axis. I initially tried using scroll-snap-points-x, but unfortunately it doesn't seem to be supported. My attempt to add invisible elements ...

Attempting to use tabs within a tab does not function correctly

Visit the CodePen link here This is the codepen example. <div class="row"> <div class="col s12"> <ul class="tabs2"> <li class="tab col s3"><a href="#test1">Test 1</a></li> <li class=" ...

The amazing property of AngularJS - Scope

I have saved this HTML code in a file. Here is the HTML : <!-- checkbox 1 --> <input type="checkbox" name="checkbox1" id="checkbox-group1" ng-model="checkbox1" value="group1"> <input type="checkbox" name="checkbox1" id="checkbox-group2" ng ...

Python: parsing comments in a cascading style sheet document

I am currently working on extracting the first comment block from a CSS file. Specifically, I am looking for comments that follow this pattern: /* author : name uri : link etc */ and I want to exclude any other comments present in the file, such as: /* ...

Creating an array using Vue.js

When I initialize a Vue data array like this [0: Object, 2: Object];, the console log in Vue shows the array as [0: Object, 1: undefined 2: Object];. This causes issues when iterating through with 'v-for="cell in row.cells"' as it results in tryi ...