1

Problem: Safari displays overlapping elements in CSS media query styling.

On Chrome and Firefox, everything displays properly. Here's a screenshot:

enter image description here

However, when I test on Safari desktop ( Safari 8.0, with browser width adjusted to mobile size) and on an actual mobile device (iphone 6.0 / iOS 8.3 / safari browser) -- article is getting overlapped by aside, which is overlapped by footer.

enter image description here

I have found an undesirable workaround -- by setting the min-height in the media query styling to something like 600px (for example). This will position the content so it would look ok on mobile device.

Since this is not a one-page website, I need the css to do this for me automatically, without me having to figure out the correct min-height on every page.

I'm new at this, so if you can please provide actual code, that will be most helpful. Your help is appreciated.

<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1">

<style>

#main {
border: 5px solid purple;
width:80%;
min-height:500px;
display: -webkit-flex;
display:         flex;
-webkit-flex-flow: row;
        flex-flow: row;
}

#main > article {
    min-width:400px;
      border: 5px solid green;
    -webkit-flex: 60%;
            flex: 60%;
}

#main > footer {
      border: 5px solid yellow;
}

/* Too narrow to support two columns */
@media all and (max-width: 640px) {

    #main {
    width:100%;
    -webkit-flex-flow: column;
          flex-direction: column;
    }

    #main > article {
    min-height:320px;
    }

    #main > aside {
    min-height:200px;
    }
}
</style>

<div id="container">
    <center>
    <div id="main">
      <article> 
      <div style="background-color:linen;width:100%;height:500px" id="div1"> 
        <div style="background-color:red;width:386px;height:386px"> 
        <p>article</p>
        </div>
      </div>
      </article>

      <aside style="border:5px solid black">
      <center>
      <div style="background-color:orange;width:300px;height:300px" id="div2">
      <p>aside</p>
      </div>
      </center>
      </aside>
    </div> 
    </center>

    <footer>
    <div style="background-color:green;width:100%;height:50px" id="div3">
    <p>footer</p>
    </div>
    </footer>
</div>
TheGrayVacuum
  • 703
  • 2
  • 7
  • 12
  • What mobile device (browser, version, OS) are you using that is causing this problem? – Maximillian Laumeister Aug 18 '15 at 00:46
  • Have you tried using percentage heights on the mobile view? – Michael Benjamin Aug 18 '15 at 01:10
  • The problem appears when I test with an iPhone 6 running iOS 8.3, and the browser is Safari. – TheGrayVacuum Aug 18 '15 at 01:26
  • @Michael_B, I just tested with percentage heights, by changing both article and aside to 100%, but when I tested on the device, that didn't fix the problem. – TheGrayVacuum Aug 18 '15 at 01:34
  • Is this the effect you're looking for? (re-size vertically) http://jsfiddle.net/e9vsf1tr/2/ – Michael Benjamin Aug 18 '15 at 01:39
  • I just tested on Safari 8.0 using iMac desktop running OS 10.10.1, and I see that the problem is with Safari. Firefox and Chrome both work fine on desktop. Chrome displays properly when testing on a mobile device. So it seems to be a Safari issue. – TheGrayVacuum Aug 18 '15 at 02:29
  • @Michael_B, thanks, but no actually that's not what I'm going for. The content will be of variable length, so I can't just divide the screen into sections of 30% vertical / 40% vertical, etc. There will be actual content in the article, aside and footer and that content will change / be of variable length. – TheGrayVacuum Aug 18 '15 at 02:31
  • @TheGrayVacuum I would edit your question and tags to make it clear that it's a Safari-only issue. – Maximillian Laumeister Aug 18 '15 at 02:40
  • @MaximillianLaumeister Done. I have updated the question, now that it is clear this is a safari issue. – TheGrayVacuum Aug 18 '15 at 05:09
  • @Michael_B -- thanks for checking in. Didn't get this solved yet, although I spent several hours yesterday working on it. – TheGrayVacuum Aug 19 '15 at 17:00

1 Answers1

1

Okay, I think I solved your problem.

I tested this solution in Safari on my iPad and it works.

The source of the problem is the HTML5 <article> tag.

Although article appears to be supported by all major browsers, Chrome and Firefox apply one behavior to this element, and Safari applies a different behavior (at least in this case). I haven't delved deep into the spec to know which behavior adheres more closely to the standard, so I can't tell you which rendering is correct.

But bottom line:

Replace the <article> tags with simple <div> tags. That will keep your layout intact in Safari.

I also tested the HTML5 aside and footer elements in your code and didn't notice any problems. Although if you hit any snags in the future that may be a good place to start troubleshooting.


Lastly, as a side note, you may want to consider finding a replacement for the HTML <center> tag. This tag has been deprecated and is currently in the process of being dropped by the browsers.

There are many other ways to center elements in a way that adheres to best practices. I recently wrote an answer that covers this topic: How to Center Elements Within a Div

Hope this helps. Good luck!

Community
  • 1
  • 1
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • thanks for the suggestion. I like the logic of your answer, and I appreciate the suggestions. Unfortunately, when I tested in Safari 8.0 on my iMac, it didn't solve the problem. Here is what I did: (1) I replaced article tags with div id="article1". (2) I updated the css from #main > article to #main > #article1. But as mentioned, safari is still overlapping the elements in mobile view. – TheGrayVacuum Aug 20 '15 at 15:51
  • Strange. Did you try simply replacing the `article` with the `div`? Just seeing if the elements alone work. Because that worked perfectly in Safari on my iPad. Also, like I mentioned in my answer, try switching `aside` and `footer` to `div`s, as well. – Michael Benjamin Aug 20 '15 at 15:55
  • Based on Safari testing on an iPhone and iPad, and the fact that the original layout doesn't break on other browsers, I'm pretty convinced this is an HTML5 elements issue. But of course I could be wrong. Would be interested to know if you found a solution. – Michael Benjamin Aug 20 '15 at 18:38
  • Yeah, it's weird. I replaced article and footer with div, which wasn't a complete solution for me. Then I started changing the names of div ids and removing various lines of css styling. Apparently the problem is caused by webkit. When I remove line 19 in the css > -webkit-flex: 60%; safari will no longer overlap elements in the mobile view. Unfortunately, this also removes the styling. My solution was just to redo the styling, using a variety of different width values in the divs, without using these elements: article, footer, and -webkit-flex:60%. At least I've got something working now :) – TheGrayVacuum Aug 20 '15 at 22:46
  • 1
    Thanks for your help, Michael_B. I really appreciate it! – TheGrayVacuum Aug 20 '15 at 22:47
  • Glad you got it to work (someway, somehow ;-) Good luck with your website. – Michael Benjamin Aug 20 '15 at 23:34