-1

I am trying to center an svg file into its parent div without success.

I have created this fiddle -> https://jsfiddle.net/wfukyd4q/

where you can see I did the following:

<div class="col-md-10 col-md-offset-1 col-sm-12 text-center">
    <div style="background-color:#489734;height:500px">
         <img src="https://42f2671d685f51e10fc6-b9fcecea3e50b3b59bdc28dead054ebc.ssl.cf5.rackcdn.com/illustrations/compose_music_ovo2.svg">
    </div>
</div>

My goal is to make the svg let's say 50% and put it in the centre, but to be responsive for mobile devices too.

Thank you

Irina Potapova
  • 784
  • 6
  • 17
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179
  • 1
    have you tried to set `width: 50%;` for your `img`? – Irina Potapova Dec 10 '18 at 14:21
  • @IrinaPotapova I did, but it makes sense only in the desktop. If you try it on mobile it looks tiny – EnexoOnoma Dec 10 '18 at 14:23
  • 1
    it looks tiny because 50% of a mobilescreen is tiny. You can use a media query to adjust that – Naomi Dec 10 '18 at 14:25
  • Hi its not technically an svg when you put it in an img tag. Just to let you know, if you wanted to adjust it like you would an svg – Craicerjack Dec 10 '18 at 14:25
  • Possible duplicate of [How to make an image center (vertically & horizontally) inside a bigger div](https://stackoverflow.com/questions/388180/how-to-make-an-image-center-vertically-horizontally-inside-a-bigger-div) – Craicerjack Dec 10 '18 at 14:26
  • Possible duplicate of [vertical center svg in div container](https://stackoverflow.com/questions/39420338/vertical-center-svg-in-div-container) – Rob Dec 11 '18 at 14:24

4 Answers4

2

Let's say your image has class svg-image:

<img class="svg-image" src="https://42f2671d685f51e10fc6-b9fcecea3e50b3b59bdc28dead054ebc.ssl.cf5.rackcdn.com/illustrations/compose_music_ovo2.svg">

Please add css similar to this:

.svg-image {
    width: 50%;
}
@media only screen and (max-width: 992px) {
    width: 90%;
}

You can specify several @media queries for different screen sizes and apply desired width of the image.

EDIT:

To center your image vertically and horizontally you can add styles to your div (which wraps your image):

.class-of-your-div {
    display: -webkit-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex; 
    justify-content: center;
    align-items: center;
}
Irina Potapova
  • 784
  • 6
  • 17
1

The CSS background-* properties will do what you want.

div.text-center div {
  background-color: #489734;
  height: 500px;
  background-image: url("https://42f2671d685f51e10fc6-b9fcecea3e50b3b59bdc28dead054ebc.ssl.cf5.rackcdn.com/illustrations/compose_music_ovo2.svg");
  background-size: auto 50%;
  background-repeat: no-repeat;
  background-position: center;
}
<div class="col-md-10 col-md-offset-1 col-sm-12 text-center">
  <div></div>
</div>

https://jsfiddle.net/wfukyd4q/7/

If this doesn't exactly behave how you want on mobile devices, then you can use media queries to tweak the CSS as needed for certain screen sizes.

Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181
0

Have you tried giving the image element an Id then referencing that element inside of your CSS like so:

div#foo {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}

where your img tag has an example id of 'foo'

Update:

You might need to change the position of your parent like so:

  #parent {
    position: absolute

   }

div#foo {
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 50%;
}
0

Yet an other way of doing it:

.col-md-10{position:relative;}
.col-md-10 img{
  display:block;
  width:50%;
  height:auto;
  position:absolute;
  margin:auto;
  top:0;bottom:0;right:0;left:0;
}
<div class="col-md-10 col-md-offset-1 col-sm-12 text-center">
                <div style="background-color:#489734;height:500px">
                    <img src="https://42f2671d685f51e10fc6-b9fcecea3e50b3b59bdc28dead054ebc.ssl.cf5.rackcdn.com/illustrations/compose_music_ovo2.svg">
                </div>

</div>
enxaneta
  • 31,608
  • 5
  • 29
  • 42