10

As part of my learning modern web development, I'm making a sample landing page using Bootstrap and jQuery. For demo purposes, the code was simplified with only HTML and CSS bits left.

The idea is to have several pages of HTML all on one page, each of which occupies 100% of browser's viewport.

Part of the problem is that on portable devices content often does not fit, so height:100% no longer works, and content overlaps. I tried various combinations of min-height and height, none of them seem to work. I would like to keep responsive design principle, and make content shrink as much as possible, but keep everything readable without overlap.

body {
  font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
  margin: 0;
}

#divAbout {
  background-color: white;
}

#divDownload {
  background-color: lightgoldenrodyellow;
}

#header {
  margin: 0;
  height: 50px;
  position: fixed;
  background-color: rgba(255, 255, 255, 0.25);
  width: 100%;
}

.page {
  text-align: center;
}

#divHome {
  background-color: lightblue;
}

#divHomeCenter {
  max-width: 700px;
  background-color: rgba(255, 255, 255, 0.35);
  border-radius: 20px;
  padding: 10px 40px;
  margin: 0 auto;
  position: relative;
  top: 200px;
}

#divDownloadCenter {
  padding-top: 150px;
}

html,
body,
.page {
  height: 100%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<div id="header">
  Bootstrap's header
</div>

<div id="divHome" class="page">
  <div id="divHomeCenter">
    <h1>My Awesome App</h1>
  </div>
</div>

<div id="divAbout" class="page">
  <div id="divAboutCenter">
    <h1>Why This App Is Awesome</h1>

    <div class="row">
      <div>
        <h3>Reason 1</h3>
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam interdum arcu at ante tempor, a pretium sem posuere. In hac habitasse platea dictumst. Integer lectus odio, porttitor et libero ac, mattis commodo risus. Morbi sit amet orci lacinia, feugiat
          nulla eu, tincidunt lacus. Fusce semper, neque vel iaculis mollis, lorem risus ultricies enim, quis congue metus neque a nibh. Curabitur ut turpis rutrum, volutpat odio quis, fringilla risus. Aliquam erat volutpat. Proin eget risus pretium,
          malesuada risus id, feugiat magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin placerat, ex ac varius aliquet, nisl nunc finibus nisl, at finibus odio neque a nulla.
        </p>
        <button>Sign Up</button>
      </div>
      <div>
        <h3>Reason 1</h3>
        <p>
          Nunc sed diam ac dui fringilla maximus. Mauris dignissim tincidunt nunc, vitae pulvinar nibh maximus eu. Aliquam eleifend libero non pharetra varius. Cras eleifend sagittis lectus, vel commodo mi mollis vel. Quisque tempus interdum mi, eget vulputate
          ligula feugiat tincidunt. Integer et diam gravida, vulputate tortor ac, tincidunt lectus. Nunc ac nisi felis. Aliquam ac felis sapien. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse eleifend
          ex neque, at sagittis ipsum venenatis in. Vivamus ac consequat arcu. Sed sit amet risus a elit viverra venenatis sed vel ligula. Curabitur tincidunt malesuada augue at suscipit. Nam semper tristique ultrices. Sed in lorem sem.
        </p>
        <button>Sign Up</button>
      </div>
      <div>
        <h3>Reason 1</h3>
        <p>
          Curabitur sapien nunc, rhoncus et justo nec, feugiat faucibus tellus. Sed in molestie nisi, at pretium turpis. Phasellus nulla augue, porttitor non elit sit amet, gravida auctor ante. Nulla malesuada nisl urna, sit amet facilisis magna porttitor ut. Nullam
          pretium, felis et pellentesque iaculis, libero nisl venenatis est, sit amet molestie augue mi at sapien. Ut imperdiet mauris eget ultricies tincidunt. Etiam tincidunt enim justo, in gravida massa ornare sit amet. Fusce id cursus orci, vel lobortis
          dui. Morbi pretium, felis eu lobortis vestibulum, erat mauris facilisis dui, et suscipit risus neque in nisi. Praesent pellentesque facilisis nisi sed tincidunt. Cras consectetur imperdiet elit, dictum tempor elit vehicula eget. Maecenas nec
          ex leo. Nunc eu erat eu erat venenatis faucibus a in nulla. Sed auctor suscipit nibh id consequat.
        </p>
        <button>Sign Up</button>
      </div>
    </div>
  </div>
</div>

<div id="divDownload" class="page">
  <div id="divDownloadCenter">
    <h1>Download The App!</h1>

    <button>Download</button>
  </div>
</div>

Demo on JSFiddle:

How to reproduce the problem on JSFiddle:

Simply scroll down, you'll see content overlapping.

How to reproduce the problem on Live version:

Shrink browser height to show half of internal container on page 1, then scroll down, you'll see page 2 starts immediately.

Before scroll (low screen height simulation - page #1 does not fit in 100% height):

enter image description here

After scroll (second page overlaps #1):

enter image description here

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • Post your code in your question please. – j08691 Feb 09 '16 at 17:27
  • Using `min-height` instead of `height` for the pages seems to solve it (see [updated fiddle](https://jsfiddle.net/koala_dev/94o8b9ng/5/)) or am I missing something? You would then just need to look into centering the content vertically – omma2289 Feb 09 '16 at 19:22
  • @koala_dev: Nope, doesn't work. See my screenshots - added to question. Content from page 2 overlaps #1, which is what I'm trying to avoid. – Victor Zakharov Feb 09 '16 at 19:32
  • @Neolisk That's weird, I don't see that in the updated fiddle. What browser are you testing on? – omma2289 Feb 09 '16 at 20:52
  • @koala_dev: I didn't update my fiddle, just used yours - did not seem to make a difference. What I'm seeing in Firefox here - I attached pictures to the question. – Victor Zakharov Feb 09 '16 at 22:15
  • Removed dead link. – Victor Zakharov Jun 24 '18 at 13:04

3 Answers3

1

I have a simple idea that might help you solve the issue. As I noticed the font size is always the same, whatever the screen resolution, therefore the content gets overlapped.

You can use media queries like these:

/* Small devices (tablets, 768px and up) */
@media (min-width: 768px) { 
  body{
    font-size: 9px;
  }
}

/* Medium devices (desktops, 992px and up) */
@media (min-width: 992px) { 
  body{
    font-size: 11px
  }
}

/* Large devices (large desktops, 1200px and up) */
@media (min-width: 1200px) { 
  body{
    font-size:13px;
  }
}

You can adjust the font sizes according to you application.

Another option is using Viewport Sized Typography units like vh and vw but they are not such a good solution, I guess.

Try this updated fiddle

Kristijan Iliev
  • 4,901
  • 10
  • 28
  • 47
  • This doesn't solve the problem. Depending on how much content I have and how small the screen size is, there will always be a case when it stops fitting. When it's just a header, yes, reducing font size helps, but imagine there is a paragraph of regular text there as well, which is already small enough. – Victor Zakharov Feb 09 '16 at 17:49
  • That's what responsive design is for. You must hide content on smaller resolutions – Kristijan Iliev Feb 09 '16 at 17:51
  • I can't fit one page of desktop website into one page of mobile, in most cases it's just impossible. Responsive design in my opinion is making sure content is accessible from all devices. Yes, usually there will be less content, but no guarantee of a 1-to-1 page map. – Victor Zakharov Feb 09 '16 at 17:52
  • "You must hide content on smaller resolutions" No you mustn't. Change the layout to fit the screen, don't arbitrarily remove elements just because you can't figure out where to put them. – Daniel Beck Feb 09 '16 at 19:44
  • @Daniel Beck Neolisk is correct, in that RWD you'll have to decide on what's crucial and what can be removed when in lower resolutions. How many phone layouts have you seen with navbars, banners, footers, headers, jumbotron, carousel, video backgrounds, etc..? – zer00ne Feb 09 '16 at 19:52
  • @zer00ne "remove jumbotrons, carousels and video background" !== "hide content". (Personally I'd argue that if it's inessential enough that you can remove it from mobile, you don't need it on desktop either.) – Daniel Beck Feb 09 '16 at 20:50
  • @DanielBeck I agree minimal essentials are best, I think most of that clutter should died off at the turn of the century, yet I deal with people who love that crap and you see in SO there are a ton of users that slop that stuff in their projects. – zer00ne Feb 09 '16 at 21:02
  • @zer00ne Right there with ya. I think we're on the same page, I just couldn't resist speaking out against the "just hide content from mobile users" attitude. – Daniel Beck Feb 09 '16 at 21:43
1

Updated

Here is an updated fiddle

I changed these CSS rules

html, body {
  height: 100%;  
}
.page {
  min-height: 100%;  
}
#divAbout h1 {
  margin: 0;
}

And I commented out the following

#divHomeCenter {
  max-width: 700px;
  background-color: rgba(255,255,255,0.35);
  border-radius: 20px;
  padding: 10px 40px;
  margin: 0 auto;
  position: relative;
  /* top: 200px;             */
}

When you set top: 200px; on an element that have position: relative;, it will push that element 200px down but leave all other element where they were initially, which will cause that pushed element to overlap the other(s).

Asons
  • 84,923
  • 12
  • 110
  • 165
  • This is almost perfect. I [updated your fiddle to move inner div back up](https://jsfiddle.net/LGSon/94o8b9ng/8/), using outer div's padding instead. The only problem left is this horizontal grey area between page 1 and 2, when you scroll down. It happens in JSFiddle's browser, just sits there, and on my Android's Chrome browser, where page 1's background suddenly stretches down 50px or so. Then when scrolling up, it goes back to normal. I believe it's the same issue in JSFiddle and Chrome for Android. Does not happen on desktop though. Do you know what could be causing it? – Victor Zakharov Feb 09 '16 at 22:32
  • Correction: updated fiddle - https://jsfiddle.net/94o8b9ng/9/, regarding this "stretch glitch", after some testing, seems to only happen on portrait layout, not on landscape. Phone is Galaxy S5. – Victor Zakharov Feb 09 '16 at 23:00
  • @Neolisk That extra glitch is caused by the `h1` element's margin. I updated my fiddle link and answer with a fix: `#divAbout h1 { margin: 0; }` ... if you want a space at the top, you can use padding as you did in the `#divDownloadCenter` – Asons Feb 10 '16 at 07:59
  • The grey area is gone, page jumping down on mobile seems to be another issue. It happens because address bar minimizes, and as a result height 100% changes, so content readjusts, and page 1 becomes higher. You can see it live [here](http://neolisk.pw/demo) - updated from your new fiddle, need to load on your phone, and scroll down until address bar hides itself. Is there a clean workaround? Or do I have to resort to letting JS handle resize event and fix page scroll to match old position? – Victor Zakharov Feb 10 '16 at 13:16
  • This seems to be a bug, or odd behavior by design :) ... http://stackoverflow.com/questions/24944925/background-image-jumps-when-address-bar-hides-ios-android-mobile-chrome – Asons Feb 10 '16 at 13:36
  • Ok, so I have to use JS it seems. Thanks for your help and accepting. – Victor Zakharov Feb 10 '16 at 13:46
0

I applied my usual CSS defaults:

html {
  box-sizing: border-box;
  font: 500 16px/1.4 'Raleway';
  height: 100vh;
  width: 100vw;
}
*, 
*:before, 
*:after { 
   box-sizing: inherit; 
   margin: 0; 
   padding: 0; 
}
body { 
   position: relative; 
   font-size: 1rem; 
   line-height: 1; 
   height: 100%; 
   width: 100%; 
}

Then I removed the following:

#divHomeCenter {...position: relative; top: 200px; }

In addition to this stack snippet I have provided the webpage unhindered by an editor.

html {
  box-sizing: border-box;
  font: 500 16px/1.4'Raleway';
  height: 100vh;
  width: 100vw;
}
*,
*:before,
*:after {
  box-sizing: inherit;
  margin: 0;
  padding: 0;
}
body {
  position: relative;
  font-size: 1rem;
  line-height: 1;
  height: 100%;
  width: 100%;
}
#divAbout {
  background-color: white;
}
#divDownload {
  background-color: lightgoldenrodyellow;
}
#header {
  margin: 0;
  height: 50px;
  position: fixed;
  background-color: rgba(255, 255, 255, 0.25);
  width: 100%;
}
.page {
  text-align: center;
  height: 100vh;
}
#divHome {
  background-color: lightblue;
}
#divHomeCenter {
  max-width: 700px;
  background-color: rgba(255, 255, 255, 0.35);
  border-radius: 20px;
  padding: 10px 40px;
  margin: 0 auto;
  /*position: relative; top: 200px;*/
}
#divDownloadCenter {
  padding-top: 150px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->

  <title>Height 100% demo</title>

  <link href='https://fonts.googleapis.com/css?family=Raleway:600' rel='stylesheet' />
</head>

<body>
  <div id="header">Bootstrap's header</div>
  <div id="divHome" class="page">
    <div id="divHomeCenter">
      <h1>My Awesome App</h1>
    </div>
  </div>
  <div id="divAbout" class="page">
    <div id="divAboutCenter">
      <h1>Why This App Is Awesome</h1>
      <div class="row">
        <div>
          <h3>Reason 1</h3>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam interdum arcu at ante tempor, a pretium sem posuere. In hac habitasse platea dictumst. Integer lectus odio, porttitor et libero ac, mattis commodo risus. Morbi sit amet orci lacinia,
            feugiat nulla eu, tincidunt lacus. Fusce semper, neque vel iaculis mollis, lorem risus ultricies enim, quis congue metus neque a nibh. Curabitur ut turpis rutrum, volutpat odio quis, fringilla risus. Aliquam erat volutpat. Proin eget risus
            pretium, malesuada risus id, feugiat magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin placerat, ex ac varius aliquet, nisl nunc finibus nisl, at finibus odio neque a nulla.</p>
          <button>Sign Up</button>
        </div>
        <div>
          <h3>Reason 1</h3>
          <p>Nunc sed diam ac dui fringilla maximus. Mauris dignissim tincidunt nunc, vitae pulvinar nibh maximus eu. Aliquam eleifend libero non pharetra varius. Cras eleifend sagittis lectus, vel commodo mi mollis vel. Quisque tempus interdum mi, eget
            vulputate ligula feugiat tincidunt. Integer et diam gravida, vulputate tortor ac, tincidunt lectus. Nunc ac nisi felis. Aliquam ac felis sapien. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse
            eleifend ex neque, at sagittis ipsum venenatis in. Vivamus ac consequat arcu. Sed sit amet risus a elit viverra venenatis sed vel ligula. Curabitur tincidunt malesuada augue at suscipit. Nam semper tristique ultrices. Sed in lorem sem.</p>
          <button>Sign Up</button>
        </div>
        <div>
          <h3>Reason 1</h3>
          <p>Curabitur sapien nunc, rhoncus et justo nec, feugiat faucibus tellus. Sed in molestie nisi, at pretium turpis. Phasellus nulla augue, porttitor non elit sit amet, gravida auctor ante. Nulla malesuada nisl urna, sit amet facilisis magna porttitor
            ut. Nullam pretium, felis et pellentesque iaculis, libero nisl venenatis est, sit amet molestie augue mi at sapien. Ut imperdiet mauris eget ultricies tincidunt. Etiam tincidunt enim justo, in gravida massa ornare sit amet. Fusce id cursus
            orci, vel lobortis dui. Morbi pretium, felis eu lobortis vestibulum, erat mauris facilisis dui, et suscipit risus neque in nisi. Praesent pellentesque facilisis nisi sed tincidunt. Cras consectetur imperdiet elit, dictum tempor elit vehicula
            eget. Maecenas nec ex leo. Nunc eu erat eu erat venenatis faucibus a in nulla. Sed auctor suscipit nibh id consequat.</p>
          <button>Sign Up</button>
        </div>
      </div>
    </div>
  </div>
  <div id="divDownload" class="page">
    <div id="divDownloadCenter">
      <h1>Download The App!</h1>
      <button>Download</button>
    </div>
  </div>
</body>

</html>
zer00ne
  • 41,936
  • 6
  • 41
  • 68
  • You moved inner block on the first page up, so now it overlaps with fixed header, which is another issue. The original problem is still unsolved - if you shrink browser to half height, and scroll down, you'll see page #3 overlapping page #2. – Victor Zakharov Feb 09 '16 at 22:21