Utilizing SVG images as interactive elements without embedding them directly into the HTML code

I am facing a challenge with incorporating button images saved as svgs into my HTML site. These images are intricate, so I prefer to have them stored separately as .svg files rather than directly within the HTML file. Additionally, the buttons are circular in shape.

My initial thoughts were to add the buttons as background images in CSS or inserting them as standard images with an onclick method. However, both methods seem overly complex and counterintuitive. My uncertainty on the best approach led me here seeking advice.

Here is a snippet of what I have attempted so far:

<div class="sw-buttons">
                <button id="sw-button-1" />
                </button>

                <button id="sw-button-2" />
                </button>
                ...(more buttons here)
            </div>

While researching, I came across this resource, but it includes the svg images directly in the HTML which is not ideal for my case.

Answer №1

To incorporate an external SVG into a button, you can use the src attribute within an <img /> tag nested inside a <button>...</button> element:

button, button img {
width: 80px;
height: 80px;
border-radius: 50%;
}

button {
position: absolute;
background-color: rgb(255,0,0);
}

button img {
position: relative;
top: 10px;
left: 10px;
}
<button type="submit">
<img src="/external-button.svg" />
</button>

Answer №2

To access custom buttons on your website, you can store them on your server and use the following code snippet:

    #custom-button-1:
    background: url(your_image_link_goes_here) no-repeat left top;
    height: auto;
    width: auto;

    #custom-button-2:
    background: url(your_image_link_goes_here) no-repeat left top;
    height: auto;
    width: auto;

You have the flexibility to adjust the size of the buttons as needed.

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

Is it necessary for an Angular 2 '@Component' decorator to have an element name selector?

Within the official Angular 2 documentation, an example of a decorator can be seen: @Component({ selector: 'my-app', template: '<h1>My First Angular App</h1>' }) For instance: I'd rather avoid cluttering my HTML c ...

What techniques can I use to modify an object while it's being iterated through?

I've been attempting to manipulate the object while looping through it, but unfortunately, it's not working. How can I modify it so that the constant patient includes the property lastActivity inside the this.model array? My code looks like this ...

Netlify encountered an error with mixed content, indicating that the page was successfully loaded over HTTPS, but it attempted to request an insecure HTTP

hey everyone, Recently, I deployed my vue-cli website project on Netlify. However, upon opening the website, I encountered the following error message: Mixed Content: The page at 'https://xxxx.netlify.app/' was loaded over HTTPS, but requested a ...

Implementing lazy loading functionality with jQuery for an image grid within an AngularJS single page application

Recently, I decided to enhance my GUI for boom by incorporating Lazy Loading of images. This GUI is constructed using AngularJS. I have a working version that loads all images, which I have shared through this Gist link: https://gist.github.com/brock/67241 ...

When I use AJAX to load a PHP file, the function's content returns as [object HTMLDivElement]

Hello there, When I use AJAX to load a PHP file, the content of my function returns [object HTMLDivElement], but if I load my function without loading the PHP file, it displays normally. index.php <h1>API Football</h1> <nav> ...

AngularJS HTML template language is a powerful tool for creating dynamic web

Having transitioned from Rails to Angularjs, I am comfortable with Sass and have quickly adapted to Less. Now, I am interested in exploring options for HTML and possibly JavaScript as well. Is there a templating language similar to SLIM for projects ...

Incorporating the angular UI router effectively by reusing the same templateUrl and controller multiple times

Exploring the AngularUI Router framework for the first time, I am curious about how to enhance the code snippet below. Everything is functioning well at the moment, but as the project progresses, it will feature 20 questions or more. I want to avoid repea ...

Is there a quicker method to update the state of an array of objects?

Here is my React state example: const [questionList, setQuestionList] = useState([ { _type: "radio", answer: "", point: "", question: "", options: ["A", "B"], ...

An issue with importing chatgpt: ES Module require() not functioning as expected

I am facing an issue when trying to require the chatgpt package in my code. Despite having installed node fetch versions 2.6.6 and 2.7.1, the package is still throwing errors. Additionally, I have updated my package.json file with "type": "m ...

What is the best way to transfer functions connected to an Object over to Object.prototype?

Imagine having this: var exampleObject = {age: 25, name: 'John'}; If you do this: Object.keys(exampleObject); // it will return ['age', 'name'] Now, what if you want to add this functionality to the object prototype? You c ...

What is the most straightforward method to access these four buttons?

Looking to create 4 interactive buttons with a unique behavior. When clicked, the chosen button will change its background image while the other 3 buttons retain their original background image unless hovered over by the user. Additionally, hovering over a ...

Struggling to align the login button accurately within the navbar

For proper alignment, the "Login" button needs to be pushed to the right of the navbar alongside other buttons. To see the issue and the code, check out this JSFiddle example: https://jsfiddle.net/sterlingmd17/tk17zjfq/1/ <nav class="navbar navbar ...

Is there a way to initiate an Edge animation when an onclick event occurs?

I am interested in incorporating an interactive Adobe Edge animation that activates upon the click of a conventional HTML form button. Could you please guide me on how to achieve this? ...

Converting data from an HTML file to JSON with the help of <input file> feature in Javascript (Vue)

Currently, I am attempting to upload an HTM file using in Vue. Within this file, there is a styled table utilizing , and other HTML elements... I am curious, is it feasible to extract the tabulated data from the HTM file and convert it into another forma ...

A scenario in a Jasmine test where a function is invoked within an if statement

My coding dilemma involves a function: function retrieveNames() { var identifiers = []; var verifyAttribute = function (array, attr, value) { for (var i = 0; i < array.length; i++) { if (array[i][attr] === va ...

Tips for creating a sequence pattern in JavaScript or jQuery resembling 12345-1234567-8

I am looking to adjust this code so that a hyphen "-" is automatically inserted after every 5 characters, and then again after every 7 characters. Currently, the code only inserts a hyphen after every 5 characters. <html> <head> <scri ...

What causes inline CSS and internal CSS to exhibit different behaviors?

It’s kind of strange that I have to click twice to see Foo’s content, but only once to see Bar’s content. The main difference seems to be that Foo’s content has internal style while Bar has inline style. <html> <head> <style> ...

alignment problems in dropdown menus

I'm currently working with 3 bootstrap dropdown elements. The toggle for the first two is not controlled by jQuery, but for the third one, I am using jQuery to show and hide it. However, I've encountered some issues that need fixing. When clickin ...

Is there a way to have the even numbers in an array printed before the odd numbers?

Looking for a way to separate even and odd numbers into two arrays? This function will do just that, but with a twist - the evens array will be printed before the odds. var numbersArray = [1,2,34,54,55,34,32,11,19,17,54,66,13]; function customDivider(n ...

Initiating a conversation from deep within a conversation using JQuery Mobile

I've been attempting to open a Dialog from within another Dialog, but so far no success. Take a look at the HTML code below: <a href="#popupDialog" data-rel="popup" data-position-to="window" data-role="button" data-inline="true" data-transition=" ...