What is the best way to align a button with the edge of an image?

How can I use CSS to position a button on the edge of an image?

https://i.stack.imgur.com/FEFDC.png

Here is my code:

<div>

   <button class="" aria-label="Eat cake">Btn</button>
   <img class="pull-left" src="style.png" style="width: 160px; border: 1px solid #DEDEDC;"/>


</div>

Thanks,

Answer №1

Just as an illustration, I am demonstrating how the red button is positioned absolutely within its relative parent.

.blue-div{
  margin: 10%;
  position: relative;
  width: 120px;
  height: 120px;
  background: blue;
}

.red-btn{
  position: absolute;
  top: -10px;
  right: -10px;
  background: red;
  border: none;
  outline: none;
  border-radius: 50%;
  padding: 8px 4px;
  color: white;
}
<div class="blue-div">

<button class="red-btn">Btn</button>

</div>

Answer №2

Sure thing, happy to help!

Just a quick tip: whenever you want an element to be positioned relative to its parent, make sure the parent's position is set to relative and the child's position is set to absolute.

.container{
  position:relative;
  width: 150px;
  margin: 0 auto;
  margin-top: 100px;
}
.container .badge{
  position:absolute;
  top:-25px;
  right:-25px;
}
.container img{
  width:100%;
  height:auto;
}
.badge{
  background-color:red;
  border-radius:50%;
  height:50px;
  width:50px;
  text-align:center;
}
<div class="container">
  <span class="badge"></span>
  <img src="http://via.placeholder.com/150x260"/>
</div>

Answer №3

.round-close {
  border-radius: 50%;
  padding: 4px 6px;
  position: absolute;
  top: 0px;
  left: 155px;
  background-color: #900;
  color: white;
}
<div>
  <button class="round-close" aria-label="Eat cake">x</button>
  <img class="pull-left" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOgAAADZCAMAAAAdUYxCAAABZVBMVEX////qQzU0qFNChfT7vAVsbGxvb29qamo3gPSjv/lycnJra2tmZmbr8v4pefPV1dX09PSIiIjj4+Pd3d3FxcXs7Oy9vb2BgYHLy8u0tLR5eXmSkpL7twCjo6PpMh/w8PAmpUqPj4+tra3pLBfpNyagoKAToT/oJQv629nqPS4zqz7P3fz8wwDW4vz2+/e53MHwhX/g6f2VzKFVs2xTjvVluHiyyfq/0vtbW1tDg/x2v4fj8e...

Answer №4

I always follow this technique:

.element {
  margin: 10px;
  padding: 5px;
  border: 1px solid #ccc;
  background-color: #f7f7f7;
  font-size: 16px;
}
.label {
  font-weight: bold;
  color: blue;
}
<div class="element">
  <p class="label">Sample Text</p>
</div>

Answer №5

button {
  position: absolute;
  top: -5px;
  right: -5px;
}

.wrapper {
  position: relative;
}

button {
  position: absolute;
  top: -5px;
  right: -5px;
}
<div class="wrapper">
  <button></button>
  <img />
</div>

Answer №6

This technique is quite simple! Just use the combination of position relative and absolute. Set the parent element to position:relative. Then set the child element to position:absolute along with specifying values for top, left, right, and bottom according to your requirements. In your scenario, make sure to specify top: margin and left: margin+picture width. I have included a code snippet to help you visualize this setup. I hope this explanation makes sense.

img {
    position: relative;
    margin:50px;
}

button {
    position: absolute;
    left: 210px;
    top:50px;
}
<div>                        
   <img class="pull-left" src="https://i.stack.imgur.com/FEFDC.png" style="width: 160px; border: 1px solid #DEDEDC;"/>
    <button class="" aria-label="Eat cake">Btn</button>
</div>

For more information, visit: https://www.w3schools.com/cssref/pr_class_position.asp

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

Steps for Creating an Animation Tool based on the Prototype:

One question that recently came up was: What is the best approach to tackling this issue? Developing a tool that enables designers to customize animations. To make this process easier, it is essential to create an AnimationSequence using JavaScript that ca ...

Jquery Query: Is it possible to incorporate variables into CSS properties?

I manage a website that supports multiple languages, and I want to adjust the position of my container based on the selected language. To achieve this, I attempted the following code: prop = lang == 'ar' ? 'right' : 'left'; $ ...

Perform the same actions on every element within the ul li

I'm facing an issue with my unordered list, where each list item contains a span element with an image inside. My goal is to set the background-image of each span to be the same as the image it contains, while also setting the opacity of the image to ...

Choose the div without a class

I currently have several divs displayed on my website. <div class="slide bx-clone"></div> <div class="slide"></div> <div class="slide"></div> <div class="slide bx-clone"></div> <div class="slide bx-clone" ...

The erratic appearance of inactive elements within ExtJS components

I am currently working on an ExtJS form that utilizes hbox-layout containers to generate sentences containing various form inputs. There is a specific requirement to disable the form under certain conditions. These hbox-layout containers consist of compone ...

Ways to prevent the jQuery simple slider from transitioning slides while it is in an invisible state

There is a jQuery slider on my website that behaves strangely when I navigate away from the Chrome browser and return later. It seems to speed through all pending slides quickly when it was not visible. Now, I want the slider to pause when it becomes invi ...

Setting the width of a div element dynamically according to its height

Here is a snippet of my HTML code: <div class="persoonal"> <div class="left"></div> <div class="rigth"></div> </div> This is the CSS code I have written: .profile { width: 100%;} .left { width: 50%; float: le ...

Scrollbar Excess in Windows 10 on Chrome Version 76

<div style="overflow-x: hidden"> <div style="height: 100%"> <p style="margin-bottom: 1rem"> lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam accumsan nisl id maximus gravida. </p> ...

Information backed by the HTML5 Local Storage feature

Is it possible to use a Local Storage object to store another Local Storage object? Thank you in advance. ...

Does the a:hover:not() exception only apply to elements with the .active class?

I developed a navigation bar and split it into 3 distinct sections: 1. id="menu" 2. id="fake" (button to fill space) 3. id="social" My main goal is to exclude the .active page and the fake button from the hover effect, but it seems that using a:hover:not( ...

Capture the selected hyperlink and show the corresponding page title in a designated box for reference

I want to track the links that users click on and display them in a box with an image and name of the page. Additionally, I would like to show how long the user spent on each page below the image. The images in the box should also be clickable links to the ...

The act of sorting an item in jQuery triggers a reordering of items

I am working with a collection of items that represent different layers within div elements. One of my goals is to ensure that when I rearrange these items, their corresponding div layers are also sorted accordingly. Here is the list of sortable items: & ...

Show image details on hover?

I have six images displayed full width using a flex grid. I would like the image information (along with a visit button) to appear on hover, and the brightness of the image should decrease. I am struggling to position the information in the center of the i ...

Leveraging IPFS to host a CSS stylesheet

I've been trying to load a css stylesheet using this link... I attempted adding the link tag to both the main and head tags. <link type="text/css" rel="stylesheet" href="https://ipfs.io/ipfs/Qmdun4VYeqRJtisjDxLoRMRj2aTY9sk ...

Easily move a group of HTML elements at the same time with a simple

Exploring HTML5 Drag and Drop with Multiple Elements When it comes to dragging and dropping multiple elements in HTML5, there seems to be a limitation with the default draggable attribute. This attribute allows only one element to be dragged at a time, ma ...

Python Selenium for Element Detection

I am a beginner when it comes to using Selenium and I am currently seeking guidance on how to locate the element that is highlighted in this image: Despite my attempts, the following code resulted in an error message being displayed: create_a_detector_b ...

Is there a way to stop TinyMCE from adding CDATA to <script> elements and from commenting out <style> elements?

Setting aside the concerns surrounding allowing <script> content within a Web editor, I am fully aware of them. What I am interested in is permitting <style> and <script> elements within the text content. However, every time I attempt to ...

Exploring ways to obtain the value of an unchecked checkbox in CodeIgniter

I am currently working on an attendance system using CodeIgniter. I have a list of checkboxes and I have successfully managed to insert the checked data into the database. However, I am now trying to figure out how to insert the unchecked data into the dat ...

The onblur event is triggering prior to the value being updated

There are two input fields within a <form> element. I am trying to retrieve the value of one input field (dpFin) once it has been changed. The issue is that when I attempt to get the new value inside the event using var endDt = document.getElementByI ...

Problem with displaying fonts in web browsers

I decided to create a React App using the Material UI library, but I wanted to customize it by changing the default font from Roboto to Overpass. I successfully imported the fonts using their library. <link rel="preconnect" href="https:// ...