Results 1 to 5 of 5

Thread: How do you scale a SVG file with an OBJECT tag?

  1. #1
    Join Date
    Jan 2013

    How do you scale a SVG file with an OBJECT tag?

    I can't figure out how to change the size of an SVG image loaded with the OBJECT tag. I would like to use the same image in different sizes.
    If I use the IMG tag and specify a width, it shrinks the SVG image to fit into the size I specified.
    I want to use the object tag so I can have a PNG fallback.

    I have tried many variants of the following:
        <object data="logo.svg" type="image/svg+xml" width="75" alt="Logo svg" >
        <img src="logo.png" width="75" alt="Logo png" />
    But every time I leave out width and height it it puts in the large image I originality designed.
    If I put in width and/or height attributes it does not scale them, it cuts them off, giving me the upper left corner of the logo. I want the whole image shrunk to fit the correct width.

    Any idea of how this is done with the OBJECT tag?


  2. #2
    Join Date
    Mar 2005
    Behind you...
    I haven't really worked with SVG much but from what I understand there should be 2 ways to do this. The first (preferred) would be to place your <object> element inside of something like a <div> and then size your <div> element appropriately. Supposedly SVG elements should automatically scale to their parent elements and so this should give you a solution.

    Also, SVG files themselves can be edited in any text editor as it uses an XML format. You can edit the default width/height values here.

    I think in either case you may end up needing to open up the SVG file anyway to view the width and height values. If you want to use a containing <div> element you will want the SVG to have a default width and height of 100% so it scales with its parent element. This would allow you to control each instance of that SVG image seperately and with some simple inline or CSS styling. However if you just want to set the default width/height and not worry about resizing it with your HTML you can simply set an exact value in the SVG file itself.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Jan 2013


    OK, thanks! That solved it!
    Just in case anybody else is having the same problem I will now give more details.

    I had previously tried something similar to both of those methods and could not get them to work.
    I had tried several methods of containing the image in a smaller container, but each that I tried simply showed a portion of the full sized image. So if I put the image into a 100 by 100 container, I would see the cropped upper left corner of my image, not the whole image shrunk down to 100 by 100.

    Doing a lot more reading, I finally gained an appreciation for the term "viewBox". While some tags (such as the img tag) create a new viewport. The object tag does not. Thus any height and width constraints you put on the object tag are just changing how much of the full sized object you see. Not scaling it at all. I had created my svg file with Inkscape, and Inkscape does not put in a viewBox tag automatically, so if you want one, you must put it in manually.

    I was unsurprised that changing the height and width with a text editor did nothing but make the resulting file unrenderable. Each of the images component parts is specified using X Y coordinates. Simply changing the outer total without changing each component did not do any good at all. both a smaller total size measured in pixels, or changing them to read "100%" resulted in an unrenderable image.

    What finally worked was adding a "viewBox" tag inside the svg file and changing the svg width and height to 100%. I went ahead and used the height and width of the svg file in my viewBox tag. That did not matter.
    Then when my html called in the svg file with and object tag, and the object tag had a width (it only needs a height or a width, not both since it scales proportionality) it showed the entire view, scaled perfectly.

  4. #4
    Join Date
    Jan 2014
    I am getting the same issue what you have faced. I tried using viewbox but svg file is not at all getting decreased. its either cutting down the image or showing its original size. Please help.

    below is my svg file content

    <?xml version="1.0" encoding="utf-8" ?>
    <svg xmlns="http://www.w3.org/2000/svg" version="1.1" preserveaspectratio="none" height="520" width="520" viewbox="0 0 520 700">
    <path style="fill:#2C768A; color:#2C768A" stroke-width="0" d="M 7 24.3623 C 6.9989 26.1058 7.3475 26.6644 9.0975 25.0108 L 18.6076 16.0141 C 21.8005 12.9948 21.7925 13.0302 18.6133 10.022 L 9.0975 1.0422 C 7.3509 -0.6098 7.0011 -0.2295 7 1.6997 L 7 24.3623 Z"></path>

    and below is my object tag

    <div style="padding-top:4px;padding-right:10px;">
    <object style="height:20px ;width:20px;" type="image/svg+xml" data="svg/Arrow.svg"></object>


  5. #5
    Join Date
    Jun 2013
    We know, SVG is a vector image form, well, scaling vector image can be as simple as scaling raster images, like jpeg, bmp, gif, png with the hejp of methods below.

    Public Shared Function ApplyResize(img As REImage, ratio As Single) As Integer
    End Function

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.09550 seconds
  • Memory Usage 2,883KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates