0

This fiddle demonstrates my problem: http://jsfiddle.net/pn04dsvq/

I need to use the transform: scale for the image at the top for viewports below 500px. However for some reason after the scaling is applied the image stays on the right. I would like the image to stay in the middle horizontally.

I've tested the transform: scale function on the text at the bottom and it manages to stay in the middle. Would you know how I can achieve this for the image at the top?

On some forums I've found a transform-origin property and when I add transform-origin: left; to the image container it keeps the image on the left, however when I add transform-origin: center; it does not make any difference.

Please note that I need to use only the transform: scale function on the image. Making the image responsive based on viewport size will not work for my application. Many thanks.

The HTML code:

<div id="grey-bar">
    <div id="image-container">
        <img src="http://www.refugeeweek.org.uk/Resources/RefugeeWeek2012/Images/CounterpointsArts3.bmp">
    </div>
</div>

<h3 id="main-heading">How can I make the image above to stay in the middle after the 'transform: scale' is applied for viewports below 500px? For some reason it stays on the right.</h3>

<div id="grey-bar">
    <div id="bottom-heading-container">
        <h1 id="bottom-heading">This sentance stays in the middle after the 'transform: scale' is applied.</h1>
    </div>
</div>

CSS:

div#grey-bar {
    width:100%;
    margin-left:auto;
    margin-right:auto;
    padding-top:30px;
    padding-bottom:30px;
    background-color:grey;
}
div#image-container {
    width:100%;
    margin-left:auto;
    margin-right:auto;
    text-align: center;
}
@media screen and (max-width:500px) {
    div#image-container {
        max-width:100%;
        margin-left:auto;
        margin-right:auto;
        text-align: center;
        transform: scale(0.5);
    }
}
h3#main-heading {
    max-width:728px;
    text-align:center;
    margin-left:auto;
    margin-right:auto;
    color:grey;
}
div#bottom-heading-container {
    width:100%;
    margin-left:auto;
    margin-right:auto;
    text-align: center;
}
@media screen and (max-width:500px) {
    div#bottom-heading-container {
        max-width:100%;
        margin-left:auto;
        margin-right:auto;
        text-align: center;
        transform: scale(0.5);
    }
}
h1#bottom-heading {
    max-width:728px;
    text-align:center;
    margin-left:auto;
    margin-right:auto;
    color:white;
}
DaBler
  • 2,471
  • 2
  • 24
  • 41
Piotr Berebecki
  • 6,698
  • 2
  • 28
  • 39

1 Answers1

0

On my Firefox the problem you described occurred. I suggest adding 'center' tag to solve it:

<center>   </center>

then it showed at center on my Firefox as expected.

Since you want your img centered in your container, it should look like this:

<div id="image-container">
  <center>
    <img/>
  </center>
</div>

So as to make it compatible to both newer html5 and older html resolver, you'd better keep both "center" tag and "text-align" attr. The code above showed to be like this:

like this on my Firefox

Chiron
  • 844
  • 1
  • 7
  • 19