Position an icon at the top-right corner of an image that is absolutely positioned

I've come across many questions and answers similar to mine, but none quite fit what I'm aiming for.

Here's the HTML code:

<div class="container">
  <div class="inner">

    <div class="image-container">
      <img src="http://fpoimg.com/600x300" alt="" />
      <a href="" class="close-link">x</a>
    </div>

    <div>image name</div>
    <div>id: 123456</div>
</div>

and the corresponding CSS:

  * {
    font-family: sans-serif;
  }
  .container {
    position: relative;
    border: 1px solid;
    width: 500px;
    height: 500px;

  }

  img {
    max-height: 100%;
    max-width: 100%;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
  }

  .close-btn {
    position: absolute;
    background: red;
    padding: 4px 8px;
    border-radius: 50%;
    color:white;
    text-decoration: none;
  }

View the jsfiddle demo here - https://jsfiddle.net/61afw70q/10/

My goal is to position the red cross button at the top-right corner of the IMAGE, not the container. It needs to adjust according to each image size as they all differ. The image should be centered within the container.

This is a simplified version of my overall webpage layout.

Answer №1

There is no real necessity for the inner div as the image is already within a container that eliminates the need for specific positioning.

* {
  font-family: sans-serif;
}
.container {
  position: relative;
  border: 1px solid;
  width: 500px;
  display: flex;
  padding: 15px;
  flex-direction: column;
}
.image-container {
  position: relative;
  display: inline-block;
}
img {
  max-height: 100%;
  width: 100%;
}
.close-btn {
  position: absolute;
  background: red;
  top: 4px;
  right: 4px;
  padding: 4px 8px;
  border-radius: 50%;
  color: white;
  text-decoration: none;
}
a.close-link {
  position: absolute;
  background: red;
  width: 1.5em;
  height: 1.5em;
  text-align: center;
  line-height: 1.5em;
  border-radius: 50%;
  color: white;
  text-decoration: none;
  top: 4px;
  right: 4px;
}
<div class="container">

  <div class="image-container">
    <img src="http://fpoimg.com/600x300" alt="" />
    <a href="" class="close-link">x</a>
  </div>


  <div>image name</div>
  <div>id: 123456</div>
</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

When the mouse hovers over the slider, the final image jumps into view

After successfully building an image slider that displays 2 partial images and 1 full image on hover, I encountered a bug when setting the last image to its full width by default. This caused a temporary gap in the slider as the other images were hovered o ...

The button is not properly aligned at the center of the flex container

My attempt to center a button in a fluid container with 100% vertical height and provide it with an offset from the bottom of the screen is almost successful, but the button is not perfectly centered - it is offset from the left. You can view the example i ...

HTML block failing to implement Bootstrap styling

I am struggling to integrate Bootstrap into Drupal. I have used the 'row' class for the contact ID and added col-sm-12 for title and desc, but it is not working as expected. The width of the container seems off. Also, the accordion.js script work ...

JavaScript code to generate a random color for the box shadow effect

Recently, I developed a function that generates random divs containing circles. The function randomly selects a border color for each circle, and this feature is functioning correctly. To enhance the appearance, I decided to add a box shadow to the circl ...

What is the best way to alter the color of the "checkmark" in an HTML checkbox using CSS?

Is it possible to change the color of the check mark in an HTML checkbox? Currently, on Windows XP using Firefox, Chrome, or IE, the check mark appears green. I am interested in changing the check mark within an INPUT type="checkbox" to be orange. ...

Style the labels on the axis of Frappe Charts with colors (potentially utilizing the appropriate CSS selector)

Is it possible to style the x and y axis labels in a Frappe chart with different colors? https://i.stack.imgur.com/A3vUq.png While trying to identify the CSS selectors using Chrome DevTools, I found that a single text element (representing an x axis labe ...

Divs in multiple columns within a table: The containing div is limited to the width of the first column

I'm attempting to enclose one "div" within another that might have multiple columns inside. But, as shown in this example <table class="datatable"> <tr> <td class="leftcolumn">Radiobuttons:</td> <td class="rightcolum ...

Customizing the appearance of the Bootstrap Typeahead

I've been experimenting with the Bootstrap Typeahead for my search feature. I successfully implemented the dropdown list using Ajax. However, I am looking to customize the width, padding, and background color of the dropdown menu, which is currently w ...

Animating SVG from upper left corner to lower right corner

I am looking to create an animation on this SVG that starts from the top left and ends at the bottom right, similar to a gif. You can view the example I want to replicate in the following link: body { background: black } path { fill: white; } /* ...

Issue with CSS/SASS within a container containing multiple elements

I am experiencing an issue with an image that I have built: box Initially, the image appears to be a box with 4 elements inside. However, when I resize the window, the layout gets distorted. I am unsure which property I am using incorrectly, so I will pr ...

PrimeFaces: Achieving Conditional Coloring Based on Status (Passed/Failed)

Attempting to dynamically change the color of a column based on its status value (Passed/Failed/InProgress) using jQuery. I tried copying and pasting the table into jsfiddle, where it worked. However, when implemented in the actual XHTML file, the jQuery ...

What is the best way to adjust the position of the previous and next buttons on a Bootstrap 5 carousel?

I am having trouble figuring out how to properly position the previous and next buttons in a bootstrap5 carousel. The original code I used as a template was for a carousel that displayed 4 images at once, but I changed it to display 3 images at once. I a ...

Images within a row element do not decrease in size when the screen is resized

I am in the process of creating a small website aimed at helping my daughter (and other children) learn new words. The illustration demonstrates how I envision the website looking across all types of devices. My initial attempt was based on a template an ...

The CSS table is not displaying the border on the last td when using the nthchild() function

My goal is to display the table td's with alternating borders - one with a border and one without on the bottom. Everything seems to be working fine except for the last td of the table, where the border-bottom does not appear. Additionally, the issue ...

Creating an HTML table that adjusts to the screen height with square-shaped cells

var grid = document.getElementById("grid"); var size = 50; for (var y = 0; y < size; y++) { var currentRow = grid.insertRow(0); for (var x = 0; x < size; x++) { var currentCell = currentRow.insertCell(-1); currentCell.style.height = currentCell.styl ...

Having difficulty adjusting the size of open-iconic icons in Bootstrap

I recently added the open iconic plugin to my project and I'm trying to adjust the size of the icon. While the original library has a property to change the size, it doesn't seem to work with the Bootstrap version. Is there an alternative way to ...

Burger on a website designed for desktop users

In my project, I am looking to only display a hamburger icon with a list of items on mobile view. I do not want any menu items in the navbar on desktop and tablet views. Is there a way to achieve this using Bootstrap or another method? Any suggestions for ...

The location of the footer is not aligned correctly

Recently, I encountered an issue that has left me puzzled. The problem is that the footer on my webpage is appearing between the calendar button and the calendar itself. Here is a snippet from my calendar.html file: {% extends 'base.html' %} {% ...

What is the process for resetting a JQueryUI effect animation?

I'm facing an issue with two timer bars that each wind down over a 5-second period. When I try to stop and reset the first timer, it simply continues from where it left off instead of resetting. The desired behavior is as follows: Timer1 starts User ...

Updating the position of an element in HTML is not possible

For my webpage, I am attempting to adjust the position of all images that are displayed. Despite trying to change the position of a single image using the DOM method, I have run into a problem where the position does not update as expected. Although the co ...