Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

css absolute position + count element above

Is possible set position of absolute element with count height of element above? The goal is get elements with class "pgafu-post-categories" to one line above H2, when is different length.

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>

Most simple would be move <div class="pgafu-post-categories"> above <h2> in HTML, but it is not possible edit HTML code. Is there a way to do this with css?

Edit: Maybe there is possible also use jquery/javascript to count height of h2 element and then set position to absolute element?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use display: flex; and the property order: 1; on the children. If you want it to only apply to a specific column you can use the class column--move-category on the column in my example. Hope this helps.

.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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading