1

I am a complete newbie to HTML & CSS3. I am learning as I go along and try to build a new webpage. My image gallery works in all browsers except Opera and the width of a portrait image is distorted only in Opera, but works fine with a landscape image.

HTML

    <!-- Gallery Image 1 -->
            <div class="thumbox">
                    <a href="#openphoto1">
                        <img src="http://www.ghyllfarm.co.uk/_fiddle/image_1_thumb.jpg" width="110" height="110" alt=" ">
                    </a>
                <div id="openphoto1" class="modalDialog">
                    <div id="landscapephoto">
                        <a href="#close" title="Close" class="close">X
                        </a>
                            <img src="http://www.ghyllfarm.co.uk/_fiddle/image_1.jpg" width="780" height="520" alt=" "> 
                        <div class="photolabel">Landscape Image
                        </div>
                    </div>
                </div>
            </div>
                <div class="thumblabel">Click to open
                </div>


    <!-- Gallery Image 2 -->

            <div class="thumbox">
                    <a href="#openphoto2">
                        <img src="http://www.ghyllfarm.co.uk/_fiddle/image_2_thumb.jpg" width="110" height="110" alt=" ">
                    </a>
                <div id="openphoto2" class="modalDialog">
                    <div id="portraitphoto">
                        <a href="#close" title="Close" class="close">X
                        </a>
                            <img src="http://www.ghyllfarm.co.uk/_fiddle/image_2.jpg"  height="780" width="520" alt=" ">    
                        <div class="photolabel">Portrait Image
                        </div>
                    </div>
                </div>
            </div>
                <div class="thumblabel">Click to open
                </div>

CSS

img {
width: 100%;
height: 100%;
width: auto\9; /* ie8 */
}

.thumbox {
width: 110px;
height: 110px;
Margin: 0px auto 0px auto;
}

.thumblabel {
width: 110px;
height: 20px;
Margin: 10px auto 10px auto;
font-family:Tahoma, Geneva, sans-serif;
font-size: 12px;
text-align: center;
color:#060;
}

.modalDialog {
position: fixed;
font-family: Arial, Helvetica, sans-serif;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0,0,0,0.6);
z-index: 99999;
opacity:0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;
} 

.modalDialog:target {
opacity:1;
pointer-events: auto;
}

.modalDialog > div {
position: fixed;
top: 50%;
left: 50%;
-ms-transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
padding: 15px 15px 5px 15px;
border-radius: 10px;
background: #fff;
}

#landscapephoto {
width: 50%;
height: auto;
}

#portraitphoto {
width: auto;
height: 70%;
padding-bottom: 40px;
}

.close {
background: #606061;
color: #FFFFFF;
line-height: 25px;
position: absolute;
right: -12px;
text-align: center;
top: -10px;
width: 24px;
text-decoration: none;
font-weight: bold;
-webkit-border-radius: 12px;
-moz-border-radius: 12px;
border-radius: 12px;
-moz-box-shadow: 1px 1px 3px #000;
-webkit-box-shadow: 1px 1px 3px #000;
box-shadow: 1px 1px 3px #000;
}

.close:hover { background: #00d9ff; }

.photolabel {
width: 300px;
height: 20px;
Margin: 10px auto 10px auto;
font-family:Tahoma, Geneva, sans-serif;
font-size: 14px;
text-align: center;
color:#060;
}
RHP
  • 13
  • 4
  • 1
    Which version? Could you create a fiddle or publish this somewhere? – Gabriel May 03 '16 at 21:44
  • The first thing I'd try is rename one of your `div`s with `id="gallery_imgbox"` - IDs should be unique on your page. And as @Gabriel said, please specify which version of Opera you mean (I'm guessing the old Opera 12.x based on Presto, not the latest based on Blink?), and creating a [JS Fiddle](https://jsfiddle.net/) of this problem would help others confirm/test your issue. And finally... welcome to SO. :-) – Amos M. Carpenter May 04 '16 at 00:48
  • I have been testing with Opera version 36.0.2130.65. I have uploaded some images and created a JS Fiddle https://jsfiddle.net/RHPGhyll/5fvm7rgb/3/ (I hope it's right as I've never used it before) My fiddle has the same problem in Opera with the width being distorted on the portrait image. The fiddle also seems to distort the image so I assume it is something to do with the scaling, but I cannot see how to sort it. p.s. thanks for the welcome, Stackoverflow has been an invaluable source when learning to code. – RHP May 04 '16 at 17:04
  • @AmosM.Carpenter I forgot to mention why I had not used width: 30%; and height: auto; as for the landscape image. If I do that, the height becomes distorted in Safari and the text doesn't centre when the window is re-sized. – RHP May 06 '16 at 07:17
  • @RHP: Thanks for supplying the browser version and the fiddle. I'm still not sure exactly what you're saying is wrong, though - how is the width distorted? Opera is based on Blink, i.e. uses the same rendering engine as Chrome, Vivaldi, and other derivatives, so I very much doubt it "works in all browsers except Opera". From looking at your fiddle, it seems there's some JavaScript missing that, I'm guessing, should show the divs with the larger pictures when you click on the thumbnails. You can add JavaScript to your fiddle and edit your question to link to it. – Amos M. Carpenter May 06 '16 at 10:20
  • @AmosM.Carpenter The landscape img is 4wide by 3 high proportionally and the portrait around 2.5w x 4h. When I click the thumbnail it should open a modal dialogue with the image scaled to fit the browser window and shrinks when you reduce the window size. This works for the landscape in all browsers but the portrait only looks right in every browser but not Opera. In Opera (and you are right Chrome too) it looks to be 4 X 4, almost square. The fiddle works for me and the thumbnails display the modal ok. The Landscape shrinks proportionally, but the Portrait won't. – RHP May 06 '16 at 15:44
  • @AmosM.Carpenter My brain hurts already with just HTML & CSS so I haven't got around to learning Javascript yet. I am afraid I have no idea how to edit to do what you are suggesting. Could the fiddle work for me but not for other users? As the fiddle is a reduced window it shows that the Landscape img scaling correctly, but the Portrait not scaling correctly, when I access it. – RHP May 06 '16 at 16:08
  • @RHP: My bad about the JavaScript, I simply skimmed over the fiddle and assumed JS was missing, so didn't even try clicking on the images... please see my answer below. – Amos M. Carpenter May 07 '16 at 04:03

1 Answers1

0

If I understand you correctly, you're basically trying to maintain the aspect ratio of your portrait and landscape images when the window gets resized. Adapting this answer to a very similar question for your case, what you need to do is use viewport units, something like this:

#landscapephoto {
    width: 50vw; /* 50% of viewport width */
    height: 37.5vw; /* = 50 * (3/4), i.e. three-quarters of viewport width */
}

#portraitphoto {
    width: 46.67vh; /* = 70 * (2/3), i.e. two-thirds of viewport height */
    height: 70vh; /* 70% of viewport height */
    padding-bottom: 40px;
}

Here's the forked fiddle:

https://jsfiddle.net/7cb24j8d/

I've kept your initial percentages of landscape width (50%) and portrait height (70%) and set the other width and height to be relative to those, but obviously you'll be able to adjust these numbers to your liking. You'll also have to adjust the div.photolabel, as it's currently moving off-centre relative to the image when you resize (but that's a different question...). You should also check caniuse to make sure your target browsers all support viewport units.

Community
  • 1
  • 1
Amos M. Carpenter
  • 4,848
  • 4
  • 42
  • 72
  • M.Carpenter Many thanks I have played around with the fiddle and all seems to work the way I expected. The other answer is a big help as well (shame I missed aspect ratio in my earlier searches) and I will read up on viewport units. – RHP May 07 '16 at 18:37
  • @RHP: Glad to help. If it helped solve your problem, please feel free to accept the answer. – Amos M. Carpenter May 08 '16 at 00:09
  • I have tidied up my code and corrected the image size so the 3:2 ratio works properly. I have also revised the image location so that this post will still work after I review our existing website. I have revised the JS Fiddle to reflect these changes https://jsfiddle.net/RHPGhyll/g81bvdqt/ These all work now in IE, Firefox, Opera and Chrome, but they won't in Safari. I have tried your forked fiddle and this doesn't seem to work in Safari either. I have checked http://caniuse.com/#feat=viewport-units and they should all work. Any suggestions? – RHP May 08 '16 at 17:09
  • @RHP: I don't use Safari, so I'm afraid I can't help you there. I suggest asking a separate question since this is a separate issue, tagging it with Safari, and specifying what exactly "doesn't seem to work". Link to this question for context, and paste your fiddle link. Good luck. – Amos M. Carpenter May 08 '16 at 23:25