简体   繁体   中英

How to make SVG image a specific size when responsive screen hits certain display?

I don't know if I need to use javascript or not, but any idea how I can make a SVG image a specific size when the screen hits a certain display (example, 640×480 pixels)?

My SVG image is already responsive, but I want it to appear smaller in the mobile display.

thanks!

You could have a section in your CSS something like:

@media screen and (max-width: 480px) {

  svg {
    max-width: 200px;
    max-height: 200px;
  }

}

markup

  <svg 
    width="120" 
    height="120" 
    viewBox="0 0 120 120"
    xmlns="http://www.w3.org/2000/svg">
    <rect x="10" y="10" width="1000" height="1000"/>
  </svg>

</div>


styles

svg { /* make it generally responsive */
  display: block;
  width: 100%;
  height: 100%;
}

.box {
  max-width: 300px;
}

@media (min-width: 600px) { /* when 600px and bigger */
  .box {
    max-width: 200px;
  }
}

https://jsfiddle.net/sheriffderek/z9fw1ndr/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM