28

I have designed a page where four div tags are there in the page. If I test the page in mobile phone (5 inch) it fits the page perfectly, If I test the same page in tablet the page fits with in 30% of the screen. So how can I set the div size so that it will fit for all type of screens.

Fiddle link

HTML:

  <div class="bubble0" align="center">
     <h3>Three  Levels </h3> 
  </div>
  <div class="bubble"  align="center"> </div><br/><br/>
  <div class="bubble1" align="center"> </div><br/><br/>
  <div class="bubble2" align="center"> </div><br/><br/>
  <button>Play</button>

Any suggestions please,

Suganth G
  • 5,136
  • 3
  • 25
  • 44
Vinod
  • 2,263
  • 9
  • 55
  • 104

9 Answers9

73

This is called Responsive Web Development(RWD). To make page responsive to all device we need to use some basic fundamental such as:-

1. Set the viewport meta tag in head:

<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0"/>

2.Use media queries.

Example:-

/* Smartphones (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 480px) {
/* Styles */
}

/* Smartphones (landscape) ----------- */
@media only screen 
and (min-width : 321px) {
/* Styles */
}

/* Smartphones (portrait) ----------- */
@media only screen 
and (max-width : 320px) {
/* Styles */
}

/* iPads (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) {
/* Styles */
}

/* iPads (landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : landscape) {
/* Styles */
}

/* iPads (portrait) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : portrait) {
/* Styles */
}

/* Desktops and laptops ----------- */
@media only screen 
and (min-width : 1224px) {
/* Styles */
}

/* Large screens ----------- */
@media only screen 
and (min-width : 1824px) {
/* Styles */
}

/* iPhone 4 ----------- */
@media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
}

3. Or we can directly use RWD framework:-

Some of good Article

Media Queries for Standard Devices - BY CHRIS COYIER

CSS Media Dimensions

4. Larger Device, Medium Devices & Small Devices media queries. (Work in my Scenarios.)

Below media queries for generic Device type: - Larger Device, Medium Devices & Small Devices. This is just basic media types which work for all of scenario & easy to handle code instead of using various media queries just need to care of three media type.

/*###Desktops, big landscape tablets and laptops(Large, Extra large)####*/
@media screen and (min-width: 1024px){
/*Style*/
}

/*###Tablet(medium)###*/
@media screen and (min-width : 768px) and (max-width : 1023px){
/*Style*/
}

/*### Smartphones (portrait and landscape)(small)### */
@media screen and (min-width : 0px) and (max-width : 767px){
/*Style*/
}
Mike Phils
  • 3,475
  • 5
  • 24
  • 45
  • 14
    It's a bit sad to me that this answer was chosen. Responsive Web Design (or Development, if you wish) is not about adapting to *specific* viewport sizes, but to respond to *any* size. A lot of the measurements above make no sense in terms of how the device landscape works. Furthermore, it's not good practice to use device-width/device-height; the width of the device screen is usually not important, but the number of CSS pixels inside the viewport of the browser (which may be two different things). Lastly, the media query for DPR is not correct. – Emil Aug 29 '14 at 12:43
  • @Emil Please check the code from various site and verify your comment.Also learn RWD basic. – Mike Phils Sep 01 '14 at 09:21
  • I already know "RWD Basic" pretty well, thank you very much. I know a lot of devs get RWD wrong, but that's not a very good reason to copy their errors. How about this: if you check my answer below instead, you'll see a link to PPK:s slides on viewport theory. In it, you'll find explicit advice to stay away from (`min-` or `max-`)`device-width` media queries, as well as not locking your MQ:s to device-specific widths. You'll also find the correct implementation of resolution query (`-webkit-min-device-pixel-ratio` has no unprefixed counterpart, the correct one is `min-resolution`). – Emil Sep 01 '14 at 12:14
  • If you want to read more about why device-agnostic breakpoints are better, I recommend ["7 Habits of Highly Effective Media Queries"](http://bradfrostweb.com/blog/post/7-habits-of-highly-effective-media-queries/) by Brad Frost, or Jeremy Keith's ["Fanfare for the common breakpoint"](http://adactio.com/journal/5425/). – Emil Sep 01 '14 at 12:22
  • I think you need break. Once again please start from base of RWD – Mike Phils Sep 02 '14 at 16:08
  • 2
    So... When I criticize your answer (because it contains bad practice, however common as well as factual errors), and also try to help you see why that is by supplying sources, I need a break? Ok then... – Emil Sep 02 '14 at 18:58
  • 1
    I’m with @Emil here; he knows what he’s talking about. The media queries Mike is suggesting are overly complicated and prescriptive. RWD is about flexing to fill the screen in the most appropriate way possible and the best RWD is done from a mobile first perspective. "Bookending" queries (as Mike is suggesting here) doesn’t allow for style rules to inherit up from small screens to larger ones. It also seems to suggest a series of fixed size designs, which (having made that mistake before) I strongly recommend against. Emil’s answer (below) is better. – Aaron Gustafson Sep 17 '15 at 15:29
  • 1
    This answer offers only 9 media queries... what about the Samsung Galaxy S6? The Samsung Galaxy Note 5? The S6 Edge? The LG G4? The Moto X? The OneTouch Idol? The Galaxy Grand Prime? Maybe there's a better way (see @Emil's answer). – Tim Sep 17 '15 at 15:38
  • For better way please use RWD framework. Above is just form of standard format which may vary. – Mike Phils Sep 18 '15 at 06:04
20

You question is a bit unclear as to what you want, but judging from your comments, I assume you want each bubble to cover the screen, both vertically and horizontally. In that case, the vertical part is the tricky part.

As many others have answered, you first need to make sure that you are setting the viewport meta tag to trigger mobile devices to use their "ideal" viewport instead of the emulated "desktop width" viewport. The easiest and most fool proof version of this tag is as follows:

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

Source: PPK, probably the leading expert on how this stuff works. (See http://quirksmode.org/presentations/Spring2014/viewports_jqueryeu.pdf).

Essentially, the above makes sure that media queries and CSS measurements correspond to the ideal display of a virtual "point" on any given device — instead of shrinking pages to work with non-optimized desktop layouts. You don't need to understand the details of it, but it's important.

Now that we have a correct (non-faked) mobile viewport to work with, adjusting to the height of the viewport is still a tricky subject. Generally, web pages are fine to expand vertically, but not horizontally. So when you set height: 100% on something, that measurement has to relate to something else. At the topmost level, this is the size of the HTML element. But when the HTML element is taller than the screen (and expands to contain the contents), your measurements in percentages will be screwed up.

Enter the vh unit: it works like percentages, but works in relation to the viewport, not the containing block. MDN info page here: https://developer.mozilla.org/en-US/docs/Web/CSS/length#Viewport-percentage_lengths

Using that unit works just like you'd expect:

.bubble { height: 100vh; } /* be as tall as the viewport height. Done. */

It works on a lot of browsers (IE9 and up, modern Firefox, Safari, Chrome, Opera etc) but not all (support info here: http://caniuse.com/#search=vh). The downside in the browsers where it does work is that there is a massive bug in iOS6-7 that makes this technique unusable for this very case (details here: https://github.com/scottjehl/Device-Bugs/issues/36). It will be fixed in iOS8 though.

Depending on the HTML structure of your project, you may get away with using height: 100% on each element that is supposed to be as tall as the screen, as long as the following conditions are met:

  1. The element is a direct child element of <body>.
  2. Both the html and body elements have a 100% height set.

I have used that technique in the past, but it was long ago and I'm not sure it works on most mobile devices. Try it and see.

The next choice is to use a JavaScript helper to resize your elements to fit the viewport. Either a polyfill fixing the vh issues or something else altogether. Sadly, not every layout is doable in CSS.

Emil
  • 1,949
  • 2
  • 16
  • 25
11

You can use the viewport height, just set the height of your div to height:100vh;, this will set the height of your div to the height of the viewport of the device, furthermore, if you want it to be exactly as your device screen, set the margin and padding to 0.

Plus, It will be a good idea to set the viewport meta tag:

<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0" />

Please Note that this is relatively new and is not supported in IE8-, take a look at the support list before considering this approach (http://caniuse.com/#search=viewport).

Hope this helps.

Karim AG
  • 2,166
  • 15
  • 29
6

I don't have much time and your jsfidle did not work right now.
But maybe this will help you getting started.

First of all you should avoid to put css in your html tags. Like align="center".
Put stuff like that in your css since it is much clearer and won't deprecate that fast.

If you want to design responsive layouts you should use media queries wich were introduced in css3 and are supported very well by now.

Example css:

@media screen and (min-width: 100px) and (max-width: 199px)
{
    .button
    {
        width: 25px;
    }
}

@media screen and (min-width: 200px) and (max-width: 299px)
{
    .button
    {
        width: 50px;
    }
}

You can use any css you want inside a media query.
http://www.w3.org/TR/css3-mediaqueries/

Noel Widmer
  • 4,444
  • 9
  • 45
  • 69
2

Fiddle

You want to set height which may set for all devices?

Decide upon the design of the site i.e Height on various devices.

Ex:

  • Height-100px for bubbles on device with -min-width: 700px.
  • Height-50px for bubbles on device with < 700px;

Have your css which has height 50px;

and add this media query

  @media only screen and (min-width: 700px) {
    /* Styles */
    .bubble {
        height: 100px;
        margin: 20px;
    }
    .bubble:after {
        bottom: -50px;
        border-width: 50px 50px 0;
    }
    .bubble:before {
        top: 100px;
        border-width: 52px 52px 0;
    }
    .bubble1 {
        height: 100px;
        margin: 20px;
    }
    .bubble1:after {
        bottom: -50px;
        border-width: 50px 50px 0;
    }
    .bubble1:before {
        top: 100px;
        border-width: 52px 52px 0;
    }
    .bubble2 {
        height: 100px;
        margin: 20px;
    }
    .bubble2:after {
        bottom: -50px;
        border-width: 50px 50px 0;
    }
    .bubble2:before {
        top: 100px;
        border-width: 52px 52px 0;
    }
}

This will make your bubbles have Height of 100px on devices greater than 700px and a margin of 20px;

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sudheer
  • 2,955
  • 2
  • 21
  • 35
2

I use something like this in my document.ready

var height = $(window).height();//gets height from device
var width = $(window).width(); //gets width from device

$("#container").width(width+"px");
$("#container").height(height+"px");
1

Whilst I was looking for my answer for the same question, I found this:

<img src="img.png" style=max-
width:100%;overflow:hidden;border:none;padding:0;margin:0 auto;display:block;" marginheight="0" marginwidth="0">

You can use it inside a tag (iframe or img) the image will adjust based on it's device.

Bugs
  • 4,491
  • 9
  • 32
  • 41
0

Try giving your divs a width of 100%.

Mohammad Areeb Siddiqui
  • 9,795
  • 14
  • 71
  • 113
0

Simple:

<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0" />

Cheers!

Brian Nezhad
  • 6,148
  • 9
  • 44
  • 69