Change site completely if browser size is small

2019-09-20 14:26发布

问题:

I am currently using media query in my css but my site is still looking bad. Is there a way to determine first the witdh of a browser and then load different index files?

To post some code here is my media query:

@media screen and (max-width: 600px) {
  .topbar{
    opacity: 0;
  } 
....
}

回答1:

I would say do some more research on building your CSS but to answer your question:

<script type="text/javascript">
if (screen.width <= 699) {
document.location = "http://mobilesite.com";
}
</script>


回答2:

It might be an idea to load different css files for different screen sizes; essentially moving the media selection from the css to the html:

<link rel="stylesheet" media="screen and (max-width: 600px)" href="600px.css">

You might want to read Detect different device platforms using CSS for some related content.



回答3:

Generally you want to aim to use the same .html file for your website, then use CSS to customise specifically for desktop or mobile. I know you may have very different ideas for the two sites, but it can all be done in pure CSS if your markup (html code) is good enough. Check out the CSS Zen Garden for how powerful CSS can be.

If you want to completely reset your css for the mobile site, just wrap the old css in a media query targeting screens screen and (min-width: 601px), and you will find your mobile site is completely unstyled



回答4:

css has nothing to do with loading different index files according to the browser width.

If you want to style your elements differently using @media rules, make sure they are set close to the bottom of the page, in other words - after the main styles, because otherwise - they will be simply overwritten.