2017-11-06 3 views
1

좋아요, 그래서 요소가 특정 너비에 도달하면 @media 태그를 사용하여 사진을 대체하려고합니다. 원래 너비를 100 %로 설정했지만, 나는 그것이 최대 폭과 최소 너비로 변경 되었기 때문에 응답 성 문제를 일으킬 수 있다는 것을 읽었으나 어쨌든 여기서 코드는 변경되지 않습니다.@media 너비 : 100 %

참고 : 나는 크롬에 요소를 검사하고 .container는 다운 스케일링에 767px에 도달 한 것은 그래서 폭을 변경하지 않는 요소가 문제

body,html { 
 
    margin: 0; 
 
    height: 100%; 
 
} 
 

 
@media only screen and (min-width: 768px) and (max-width: 1920px) { 
 
    
 
    .container { 
 
    min-width: 768px; 
 
    max-width: 1920px; 
 
    height: 100%; 
 
    background-image: url('https://images.pexels.com/photos/214519/pexels-photo-214519.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb'); 
 
    background-position: center center; 
 
    background-repeat: no-repeat; 
 
    background-size: cover; 
 
    }; 
 
}; 
 
@media only screen and (min-width: 300px) and (max-width: 767px) { 
 
    .container { 
 
    min-width: 300px; 
 
    max-width: 767px; 
 
    height: 100%; 
 
    background-image: url('https://images.pexels.com/photos/159146/train-cargo-bow-river-banff-159146.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb'); 
 
    background-position: center center; 
 
    background-repeat: no-repeat; 
 
    background-size: cover; 
 
    }; 
 
};
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <link href="style.css" type="text/css" rel="stylesheet" /> 
 
     <meta name="viewport" content="width=device-width" /> 
 
    </head> 
 
    
 
    <body> 
 
<div class="container"></div> 
 
    </body> 
 
</html>
생각하지 않습니다 사용하여 요소의 폭을 확인

+1

매개체는 Maxwells.c 예 즉 그러나 목표는 사람이 아이폰을 사용하는 말을 들어, 그들은 'X'이미지를 가지고 것입니다 사실 @없는 요소 –

+0

크기, 화면 크기입니다 데스크톱에있는 사용자는 'y'이미지가로드됩니다. 나는 화면 크기를 감지하고 CSS 크기를 기반으로 CSS를 편집하는 방법으로 미디어를 사용하고 있습니다. – bignate132

+0

'화면 만 '대신'화면 '을 설정하려고합니다. 그리고 최소/최대 너비를 사용할 필요가 없습니다. 100 %를 사용할 수 있습니다. –

답변

0

을 문제가있어 당신의 닫는 괄호 (};를 사용하지 않는 만 } 사용).

또한 최소 너비와 최대 너비를 모두 지정할 필요가 없습니다.

body,html { 
 
    margin: 0; 
 
    height: 100%; 
 
} 
 

 
@media (min-width: 768px){ 
 
    
 
    .container { 
 
    min-width: 768px; 
 
    max-width: 1920px; 
 
    height: 100%; 
 
    background-image: url('https://images.pexels.com/photos/214519/pexels-photo-214519.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb'); 
 
    background-position: center center; 
 
    background-repeat: no-repeat; 
 
    background-size: cover; 
 
    } 
 
} 
 

 
@media (max-width: 767px) { 
 
    .container { 
 
    min-width: 300px; 
 
    max-width: 767px; 
 
    height: 100%; 
 
    background-image: url('https://images.pexels.com/photos/159146/train-cargo-bow-river-banff-159146.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb'); 
 
    background-position: center center; 
 
    background-repeat: no-repeat; 
 
    background-size: cover; 
 
    } 
 
}
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <link href="style.css" type="text/css" rel="stylesheet" /> 
 
     <meta name="viewport" content="width=device-width" /> 
 
    </head> 
 
    
 
    <body> 
 
<div class="container"></div> 
 
    </body> 
 
</html>

+0

흥미로운 피드백에 감사드립니다. 건배 – bignate132

0

궁금한 분을 위해 알아 냈습니다. 이 질문에 대한 해결책 Multiple CSS @media conditions don't seem to be working은 이미 그것을 시도했지만 어떤 이유로 든 해결했습니다. 여기에 코드를

body,html { 
 
    margin: 0; 
 
    height: 100%; 
 
} 
 
.container{ 
 
    width: 100%; 
 
    height: 100%; 
 
    background-color: black; 
 
} 
 
@media screen and (min-width: 300px) and (max-width: 1000px){ 
 
    .container { 
 
     background-image: url('https://images.pexels.com/photos/7882/light-bulb-light-old.jpg?w=1260&h=750&auto=compress&cs=tinysrgb'); 
 
     background-position: center center; 
 
     background-size: cover; 
 
     background-repeat: no-repeat; 
 
    } 
 
} 
 
@media screen and (min-width: 1001px) and (max-width: 1920px){ 
 
    .container{ 
 
     background-image: url('https://images.pexels.com/photos/531321/pexels-photo-531321.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb'); 
 
     background-position: left center; 
 
     background-size: cover; 
 
     background-repeat: no-repeat; 
 
    } 
 
}
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <link href="style.css" type="text/css" rel="stylesheet" /> 
 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 
    </head> 
 
    
 
    <body> 
 
<div class="container"></div> 
 
    </body> 
 
</html> 
 

 

 
<!-- since I created this in a full screen browser you have to make your broswer window pretty big to get it to change but it does work -->