What is the best way to utilize CSS relative positioning for aligning two rows of information on a webpage?

What is the best way to use relative positioning to show two lines of information on an image without the order of information interfering with each other?

I want the Files badge to appear at the bottom of the image, with the MusicBrainz and Discogs badges above it. Both Files and MusicBrainz should align at the left side of the image. The height is correct, but unfortunately, the badges are displayed from left to right in the order they are added. I'm not sure how to resolve this issue.

Additionally, I'm confused about why the hyperlink below the image is correctly aligned, but doesn't shift over to the right.

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<div style="display:grid;grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));grid-gap: 5px;">
  <div>
    <figure class="figure">
      <a href="FixSongsReport00398_byartistalbum00004_00001.html">
        <img src="../images/aElXsWl4r3Qd8Vr3aSYnHg==_thumbnail.jpg" class="figure-img" width="200" height="200">
      </a>
      <figcaption class="badge badge-success" style="position: relative;left: +1em; top: -5.5em;">
        MusicBrainz
      </figcaption>
      <figcaption class="badge badge-success" style="position: relative;left: +1em; top: -5.5em;">
        Discogs
      </figcaption>
      <figcaption class="badge badge-primary" style="position: relative;left: +1em; top: -3.5em;">
        9 files
      </figcaption>
      <figcaption class="figure-caption" style="position: relative;top: -2em;">
        <a href="FixSongsReport00398_byartistalbum00004_00001.html">
                        Nelson; Reed; Dupré; Gigout; Wills; Widor; Grainger; Karg-Elert; Weinberger: Pomp & Pipes!
                    </a>
      </figcaption>
    </figure>
  </div>
</div>

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

Answer №1

Based on the information provided, I will attempt to address your query. While there may be more refined solutions available, I aim to provide a concise response without delving into the semantics of the elements. It is advisable to utilize classes instead of tag names for the CSS selectors outlined below. My goal is to simplify the answer as much as possible.

Considering that all "figcaption" tags are at the same level, one method to control them independently is by using the following CSS:

figcaption {
  position: absolute;
}

Absolute positioning of elements is in relation to a parent with a specified position. Therefore, a position must be assigned to the figure:

.figure {
  position: relative;
}

By setting the position, you can adjust the badges using the top and left properties as required. An example can be viewed here.

However, a drawback of the previous solution is the manual control needed for the second green badge. To address this issue, consider grouping the badges within a div element:

<div class="badges-line">
  <figcaption...
  <figcaption...
</div>
<div class="badges-line">
  <figcaption...
</div>

Adjust the CSS selectors accordingly after implementing the div classes.

Additionally, an alternative approach is to utilize float (an entirely new suggestion). By increasing the margin of the second green badge, the blue badge will float to the next line when space is insufficient on the right. For a visual demonstration, modify the margin value from 0.2em to 0.4em in this example.

Both solutions may require refinement, and I recommend considering a tag other than figcaption for the badges, as it may not align with semantic correctness (yet this is a separate discussion).

Answer №2

If I have interpreted your message correctly, here is the strategy I would suggest.

  1. Group all the elements with "overlaps" into a single container (div) to simplify your handling of them.
  2. Apply position: absolute to this container to prevent it from affecting the layout of other elements. In essence, while position: relative allows elements to be moved, the browser keeps their original position, causing other elements to flow around them (learn more here). Note that in this scenario, the image's "container" (.figure) should have position: relative added to it.

I have also relocated the link .figure-caption outside of .figure because I believe it is preferable for the image "container" to only contain images and "floating" elements, not "static" ones like links.

Here is my recommendation:

.figure {
  position: relative;
  width: 200px;
}

.figure .badges {
  position: absolute;
  left: 0;
  bottom: 20px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<div style="display:grid;grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));grid-gap: 5px;">
  <div class="item">
    <figure class="figure">
      <a href="FixSongsReport00398_byartistalbum00004_00001.html">
        <img src="../images/aElXsWl4r3Qd8Vr3aSYnHg==_thumbnail.jpg" class="figure-img" width="200" height="200">
      </a>
      <div class="badges">
        <figcaption class="badge badge-success">
          MusicBrainz
        </figcaption>
        <figcaption class="badge badge-success">
          Discogs
        </figcaption>
        <figcaption class="badge badge-primary">
          9 files
        </figcaption>
      </div>
    </figure>
    <figcaption class="figure-caption">
      <a href="FixSongsReport00398_byartistalbum00004_00001.html">
            Nelson; Reed; Dupré; Gigout; Wills; Widor; Grainger; Karg-Elert; Weinberger: Pomp &amp; Pipes!
      </a>
    </figcaption>
  </div>
</div>

https://jsbin.com/godaqal/2/edit?html,css,output

If there are any unclear points, please feel free to let me know.

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

How to perfectly align a full-width div with a div of specific width

Consider this scenario: .d1 { width: 100px; float: left; } .d2 { width: auto; float: left; } <div class="d1">Fixed Content</div> <div class="d2">This content should expand to fill the rest of the page vertically.</div> This setu ...

Error: The Android Phonegap framework encountered an issue when trying to access the getDuration function without a properly initialized media

Encountering a LogCat error that I need assistance with. The web app code functions properly on iOS, but presents an error message on Android. Unable to call getDuration as mediaplayer is invalid Your help is greatly appreciated. ...

Divergent Bootstrap layouts for different devices

https://i.sstatic.net/g1YeJ.png I am working on creating a layout where I want to display certain content differently based on whether the user is viewing it on a mobile or desktop device. The current approach I have taken involves duplicating some divs ...

Something seems to be preventing Div from appearing and there are no error messages appearing

I've been attempting to create a menu, but the toggle div for the menu isn't visible Following a tutorial where an individual sets up a menu, there is a div with a menu icon in the top left corner. Despite meticulously copying the code from the ...

Ways to align a div in relation to a span element?

I have a large block of text within a div. I am seeking a way to create a hover effect on specific words within the text, which will then display a div below the word containing a customized div. This functionality resembles the feature on Wikipedia where ...

The Parallax effect reference hook does not seem to be functioning properly with components in NextJs

I'm attempting to implement the useParallax hook on an element within my JavaScript file. My setup involves NextJs, ReactJs, and styled components. Here is how I integrated the hook: Mainland.js import React, { useEffect, useRef } from 'react&ap ...

What steps should I take to ensure that this website is responsive across all screen sizes?

Struggling with adapting to different screen sizes, especially with Bootstrap. Below are the images for reference: https://i.stack.imgur.com/AlCjA.png https://i.stack.imgur.com/FT2mU.png Sass source code snippet: body background: $main-background ...

The variable in my scope is not reflecting the changes in the HTML view

Here is my code for handling file attachments in AngularJS: $scope.attachments = []; $scope.uploadFile = function(files){ for(var i=0; i<files.length; i++){ $scope.attachments.push(files[i]); console.log($scope.attachments.length); } } ...

Ways to stop custom Mailchimp form from redirecting upon submission

I have integrated a custom Mailchimp signup form into a webpage I am developing. The form works well, but I would prefer to avoid the automatic redirect to a new tab upon submission, which displays a confirmation message from Mailchimp about joining the ma ...

Updating divs automatically using Ajax without refreshing the page is not functioning as intended

I'm having trouble understanding why this isn't functioning properly. My goal is to have the data from load.php displayed in the div sample. However, if the code is updated (for example, when pulling information from a database), I want only the ...

Tips for adjusting the wrapper css rule of a Tabs component to left-align text in a vertical tab within Material-UI

Typically, when text is placed inside the wrapper, it is aligned in the center. Is there a way to adjust the wrapper rule in <span class="MuiTab-wrapper">Item One</span> so that the tab text is aligned to the left (similar to what w ...

Is there a way to prevent all attribute menu items within a ul/li List from being clicked when one of them has already been selected

When I quickly click on the Photos1, Photos2, Photos3, and Photos4 menus, the "$.get(..." is sent to the server four times. It's important that all of these menus are disabled after any one is clicked. My development environment includes NodeJS, Expre ...

What is the best way to retrieve the input value from post.ejs?

app.js var express = require('express'); var bodyParser = require('body-parser'); var app = express(); var passport = require('passport'); var localStrategy = require('passport-local'); var axios = require("axi ...

What is the best way to choose the initial p tag from an HTML document encoded as a string?

When retrieving data from a headless CMS, the content is often returned as a string format like this: <div> <p>1st p tag</p> <p>2nd p tag</p> </div> To target and select the first paragraph tag (p), you can extract ...

Complete a checkbox entry form and showcase it within a "division" on the current page

Hello everyone, I need some help. I have a form that I want to send to a specific div on the same page. This is what I am aiming for. I have a floating div named "basket" in the top right corner of my page where I want the form data to be displayed after s ...

Creating a table in VueJs and populating it with values retrieved from an MSSQL database in a .NET Core web application

I am developing a table within a .NET Core Web Application that includes multiple rows and columns filled with data retrieved from a MSSQL server through a WEB API Given the need for numerous rows and columns, I am considering using a for loop inside my & ...

Utilizing Bootstrap Columns for Image Alignment: Struggling to Position an Image in the Center

For my latest project, I used Bootstrap to create a template that adapts well to different device breakpoints. Everything is working smoothly except for the positioning of the arrows on the sides of the image. I want them to be centered halfway down the ...

Conceal the nearest parent element above using JavaScript (or jQuery)

I need to implement a functionality where clicking a button hides a specific HTML class. The structure of the HTML is as follows: <thead> <tr class="employee-list"> <th> <button class="show-emp">S ...

Guide on transferring control from a successful jQuery event to an HTML form

I am currently using the following jQuery code to validate user details. $.ajax({ type: "POST", url: "Login", data:'uname='+encodeURIComponent(uname)+'&'+'pass='+encodeURIComponent(pass), ...

Looking for an effective method to implement CSS styling within a list cell in JavaFX?

As a beginner in JavaFX with limited knowledge of CSS, I struggled to conduct proper research and provide relevant information here. I apologize for any confusion or duplication. I am seeking a solution to apply CSS to JavaFX controls, specifically in a Li ...