What causes my child element to be positioned with respect to the body even though its parent is set to relative positioning?

Here is the HTML code I am working with:

<html>
<head></head>
<body>
<div id="board">
<div class="person"></div>

</div>
</body>
</html>

And here is the CSS code:

*{
margin:0;
padding:0;
box-sizing:border-box;
}
#board{
position:relative;
height:500px;
width:500px;
background:yellow;
top:5rem;
left:8rem;

}
.person{
position:absolute;
top:34rem;
left:20px;
padding:1rem;
background-color:blue;
}

My question concerns the positioning of the div with the class .person in relation to the div with the id #board. I expect the .person div to be positioned absolutely to its parent element #board since the parent is positioned relatively. However, even with a significant top value for .person, it does not stay within the boundaries of the parent div. Why is this happening? Any insights or feedback would be greatly appreciated.

Answer №1

Upon reviewing your code, it appears to be functioning correctly. The issue lies in the .person class where you have specified top:34rem;

If you adjust the .person class to top:0rem; and modify the #board element's top:#rem to a different rem value, you will observe the .person element moving along with the #board

Furthermore, utilizing absolute positioning removes the element from the typical document flow, allowing you to position it anywhere on the webpage. Negative values are also effective. It is essential to note that the element searches for the first non-static element (the default position for elements) as a reference point, enabling you to use it as a marker instead of the window itself. If you omit the relative positioning on the #person and have no other non-static elements surrounding it, it will resort to the outermost element and essentially use the webpage as the reference for its initial placement. By including relative, it initiates its absolute positioning from that point, as it is the first non-static element. Despite this, you can still relocate it anywhere desired; it merely establishes its starting position from there.

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

The CSS style of the Div element is not being displayed properly when embedded with JavaScript

Currently, I am working on a simple page for practice purposes. The main issue I am facing is with a div element that has a red border and a blue background. Inside the div, there is a script tag calling an external JavaScript file. Surprisingly, the JavaS ...

Failure of default option to appear in dropdown menu in Angular

I currently have a dropdown list in my HTML setup like this: <select id="universitySel" ng-model="universityValue" ng-options="university._id for university in universities"> <option value="-1">Choose university</option> ...

Don't forget to expand or collapse

I'm struggling to make this work. My goal is to initially display 50 characters and show the rest when the "read more" button is clicked, and vice versa with "read less." Another problem I've noticed is that when I click the back browser button, ...

Bootstrap 4: Arrange input fields in a horizontal layout

I'm a newbie when it comes to Bootstrap 4 and could use some guidance. I'm looking to have my first two input fields aligned horizontally on the same line, instead of stacked on top of each other. Something like this: check out the image here B ...

How to give a stylish touch to your :after element with a css

I have a unique bubble design that pops up when hovering. I'm trying to add a border around the bubble, but I'm facing difficulty adding a border to the pointer of the bubble. The arrow is created using the .bubble:after selector. You can check ...

What is preventing me from setting the height of a span to 0 pixels?

It seems that when the height is set to 0px, the element doesn't visually shrink... <div id="bg"> <div id="animate"><span>WINNER ALERT! Click here to get a million dollars!!!</span></div> </div> #bg { back ...

Numerous Selection Boxes

I came across this code snippet on the web. How can I insert the output into an email? Usually, I follow this format: $name = $_POST['name']; $email_body = "$name"; I want to incorporate the output of this code into my $email_body variable. Is ...

Achieving compatibility with pseudo elements :before/:after in Internet Explorer 7

I've been attempting to make these functionalities compatible with IE 7, but I'm encountering difficulties. I've downloaded and included the jQuery plugin for it in the header as shown below: <!--[if lte IE 7]> <script type="text/j ...

Build a photo carousel similar to a YouTube channel

Is there a way to create an image slider similar to the one on YouTube channels? I've noticed that when there are multiple videos on a channel, YouTube displays them in a slider with back and forth buttons to navigate through the videos that aren&apos ...

Retain the jQuery dropdown menu in an open state while navigating to a different webpage on the

I am encountering an issue with a drop-down menu on my website. Whenever I click on a submenu link, the new page opens but the menu automatically closes. However, I want the active menu to remain open even on the new page. To solve this problem, I believe ...

Problems with CSS animations on mobile devices in WordPress

On my website powered by Elementor Pro, I have implemented custom CSS to animate the Shape Divider with a "Brush Wave" style. Below is the code snippet: .elementor-shape-bottom .elementor-shape-top .elementor-shape body { overflow-x:hidden; } @keyframes ...

Enhance each category changing with jQuery page transitions

Is there a way to add page transitions based on category names and quantities in PHP? For example, when clicking on the "office" category, can a popup overlay be displayed with the category name and quantity while the content loads? The focus is on creat ...

Space between an image and a div element

While creating a website in Dreamweaver CC, I noticed a significant gap between my image and a div tag. Here is a screenshot for reference: Below is the code from my index file: <!doctype html> <html> ... </div> Additionally, here is a ...

Modify the color of the header on Material-UI Date Picker

I recently integrated a Material-UI Date Picker into my React Single Page Application and now I'm facing an issue with changing the header color. I attempted to modify it using the muiTheme palette property, but unfortunately, the header color remains ...

Conditionally submit a form using JQuery

I need to implement a validation condition using jQuery for a form submit button. The requirement is that the form should only be submitted if the user enters a valid email address. HTML code : <form method="post" action="{% url sportdub.views.login ...

The default value is not displayed in the Angular dropdown menu

When using regular html select menus, if you create an option element with selected and disabled attributes and provide text for that option, the text will be displayed by default in the select menu. Below is a basic example of HTML code: <select name= ...

Switching iFrame based on dropdown selection

Hello, I'm a beginner in the world of programming and I'm currently experimenting with creating a webpage. However, I've encountered a roadblock and could use some help. My current goal is to change the source of an iFrame to a specific link ...

What is the process for deselecting text from a text field?

After performing a search and displaying the results on my search form, I've noticed that the text query in the textfield is being marked as selected text even though I don't want it to be. How can I prevent this? Fiddle 1) What is the best app ...

Ways to adjust the size of v-btn

Objective: My current task involves enhancing the customization options for Vue components, specifically focusing on adjusting the size of a button component based on user input. Our project utilizes Vuetify. Previous Code: <v-btn @click.prevent=&quo ...

Looking to retrieve a specific data element within Vue.js?

Here's a thought-provoking query for you. Imagine I have some data stored in an element in the form of an array like this: data: { product: socks, variants: [ { variantId: 2234, variantColor: 'Green', varian ...