Problem with optimal placement of SVG file within HTML document (SVG generated using Inkscape CLI)

By following this provided link, I have successfully generated an SVG file from a Postscript (which was created using latex) :

inkscape example.ps --export-plain-svg example.svg

The content of example.ps is derived from the code in example.tex below :

\begin{document}

\begin{pspicture}(-4,-2.5)(6,2.5)
\rput(-4,1.5){light\,(1\,:\,yes,\,0\,:\,no)}
\rput(-4,-0.5){noise\,(1\,:\,yes,\,0\,:\,no)}
\rput(5,1.5){eyes\,(0\,:\,closed,\,1\,:\,opened)}
\rput(5,-0.5){voice\,(00\,:\,snore,\,01\,:\,laugh,\,10:\,growl,\,11:\,cry)}
\rput(0,0){Robot toy module}

\psframe(-1.5,-2)(1.5,2)

\psline{->}(-1.5,1)(-4,1)
\psline{->}(-1.5,-1)(-4,-1)
\psline{->}(1.5,1)(4,1)
\psline{->}(1.5,-1)(4,-1)
\psline{-}(2.5,-1.2)(2.9,-0.8)
\rput(2.7,-1.4){2}

\psline{-}(-0.2,-2)(0,-1.7)
\psline{-}(0.2,-2)(0,-1.7)
\psline{-}(0,-2)(0,-2.4)
\rput(0,-2.6){H}
\end{pspicture}

\end{document}

Now, my goal is to embed this SVG file into an HTML page with precise positioning at the top of the parent container.

However, I am facing challenges in adjusting the position parameters of the SVG file within HTML; for instance, I have included the following code in the HTML page :

<object data="example.svg" width="700 " height="400 " type="image/svg+xml">

The "example.svg" file (created using inkscape) contains the following parameters:

viewBox="0 0 743.75 1052.5"
   height="1052.5"
   width="743.75"

Unfortunately, the result (visible on this designated link) is unsatisfactory as the SVG appears too small.

You can access the SVG file via this specific link.

I initially chose a width and height of 700 and 400 respectively for the <object> tag similar to sizing a standard image using the <img> tag, assuming it would provide a suitable size and position for the SVG within the HTML page.

The issue might be related to the width and height offsets present in the SVG file "example.svg" (as observed when directly viewed in the browser: check here)

I attempted to set the viewBox, height, and width values to 700 and 400 respectively:

viewBox="0 0 700 400"
height="400"
width="700"

You can view the revised result here. While the width may seem acceptable, I continue to face a significant vertical offset (blank space) with the top title ("Coding > Robot module").

Is there a universal guideline for accurately positioning an SVG file without any offset or at least minimizing it to resemble an image?

PS: I wish to maintain the usage of the <object> tag for embedding the SVG in HTML.

Answer №1

One problem arises when the SVG output from LaTeX is based on the entire page and its viewBox encompasses the whole page.

To address this issue and ensure that your diagram in the middle of the page adjusts to any desired dimensions, you should adjust the viewBox to only cover the bounding box of your specific diagram.

The appropriate viewBox values would be around:

viewBox="134 174 489 168"

By setting this (and adjusting the width and height accordingly), your SVG will function as intended.

If you wish for the diagram to resize and fit the parent element, set both the width and height to "100%".

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

What is the correct way to target an SVG element with a CSS selector?

Having trouble locating an svg object by its object name https://i.sstatic.net/Is23L.png Here's the code snippet I'm using: element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".ql-editor-ql-blank.p" ...

Is there a way to prevent the second ul from appearing?

Seeking assistance with navigation. When clicking on the "Athletics" tab in the provided link, the second ul displays. Is there a method to hide this second ul and have it only appear upon hovering? ...

Assign an onClick event to a Button that was dynamically generated within a loop in JavaScript

Is there a way to set the onClick property for buttons created within a loop? I've tried using JQuery with no success. for(started somewhere... var button = document.createElement('button') var div = document.createEl ...

Every DIV is filled with HTML and CSS

My navigation bar currently has icons placed at the center of a DIV, but I would like to fill up the entire DIV without any spaces left on the sides. The HTML code for the DIV containing the icons is as follows: <nav class="navbar navbar-expand navbar ...

Animate the transition effect as soon as the block is displayed

Looking to create a smooth page transition using CSS changes with the help of Javascript (jQuery). Initially, one of the pages is set to display none. page1 = $('.page1'); page2 = $('.page2'); page1.removeClass('animate').cs ...

Issue with login form in IONIC: Form only functions after page is refreshed

Encountering an issue with my Ionic login form where the submit button gets disabled due to invalid form even when it's not, or sometimes displays a console error stating form is invalid along with null inputs. This problem seems to have surfaced afte ...

Issues with Disabling the Browser's Back Button with jquery

I have been attempting to prevent the back button from functioning in a specific Browser, Microsoft Bing. Interestingly, my code works only if I click somewhere in the window first. If I don't click anywhere and try to go back directly, it still allow ...

Modifying HTML code within a WebView (Monodroid) explained

WebView webView = FindViewById<WebView>(Resource.Id.webView1); webView.HorizontalScrollBarEnabled = false; webView.LoadUrl("res.htm"); I am looking for a way to modify certain parts of HTML code in my file and display it using webView without actual ...

Eliminate any unnecessary space below a list item and ensure it is justified

I need help with some extra indentation that is appearing under an ordered list I have created. I want to eliminate the extra spacing to achieve a cleaner look. This is how I want it to appear: https://i.sstatic.net/O7cyG.png However, it currently looks ...

What is the best way to select a specific image from a collection of images within my Ajax form?

Currently, I am new to AJAX coding and still trying to familiarize myself with it. I have managed to create an AJAX multi-page form where each "new" page is contained within divs in HTML. The CSS is responsible for the styling, and there is some JavaScript ...

Request to api.upcitemdb.com endpoint encountering CORS issue

This code may seem simple, but for some reason, it's not working as expected. What I'm trying to achieve is to call the GET API at: I want to make this API call using either JavaScript or jQuery. I've attempted various approaches, but none ...

"Adjusting the size of a jQuery dialog for optimal viewing on

I am currently working on designing an HTML page that is responsive for both mobile and desktop browsers. One issue I am encountering involves a jQuery dialog that I need to scale properly to fit the page. I believe the problem lies within these lines of ...

Text that fades in and out based on scrolling past a specific point

There's a text containing <p> and <h1>. The text finishes with one <h1>. I'm looking to speed up the Y translation of the <p> twice when I reach the bottom of the document (where the last h1 is located in the middle of th ...

How can I modify the background color of a dimmer in Semantic-UI framework?

How can I change the color of the dimmer in my modal when I am unable to do so currently? <div class="ui modal"> <i class="close icon"></i> <div class="header"> Profile Picture </div> <div class="content"> ...

Navbar toggler button is toggling open but failing to close afterwards

Below is my code featuring a collapsible navbar. The toggler button successfully opens the menu, but it fails to close it: <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" dat ...

How to properly format the date input in Vue.js

Currently, I am utilizing bootstrap-vue and its date input type feature. Upon entering numbers, the default format displayed is yyyyyy-mm-dd. I am interested in modifying this default format to yyyy-mm-dd. ...

What's the best way to add animation to the send icon while hovering over the button?

<div class="text-center"> <button [disabled]="btnStatus" class="btn btn-secondary buttonSend" type="submit"> <div [hidden]="btnStatus"> Send Message&nbsp;&nbs ...

Providing a bottle-themed image

I'm encountering some challenges trying to show an image in my bottle app. This is how my folders are structured: project| --|views --|controllers --|static --|img --myimage --|models For the client side, I&apo ...

"Struggling with the height of Bootstrap row not filling the entire space

I've tried everything but nothing seems to be working. Whether it's a simple height: 100% or experimenting with flex-grow: 1, none of it is doing the trick. I'm having issues with this site I'm currently working on: https://i.sstatic.n ...

How can I add a label to a button group created with Bootstrap?

The Nu HTML checker is throwing an error The value of the for attribute of the label element must be the ID of a non-hidden form control. <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> & ...