The child div disregarding the usage of Bottom and Top based on its parent div

Take a look at this code snippet on jsfiddle: https://jsfiddle.net/mcgfhL10/

#description{ /*A brief description of the product. */
    font-family:"microsoft sans serif";
    font-size:22px;
    position:relative;
    overflow-y:scroll;
    bottom:40px;
    width:400px;
    height:150px;
    background:black;
}

I want the div with id description to be positioned at the bottom of the div with id shelf. However, it currently aligns itself at the bottom of the image instead. Why is this happening since they are siblings?

Answer №1

Your code is experiencing an issue because the description is positioned relatively while the parent has no positioning set at all. This causes the description to calculate its position relative to the closest element (the image) instead of recognizing that it belongs inside the shelf. To resolve this, you need to make the child absolute and the parent relative. If you simply position a child element relative and then set its bottom to 0px, it will not stay in place. By setting the child to be absolute, you are essentially telling it not to move from its current position. Here's a helpful demo for reference: jsFiddle Demo

.shelf {
    position: relative;
}
.description {
    position: absolute;
}
I recommend taking some time to learn more about CSS positioning and display before proceeding further. Understanding these fundamentals will greatly benefit your development skills.
CSS Tricks
A List Apart

Answer №2

In order for your description to be properly positioned, it should have a CSS property of absolute positioning within the parent container, rather than being relative to other elements. Additionally, make sure to place your description div above the image in the HTML structure.

Your updated HTML code should look like this:

<div id="description"><p>foobar</p></div>
<img id="image" src = "image.jpg">

And don't forget to add the following CSS:

#description{
  position: absolute;
}

You can view an example on JSFiddle here: https://jsfiddle.net/jgghpcmy/1/

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

Adjusting position in relation to the cursor's movements

I have searched extensively for similar questions but have been unable to resolve my issue. Here, I am sharing just a single list item from my code. The task at hand is to create a span element on the mousemove event over the list item, and dynamically set ...

Mastering the art of gracefully transitioning to a fallback option when CSS grid

When it comes to creating a simple grid of squares (3x3 at most, filling in top to bottom left to right), I have encountered challenges with both flexbox and grid. The DOM structure required for each method seems incompatible or too complex to support both ...

Interactive game created using JavaScript input

I've scoured the internet for guidance on creating a text-based game that utilizes user input, but all I come across are tutorials focusing on button interactions. What I envision is triggering different responses based on specific user inputs. For i ...

Displaying pictures under specific conditions

I've recently completed the coding and CSS for my website after working on it for quite some time. Now, I want to enhance it by incorporating new features. The website is fully operational and generates revenue primarily through Google AdSense. I am i ...

What is the purpose of using "javascript:function()" in the href attribute of an anchor tag to invoke a JavaScript function?

I'm trying to understand why I need to call a JavaScript function inside the href attribute as href="javascript:functionname()" rather than just using href="functionname()". Here's my code snippet where I have a JavaScript function that redirects ...

Conceal a list of items within a div that has a particular class

This is a sample of my HTML code: <div class="row"> <div class="col-md-12"> <div class="scrollbox list"> <ul class="list-unstyled"> <li id="articulate.flute">articulate flut ...

Ways to incorporate a while loop into a randomizing function

I've been diving into JavaScript and managed to create a website that displays random gifs when clicked from an array. Now, my goal is to implement a while loop to prevent duplicate images from being shown consecutively. However, I seem to be facing ...

What strategies can be implemented to avoid the delay in loading background content on a website?

Currently, I am working on a website built in aspx.net using the Microsoft Web Developer 2010 Express application. My goal is to create a dynamic background that changes as users scroll down the page and reverts back to the previous image when scrolling up ...

Enhancing server-generated content with AngularJS

Is there a way to seamlessly integrate ng-repeat with static content? In other words, is it possible to send static divs and then bind them to a JavaScript array (or create an array from the content and bind it later)? I understand that one solution could ...

How can I use jQuery to reload the page only at certain intervals?

I'm currently using jQuery to reload a page with the code provided below: <script type="text/javascript"> $(document).ready(function(){ setInterval(function() { window.location.reload(); }, 10000); }) & ...

I want to adjust the size of a <div> element as an image is being resized using Bootstrap's "img-responsive" class

When adjusting the browser size, the images are resized which is great because it's exactly what I intended. However, the lower part of the black area does not resize accordingly. This results in a black area appearing under the images when viewed on ...

Create a CSS design that features a top border tip

I have been attempting to create a CSS tip. While I have had some success, the issue arises when the width of the DIV changes, causing the tip to sometimes not be centered. My Desired Outcome: The Code I Have Implemented: .logo { color: #000 ...

Is it possible to dynamically convert percentages to pixels in real-time?

I am working on a project with a fluid percentage-based horizontal grid and I want to have the same vertical margins as my horizontal ones. This is the code I have: .block_12_24{ display:inline-block; vertical-align:top; width:48%; margin-right:2%; margi ...

Incorporating HTML coding into SharePoint Online

Can HTML codes be added to SharePoint Online? I am interested in adding an HTML code from Statista, like the one below: <a href="https://www.statista.com/statistics/262861/uk-brent-crude-oil-monthly-price-development/" rel="nofollow"><img src=" ...

Exploring the functionalities of jQuery and Local Storage: Understanding the Selector's various states

I am currently developing a new feature for font customization on a website. The drop-down menu offers several font options to choose from. When a user clicks on one of these options, the following code is executed: jQuery(function($) { if (localStorage.g ...

JQuery .hover triggering unexpectedly without actually hovering

My current code is causing $('#intro').fadeOut to activate even when I am not hovering on $('arena').hover. Are there any other issues in the code that would prevent it from working correctly? $(document).ready(function() { $(&apos ...

"Using jQuery to trigger a click function on elements with the same

Whenever I click the comment link all of the divs are opening but i need that particular only to be opened. HTML: <div> <a href="#" class="ck">comment</a> <div class="cmt"> <input type="text" class="txt"/> ...

distinguish different designs for individual components in Angular 5

My project is divided into two main parts: one for public web pages and the other for an admin control panel. Each part has its own set of CSS and javascript files for custom templates. If I include all CSS and js files in index.html, they all load at the ...

What is the method for encoding and decoding a query string within an HTML object tag?

Within my code, I am utilizing an object tag that utilizes a data attribute to call a servlet. When passing a parameter to the URL, if the value is not in English but instead Arabic, for example, the value appears distorted when retrieved in the specified ...

Varying heights based on the screen size

Currently, I am in the process of designing my website and incorporating some wave elements to enhance the background. However, I've encountered some issues when resizing the screen. Specifically, the waves seem to shift with a space between them as t ...