Select different CSS style sheet for different browser window sizes?

拥有回忆 提交于 2019-12-08 07:09:25

@media queries are my preference (saw that you don't like them as a solution per se), but they really could do the trick - especially if you adjust your css a little to accommodate.

<link...media="only screen and (max-height: 600px)" href="small-device.css" />
    small-device.css = div.container { ... height:500px; margin:50%; ...}

<link...media="only screen and (min-height: 601px)" href="big-device.css" />
    big-device.css = div.container {... height:600px; margin:50%; ...}

You may also have a little more luck by removing your absolute positioning and taking advantage of normal document flow. It would help you to add things like { overflow-y:scroll; } to those hidden-by-screen-height divs.

I think in the end, if you're trying to design around hand-held devices, you'll need media queries to some extent. My Android screen (for example) has 3 display options (low, medium, hi def). All 3 crop pages differently.

Faraona

You can determine window size by Jquery

$(window).width();
$(window).height();

or

$(document).width();
$(document).height();

then change css

$("link").attr("href", "blue.css");

Something like this:

$(document).ready(function(){

 if($(document).height()  > 600 or $(window).height() > 600){

   $("link").attr("href", "600+.css");

 } else {

   $("link").attr("href", "600-.css");

 }
});
Don Joe

A solution that works in all major browsers. No JS needed. Vertically/horizontally centered, scrollable, sticks to the top when content is larger than viewport.

HTML:

<div id="body">[your content goes here]</div>

CSS:

html, body {
    width: 100%;
    height: 100%;
    }

html {
    display: table;
}

body {
    display: table-cell;
    vertical-align: middle;
}

#body {
    margin: 0 auto;
}

Don't forget to apply the last rule, it will actually perform the horizontal centering.

Just did a bit of googling and found this:

http://www.ilovecolors.com.ar/detect-screen-size-css-style/

does that work for you?

Try the Less CSS Framework: http://lessframework.com/,

You do not need JavaScript, but rather you can use CSS @Media to set styles based on resolution/ screen size.

Best of luck

Use the following JavaScript conditional to detect screen size.

function x() will handle inserting the CSS link in the <head> tag. All you need to do is call the function and pass in the CSS file name.

< script type = "text/javascript" >
<!--
function x(y) {
    var styles = y;
    var newSS = document.createElement('link');
    newSS.rel = 'stylesheet';
    newSS.href = 'data:text/css,' + styles;
    document.getElementsByTagName("head")[0].appendChild(newSS);
}

if ((screen.width >= 1024) && (screen.height >= 768)) {
    x('file.css');
}
else {
    x('file1.css');
}
//-->
< /SCRIPT>

If "document_height" not work, try "window_height" i comment it in code!

$("link").attr("href", "css_file_path"); // here must insert path to your css, replace it in the code below

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" ></script>

<script type="text/javascript">

$(document).ready(function(){

document_height = $(document).height();
//window_height = $(window).height();

//if(window_height  > 600){
 if(document_height  > 600){
    alert('Bigger than 600px height: ' + $(document).height());
   $("link").attr("href", "600+.css"); // Here load css if window is bigger then 600px;

 } else {
    alert('Smaller than 600px height: ' + $(document).height());
   $("link").attr("href", "600-.css"); // Here load css if window is smaller then 600px;

 }
});

</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!