1

I'm having trouble getting the background to display correctly on my landing page. On my desktop, it shows up fine, but on mobile, it appears to be centered in the middle vertically at the top of the page. I'm new to front-end and have been trying all sorts of hacks over the past 4 hours. Could somebody point me in the right direction?

I've set the image to scale to cover the entire screen. There shouldn't be any blank areas. I've tried using responsive modes on my desktop, and the Wright Glider mostly stayed in view as I resized, so the image should also center in the middle of the ... viewport?/window.

For the background, I have a normal sized image, and a cropped image I use for smaller devices

My site is at http://we-fly.ddns.net

Tested only with Chrome 49 on all devices, Android is v5.1

Responsive mode on the desktop doesn't seem to produce the same results.

Source: https://gist.github.com/yanalex981/992a60dd54be82162a45

Screenshots:

Also, if anybody has any suggestions, please share them with me

Alex
  • 3,111
  • 6
  • 27
  • 43
  • @Metroidaron although it still doesn't cover the entire page on mobile, but at least it's not cut off halfway down now. It's a great improvement over what I had tried for the past 4 hours! What did you do? The only difference I can see is the `background-position` property – Alex Mar 27 '16 at 05:46
  • See my updated Answer to learn what I did :) – Metroidaron Mar 27 '16 at 05:50
  • @Metroidaron oh and I think you had all my sources. It's only about 4 files – Alex Mar 27 '16 at 05:50
  • I had the CSS file and the index, No images, and everything was relative paths so I had to change urls, but I got it working in a jiffy :) – Metroidaron Mar 27 '16 at 05:50
  • 1
    Now check my link, I modified your code a little more to yeild better results – Metroidaron Mar 27 '16 at 06:02
  • Ok, I have added my final answer/suggestions on how you should go about getting your background to display, I am going to delete my comment with my link in it, and I'm going to remove your site (copy) from my server... Hope This was all helpful, comment on the answer if you have any questions. :) Happy coding! – Metroidaron Mar 27 '16 at 06:21
  • @Metroidaron It was. Now I have some options to work around the issue. Thanks! – Alex Mar 27 '16 at 06:22

4 Answers4

2

To fix the issue of your image getting cut off part way, you need to set your background position in your media query's to initial... So in each of your css Media query's paste the following code:

background-position: initial; 

This will reset your background position to default when on mobile... from here you should be able to apply different styles to stretch/expand the image to your liking. :)

Metroidaron
  • 357
  • 2
  • 10
1

This is my second answer, I created this instead of adding it to my initial answer because the method is different.

In your HTML (index) file, add the image like this:

<img id="imgBackground" src="http://we-fly.ddns.net/images/back.jpg" />

(I suggest adding it directly after the body tag)

In your CSS, I am just going to post the entire thing, Its a little funny because your last @Media (Min-Width: 601px) is overriding your default for your desktop page... you may want to consider deleting this Media Query... See comments in code below to see changes:

    /* Set initial values for your image background */
#imgBackground{
    position:absolute; /* Absolute Positioning so we can use "top" and "Left" */
    top:0px; /* Set top edge to 0px (Top of the screen) */
    left:0px; /* Set left edge of image to 0px, so it starts at the left edge of the screen. */
    width:100%; /* We want our widt to be 100% of our browser window */
    height:auto; /* we don't care about the image height, so let it set itself based on the image's proportions */
    position:fixed; /* Make our image scroll with the user so if they can scroll they don't leave our background behind, exposing the white "body" tag background. */
}

body {
    text-align: center;
    background-position: center center;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: cover;
    margin: 0;
    padding: 0;
}

#quote-conatiner {
    position: fixed;
    margin: auto auto 24px auto;
    bottom: 20%;
    left: 0;
    right: 0;
    text-align: center;
    background-color:rgba(180, 180, 180, .4);
}

h3 {
    font-family: Garamond sans-serif;
    color: white;
    width: 80%;
    margin: .5em auto .5em auto;
}

.button {
    font-family: sans-serif;
    text-decoration: none;
    padding: .3em 0.6em;
    border-radius: 8px;
    border: 2px solid #59169c;
    background-color: #417;
    color: white;
    box-shadow: 0 0 64px black;
}

.button-container {
    position: fixed;
    margin-left: auto;
    margin-right: auto;
    top: 80%;
    left: 0;
    right: 0;
}

.button:active {
    background-color: #330855;
}

@media only screen and (max-width: 600px) {

/* set image background to achieve max Height and we don't care about width (auto) on mobile displays. */
    #imgBackground{
        position:absolute;
        top:0px;
        left:0px;
        width:auto;
        height:100%;
        position:fixed;
    }

    body {
        margin: 0;
        padding: 0;
    }

    h3 {
        font-size: 1.2em;
    }

    .button {
        font-size: 1.4em;
    }
}

@media only screen and (max-width: 400px) {

    /* set image background to achieve max Height and we don't care about width (auto) on mobile displays. */
    #imgBackground{
        position:absolute;
        top:0px;
        left:0px;
        width:auto;
        height:100%;
        position:fixed;
    }

    body {
        margin: 0;
        padding: 0;
    }

    h3 {
        font-size: 0.8em;
    }

    .button {
        font-size: 1.0em;
    }
}

/* May want to consider getting rid of this Query, if you don't it is overriding your styles set above your media querys. */
@media only screen and (min-width: 601px) {

    /* Set image background qualities for any display larger than 601px.*/
    #imgBackground{
        position:absolute;
        top:0px;
        left:0px;
        width:100%;
        height:auto;
        position:fixed;
    }

    body {
        margin: 0;
        padding: 0;
    }

    h3 {
        max-width: 600px;
        font-size: 1.3em;
    }

    .button {
        font-size: 1.3em;
    }
}
Metroidaron
  • 357
  • 2
  • 10
0

"background-size: cover" does not cover mobile screen

and

height: 100% or min-height: 100% for html and body elements?

held the answer. I had to set the height of the root elements for background-size: cover to work:

html {
    height: 100%;
}

body {
    min-height: 100%;
}

It works on the Nexus 4 and on the Galaxy Tab

Reason for having to do this (stolen from last answer):

Incidentally, the reason why you have to specify height and min-height to html and body respectively is because neither element has any intrinsic height. Both are height: auto by default. It is the viewport that has 100% height, so height: 100% is taken from the viewport, then applied to body as a minimum to allow for scrolling of content.

Community
  • 1
  • 1
Alex
  • 3,111
  • 6
  • 27
  • 43
-1

@Alex how exactly you want image to be display...for different screen size you can use the @media css property to resize image as per screen size.

@media screen and (min-width: 480px) {
body {
    property: attribute;
}
}

See more about the @media css attributes here: http://www.w3schools.com/cssref/css3_pr_mediaquery.asp

Brij
  • 259
  • 2
  • 8
  • That's what I'm using. The problem is the position is off. When I try it on the desktop with Chrome at the same resolutions as my phone and tablets, they show completely different results – Alex Mar 27 '16 at 05:48
  • @Alex can you elaborate what you mean by different results. I would suggest try once with putting image in body and then re-size it with screen size. – Brij Mar 27 '16 at 06:28
  • I have provided screenshots. Please take a look. The desktop version is what I imagine it should look like, while the other two are what it actually looks like on mobile – Alex Mar 27 '16 at 06:29
  • @Alex i tried on chrome with mobile view with this code it worked remove /* background-image: url("../images/back.jpg) from body css – Brij Mar 27 '16 at 06:35
  • I'll try that with individual media queries for portrait and landscape view if I can't get cover to work – Alex Mar 27 '16 at 06:37