What is the best way to align two absolutely positioned divs next to each other on a mobile device?

Is there a way to position two absolute div's next to each other on mobile devices without any gap between them?

I am looking for a flexible solution that will allow the divs to stay side by side even as the size of the mobile device changes.

Thank you in advance for your help!

#header {
  position: relative;
}

#menu {
  background-color: green;
  position: absolute;
  right: auto;
  left: 0;
  bottom: 0;
  top: 0;
}

#tab {
background-color:pink;
  position: absolute;
  overflow: auto;
  right: 0;
  bottom: 0;
  top: 0;
  z-index: 10;
  padding: 1.2rem;
  width: 70%;
  display: block;
}
<div class="header">
  <div id="menu">menu</div>
  <div id="search"></div>
  <div id="tab">tab</div>
</div>

Answer №1

Getting close! All you needed to do was utilize percentages and calculate the values for the opposite side.

Snippet: https://jsfiddle.net/bnx6ozh4/

<div class="header">
    <div id="menu">menu</div>
    <div id="search"></div>
    <div id="tab">tab</div>
</div>

And here's the CSS portion:

#header {
  position: relative;
}

#menu {
  position: absolute;
  bottom: 0;
  top: 0;
  width: 20%;
  right: 80%;
  background: green;
}

#tab {
  position: absolute;
  overflow: auto;
  right: 0;
  z-index: 10;
  padding: 1.2rem;
  left: 20%;
  background: red;
}

Answer №2

#header {
  position: relative;
}
*{
  box-sizing:border-box;
}

#menu {
  position: absolute;
  right: auto;
  width:50vw;
  height:100vh;
  background:red;
  left: 0;
  bottom: 0;
  top: 0;
}

#tab {
  position: absolute;
  overflow: auto;
  top:0;
  left:50vw;
  z-index: 10;
  padding:1.2rem;
  width: 50vw;
    height:100vh;
  background:yellow;
}
<div class="header">
  <div id="menu">menu</div>
  <div id="tab">tab</div>
</div>

By including box-sizing:border-box, the issue is resolved!

Answer №3

After aligning everything perfectly, I decided to add a background color and adjust the top CSS properties for better visibility. To ensure clarity, I also removed the padding.

#header {
  position: relative;
}

#menu {
  position: absolute;
  right: auto;
  left: 0;
  bottom: 0;
  top: 0;
  width: 50vw;
  height: 100vh;
  background-color: skyblue;
}

#tab {
  position: absolute;
  top: 0;
  left:50vw;
  z-index: 10;
  /*padding: 1.2rem;*/
  width: 50vw;
  height: 100vh;
  background-color: teal;
}
<div class="header">
  <div id="menu">menu</div>
  <div id="search"></div>
  <div id="tab">tab</div>
</div>

Answer №4

To adjust the positioning of an element, it's best to remove any padding and set its top property. Keep in mind that having a fixed width could impact your overall layout.

#header {
  position: relative;
}

.item {
  position: absolute;
  bottom: 0;
  top: 0;
}

#menu {
  right: auto;
  left: 0;
}

#tab {
  overflow: auto;
  right: 0;
  width: 70%;
}
<div class="header">
  <div id="menu" class="item">menu</div>
  <div id="search"></div>
  <div id="tab" class="item">tab</div>
</div>

Answer №5

Simply take out the padding that was set in the id tab, and include top : 0.

#header {
  position: relative;
}

#menu {
  position: absolute;
  right: auto;
  left: 0;
  bottom: 0;
  top: 0;
}

#tab {
  position: absolute;
  overflow: auto;
  right: 0;
  z-index: 10;
  /*padding: 1.2rem;*/
  width: 70%;
  top: 0;
}
<div class="header">
  <div id="menu">menu</div>
  <div id="search"></div>
  <div id="tab">tab</div>
</div>

Answer №6

To achieve the desired result, simply delete the padding and include top 0 for the tab class. Once you make these changes, your task is complete.

#header {
  position: relative;
}

#menu {
  position: absolute;
  right: auto;
  left: 0;
  bottom: 0;
  top: 0;
}

#tab {
    position: absolute;
    overflow: auto;   
    right: 0;  
    top: 0;      
    z-index: 10;
    width: 70%                
}
<div class="header">
  <div id="menu">menu</div>
  <div id="search"></div>
  <div id="tab">tab</div>
</div>

Alternatively, view the fiddle here: https://jsfiddle.net/6qea1os3/

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

Obtain the response body in Nest JS middleware

Currently, I am working on developing logging middleware for my project. My main goal is to log the response body specifically in 500 responses. However, I have encountered an issue where the response body is not present in the Response object when using a ...

Sending data from Node.js to PHP using POST method

Currently, I am attempting to send data from a Node.js application to a PHP script using a POST request. I am in the process of creating a proof of concept, but I am encountering an issue where the data does not seem to be reaching the PHP side. Although t ...

What is the best way to add a button over an image using Tailwind CSS in Next.js?

I've been trying to display a button on top of an image using Tailwind CSS and Next.js, but I'm having trouble getting it to align properly. Here is the code snippet I've been working with: <div> <Image src={projectAiWrite ...

Following a POST request, the redirection functionality in Next.js seems to be malfunctioning

I am facing an issue with redirecting the user after submitting a form. I have a button that triggers a post request to my API route, which then inserts a row in the database. The goal is to redirect the user to / once everything is done. However, the retu ...

What is the best way to eliminate unexplained spacing at the bottom of a webpage using CSS?

I am struggling to create a basic webpage using Bootstrap and D3, and I can't seem to figure out how to remove the excess whitespace at the bottom. Any tips on how to resolve this issue would be greatly appreciated. I attempted to set the min-height ...

Arrange text and a button side by side in a table cell using an Angular directive and an HTML template

I have successfully implemented an Angular search function on my project. You can find it here to allow users to search for courses. The search function uses a read-more directive that displays a preview of the description and keywords before allowing use ...

What is the best way to align an element based on the position of another element?

I have integrated a calendar icon above a fullcalendar, and when I click on the icon, a react-datepicker pops up. Here is my CSS : .react-datepicker { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 0.8rem; background-color: ...

What is the method for utilizing keycodes inside an array to determine which keys are recognized as input in a 'keyup' event listener?

I'm currently working on creating a game similar to Wordle, but I've hit a roadblock when it comes to filtering out only the alphabet keys as valid keyboard inputs for the game. Within my Keyboard container, I have rows of keys and a JavaScript ...

How to use jQuery to retrieve the smallest and largest dates from an object with key-value pairs

Here is an example of my associative array with start and end dates: [{"start_date":"2018-11-20", "end_date":"2019-01-20", "name":"NO Name"}, {"start_date":"2018-10-10", "end_date":"2019-02-14", "name":"Name No"}, {"start_date":"2019-02-15", "end_date": ...

Having trouble converting a JSON array into a JavaScript array upon creation

I have encountered this question multiple times before and despite trying various solutions, I have not been able to find success. Summary: My goal is to retrieve the array from the classes.json file and then assign the data in the variable classes from d ...

Issue with Firebase V9 addDoc: No indication of success or failure, and data is not being written to the

I am encountering an issue where the authentication related functions are working fine, but I am unable to make any progress with Firestore. Despite no errors or successes being returned by the try-catch block, nothing seems to happen in the Firestore data ...

How can I display two slides at once in Ionic 2/3 on a wide screen?

I have been searching for a solution that will allow me to display 1 ion-slide if the device screen is small, but if it's larger, then I want to display 2 ion-slides. Unfortunately, I have not been able to find a suitable solution yet. As an example, ...

Deleting a row from a table when a similar element is found in another location

My webpage features a table list that showcases users linked to an organization: <script type="text/html" id="userListTemplate"> <div id="user_list_box"> <table class="list" style="margin-bottom: 15px;"> {{#Users} ...

"Upon requesting three gltf files, the response was found to

Currently, I am utilizing the GLTF loader for the purpose of importing a custom model into my scene. Within my codebase, there exists a class called Spaceship.js that manages the loading of the model. // Spaceship.js import { GLTFLoader } from 'thr ...

Encountering a Sass Dart error "Bad state: Can't access parent outside of a module" while running npm start in a Create React App

My team has come across an unusual error while attempting to npm start a Create React App we are working on. The error message states: Bad state: Can't access __parent outside of a module, resulting in a failed Build process. This issue is new to us, ...

What is causing the discrepancy in functionality between these two HTML/CSS files with identical code?

In this codepen, you'll find the first example: Subpar Pen: https://codepen.io/anon/pen/jGpxrp Additionally, here is the code for the second example: Excellent Pen: https://codepen.io/anon/pen/QqBmWK?editors=1100 I'm puzzled why the buttons l ...

Rebuild object methods obtained from localStorage

Utilizing JSON.stringify and JSON.parse to save and retrieve objects from localStorage has led to a discovery - JSON.stringify seems to remove the instance functions from the object. This means that once I use JSON.parse, I lose the ability to execute m ...

Bring in a php array to populate a dataset in JavaScript

I have extracted data and organized it into an array within a .php file. Now, I am looking to visualize this data using d3 methods in another .html file. I want to import this array into the dataset for visualization purposes. Below is a snippet of the c ...

Utilize the power of React Context API to manage state within Child Components directly, eliminating the need to pass down functions

In order to update context from a nested component in React, the documentation advises passing the function defined in the Root component as a prop to the Child Component. I have followed this advice by implementing the same in my code: import React from ...

Automatically navigate to a random link on a webpage with the Selenium Python API

Is there a way to use the Selenium API with Python 2.7 to click on a random link on a specific webpage? Any suggestions would be greatly appreciated. Thank you! ...