Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't an HTML anchor tag wrap a scalable SVG <object>?

I have created a scalable SVG object, using the preserveAspectRatio and viewBox attributes in the SVG file itself:

<svg
  …
  width="800"
  height="800"
  preserveAspectRatio="xMinYMin meet"
  viewBox="0 0 800 800"
  …

In the HTML, I reference the SVG file using the <object> tag and wrap it an <a> tag (I want to do this so that I can style it later):

<a>
  <object type="image/svg+xml" data="smiley.svg">
  </object>
</a>

I style the <object> tag with some CSS to make it 50% wide, and no wider than 100%:

object {
  max-width: 100%;
  width: 50%;
}

The problem is that the anchor tag doesn't wrap all around the object!

Anchor tag not wrapping scalable SVG object

Any ideas anyone?

like image 425
Jake Rayson Avatar asked Mar 24 '14 16:03

Jake Rayson


People also ask

How do I add an anchor tag in SVG?

SVG shapes The simplest way to make a portion of an SVG clickable is to add an SVG hyperlink element to the markup. This is as easy as wrapping the target with an <a> tag, just as you would a nested html element. Your <a> tag can surround a simple shape or more complex paths.

How do I scale an SVG in CSS?

Just set the viewBox on your <svg> , and set one of height or width to auto . The browser will adjust it so that the overall aspect ratio matches the viewBox .

How do I link SVG to HTML?

SVG images can be written directly into the HTML document using the <svg> </svg> tag. To do this, open the SVG image in VS code or your preferred IDE, copy the code, and paste it inside the <body> element in your HTML document. If you did everything correctly, your webpage should look exactly like the demo below.

Can you put links in SVG?

The <a> SVG element creates a hyperlink to other web pages, files, locations in the same page, email addresses, or any other URL. It is very similar to HTML's <a> element. SVG's <a> element is a container, which means you can create a link around text (like in HTML) but also around any shape.


1 Answers

Adding display: block to anchor seems to fix it for me.

like image 156
Igor Pantović Avatar answered Sep 22 '22 23:09

Igor Pantović