Boundary around an object within a unified picture

I'm facing a bit of difficulty with a task. I need to add a border around an element within an image (I can't insert an image due to low reputation), and this border should match the width and height of the element exactly. It also needs to be responsive. I tried using Bootstrap media screen rules, but encountered issues with the width at certain resolutions. Here is the code. Thank you.

<div class="parent">
    <img />
    <span class="makeBorder"></span>
</div>

Here is the CSS:

.parent {
    position: relative;
}
.makeBorder {
    position: absolute;
    top: 15px;
    left: 23px;
    border: 2px solid red;
    width: 83%;
    height: 83%;
}

How I finally resolved it:

<div class="customClass"><img /></div>

.customClass{outline:1px solid red;outline-offset:-18px;}

Answer №1

Give this a shot: I decided to incorporate a button within the image, and it worked seamlessly. Some of the CSS properties in this example are not necessary as indicated by //.

<html>
    <head>
        <style>
            div{
                background:url("1.jpg") no-repeat;
                background-size:contain;//
                height:500px;//height of div
                width:500px;
                }
                button{
                height:50px;
                width:70px;
                outline:red solid 4px;
                margin:20px 20px;
                }
        </style>
    </head>
    <body>
        <div>
            <button >Hello</button>
        </div>
    </body>
</html>

Answer №2

<html>
    <head>
        <style>
            div{
            height:500px;
            width:500px;
            position:absolute;
            }
            button{
            position:absolute;
            height:50px;
            width:70px;
            outline:red solid 4px;
            margin:20px 20px;
            }
        </style>
    </head>
    <body>
        <div>
            <img src="1.jpg">
        </div>
        <button >Hello</button>
    </body>
</html>

For adding a second image, use the background image property:

z-index=-1;

Remember to use the positioning property mentioned above for proper placement.

Answer №3

Below is the resolved issue:

HTML

<div class="parent">
    <img src="/path/to/img/png" class="img-responsive" />  // Added a new class img-responsive
    <span class="makeBorder"></span>
</div>

CSS

.parent {
    position: relative;
}
.makeBorder {
    position: absolute;
    top: 15%; // Used percentage instead of pixels for better responsiveness
    left: 23%; // Used percentage instead of pixels for better responsiveness
    border: 2px solid red;
    width: 83%;
    height: 83%;
}

NOTE: It is suggested to utilize percentages instead of pixels to enhance the responsiveness of your HTML work!

Answer №4

.container {
  position: relative;
}
.addBorder {
  position: absolute;
  top: 81px;
  left: 83px;
  border: 2px solid red;
  width: 55px;
  height: 60px;
}
<div class="container">
  <img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRBnmmAWOlqD-8ZjLvlkXoz0sSMOd7DiA5paUl7Ug2VBjXnnqKaHw" />
  <span class="addBorder"></span>
</div>

It looks like this is the desired outcome

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

Display a JSON object on a web browser

I am trying to display a JSON object on a web browser using HTML. The object is already in a text file and has been properly formatted for readability. My goal is to maintain the same formatting when displaying it on the browser. ...

Currently, I am encountering a problem as I attempt to iterate through a dynamic table

I have a table containing various elements. An example row is Jack Smith with multiple rows like this: col1 col2 col3 col4 col5 col6 col7 col8 jack smith 23 Y Y error error_code error_desc The table is ...

What is causing the error message stating that the DateTime class object cannot be converted to a string?

I found this code snippet on a programming forum function increaseDate($date_str, ${ $date = new DateTime($date_str); $start_day = $date->format('j'); $date->modify("+{$months} month"); $end_day = $date->format(&apo ...

Effortlessly navigate to the start of a section once the expander box is collapsed with this simple trick!

The link to the page I'm talking about can be found here: If you're looking for the JQuery page, you can access it here: When you click on "Read More," my goal is for the page to scroll up to the heading (<h1>) or the beginning of the d ...

Retrieve the chosen item to automatically fill in the input fields using Ionic 2 and Angular

My goal is to create a dropdown menu populated with a list of items, and when a product is selected, its price should automatically appear in the quantity field. <ion-item> <ion-label>Item</ion-label> <ion-select (ionChange)="getP ...

Align a div vertically in a responsive design layout

How can I get the text inside "grid2" and "section2" to vertically align in the middle? Is this issue related to my HTML or is it a problem with the CSS? Can someone guide me in the right direction? I have searched for similar questions but the answers h ...

Access a document by selecting a particular page from a designated anchor tag

Seeking assistance from the community on opening a particular page within a file using an anchor tag (<a href=""></a>). The issue arises when trying to pass a parameter through the xls file, as it prevents locating the specified page. Any gui ...

How to extract the YouTube video source using jQuery from an iframe snippet

Currently, I am attempting to extract the YouTube source from an iframe embed code and store it as a variable. Suppose my YouTube embed code looks like this: <iframe width=&quot;1024&quot; height=&quot;576&quot; src=&quot;https://w ...

Retrieve the scrolling height in Vue.js 2 window

I need to apply the sticky class to the navbar when a user scrolls down, and remove it when they scroll back up. To achieve this, I am attempting to calculate the scrolled height. Currently, my code looks like: mounted(){ this.setUpRoutes(); wind ...

The HTML div element is not expanding to the full width of the screen on an iPhone device

Recently, I encountered an issue while testing my website on the iPhone. The footer was not covering the full width, especially on Safari browser. After some digging, I realized that the problem might be caused by an absolutely positioned DIV called #leaf- ...

Curvature Factor equals Overflow of Color

After trying various solutions like "background-clip: padding-box;" to address the issue of background color bleeding outside of the border, I'm still not completely satisfied with the result. Is there a more effective fix for this problem? Check out ...

What could be causing appendChild to malfunction?

I'm having an issue trying to create three elements (parent and one child) where the third element, an <a> tag, is not appending to modalChild even though it's being created correctly. modal = document.createElem ...

The PHP code I wrote will be executed on the subsequent HTML page, not the one currently being displayed

I have a school project where I am creating a quiz-game web page. Each question will have 4 possible answers. The questions, along with their 4 answer choices and the correct answer, are randomly selected from a database. However, I am facing an issue w ...

What might be the reason behind the selection value not changing in the dropdown menu?

I have two buttons, one for adding an employee and one for editing. Submit Record $("#addNewEntry").click(function(){ departmentName = $("#selectEmployeeDepartment option:selected").text(); locate = $("#selectLocation o ...

Animation failing to be queued properly

Here is a snippet of code that moves a heading icon back and forth when you hover over the heading: jQuery('h1.heading').hover( function(){ $icon = jQuery('.heading-icon', this); if( ! $icon.is(':animated') ){ ...

I'm having trouble getting my jQuery click handler to work

Here is the HTML content within my Sharepoint 2010 Web Part project: <html> <h1>Travel Form Assistance</h1> <div id="preTravel" name="preTravel"> <h2>Pre Travel</h2> <img id="imgPreTravel" name="imgPreTra ...

What could be causing my ajax request to not be successfully compiled?

I seem to be experiencing an issue with this part of my code. Despite trying multiple methods, it still isn't functioning correctly. What steps can I take to resolve this problem? $.ajax({ url: "https://corona-api.com/countries/BR", ...

Real-time console input/output feature for a gaming server utilizing either JavaScript or PHP

About My Minecraft Server: After running a Minecraft server in my basement for a few months, I realized that using TeamViewer to input commands and monitor the console was not ideal. The console of a Minecraft server provides a log of events with timestamp ...

What could be causing the uneven alignment and padding of texts in my input box and select box in Bootstrap 5?

I'm facing a challenge that has me stumped. I have a form with a text input and a select drop-down, all styled using Bootstrap 5. My goal is to reduce the default left padding on these form controls by adding padding-left:0.3rem to my style definition ...

What is the reason behind Flask's choice to return <embed> files for download rather than simply displaying them

When I try to render a template using Flask that includes images, the files are being downloaded instead of displayed. For example, <embed src="static/yes.svg" type="image/svg+xml"> If I place this code inside test.html and view it in Google Chrom ...