Unusual occurrences with border placement within content

Take a look at the following:

Fiddle1 Fiddle2 HTML:

<div class="container">
  <div class="content">
    <div class="space">
    </div>  
    <div class="item">
      DIV1
    </div>
  </div>
</div>

CSS:

.container {
  position:relative;
  height: 100%;
  width:100%;
}
.content {
  height:100%;
  border:1px solid red;
}
.space {
  margin-top:80px;
 border:1px solid blue;
}

.item {
  position: absolute;
  top:80px;
  left:50px;
  font-size:20px;
  font-weight:bold;
  color: #999999;
  text-align: center;
}

The only change is in the border statement in the content class but there is a difference in the output.

Could you explain what is going on?

Answer №1

This issue is related to margin collapsing. The presence of the border prevents the top margin of the .space from collapsing. In contrast, if the border is removed, the margin collapses with all its ancestors including the body, leading to a downward shift of 80px.

Answer №2

.item is being positioned absolutely with a top value of 80px relative to the .container div. When an element is set to position absolute, it will be placed based on its nearest parent element that has a defined position, whether it's relative or the body element. In this scenario, the topmost parent element, .container, has a position of relative, so the top: 80px for the .item element will be calculated from the top of the .container. Since the .item element is absolutely positioned, it will remain contained within its parent with a position of relative or the body element, making the top most element in this case the .container.

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 could be the reason for my application not utilizing its bootstrap classes effectively?

Just starting out with Bootstrap and have made some progress. However, the bootstrap classes I'm using are not rendering properly. What could be the issue? <div class="position-absolute bottom-0 end-0 border border-dark"> https://i.ss ...

I've developed a JQuery function that dynamically changes the active state on hover. My goal is to automate this process in order to streamline the workflow and

My JQuery code is quite simple and it controls the hiding/unhiding of an element depending on which tab is hovered over: HTML: <div class="row col-sm-4"> <ul class="text-center"> <li><p class="chat-provider-tab tab" ...

Invoke a PHP function from a class located in a separate file using an HTML button

I have an HTML input form with a SAVE button at the end. I want the SAVE button to trigger a PHP function written in a separate class file. The PHP code at the beginning of the file nabava_vnos.php that contains the HTML form: <?php // Script for sta ...

selecting unique elements using classes is not possible with jquery

I am experimenting with using ajax on dynamically generated html elements from django. I have tried selecting the instances by class, but for some reason, it is not working as expected. Can anyone point out my mistake here? javascript $(document).ready(fu ...

Ways to turn off the dragging animation for cdkDrag?

I am facing an issue with cdkDrag where a small preview of the item being dragged shows up. I want to disable this feature and remove any CSS classes related to the dragging state. Can anyone guide me on how to achieve this? https://i.sstatic.net/nYU07.pn ...

Navigate to a different page after completing form submission

I am facing an issue with navigating between pages on my website. I have a form that processes data upon submission, and after processing, I want to redirect the user back to the previous page. Currently, my webpage consists of a list of links that lead to ...

What is a way to include a ":hover" effect in Elm without relying on signals?

I'm looking to apply a :hover style to an element using Elm's HTML library. I've considered using Signals and Sets to manage selected nodes, but it feels like overkill for such a simple task. Another option is adding an external stylesheet, ...

Modifying the cursor appearance during object dragging is a simple task that can enhance

html .container, .container * { cursor: url('img/arrowPointer.png'), auto; } javascript $('html').on('dragstart', function () { $('html').addClass('container'); }); $('html').on('d ...

JavaScript Evaluation Test Outcome

(Apologies, I am still in the process of learning JavaScript) My goal is to create a checklist quiz that provides different outcomes based on the number of checkboxes selected by the user. There are a total of 7 checkboxes, and if the user selects 1 or few ...

Tips for effectively redirecting traffic to another website

Is there a way to redirect someone from a page URL like example.com/2458/233 to example2.com/2458/233? The first URL contains only a file that redirects to the other domain. Can anybody provide instructions on how to achieve this process? To clarify furt ...

What is the best way to insert hyperlinks within every cell of a table using Angular?

I am currently working on a table created with ng-repeat in Angular. The cells are populated by variables in the scope. <tbody> <tr ng-repeat="item in items" myDirective> <td>{{item.title}}</td> <td>{{item.field}}&l ...

Guide on utilizing the latest insertCSS and removeCSS features in manifest version 3

Need help with using the new insertCSS and removeCSS in manifest v3. The documentation is not very clear and lacks examples. I have a CSS file that I need to inject into a page and then remove it. The code looks something like this: background.js documen ...

PC media query not functioning as expected

At first, my media query was working perfectly fine. However, it suddenly stopped working. Interestingly, when I use the browser's developer tool, the media query functions correctly. https://i.sstatic.net/w8AMQ.png https://i.sstatic.net/Srnsi.png E ...

Step-by-step guide on integrating node.js and MySQL to store data from an online form in a database

Currently, I am attempting to insert data into a MySQL database using node.js by clicking the submit button. However, an error message has appeared and despite understanding it somewhat, I am unsure of how to proceed. Any assistance would be greatly apprec ...

Unlocking the attributes of Angular form elements: A guide

Imagine we have a form like this <form name="myForm" data-ng-controller="Ctrl"> <input name="input" data-ng-model="userType" data-description="User Type" required> </form> When working in the controller, we can refer to the input el ...

PHP authentication using $_SESSION variable

I am currently developing a login form with sessions and I need help with my index.php page code: <?php include "../config/MainClass.php"; include "../connection.php"; session_start(); if (empty($_SESSION['admin& ...

performing a GET request directly from a <script> element in the index.html file

I am currently developing an application that utilizes Angular.js on the client side and Node Express on the server side. On my server side, I am using a staticAsset module. Within my index.html file, I have a lengthy list of JavaScript files that need t ...

Dynamic Sizing of Elements + Adaptive Text Overlay

There were 2 obstacles I encountered when trying to create an image-based drop-down menu : No matter how much effort I put in, I couldn't figure out how to make a flexed element inside a container maintain the same height as the adjacent element. [ ...

Creating a Triangle Slider Component with React and Material-UI (MUI)

Struggling with creating a price control using react js and MUI, similar to the image below: https://i.stack.imgur.com/ZP8oc.png I've searched online for libraries or solutions, but haven't found anything that works. ...

Enter the Kannada language into the HTML text box or input field

My html form consists of about 15 - 20 input and textarea fields. As a user, how can I enter information in Kannda or any other local language? https://i.stack.imgur.com/60PVT.png ...