CSS positioning with absolute value + determine the number of elements positioned above

Is it feasible to adjust the position of an absolutely positioned element based on the height of the element above it? The objective is to align elements with the class

"pgafu-post-categories"
one line above an H2 heading, even when the lengths vary.

.pgafu-post-categories {
  position: absolute;
  top: -82px;
  width: fit-content;
  white-space: nowrap;
  padding: 4px 16px;
  font-size: 14px;
}
<div class="row">
  <div class="column">
    <div class="image-wrapper"></div>
    <h2>Small title</h2>
    <div class="pgafu-post-categories">category</div>
  </div>

  <div class="column">
    <div class="image-wrapper"></div>
    <h2>Long two <br> lines title</h2>
    <div class="pgafu-post-categories">category</div>
  </div>
  <div>

The simplest solution would involve moving the

<div class="pgafu-post-categories">
above the <h2> in the HTML code, but editing the HTML code is not an option. Is there a CSS-based approach to achieve this?

Edit: Perhaps it's also viable to use jQuery or JavaScript to calculate the height of the H2 element and then adjust the position of the absolutely positioned element accordingly?

Answer №1

If you want to rearrange the order of elements using CSS, you can utilize the display: flex; property along with order: 1; on the child elements. To target a specific column, you can apply the class column--move-category. Check out the example code below for more clarity.

.column {
  display: flex;
  flex-direction: column;
}

.column h2 {
  order: 2;
}

.pgafu-post-categories {
  order: 0;
}

.column--move-category h2 {
  order: 2;
}

.column--move-category .pgafu-post-categories {
  order: 0;
}
<div class="row">
  <div class="column">
    <div class="image-wrapper">image</div>
    <h2>Small title</h2>
    <div class="pgafu-post-categories">category</div>
  </div>

  <div class="column column--move-category">
    <div class="image-wrapper"></div>
    <h2>Long two <br> lines title</h2>
    <div class="pgafu-post-categories">category</div>
  </div>
  <div>

Answer №2

Implementing flexbox:

.container {
  display: flex;
  flex-direction: column;
}

.item1 {
  order: 2;
}

.item2 {
  order: 1;
}
<div class="container">
  <p class="item1">Item 1</p>
  <p class="item2">Item 2</p>
</div>

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

Is it feasible to generate emojis using a gulp Icon-font task?

I'm in the process of creating a script that can generate fonts from SVG images, and so far it's been successful (using similar code to what's provided on https://www.npmjs.com/package/gulp-iconfont) Now I have a more specific question - is ...

Ways to update the cart page automatically after quantity changes in Shopify

I have made some updates to the Approach and now I am using JavaScript. I've also updated the script and its logic, which is pasted below. Please take a look and see if you can assist me. I am trying to display information on the top of the cart page ...

Identify the moment when the SPAN element reappears in sight

I have a question that may have been asked before, but I haven't found a satisfactory answer yet. Within my HTML code, I have a SPAN element with an onclick event, like so: <span onclick='loadDiv()'>Text</span> When a user cli ...

Utilizing radio buttons for displaying or hiding a div element, along with implementing validation using jqueryvalidation.org

I am currently working on a functionality where I need to toggle the visibility of specific divs based on user input. Additionally, I only want to validate the fields within the visible div and not the hidden ones. If the user selects "Yes" in the radi ...

Angular can be used to compare two arrays and display the matching values in a table

Having two arrays of objects, I attempted to compare them and display the matching values in a table. While looping through both arrays and comparing them by Id, I was able to find three matches. However, when trying to display these values in a table, onl ...

What could be causing my HTML form elements to shift position when I click on them in Internet Explorer 8?

I'm facing an issue with my HTML form that has multiple input fields (text and select types) arranged in pairs on each row. Surprisingly, everything works fine in all browsers, including IE7. However, when it comes to IE8, I encounter a peculiar probl ...

The Symfony API failed to generate a response

There seems to be a problem when trying to link the Symfony API with a React application. The API is not providing any response, even though it works fine when accessed directly through the link. ApiDirectURL Fetching this data from the React app is yiel ...

Switch between viewing outcomes retrieved from a database

I'm fairly new to working with jQuery, PHP and databases, but I have managed to successfully create a database and retrieve data using PHP. When a search term is entered, the retrieved data from the database is displayed on the results page. For exam ...

Acquiring div elements from a collection of divs using selenium in Java

Currently, I am attempting to retrieve a list of all elements using the XPath below. Unfortunately, no matter what I try (class, xpath, etc.), I'm always getting a result size of 1. My objective is to search for records and then loop through them, but ...

Node.js - Headers cannot be sent again once they have been sent

I came across a similar issue to mine at this link. I suspect that the error is occurring due to how my .end() calls are configured. Let's take a look at the code in question: app.get('/anihome',function(req,res){ var context = {}; functi ...

Managing all mouse interactions simultaneously in ReactJS

I've successfully created a button: <button className='collapse-button' onClick={() => {setTopNavigationBarCollapse(!topNavigationBarCollapse)}}>&#9776;</button> Now, I'm wondering if there's a way to call the s ...

Using jquery to update a link when a different link is clicked

Recently, I've started using JQuery and encountered a challenge. When I click on the first link, an Ajax request is sent to the server and data is received successfully. However, in the callback function, I'm struggling to change the display text ...

Opacity of absolutely positioned elements

I am currently working on creating a popup box that will gray out the surrounding area. The problem I'm facing is that the opacity of the shadow div seems to be overriding that of the popup. I have tried changing the position from absolute to fixed an ...

The invocation of $.ajax does not work as a function

I'm encountering an issue when running npm start and trying to access the browser, I keep getting this error message in the stack trace. Error: $.ajax is not functioning properly at findLocation (G:\CodeBase\ExpressApp\routes\ind ...

Combine es6 imports from the identical module using an Eslint rule or plugin

Looking to consolidate my ES6 imports from a single module into one for my React project. For example: import { Title } from "@mantine/core"; import { Center } from "@mantine/core"; import { Divider } from "@mantine/core"; T ...

When running the `vue-cli-service test:unit` command, an error involving an "Unexpected token" message related to the usage of the spread operator

Within my code, I am utilizing the destructuring operator. However, during the module build phase, I encountered an "Unexpected token" error. Any suggestions on how to resolve this issue without completely rewriting my code to avoid using the destructuring ...

Adjusting the height of the Tinymce Editor in React Grid Layout after the initial initialization

I am facing a challenge with my React Component that displays a tinymce editor. The task is to dynamically adjust the height of the editor after it has been initialized. To achieve this, I am utilizing the "React Grid Layout" package for resizing the compo ...

Trigger a function when clicking outside the element, similar to how a Bootstrap modal is closed

I have successfully created a popup box using angular in my project. It appears when I click on an icon and disappears when I click on the close button. However, I would like it to also close if someone clicks outside of the popup. Can anyone help me with ...

Utilize a React function to incorporate an external link

Looking to create a link to Twitter using the href attribute but encountering errors with the atag. Is there an alternative method I could use? In essence, I want to have a picture on my homepage that, when clicked, redirects the user to Twitter. I' ...

Serialize all form fields using JQuery

I'm currently utilizing the following code snippet: var data = $(":input,:submit", this).serializeArray(); This code successfully captures form data for the specified elements. However, I am now facing an issue with retrieving selected entries from ...