Utilize CSS to set a fixed position for a div based on its height

Hey there!

Take a look at this piece of code:

<div className="box-and-clock">
                    
    {isBoxShown && (
        <div className="box">
            {value}
        </div>
    )}
    
    <img
        onMouseOver={() => setIsBoxShown(true)}
        onMouseOut={() => setIsBoxShown(false)}
        src={clockIcon}/>
</div>

This is the layout I have (trimmed down for simplicity)

https://i.sstatic.net/GGjTy.png

When the user hovers over the clock icon, they see some data. However, when there is too much data in the black box, the arrow (created using pseudoelements) doesn't align with the clock icon anymore, leading to something like this

https://i.sstatic.net/BMqfb.png

I'm trying to position that black box relative to the clock icon without being affected by its height.

I attempted using display: table on the parent div and display: table-cell on the child, but it didn't work. Any suggestions?

Here's my CSS:

.box {
    width: 200px;
    min-height: 130px;
    height: auto;
    background: black;
    border-radius: 2px;
    position: absolute;
    top: 320px;
    right: 228px;
}

.box-and-clock {
    display: flex;
    flex-direction: column;
}

Answer №1

One helpful way to position child elements is by setting the parent element's CSS property to position: relative;. This allows children elements to use position: absolute; in order to be placed relative to their parent's position.


.box {
  width: 200px;
  min-height: 130px;
  /* height: auto; */
  background: black;
  border-radius: 2px;
  /* top: 320px; */
  /* right: 228px; */

  position: absolute;
  top: 0px;
  transform: translateY(-100%); /* offset element */
}

.box-and-clock {
  display: flex;
  flex-direction: column;

  position: relative; /* you need this! */
}

For a more detailed explanation of CSS positioning, consider searching for: css position property

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

ZeroClipboard intricate CSS issue

I have a list of images with an option box that appears on mouseover. The box contains an embedded code input field for copying purposes. I recently implemented zeroclipboard to enable the copy function on click. However, when I hover over an image and try ...

Having trouble activating the Samsung Tizen TV through the designated APIs

I currently have a Tizen Application for managing TV Operations such as Volume Controls and Power On/Off. I have implemented the use of b2bapis for Power Off functionalities, but I am struggling to locate comprehensive documentation on the same. The code s ...

What is the reason that certain CSS pages are not being utilized on my website?

I am facing issues with rendering my website on a tablet device, specifically with my last 2 css pages (800px.css and 800pxland.css). Despite working fine just hours ago, now they seem to be neglected while all the other ones work perfectly. I have tried ...

Having trouble with selecting JS dropdown options in Python Selenium

My HTML view- Check out the website design HTML code- View the HTML code here When I click on the dropdown arrow, a new code appears - view the code snippet Although my code successfully displays the dropdown options, it does not allow for selecting an ...

Step-by-step guide on achieving rounded borders with a gap:

Struggling to design blocks and elements with rounded corners that have a gap between the corner and the straight line. It's proving to be quite challenging :) Additionally, I need to create buttons that look like this, and If CSS alone doesn' ...

Make the element switch from hidden to visible when hovering over it using CSS

I had a challenge to only show some rating text on my website when I hovered over it. Being new to coding with CSS and JavaScript, I was experimenting with overriding styles using the Stylebot extension in Chrome. I encountered an issue while trying to mo ...

What is the best way to distinguish between the paths for administrators and regular users?

Currently, I am in the process of setting up routes using node js for a dashboard. Firstly, users will need to log in to obtain a JWT token. With the Token, users can access various user-related routes such as editing, deleting, and logging out. However, ...

How can I collapse the dropdown menu in typeahead.js?

I am currently utilizing typeahead.js for a typeahead functionality. My goal is to achieve the opposite of what was discussed in this thread: Programmatically triggering typeahead.js result display Despite attempting to trigger a blur event on the typeah ...

troubleshooting color problem with video textures in three.js

There seems to be a slight variation in the colors of my video texture compared to the original video. I've experimented with different three.js encoding options, but I still notice this discrepancy. Does anyone have any tips on how to avoid this is ...

Dropdown menu for selecting state in multiple countries built with React JS

In my project, I'm attempting to create a dropdown menu that lists multiple countries along with their associated states by mapping over an array. The sample data from the array is shown below: [ { "country" : "USA", ...

The values being utilized in my React app are coming from the .env file instead of the .env.local file

The dotenv module is not prioritizing my .env.local file over my .env file as expected. Despite having the same environment variable, REACT_APP_API_BASE, defined in both files, the app consistently uses the value from .env. It only switches to using the va ...

Dealing with errors in external URLs with AngularJS: A guide to intercepting errors in $resource or $http services

I have set up a separate server for my angularjs app, which is different from my grunt server. This external server runs on Apache and uses PHP to serve JSON data. I want to be able to catch any potential server errors, ranging from "server down" to "404". ...

Images and CSS are functioning properly when viewed locally, but are not displaying as expected on GitHub pages

There seems to be some issues with transferring my website from a custom domain on Hover. When I try to import it to anthematics.github.io, only the HTML loads while images and CSS are missing. <link rel="stylesheet" href="theme.css" type="text/css"> ...

Tips for choosing a specific cell in a table

I currently have a total of 14 cells that have been generated. Is there a way for me to individually select and manipulate a specific cell out of the 14? I am looking to make only one cell unique, while leaving the rest to be displayed as they are in the ...

What is causing this code to malfunction?

Currently delving into the world of Ajax, I've been following a tutorial and have crafted the script below: <!DOCTYPE html> <html> <head> <script type="text/javascript"> function MyFunction(){ var xmlhttp; if(windo ...

Tips for creating a full screen div layout with fixed top and bottom navigation bars

Can someone help me achieve the following layout style? +-----------------------------------------------------------+ | Top Sticky Nav | +--------------------------------------------------------+--+ | ...

What is the best way to dynamically add the 'required' attribute to an input field?

My responsibility was to dynamically add required fields to all elements on each state that the user selected as required. In my database, I have a table containing the input ID (each input has a unique ID) and a boolean field indicating whether or not a r ...

Arranging a group of objects in Angular2

I am facing challenges with organizing an array of objects in Angular2. The structure of the object is as follows: [ { "name": "t10", "ts": 1476778297100, "value": "32.339264", "xid": "DP_049908" }, { "name": "t17", "ts": 14 ...

Vue: Optimizing JSON response filtering

I am struggling with filtering a JSON response using Vue. return this.offers.filter(type => type.offers == 'Junior'); When I keep it as return this.offers, the following is displayed in my HTML: {"-MN5agCddYAdy7c8GSSz": { "comp ...

Print-ready guide to CSS and HTML

If I were to embark on the ambitious task of writing a book solely using HTML and CSS, where would I begin in defining a page header and footer with page numbers? How can I ensure that page breaks and margins are displayed in the browser, akin to a preview ...