How can I make my div's svg background image scale dynamically and stay in the center of the div

lacrosse1991

I'm currently working on the following website design:

enter image description here

I have two sections (top and bottom) that are both divided into two sections (icon and content). I'm trying to use a centered svg image in the icon section although the image gets cut off whenever the size of the viewing area/browser window changes. How can I prevent this from happening?

Here is what I am currently using:

Example - js fiddle link

SVG Image - svg image

HTML -

<div id="main1">
   <div id="icon1">&#160;</div>​ 
   <div id="links1">
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
   </div>
</div>
<br/>
<div id="main2">
   <div id="icon2">​​​</div>
   <div id="links2">
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
   </div>
</div>
<div class="ms-rtestate-read ms-rte-wpbox" contenteditable="false">
   <div class="ms-rtestate-notify  ms-rtestate-read a64b9522-0ca6-4ff7-a9fc-67a7a27d3071" id="div_a64b9522-0ca6-4ff7-a9fc-67a7a27d3071" unselectable="on">
   </div>
   <div id="vid_a64b9522-0ca6-4ff7-a9fc-67a7a27d3071" unselectable="on" style="display: none;">
   </div>
</div>

CSS -

<style>
#main1 > div { min-height: 100px; }
#main1 {
    /* overflow to handle inner floating block */
    overflow: hidden;
    border-radius: 25px;
    border: 2px solid #73AD21;
    padding: 20px; 
    margin-right: 5%;
    margin-left: 5%;
}

#icon1 {
    float: left;
    width: 29%;
    border-right:thin solid red;
}

#links1 {
    float: right;
    width: 70%;
}
#main2 > div { min-height: 100px; }
#main2 {
    /* overflow to handle inner floating block */
    overflow: hidden;
    border-radius: 25px;
    border: 2px solid #73AD21;
    padding: 20px; 
    margin-right: 5%;
    margin-left: 5%;
}

#icon2 {
    float: left;
    width: 29%;
    border-right:thin solid red;
    /* background: url('/support/Shared%20Documents/065__calendar.svg') no-repeat center; */
    background-image: url('/support/Shared%20Documents/065__calendar_crop.svg');
    background-repeat: no-repeat;
    background-size: cover;
}

#links2 {
    float: right;
    width: 70%;
}
</style>
justinw

Use background-size: contain instead of cover and use background-position: center center to keep it centered.

DEMO

Review the documentation from W3schools:

Cover

Scale the background image to be as large as possible so that the background area is completely covered by the background image. Some parts of the background image may not be in view within the background positioning area

Contain

Scale the image to the largest size such that both its width and its height can fit inside the content area

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Make image scale with and stay in middle of div

From Dev

How can i make the div clickable with entire background image?

From Dev

How can I make a fixed background image not stay fixed on scroll?

From Dev

How can get the div background image to scale to the page size?

From Dev

twitter-bootstrap: make a row (div) background image auto scale and center to fill 100%?

From Dev

twitter-bootstrap: make a row (div) background image auto scale and center to fill 100%?

From Java

when I add an svg dynamically in the height of my div overflows, how can I prevent it?

From Dev

Using angular, how can I change a div's background image to match my user object's profileImage property?

From Dev

How can I make my icon stay in the center of my accordion header?

From Dev

How do I make my div scale to larger than it's original size using css animation?

From Dev

How can I make an image in a div responsive?

From Dev

How do I make my div with a background image and no content show and extend from the top to bottom of the page?

From Dev

How do I make my <div> stay in one position while I move the page around

From Dev

How can I center a div inside a div?

From Dev

How do I add a background image on my DIV tag with CSS

From Dev

Scale div to fit background image

From Dev

How do I make my DIV and SELECT elements stay on the same horizontal plane?

From Dev

How to center a background div?

From Dev

How do I make my website's background image responsive?

From Dev

How can i make a div stay below two other divs that are laternating with jqueryui slide?

From Dev

How do I scale my SVG to fit the size of its container div in d3?

From Dev

how to make div background image responsive

From Dev

How to make div background image responsive?

From Dev

How do I make the <div> in the center of the page?

From Dev

How do I make the div to stay at the top when rotatingX

From Dev

How do I make this div to stay in a position after it has moved?

From Dev

How do I make the div to stay at the top when rotatingX

From Dev

Dynamically resize div for background image

From Dev

How can I dynamically update a div's max-height by just using the div above and the div below?

Related Related

  1. 1

    Make image scale with and stay in middle of div

  2. 2

    How can i make the div clickable with entire background image?

  3. 3

    How can I make a fixed background image not stay fixed on scroll?

  4. 4

    How can get the div background image to scale to the page size?

  5. 5

    twitter-bootstrap: make a row (div) background image auto scale and center to fill 100%?

  6. 6

    twitter-bootstrap: make a row (div) background image auto scale and center to fill 100%?

  7. 7

    when I add an svg dynamically in the height of my div overflows, how can I prevent it?

  8. 8

    Using angular, how can I change a div's background image to match my user object's profileImage property?

  9. 9

    How can I make my icon stay in the center of my accordion header?

  10. 10

    How do I make my div scale to larger than it's original size using css animation?

  11. 11

    How can I make an image in a div responsive?

  12. 12

    How do I make my div with a background image and no content show and extend from the top to bottom of the page?

  13. 13

    How do I make my <div> stay in one position while I move the page around

  14. 14

    How can I center a div inside a div?

  15. 15

    How do I add a background image on my DIV tag with CSS

  16. 16

    Scale div to fit background image

  17. 17

    How do I make my DIV and SELECT elements stay on the same horizontal plane?

  18. 18

    How to center a background div?

  19. 19

    How do I make my website's background image responsive?

  20. 20

    How can i make a div stay below two other divs that are laternating with jqueryui slide?

  21. 21

    How do I scale my SVG to fit the size of its container div in d3?

  22. 22

    how to make div background image responsive

  23. 23

    How to make div background image responsive?

  24. 24

    How do I make the <div> in the center of the page?

  25. 25

    How do I make the div to stay at the top when rotatingX

  26. 26

    How do I make this div to stay in a position after it has moved?

  27. 27

    How do I make the div to stay at the top when rotatingX

  28. 28

    Dynamically resize div for background image

  29. 29

    How can I dynamically update a div's max-height by just using the div above and the div below?

HotTag

Archive