Creating responsive lines with CSS on top of an image

I would like to add lines on an image using CSS3 and HTML5 Canvas. I came across a tutorial and demo that utilizes an html div:

However, when attempting this effect on an image, the line appears outside of the image.

How can I ensure that the line is drawn directly on top of the image?

Additionally, I need the line width to be responsive. For example, if I resize the browser window (which dynamically resizes the image with Javascript), the pixel width of the line should also adjust accordingly.

Should I switch to using Canvas HTML5 instead? What about browser compatibility?

The full code is provided below:

<!DOCTYPE html>
<html lang="es">
 <head>
    <meta charset="utf-8>
    <title>Lines with CSS</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>
    <style>
        div.line{
            -webkit-transform-origin: 0 50%;
               -moz-transform-origin: 0 50%;
                    transform-origin: 0 50%;

            height: 5px; /* Line width of 3 */
            background: #948C79; /* Black fill */
            opacity: 0.5;
            box-shadow: 0 0 8px #B99B7E;

            /* Animation for rotation effects: */
            -webkit-transition: -webkit-transform 1s;
               -moz-transition:    -moz-transform 1s;
                    transition:         transform 1s;
          }

          div.line:hover{
            background: #C30;
            box-shadow: 0 0 8px #C30;
            opacity: 1;
          }

          div.line.active{
            background: #666;
            box-shadow: 0 0 8px #666;
            opacity: 1;
          }
    </style>
    <script>
        function createLine(x1,y1, x2,y2){
            var length = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
            var angle  = Math.atan2(y2 - y1, x2 - x1) * 180 / Math.PI;
            var transform = 'rotate('+angle+'deg)';
            var line = $('<div>')
            .appendTo('#page')
            .addClass('line')
            .css({
              'position': 'relative',
              'transform': transform
                })
                .width(length)
                .offset({left: x1, top: y1});

            return line;
            } // function           

    </script>
</head>
<body>
    <p>Lines with CSS</p>

    <input type="button" value="Draw line" onClick="createLine(0,0,20,20);" />
    <div id="page" style="height:auto;"><p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/86/Man_o%27war_cove_near_lulworth_dorset_arp.jpg/300px-Man_o%27war_cove_near_lulworth_dorset_arp.jpg" class="image" /></p></div>
</body>

The jsfiddle link is available here: https://jsfiddle.net/30xh1xpm/ (please note that it does not draw the line).

Answer №1

After successfully resolving all the problems, including fixing the issues with IE8 using a JS library, I thought I was in the clear.

But to my surprise, Firefox continued to exhibit strange behavior, forcing me to resort to PHP + GD for a solution that works across all modern browsers. It appears that dealing with CSS3 can be quite challenging for even the most experienced developers.

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

Should pages be indexed to index.php or should the top and bottom parts of the page be created and included in all content pages?

I've been out of the webpage creation game for a while, but I've recently decided to get back into it. Like everyone else, I want to learn the best way to do this. However, I find myself facing a dilemma on how to structure my pages. My initial i ...

Tips for displaying a title within the border of a div

We have a client who is asking for a unique feature on their new website - they want the title to appear within the border of a text area. We need help figuring out how to achieve this using CSS/HTML. Take a look at the effect we are trying to create: htt ...

Tips for positioning a div between two vertically aligned input elements

I'm trying to figure out how to place a div between two inputs with the same height and vertically aligned. It seems like a simple task, but for some reason, the code below isn't working: input{ width: 35%; border: 1px solid #A7A7A7; ...

Using the linearGradient feature within an SVG to create a transitional effect on the Fill property

Looking to implement a stepper functionality, everything is working smoothly except for the transition on the fill. let step = 0; document.querySelector("button").addEventListener('click', () => { step++; document.querySelector("svg ...

Navigating with Anchors, Styling and jQuery

Firstly: Apologies in advance for any language errors as English is not my native tongue. :) The Scenario Here's the deal: I'm attempting to create a single button that, when clicked by the user, automatically scrolls down to the next DIV. Each ...

How come my flex-box navigation bar is not collapsing as I shrink the browser window size?

I'm currently experimenting with FlexBox to enhance the design of my website, specifically focusing on creating a responsive and collapsible navigation bar for mobile users. I'm encountering issues with the flex commands in the .nbar class not wo ...

Ramjet: Unveiling the Magic of Making Elements Appear and Disappear

Currently, I am attempting to implement the code for ramjet from . However, I am facing an issue where element a does not disappear when transitioning into b. Additionally, I am encountering an error message "--Uncaught TypeError: Cannot read property &apo ...

Is it necessary for each React component to have its own individual stylesheet?

Looking for some advice on React as a newbie here. I'm wondering whether each React component should have its own stylesheet. For instance, if I have my main App component that gets rendered to the browser, is it sufficient to include a CSS file the ...

Move the Div element up and down when clicked

When a tag is clicked, the corresponding div opens and closes. I would like the div to slide down and up slowly instead of appearing immediately. <a href="" class="accordion">Click here</a> <div class="panel" id="AccordionDiv"> ...

Adding an external link in the `require()` function within the `src` attribute of an `<img>` tag

I am facing a similar issue to the one described in this question here: Linking to images referenced in vuex store in Vue.js The main difference is that I am dealing with an external link for the src attribute of an img tag, like 'https://....' ...

Troubleshooting: My jQuery script for fading in content upon document ready is not

I'm currently working on a website where I want everything to fade in when the user enters the site. Take a look at my code: var celyLogin = $(".container"); $(document).ready(function () { /*! Fades in page on load */ $("container") ...

Activate Dropdown on hover and automatically close the menu when a link is clicked

Looking for assistance with a Dropdown menu that opens on mouse hover but needs to close when a link is clicked within the menu. Do you think JavaScript would be necessary for this function? .dropdow { position: relative; ...

Unable to manipulate or customize the V-slot component

I recently implemented the v-flip feature on my Nuxt webpage, resulting in the following template: <div class="about__cards"> <vue-flip class="about__single-card" active-click width = "350px" height="450px"> <template v-slot:front> ...

Whenever I adjust the layout of the navigation bar, the edges end up getting clipped

I'm having trouble with the border shape of my navbar. When I try to make it a rounded pill shape, the edges get cut off instead of being properly displayed. https://i.stack.imgur.com/sUN2Y.png Below is the HTML template: <template> <div cl ...

What is the best way to stack several elements on top of each other?

<div class="parent"> <div class="child" id="child-A"> <div class="child" id="child-B"> <div class="child" id="child-C"> </div> The main concept here ...

The unexpected blank space appearing beneath my website as a result of images and videos placed on the

There seems to be some random white space on my website between the main body elements and the footer. Interestingly, removing the cat image and videoplayer eliminates this white space. However, I don't want to remove them completely, so I'm tryi ...

Parent div overflowing due to vertical flex container

In my current project, I am attempting to design a layout with a fixed header and footer along with a dynamic content area that utilizes the remaining vertical space. The content-wrapper contains a lot of data, which may require a scrollbar to navigate. H ...

Applying CSS to Dynamic JavaScript Elements: A Guide to Styling Without In-line Code

I'm a beginner in web development and I've been experimenting with applying styles to dynamically generated div elements using JavaScript. So far, I've only been able to manually add inline styling through this function: function applyStyle( ...

Avoiding Duplicate Paths in URLs with Perl CatalystOne common issue that can occur in web development is the presence of double

I recently developed a web application using Catalyst framework. The login page can be accessed by typing the URL: http://mydomainname/login When accessing this URL, the login page is displayed beautifully with all its styling from the CSS file. However ...

Using a percentage in CSS translate can result in an image appearing blurry

I am facing a frustrating issue. Whenever I align an image using percentage-based transform translate, it results in a slight blur. This problem is specifically related to percentage alignment. Here is the CSS snippet: img { display: block; height: ...