简体   繁体   English

针对不同设备大小编写媒体查询

[英]writing media query for different device sizes

I want to do some responsiveness fixes for certain devices. 我想对某些设备进行一些响应性修复。

For eg for Samsung Galaxy s5 , I need to change some code. 例如对于Samsung Galaxy s5 ,我需要更改一些代码。

In chromes inspect element , It shows something like 360px X 640px . 在chromes inspect element ,它显示出类似360px X 640px

Now how do I write the specific code for this device? 现在如何为该设备编写特定的代码?

For iphone5 It shows 320px X 568px . 对于iphone5它显示为320px X 568px

I read somewhere that If I write like this 我读过某处,如果我这样写

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

It'll work for all the devices but this doesn't seem to be working. 它适用于所有设备,但这似乎不起作用。

So overall for all these devices how do I write media query. 因此,总体而言,对于所有这些设备,我该如何编写媒体查询。

Please suggest. 请提出建议。

EDIT : So After first comment's url, This is what I am doing 编辑 :所以在第一个评论的URL之后,这就是我在做什么

/* Portrait */
@media only screen 
 and (min-device-width: 375px) 
 and (max-device-width: 667px) 
 and (-webkit-min-device-pixel-ratio: 2)
 and (orientation: portrait) { 
  margin-left: 30px;
 }

But this doesn't work. 但这是行不通的。

Below are the all media queries for different resolution. 以下是针对不同分辨率的所有媒体查询。 So you can use it to make your website responsive. 因此,您可以使用它来使您的网站具有响应能力。

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



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



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



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



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



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



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



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

/* iPhone 5 (portrait & landscape)----------- */
@media only screen
and (min-device-width : 320px)
and (max-device-width : 568px) {
/* STYLES GO HERE */
}



/* iPhone 5 (landscape)----------- */
@media only screen
and (min-device-width : 320px)
and (max-device-width : 568px)
and (orientation : landscape) {
/* STYLES GO HERE */
}



/* iPhone 5 (portrait)----------- */
@media only screen
and (min-device-width : 320px)
and (max-device-width : 568px)
and (orientation : portrait) {
/* STYLES GO HERE */
}

The media queries do work for phones, but you probably are missing this in your head tag: 媒体查询确实适用于电话,但您的head标签中可能缺少它:

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

Also, inside the @media tag, you need to specify the class that you want to give the styles to: 另外,在@media标记内,您需要指定要为其提供样式的类:

@media only screen and (max-width: 766px) {
    .my-div {
        /* Styles Here */
    }
}

It would be good if you can go through the W3schools to get the clear understand on media queries 如果您可以通过W3schools以获得对媒体查询的清晰了解,那将是很好的

Using media queries 使用媒体查询

and after this you check Media Queries for Standard Devices 然后检查标准设备的媒体查询

here is the basic media query implementation 这是基本的媒体查询实现

<div id="container">
  <div class="row">
    <div class="col one">
            <p>Nullam quis risus eget urna mollis ornare vel eu leo. Donec id elit non mi porta gravida at eget metus. Curabitur blandit tempus porttitor. </p>
        </div>
        <div class="col two">
            <p>Nullam quis risus eget urna mollis ornare vel eu leo. Donec id elit non mi porta gravida at eget metus. Curabitur blandit tempus porttitor. </p>
        </div>
  </div>
</div>


.col{
    color:#fff;
        float:left;
        margin:2%;
        width: 46%;
}
.one{
  background-color: #e95b33;
}

.two{
  background-color: #40b7d7;
}

    @media screen and (max-width: 600px) { 
        .col{
            float: none;
            margin:0;
            width: 100%;
        }
    }

CodePen 密码笔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM