Using CSS to position elements absolutely while also adjusting the width of the div

In one section of my website, I have a specific div structure. This structure consists of two divs stacked on top of each other. The first div is divided into two parts: one part with a width of 63% and another part with a button.

Beneath the first div, there is another div with the same width (63%) and positioned absolutely.

However, setting the position to absolute does not result in both divs being the same size, even though their widths are identical.

Here is a snippet of the CSS code:

#two{
  border: 1px solid;
  width: 63%;
  position: absolute; //Enabling this results in varying sizes despite having the same width
}

You can view my code pen link here: https://codepen.io/JGSpark/pen/bZyvEV?editors=1100

I am looking for a solution to make both divs the same size when using position absolute. Any suggestions on what I can try?

Answer №1

When you apply position: absolute without specifying a relative element, it will be positioned relative to the root element.

If a 63% textValue is set for #one element, then this percentage refers to 63% of that specific element. However, if the same percentage is applied to #two, it indicates 63% of the whole document including the default margin of the body. In this case, the body margin should be reset to zero:

body {
  margin: 0; /* reset */
}

#template {
  width: 30%;
}

#textValue {
  border: 1px solid red;
  width: 63%;
  float: left;
}

#icon {
  width: 5%;
}

#text {
  width: 95%;
  float: left;
}

#one {
  width: 100%;
}

#two {
  border: 1px solid;
  width: 63%;
  position: absolute;
}
<div id="one" class="row">
  <div id="textValue"><span id="text">ONE Inner text</span><span id="icon"><i class="fa fa-angle-up"></i></span></div>
  <button id="template" class="btn primary">Template</button>
</div>
<div id="two">TWO</div>

Alternatively, you can wrap the element with position: relative in a wrapper - as illustrated in the demo below:

.wrapper {
  position: relative; /* added */
}

#template {
  width: 30%;
}

#textValue {
  border: 1px solid red;
  width: 63%;
  float: left;
}

#icon {
  width: 5%;
}

#text {
  width: 95%;
  float: left;
}

#one {
  width: 100%;
}

#two {
  border: 1px solid;
  width: 63%;
  position: absolute;
}
<div class="wrapper">
  <div id="one" class="row">
    <div id="textValue"><span id="text">ONE Inner text</span><span id="icon "><i class="fa fa-angle-up "></i></span></div>
    <button id="template" class="btn primary ">Template</button>
  </div>
  <div id="two">TWO</div>
</div>

Answer №2

To incorporate a parent div with position:relative, simply include the style attribute "position: relative;" within the opening div tag. Alternatively, you can add position:relative directly to the body tag.

<div style="position: relative;">
<div id="one" class="row">
    <div id="textValue"><span id="text">ONE Inner text</span><span id="icon"><i class="fa fa-angle-up"></i></span></div>    
  <button id="template" class="btn primary">Template</button>
</div>
  <div id="two">TWO</div>
</div>

Answer №3

To ensure that the position absolute is relative to a specific element, it must be relative to something else. In this scenario, it is relative to the document which has default margin and padding. Consider implementing the following solution:

<div class="container">
  <div id="one" class="row">
    <div id="textValue"><span id="text">ONE Inner text</span><span id="icon"><i 
class="fa fa-angle-up"></i></span></div>    
  <button id="template" class="btn primary">Template</button>
</div>

  <div id="two">TWO</div>
</div>

Additionally, include the following CSS:

.container {
  position:relative;
}

Answer №4

Within your specific scenario, the content value inside element #one is calculated at 63%, representing 63% of the total space within #one div.https://example.com/image.png On the other hand, div #two has been assigned an absolute position without a parent element having relative positioning, causing it to be positioned with respect to the larger body element rather than #one div. This difference becomes apparent even when both elements are given the same width.

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

What are the best ways to integrate markdown into my Vue.js projects?

Is there a way to utilize markdown within the context of vue.js instead of regular HTML paragraphs? ...

Building a matrix of checkboxes

I am looking to design a grid of checkboxes that are displayed in columns, with 5 checkboxes in each column. <ul class="checkbox-grid"> <li><input type="checkbox" name="text1" value="value1" /><label for="text1">Text 1</lab ...

The property 'clone' is undefined and cannot be read

Currently, I am using Fullcalendar to update events. My goal is to execute an ajax callback to retrieve the edited version of a specific event. The endpoint for this request should be at /controls/:id/edit. To achieve this functionality, I have implemented ...

Show one class with Jquery, hide the rest - a jsfiddle is linked below for

I'm attempting to create a gallery using this code snippet. It currently requires two separate containers for buttons and content, but I'm wondering if there's a way to combine everything into one container? Additionally, is it possible to ...

Encountering a Jquery 404 error while attempting to locate a PHP file through Ajax requests

I've been struggling with this issue for the past couple of hours, but I just can't seem to get it fixed. Here's my current file setup: classes -html --index.html --front_gallery.php -javascript --gallery.js I've been following a tuto ...

Getting a Next.js error after performing a hard refresh on a page that contains a dynamic query

I have encountered an issue with my Next.js app when I attempt to hard reload it in production mode. The error message I receive is 404 - File or directory not found. Below is the code snippet I am using: import { useRouter } from "next/router"; import ...

Stop users from signing up if the chosen username is already in use

I have a script that successfully checks if a username is available, but even if the username is already taken, the user can still register. I am looking for a way to prevent registration if the username is not free. Here is the code snippet: index.php $ ...

Develop a program in Python using Selenium to automate web scraping in a loop

I am looking to create a loop that will allow me to extract the individual time figures for each horse in all eight races from the at the races website. Here is an example of the first race (17:15) out of the eight: from selenium import webdriver from s ...

Guidance on establishing an Array data type for a field in GraphQL Type declarations

Hey there! Currently, I'm working on my Nodejs project and facing a little dilemma. Specifically, I am trying to specify the type for graphQL in the code snippet below. However, I seem to be struggling with defining a field that should have a Javascri ...

Could you provide suggestions on how to update the content of an HTML tag within a Vue component?

I am new to Vue.js and struggling to grasp its concepts. I am interested in finding a way for my custom Vue component (UnorderedList) to manipulate the content within it. For example, if I have <p> tags inside my component like this : <UnorderedL ...

Having trouble getting Emberjs to properly handle an Ajax POST request

Currently, my goal is to send data to the database using Ajax POST. To start off, I have an HTML form as follows: <script type="text/x-handlebars" id="project"> <div class="row"> <div class="span6"> <form class ...

What is the most foolproof method for detecting when a checkbox has been marked as checked, regardless of the circumstances?

Is there a way to detect changes in the checked value of a checkbox when it is changed by a script that I do not control? I have tried using `addEventListener()` and jQuery's `on()` method, but they do not work in all cases. <input type="checkbox" ...

What is causing the colorful background to not fully cover my page when I make the window smaller?

I'm currently designing a webpage that features a dynamic gradient background. Within this webpage are two columns - one displaying an image of a phone, and the other containing text. In full screen mode, everything looks perfect. The gradient stretch ...

Struggling with the navbar-toggler in Bootstrap 4 Beta 2?

As part of my Bootstrap practice, I have implemented a navbar on my webpage. However, I am facing issues with the nav-bar toggler not working for small screens and the icon navbar-toggler-icon not appearing. Below is my current navbar code: <nav class ...

Issues with loading NextJS videos can occur when accessing a page through a link, as the videos may fail to load without

Issue Greetings. The problem arises when attempting to load muse.ai videos on-page, specifically when accessing the page with a video embedded through a NextJS link. To showcase this issue, I have provided a minimal reproducible example on StackBlitz her ...

Having trouble pinpointing the element with protractor's binding locator

<div class="apiRequestDisplay ng-scope"> <pre class="ng-binding">GET</pre> <pre class="ng-binding">v1/securityprofiles/{securityProfileID} </pre> </div> I am trying to target the specific text within v1/secur ...

Developing web applications using Express.js and Jade involves connecting CSS stylesheets

I'm currently in the process of developing a web application using Express.js along with some Bootstrap templates. I utilized jade2html for conversion, and while the page loads successfully, it lacks any styling. The following code snippet is what I ...

PHP: Communicating Data with JavaScript

What if my PHP script requires some time to complete its operations? How can I keep the client updated on the progress of the operation, such as during a file download where the estimated time and data size need to be communicated? In PHP, calculating all ...

eliminate whitespace characters within a string

Here is a question that suggests an easy way to remove space characters in a string using R. However, I encountered an issue when trying to remove a space between two numbers (e.g. 11 846.4) while working with the following table: require(XML) require(RCur ...

Where is the appropriate location to insert a <script> tag in NextJs?

I am facing a challenge in my NextJs application when trying to include the <script> code. I attempted to add it in a .js file but it did not work as expected. In traditional React applications, we typically use the index.html file to incorporate sc ...