While replacing some .png icons to .svg I'd run onto some problem, consider this:
<div class="container"></div>
.container {
width: 100px;
height: 100px;
background-image: url("some-image.png");
background-size: cover;
transition: 1s;
}
In this case I had applied CSS transition to container and change its size (enlarge). Image inside the container onenlarge scales from center point inline with the container.
But when I changed it to .svg code (bellow) it starts to scaling from the left top corner. Does it possible to make it scale from the center of the container with my current set up (or at all)?
<div class="container"> <svg></svg> </div>
.container {
width: 100px;
height: 100px;
transition: 1s;
}
// I already tried play with transform-origin all the way possible, applying it to the container/svg tag/and all elements inside it...
This is not an answer but I need to add some code. I don't see any difference. Please take a look. First I'm using two elements image.
.container {
position: absolute;
top: 0;
width: 100px;
height: 100px;
border: 1px solid;
background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/apple.png");
background-size: cover;
transition: 1s;
}
.svg {
background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/apple.svg");
left: 250px;
}
.container:hover {
width: 200px;
height: 200px;
}
<div class="container"></div>
<div class="container svg"></div>
The second attempt: this time I'm using an svg element instead of a background image for the second div. No difference either.
.container {
position: absolute;
top: 0;
width: 100px;
height: 100px;
border: 1px solid;
background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/apple.png");
background-size: cover;
transition: 1s;
overflow:hidden;
}
.svg {
background:black;
left: 250px;
}
.container:hover {
width: 200px;
height: 200px;
}
<div class="container"></div>
<div class="container svg">
<svg viewBox="41 54 106 122">
<g>
<path fill="#FFFFFF" stroke="#ED1D24" stroke-width="2" stroke-miterlimit="10" d="M143.099,93.757c0,0-14.173,8.549-13.724,23.173 c0.449,14.624,11.954,23.413,15.974,24.073c1.569,0.258-9.245,22.049-15.984,27.448c-6.74,5.4-13.714,6.524-24.513,2.25c-10.8-4.275-18.449,0.275-24.749,2.612c-6.299,2.337-13.949-0.137-24.298-14.987c-10.349-14.849-21.823-49.271-6.074-66.146c15.749-16.874,33.298-10.124,38.022-7.875c4.725,2.25,13.05,2.025,22.499-2.25C119.7,77.782,138.374,86.782,143.099,93.757z"/>
</g>
<g>
<path fill="#FFFFFF" stroke="#ED1D24" stroke-width="2" stroke-miterlimit="10" d="M118.575,54.609c0,0,0.9,5.625-1.35,10.349
s-10.718,20.936-22.994,17.999c-0.308-0.073-2.102-5.506,0.532-11.027C98.48,64.138,108.171,55.399,118.575,54.609z"/>
</g>
</svg>
</div><div class="container"></div>
<div class="container svg">
<svg viewBox="41 54 106 122">
<g>
<path fill="#FFFFFF" stroke="#ED1D24" stroke-width="2" stroke-miterlimit="10" d="M143.099,93.757c0,0-14.173,8.549-13.724,23.173 c0.449,14.624,11.954,23.413,15.974,24.073c1.569,0.258-9.245,22.049-15.984,27.448c-6.74,5.4-13.714,6.524-24.513,2.25c-10.8-4.275-18.449,0.275-24.749,2.612c-6.299,2.337-13.949-0.137-24.298-14.987c-10.349-14.849-21.823-49.271-6.074-66.146c15.749-16.874,33.298-10.124,38.022-7.875c4.725,2.25,13.05,2.025,22.499-2.25C119.7,77.782,138.374,86.782,143.099,93.757z"/>
</g>
<g>
<path fill="#FFFFFF" stroke="#ED1D24" stroke-width="2" stroke-miterlimit="10" d="M118.575,54.609c0,0,0.9,5.625-1.35,10.349
s-10.718,20.936-22.994,17.999c-0.308-0.073-2.102-5.506,0.532-11.027C98.48,64.138,108.171,55.399,118.575,54.609z"/>
</g>
</svg>
</div>
Maybe you should edit your question and add more detail
I found the problem as well as the solution
All seems fine when animating only height and width, but some of my icons have top and left properties to animate to. And these ones are cousins the problem.
In my case it was looked like the svg is constantly "jumps" to the upper left corner while its container was resizing, but actually it was just snapping to the whole pixel value.
Animation of top/left properties are not subpixel, so in order to animate it smoothly it is better to animate X and Y, instead of manipulating them directly.
When I did so, the problem was gone.
Try to use the CSS attribute transform-origin see here
div {
transform-origin: 50% 50%;
}
transform-origin: 50% 50%; should center it.
Related
I have a colored div followed by an svg. When changing my browser window, sometimes a small white line appears. How can I get rid of it?
.wave {
background-image: url('data:image/svg+xml;charset=UTF-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320"><path fill="red" d="M0,96L80,85.3C160,75,320,53,480,90.7C640,128,800,224,960,266.7C1120,309,1280,299,1360,293.3L1440,288L1440,0L1360,0C1280,0,1120,0,960,0C800,0,640,0,480,0C320,0,160,0,80,0L0,0Z"/></svg>');
height:180px;
background-size:100%;
background-repeat: no-repeat;
margin-top:-20px;
background-color:white;
}
<div style="background-color:red;width:100%;height:100px">
</div>
<div class="wave">
hallo
</div>
These are rounding artefacts that cannot be avoided. background-color:white introduces a white rectangle behind the svg, but on top of the top div that might be slightly too large. Remove it, and the svg content will sit directly on top of the div.
If you need an explicitely white background, apply it to an element that sits behind both the top div and the svg. For example like this, avoiding an extra DOM object:
.top {
position: relative;
background-color:red;
width:100%;
height:100px;
}
.top::after {
content: "";
position: absolute;
top: 100%;
width:100%;
height:180px;
z-index: -1;
background-color:white;
}
.wave {
background-image: url('data:image/svg+xml;charset=UTF-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320"><path fill="red" d="M0,96L80,85.3C160,75,320,53,480,90.7C640,128,800,224,960,266.7C1120,309,1280,299,1360,293.3L1440,288L1440,0L1360,0C1280,0,1120,0,960,0C800,0,640,0,480,0C320,0,160,0,80,0L0,0Z"/></svg>');
height:180px;
background-size:100%;
background-repeat: no-repeat;
margin-top:-20px;
}
<div class="top">
</div>
<div class="wave">
hallo
</div>
I am trying to add dashed border top on a div using css. I have this:
https://jsfiddle.net/uexma4o6/74/
div {
display: inline-block;
width: 50px;
height: 50px;
border-top: 2px dashed #AAA;
}
The problem is that first and last dash are little longer than others. I think it's because actual borders on the left and right are included? How can I make all dashes same width? I am looking for solution without using border-image. Thanks.
Try this. you can adjust the size and width based on your requirement.
background-image: linear-gradient(to right, white 33%, rgba(255,255,255,0) 0%);
background-position: top;
background-size: 5px 3px;
background-repeat: repeat-x;
border-right-width: 2px;
You could use a SVG line within your div like -
<div>
<svg width="50" height="2" viewBox="0 0 50 2" xmlns="http://www.w3.org/2000/svg">
<line x1="0" y1="0" x2="50" y2="0" stroke-width="2" stroke="#AAA" stroke-dasharray="2" stroke-width="2"/>
</svg>
</div>
So I have heart on CSS.
But now I want to modify it and add inside dynamic image.
Here is what I have:
.heart {
position: relative;
width: 100px;
height: 90px;
float: left;
width: 100px;
height: 90px;
overflow: hidden;
}
.heart.right {
left: auto;
right: 0;
}
.heart:before,
.heart:after {
position: absolute;
content: '';
left: 50px;
top: 0;
width: 50px;
height: 80px;
background: #fc2e5a;
border-radius: 50px 50px 0 0;
transform: rotate(-45deg);
transform-origin: 0 100%;
}
.heart:after {
left: 0;
transform: rotate(45deg);
transform-origin: 100% 100%;
}
Here is my fiddle: https://jsfiddle.net/9g1qswdd/
All the existing answers help you to place an image on top of a heart shape but not crop or cut it into a heart shape. The only existing answer that would help you achieve the latter is the clip-path model but that code produces a different output shape (its probably more a sample on how to do than a direct answer to your question).
For inserting an image in to the heart shape (I assume you mean cutting the image into a heart shape), don't use your existing approach. It is very tough because you that CSS method creates the shape by using two rotated elements. So, you'd have to go through the pain of - (a) split the image into two bits (b) place each half on each side (c) reverse rotate the images to nullify the effect the initial rotation that was set on the element (d) set background-position for each half of the image such that they match accurately etc. Even after going through all these troubles, you'd still face problems when the image is dynamic because setting background-position via percentage value work a lot more differently than what we think.
Use SVG: SVG is the recommended tool for creating such complex shapes with a background that is not a solid color.
With SVG, we can easily create complex shapes using the path element and also add an image as background or fill. SVGs are scalable and so are highly useful in responsive design. Using SVGs also allow us greater control over the shape itself.
Below is a heart shape that is created using SVG and having an image inserted into it as background.
svg {
height: 200px;
width: 200px;
}
path {
fill: url(#bg-image);
}
body {
background-image: radial-gradient(circle, #3F9CBA 0%, #153346 100%);
}
<svg viewBox='0 0 100 100'>
<defs>
<pattern id='bg-image' width='1' height='1' patternUnits='objectBoundingBox'>
<image xlink:href='https://placeimg.com/100/100/nature/7' width='100' height='100' />
</pattern>
</defs>
<path d='M50,90 L20,60
A15,15 0 0,1 50,30
A15,15 0 0,1 80,60 z' />
</svg>
Below is a very short explanation of what the commands used in the path element's d attribute do. A more detailed explanation can be found in this MDN page:
M - Moves pen to the point specified by the coordinate given immediately after the command.
A - Draw an arc with the specified X and Y radius, ending at the point specified after command.
L - Draw a straight line from one specified point to another.
z - Close the path by drawing a straight line from path's last point to its first point.
You can also use a SVG based clip-path definition to clip the image like in the below snippet but the browser support for clip-path is lower.
img {
-webkit-clip-path: url(#clipper);
clip-path: url(#clipper);
}
body {
background-image: radial-gradient(circle, #3F9CBA 0%, #153346 100%);
}
<svg viewBox='0 0 100 100' height='0' width='0'>
<defs>
<clipPath id='clipper' clipPathUnits='objectBoundingBox'>
<path d='M.50,.90 L.20,.60
A.15,.15 0 0,1 .50,.30
A.15,.15 0 0,1 .80,.60 z' />
</clipPath>
</defs>
</svg>
<img src='https://placeimg.com/100/100/nature/7' />
<img src='https://placeimg.com/200/200/nature/7' />
Assuming that the heart is to be placed on a solid background, you can hide selectively the image with several gradients placed on the pseudo elements.
This has the advantage of enabling the use of cover or contain on the background image size:
.heart2 {
width: 300px;
height: 300px;
background-image: url(http://lorempixel.com/400/200);
background-size: cover;
position: relative;
border: solid 1px red;
}
.heart2:before {
content: '';
position: absolute;
width: 450px;
height: 450px;
left: -75px;
top: 75px;
transform: rotate(45deg);
background-image: linear-gradient(white, white), linear-gradient(white, white);
background-size: 50%;
background-repeat: no-repeat;
background-position: right top, left bottom;
}
.heart2:after {
position: absolute;
width: 114%;
height: 114%;
content: '';
left: -21px;
bottom: 70px;
transform: rotate(45deg);
background-image: radial-gradient(circle at center 170px, transparent 85px, white 70px), radial-gradient(circle at 170px center, transparent 85px, white 75px), linear-gradient(white, white);
background-size: 50%;
background-repeat: no-repeat;
background-position: right top, left bottom, left top;
}
<div class="heart2"></div>
your css should be
.heart-img{
position:absolute;
width:40px;
height:40px;
top:20px;
left:30px;
z-index:1000;
}
html
<div class="heart">
<img src="http://lorempixel.com/400/200/sports/1" class="heart-img">
</div>
https://jsfiddle.net/9g1qswdd/3/
If you want to add a dynamic image inside of the heart something like this should do the trick:
https://jsfiddle.net/kn1m081z/
I've added some simple CSS to set the size of the image like this:
.heart img
{
position:relative;
z-index:3;
left:34px;
top:20px;
width:30px;
height:30px;
background:#ff5500
}
I've given the image a background colour so you can see where the image is - once you dynamically add an image src to the image tag I have added you will see the dynamic image inside of the heart - this should be what you're after!
You can add background image on .heart and shift 5px bottom to the heart
.heart {
background-image: url('');
}
.heart:before,
.heart:after {
top: 5px;
}
I would suggest to make use of clip-path property of CSS. I tried making a dialog box:
.clip-path {
clip-path: polygon(5% 5%, 100% 0%, 100% 75%, 75% 75%, 75% 100%, 50% 75%, 0% 75%);
}
https://jsfiddle.net/9g1qswdd/5/
similarly you can try making a heart with clip-path property. To make life easier, you can use this tool to do that for you: http://bennettfeely.com/clippy/
you have to make img class "relative" and heart class "absolute"
<style>
.img
{
position:relative;
background-image: url('');
}
.heart{position:absolute; top:50; left:50%;}
</style>
<div class="img"><div class="heart"></div></div>
All day long I am working on making SVG responsive and reading various articles, and testing different kind of code even in stack overflow.
Unfortunatly I am unsucessful in making my svg expand based on the container div, like I normally do with images when I define max-width:100%
I am using SVG stacks as technique:
http://simurai.com/blog/2012/04/02/svg-stacks/
example of his code: http://jsfiddle.net/simurai/7GCGr/
I noticed that after a certain size of the svg, it doesn't expand anymore and his maximum size become 150px even if I give it 100% as width.
if I force by inserting width & height sizes it grows bigger, but it is not what I want. I want that it resizes ALWAYS based on the width of the container, even becoming something terribly huge without any size limitations.
In the svg the height and width are removed.
My code:
/* RESET - http://meyerweb.com/eric/tools/css/reset/ */
html, body, applet, object, iframe, svg, h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li,
fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, figure{
margin:0; padding:0; border:0; font-size:100%; font-weight:inherit; font-style:inherit;
font-family:inherit; text-decoration:inherit; text-align:left; background:transparent;
}
header, footer, figure, details, hgroup, section, article, nav, aside{display:block;}
img, object, audio, video, svg{width:100%;}
.vector{display:block; width:100%; height:auto;}
.vector embed{border:1px solid #f00; width:100%;}*/
.box{border:1px solid #f00; width:700px;}
<div class="box">
<object class="vector">
<embed src="http://life-is-simple.net/sprite.svg#winner-cup" />
</object>
<img class="vector" src="http://life-is-simple.net/sprite.svg#winner-cup">
<img class="vector" src="http://life-is-simple.net/sprite.svg#lottery-ticket">
<img class="vector" src="http://life-is-simple.net/sprite.svg#monitor-number">
<img class="vector" src="http://life-is-simple.net/sprite.svg#emblem">
</div>
I would really appreciate some help, I do not know what to test anymore to make it work it, especially when I see that simple included svg inserted they do go 100%, I was trying with and techniques but I do not see any difference (only IE behave expanding svg, but firefox and chrome do not)
Scale svg the how to:
You can set the width of the svg container so the image wil scale there after:
This requires:
svg element needs a viewBox
svg element width and height are 100%
.container1 {
border: 1px solid green;
display: inline-block;
width: 100px;
}
.container1 .shape1 {
width: 100%;
}
.container2 {
border: 1px solid green;
display: inline-block;
width: 200px;
}
.container2 .shape2 {
width: 100%;
}
<div class="container1">
width set to 100px
<svg class="shape1" viewBox="0 0 100 100">
<circle fill="#15d" cx="50" cy="50" r="50" />
</svg>
</div>
<div class="container2">
width set to 200px
<svg class="shape2" viewBox="0 0 100 100">
<circle fill="#a2b" cx="50" cy="50" r="50" />
</svg>
</div>
The commen error
This will scale the element but keep the aspect ratio of the <svg> image.
Example:
.text span {
display: inline-block;
border: 1px solid rgba(0,0,0, 0.2);
}
.text span:nth-child(2) {
margin-left: 110px;
}
.container1 {
display: inline-block;
border: 1px solid red;
width: 100px;
height: 200px;
}
.container1 .shape1 {
width: 100%;
height: 100%;
}
.container2 {
margin-left: 100px;
display: inline-block;
border: 1px solid green;
width: 200px;
height: 100px;
}
.container2 .shape2 {
width: 100%;
height: 100%;
}
<div class="text"><span>width set to 100px<br> height set to 200px</span>
<span>width set to 200px
<br>height set to 100px</span><div>
<div class="container1">
<svg class="shape1" viewBox="0 0 100 100">
<circle fill="#15d" cx="50" cy="50" r="50" />
</svg>
</div>
<div class="container2">
<svg class="shape2" viewBox="0 0 100 100">
<circle fill="#a2b" cx="50" cy="50" r="50" />
</svg>
</div>
The elemetent DONT SCALE!
The reason they "dont scale" is as mentioned the aspect ratio
There is actually a property for this: preserveAspectRatio
How do i use this propert?
Wel:
.text span {
display: inline-block;
border: 1px solid rgba(0, 0, 0, 0.2);
}
.text span:nth-child(2) {
margin-left: 110px;
}
.container1 {
display: inline-block;
border: 1px solid red;
width: 100px;
height: 200px;
}
.container1 .shape1 {
width: 100%;
height: 100%;
}
.container2 {
margin-left: 100px;
display: inline-block;
border: 1px solid green;
width: 200px;
height: 100px;
}
.container2 .shape2 {
width: 100%;
height: 100%;
}
<div class="text"><span>width set to 100px<br> height set to 200px</span>
<span>width set to 200px
<br>height set to 100px</span>
</div>
<div class="container1">
<svg class="shape1" viewBox="0 0 100 100" preserveAspectRatio="none">
<circle fill="#15d" cx="50" cy="50" r="50" />
</svg>
</div>
<div class="container2">
<svg class="shape2" viewBox="0 0 100 100" preserveAspectRatio="none">
<circle fill="#a2b" cx="50" cy="50" r="50" />
</svg>
</div>
The reason your image won't scale is because it doesn't have a viewBox.
You are linking to a <g> element. That group is inside an SVG that has a viewBox, but that viewBox won't be used. The browser will look for a viewBox on the outer-most <svg> element. That element doesn't have one.
To prove my point, copy the winner-cup viewBox to the root/outermost <svg> element. The SVG will now scale to 100%.
<svg id="icon" class="icon" version="1.1" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 971.9 842.9">
As to the reason why it was only scaling to a height of 150px. You will find the answer here:
SVG height percentage not working under Linux browsers?
In your case the parent element is <embed> (and <object>) rather than <body>, but the reason is the same.
This question already has answers here:
Easier way to create circle div than using an image?
(15 answers)
Closed 8 years ago.
i want to create a div with background color red and completely circular in shape
How can i do that?
Css or jquery both can be used
You can do following
FIDDLE
<div id="circle"></div>
CSS
#circle {
width: 100px;
height: 100px;
background: red;
-moz-border-radius: 50px;
-webkit-border-radius: 50px;
border-radius: 50px;
}
Other shape SOURCE
By using a border-radius of 50% you can make a circle.
Here is an example:
CSS:
#exampleCircle{
width: 500px;
height: 500px;
background: red;
border-radius: 50%;
}
HTML:
<div id = "exampleCircle"></div>
Demo
css
div {
width: 100px;
height: 100px;
border-radius: 50%;
background: red;
}
html
<div></div>
HTML div elements, unlike SVG circle primitives, are always rectangular.
You could use round corners (i.e. CSS border-radius) to make it look round. On square elements, a value of 50% naturally forms a circle. Use this, or even a SVG inside your HTML:
document.body.innerHTML+='<i></i>'.repeat(4);
i{border-radius:50%;display:inline-block;background:#F48024;}
svg {fill:#F48024;width:60px;height:60px;}
i:nth-of-type(1n){width:30px;height:30px;}
i:nth-of-type(2n){width:60px;height:60px;}
<svg viewBox="0 0 120 120" xmlns="http://www.w3.org/2000/svg">
<circle cx="60" cy="60" r="60"/>
</svg>
Use a border-radius property of 50%.
So for example:
.example-div {
border-radius: 50%
}
.circle {
border-radius: 50%;
width: 500px;
height: 500px;
background: red;
}
<div class="circle"></div>
see this FIDDLE