Задать размер svg css

How to Resize SVG in HTML

In this snippet, you can see how to resize SVG in HTML. For that, you need to change the width and height attributes.

Open the SVG file with your text editor. It will show lines of code like the following:

width="40" height="40" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg">

Here, you can see the height and width attributes that can be replaced with the values you need.

Example of using SVG image in HTML:

html> html> head> title>Title of the document title> head> body> svg width="40" height="40" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg"> g clip-path="url(#clip0)"> path d="M21.3933 40H18.6067C8.33091 40 0 31.1351 0 20.2008V19.7992C0 8.86486 8.33091 0 18.6067 0H21.3933C31.6691 0 40 8.86486 40 19.7992V20.2008C40 31.1351 31.6691 40 21.3933 40Z" fill="#F2D03B" /> path d="M2.55461 15.4131C2.49655 15.4131 2.4385 15.4131 2.38044 15.3822C2.06114 15.2896 1.85795 14.9189 1.94503 14.5483C3.51252 8.83399 7.60541 4.23167 12.8884 2.25484C13.2077 2.13128 13.5561 2.31661 13.6722 2.65638C13.7883 2.99615 13.6141 3.3668 13.2948 3.49036C8.38915 5.34364 4.58654 9.60619 3.13516 14.9189C3.07711 15.2278 2.81586 15.4131 2.55461 15.4131Z" fill="white" /> path d="M29.5794 2.00769C34.0787 5.62159 36.9814 11.3359 36.9814 17.7915V18.193C36.9814 29.1274 28.6505 37.9922 18.3748 37.9922H15.5881C12.6563 37.9922 9.86969 37.2818 7.40234 35.9845C10.5083 38.4865 14.398 39.9691 18.607 39.9691H21.3936C31.6694 39.9691 40.0003 31.1042 40.0003 20.1699V19.7683C40.0003 11.9845 35.7623 5.25093 29.5794 2.00769Z" fill="#E8B938" /> path d="M14.8911 18.1003C14.3686 18.1003 13.9622 17.6679 13.9622 17.1119V16.5251C13.9622 15.5366 13.2075 14.7027 12.2495 14.7027C11.2626 14.7027 10.4789 15.5058 10.4789 16.5251V17.1119C10.4789 17.6679 10.0725 18.1003 9.54998 18.1003C9.02748 18.1003 8.62109 17.6679 8.62109 17.1119V16.5251C8.62109 14.4247 10.2176 12.7258 12.1915 12.7258C14.2234 12.7258 15.8199 14.4247 15.8199 16.5251V17.1119C15.8199 17.637 15.4135 18.1003 14.8911 18.1003Z" fill="#3D3813" /> path d="M30.4789 18.1003C29.9564 18.1003 29.5501 17.6679 29.5501 17.1119V16.5251C29.5501 15.5366 28.7953 14.7027 27.8374 14.7027C26.8505 14.7027 26.0667 15.5058 26.0667 16.5251V17.1119C26.0667 17.6679 25.6604 18.1003 25.1379 18.1003C24.6154 18.1003 24.209 17.6679 24.209 17.1119V16.5251C24.209 14.4247 25.8055 12.7258 27.7794 12.7258C29.8113 12.7258 31.4078 14.4247 31.4078 16.5251V17.1119C31.4078 17.637 31.0014 18.1003 30.4789 18.1003Z" fill="#3D3813" /> path d="M20.0288 32.2162C11.6979 32.2162 9.05635 26.4402 8.94024 26.1931C8.70802 25.6989 8.91121 25.112 9.37565 24.8649C9.84009 24.6178 10.3916 24.834 10.6238 25.3282C10.7109 25.5444 12.946 30.2085 20.0288 30.2085C27.1986 30.2085 29.4047 25.3591 29.4337 25.3282C29.6659 24.834 30.2175 24.6178 30.6819 24.8649C31.1463 25.112 31.3495 25.6989 31.1173 26.1931C31.0012 26.4402 28.3597 32.2162 20.0288 32.2162Z" fill="#3D3813" /> g> defs> clipPath id="clip0"> rect width="40" height="40" fill="white" /> clipPath> defs> svg> body> html>

Now, let’s change the width and height of our SVG image.

Example of resizing SVG image in HTML:

html> html> head> title>Title of the document title> head> body> svg width="200" height="200" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg"> g clip-path="url(#clip0)"> path d="M21.3933 40H18.6067C8.33091 40 0 31.1351 0 20.2008V19.7992C0 8.86486 8.33091 0 18.6067 0H21.3933C31.6691 0 40 8.86486 40 19.7992V20.2008C40 31.1351 31.6691 40 21.3933 40Z" fill="#F2D03B" /> path d="M2.55461 15.4131C2.49655 15.4131 2.4385 15.4131 2.38044 15.3822C2.06114 15.2896 1.85795 14.9189 1.94503 14.5483C3.51252 8.83399 7.60541 4.23167 12.8884 2.25484C13.2077 2.13128 13.5561 2.31661 13.6722 2.65638C13.7883 2.99615 13.6141 3.3668 13.2948 3.49036C8.38915 5.34364 4.58654 9.60619 3.13516 14.9189C3.07711 15.2278 2.81586 15.4131 2.55461 15.4131Z" fill="white" /> path d="M29.5794 2.00769C34.0787 5.62159 36.9814 11.3359 36.9814 17.7915V18.193C36.9814 29.1274 28.6505 37.9922 18.3748 37.9922H15.5881C12.6563 37.9922 9.86969 37.2818 7.40234 35.9845C10.5083 38.4865 14.398 39.9691 18.607 39.9691H21.3936C31.6694 39.9691 40.0003 31.1042 40.0003 20.1699V19.7683C40.0003 11.9845 35.7623 5.25093 29.5794 2.00769Z" fill="#E8B938" /> path d="M14.8911 18.1003C14.3686 18.1003 13.9622 17.6679 13.9622 17.1119V16.5251C13.9622 15.5366 13.2075 14.7027 12.2495 14.7027C11.2626 14.7027 10.4789 15.5058 10.4789 16.5251V17.1119C10.4789 17.6679 10.0725 18.1003 9.54998 18.1003C9.02748 18.1003 8.62109 17.6679 8.62109 17.1119V16.5251C8.62109 14.4247 10.2176 12.7258 12.1915 12.7258C14.2234 12.7258 15.8199 14.4247 15.8199 16.5251V17.1119C15.8199 17.637 15.4135 18.1003 14.8911 18.1003Z" fill="#3D3813" /> path d="M30.4789 18.1003C29.9564 18.1003 29.5501 17.6679 29.5501 17.1119V16.5251C29.5501 15.5366 28.7953 14.7027 27.8374 14.7027C26.8505 14.7027 26.0667 15.5058 26.0667 16.5251V17.1119C26.0667 17.6679 25.6604 18.1003 25.1379 18.1003C24.6154 18.1003 24.209 17.6679 24.209 17.1119V16.5251C24.209 14.4247 25.8055 12.7258 27.7794 12.7258C29.8113 12.7258 31.4078 14.4247 31.4078 16.5251V17.1119C31.4078 17.637 31.0014 18.1003 30.4789 18.1003Z" fill="#3D3813" /> path d="M20.0288 32.2162C11.6979 32.2162 9.05635 26.4402 8.94024 26.1931C8.70802 25.6989 8.91121 25.112 9.37565 24.8649C9.84009 24.6178 10.3916 24.834 10.6238 25.3282C10.7109 25.5444 12.946 30.2085 20.0288 30.2085C27.1986 30.2085 29.4047 25.3591 29.4337 25.3282C29.6659 24.834 30.2175 24.6178 30.6819 24.8649C31.1463 25.112 31.3495 25.6989 31.1173 26.1931C31.0012 26.4402 28.3597 32.2162 20.0288 32.2162Z" fill="#3D3813" /> g> defs> clipPath id="clip0"> rect width="40" height="40" fill="white" /> clipPath> defs> svg> body> html>

Note that in our examples, we used the viewBox attribute. It is an optional attribute on the root of the tag. Without the viewBox attribute, you can get only a small part of the actual image.

Читайте также:  Http mmabox online ufcru html

Источник

SVG:Sizing

This page is an in-progress attempt of mine to make sense of viewport sizing for SVG content (the sizing of the rectangular area the SVG is given to draw into). You’d think it would be simple, what with one ‘width’ and one ‘height’ attribute. Unfortunately, there are a lot of other things to consider too, such as different unit types, omitted attribute(s), the CSS ‘width’, ‘height’, ‘min-width’, ‘min-height’, ‘max-width’, ‘max-height’ and ‘background-image’ properties, the ‘viewBox’ attribute, intrinsic width, height and aspect ratio, and whether the SVG is rendering standalone, embedded inline or embedded by reference. All add to the fun. In this document I’m only going to focus on a few important issues that I consider to be unresolved/troublesome and in need of further thought before implementation begins.

SVG width/height vs. CSS width/height

The effect of the CSS properties is reasonably well defined. An important question to answer is, what part do the SVG ‘width’, ‘height’ and ‘viewBox’ attributes play, and how do they interact with the CSS properties? It has become clear that SVG is to be treated as a replaced element in CSS contexts. It has also become clear that the intrinsic dimensions that the SVG must pass to the CSS replaced element algorithms are to come from the SVG ‘width’, ‘height’ and ‘viewBox’ attributes. However, current implementations (e.g. Opera 9.23) appear to treat the ‘width’ and ‘height’ attributes as presentation attributes and map them into style (i.e. translate them directly to CSS ‘width’ and ‘height’ properties). As it turns out, there are problems with both these approaches, and unfortunately the new text specifying SVG as a replaced element creates some horrible authoring gotchas (as currently worded).

Читайте также:  Android java start app

goes against the letter of the specification, but actually honors the intent behind the letter.

The way SVG requires the ‘width’ and ‘height’ attributes to be overridden by xxx does not fit well with mapping into style where uses values are expected to be close/related to the specified values.

The former approach would have the advantage of being the most intuitive for authors. The latter approach solves the problem of the ‘display’ property (i.e. what you do with |display:inline;| on SVG), and unless the user sets only one of the CSS properties ‘width’ and ‘height’ on the SVG while also having ‘width’ and ‘height’ attributes, they won’t notice a difference to the former approach (well, as long as percentage attribute values are intrinsic).

Common ground

It’s worth stating a few of my assumptions that everyone now seems to have agreed on:

1) The ‘width’ and ‘height’ attributes are the only source of intrinsic width and height values.

2) The ‘viewBox’ attribute specifies an intrinsic aspect ratio if it has a valid width/height component (we don’t need to concern ourselves with the case where the aspect ratio defined by the ‘width’ and ‘height’ attributes is different from that defined by the ‘viewBox’ attribute since CSS only cares about aspect ratio in cases where an aspect ratio can’t be determined from intrinsic width/height). XXX true?

Percentage intrinsic width and height

One of the key questions to answer is whether percentage values for the ‘width’ and ‘height’ attributes specify intrinsic dimensions or not. Clearly the understanding/intention of the author of the current wording in the relevant section of the SVG Tiny 1.2 CR is that the answer is yes. Just as clear is that the understanding/intention of the SVG WG chair is that the answer is no and the part of the spec on ‘width’ says no.

    If SVG is treated as a replaced element when embedded inline or embedded by reference the ‘width’ and ‘height’ attributes will not have any effect if they have percentage values. The SVG will either render at the width of the containing block minus margin/padding/border if it has an intrinsic aspect ratio (so the height can be calculated), or it will render at 300×150 px. For example, confusingly, this snippet of SVG embedded inline in an XHTML document will render in a 300×150 px rectangle:

  • The CSS spec says intrinsic width/height are resolved as a percentage of the containing block’s width/height. As a result, once again with CSS, you have the problem that none-zero values for border/padding/margin will cause the SVG to burst out of its containing block. (For inline SVG this may be what’s expected since it’s how things would behave if the attributes were mapped into style.)
  • There is no way for some SVG content to say it wants to take up whatever space is available after borders, padding and margins have taken their share (there is no way to create SVG that does NOT have an intrinsic width/height since the ‘width’ and ‘height’ attributes default to 100%).
Читайте также:  Writing android apps with java

Does it actually make sense to have percentage intrinsic dimensions? In the general case, probably not. It does make sense to be able to say «take up all the room available», i.e. 100% width/height, but why would you want to take up some arbitrary percentage of some unknown area? The SVG knows nothing about what’s outside it. It makes no sense.

Sizing of standalone SVG

SVG Tiny 1.2, The initial viewport, says «If there is no [containing] document, the SVG user agent must use the ‘width’ and ‘height’ attributes on the ‘svg’ element as the width and height for the viewport.» For this scenario we could map the ‘width’ and ‘height’ attributes into style (width/height defaulting to 100% if not specified) which would make implementation much simpler in Mozilla (this is also what Opera does). Percentage values would then set the viewport of the SVG to a percentage of the client area as the user would expect.

Treating SVG as a replaced element in this context

In this scenario we don’t care about intrinsic size/ratio (and hence, for the purposes of viewport sizing, the ‘viewBox’ attribute).

One note: we would want to make sure to force |display:block;| for such SVG and prevent content from changing this.

Sizing of SVG embedded inline

SVG Tiny 1.2, The initial viewport, says «If the [containing] document is styled with CSS, then the negotiation process must follow the CSS rules for replaced elements.» To allow this, SVG Tiny 1.2 specifies how the intrinsic width, intrinsic height and intrinsic aspect ratio are found for SVG content. This information is used as specified in CSS 2.1, Visual formatting model details to determine the width and height occupied by the SVG content.

Источник

Оцените статью