我尝试实现我的第一个媒体查询,但我不明白为什么它没有效果。我有4列(弹性项目),并且我试图将其减少到800像素以下的屏幕上的2列(弹性项目)。
我遵循了w3schools示例。
如果有人可以看一下我的代码并告诉我所缺少的内容,我将非常感激。
https://codepen.io/mrlondon/project/live/DdvONp/
我正在尝试建立我的第一个静态网站。我现在正在尝试弄清楚如何使其具有响应能力。它不会让我在不添加更多详细信息的情况下发布此信息,但我只想知道此媒体查询出了什么问题。
* {
box-sizing: border-box;
}
@media screen and(max-width:800px) {
.column {
flex: 50%;
padding: 0 1%;
}
}
body {
width: 100%;
margin: 0 auto;
background-image: url("wallpaper2.jpg");
background-repeat: repeat;
}
.header {
width: 100%;
background: black;
height: 100px;
color: white;
}
h1 {
margin: 0 auto;
position: relative;
top: 30px;
left: 15px;
font-size: 2em;
color: #f0c330;
}
ul {
list-style-type: none;
float: right;
margin: auto;
position: relative;
}
li {
display: inline-block;
padding-left: 20px;
font-size: 1.4em;
}
.lastlist {
padding-right: 65px;
}
.nav {
text-decoration: none;
color: #f0c330;
}
.nav:hover {
color: white;
}
.wrapper {
width: 75%;
background: pink;
margin: 0 auto;
height: max-content;
padding: 2%;
}
.row {
display: flex;
flex-wrap: wrap;
}
.column {
flex: 0 1 25%;
padding: 0 1%;
}
.gallery {
width: 100%;
margin-bottom: 4%;
max-width: 100%;
}
.column img {
width: 100%;
max-width: 100%;
border-radius: 10px;
border: solid 2px #f0c330;
vertical-align: middle;
}
<div class="header">
<nav>
<h1>Daniel Savva</h1>
<ul>
<li> <a class="nav" href="#"> Home</a> </li>
<li> <a class="nav" href="#"> Gallery</a> </li>
<li> <a class="nav" href="#"> About</a> </li>
<li class="lastlist"> <a class="nav" href="#"> Contact</a> </li>
</ul>
</nav>
</div>
<div class="wrapper">
<div class="row">
<div class="column">
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=jySfU10IQu4">
<img src="picture1.jpg" alt="Picture1">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=S-9dxYcH7sg">
<img src="picture55.jpg" alt="picture55">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=jEgX64n3T7g">
<img src="picture10.jpg" alt="picture10">
</a>
</div>
</div>
<div class="column">
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=hF-QqKiT1bI">
<img src="picture2.jpg" alt="picture2">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=QUvVdTlA23w">
<img src="picture77.jpg" alt="picture77">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=lVL-zZnD3VU">
<img src="picture11.jpg" alt="picture11">
</a>
</div>
</div>
<div class="column">
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=K_9tX4eHztY">
<img src="picture3.jpg" alt="picture3">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=Nu__TzWfpss">
<img src="picture66.jpg" alt="picture66">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=S71_vIMQ0YY">
<img src="picture12.jpg" alt="picture12">
</a>
</div>
</div>
<div class="column">
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=K_9tX4eHztY">
<img src="picture45.jpg" alt="picture45">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=Soa3gO7tL-c">
<img src="picture9.jpg" alt="picture9">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=_FrOQC-zEog">
<img src="picture13.jpg" alt="picture13">
</a>
</div>
</div>
</div>
</div>
答案 0 :(得分:1)
您需要在and
和(max-width-:800px)
之间留一个空格。
您还需要将媒体查询放置在末尾或增加选择器的权重,否则.column {flex: xx}
的另一条规则将其覆盖。
示例:
* {
box-sizing: border-box;
}
@media screen and (max-width:800px) {
.row .column {/* increase selector wheight or send me after .column selector */
flex : 50%;
padding: 0 1%;
}
}
body {
width: 100%;
margin: 0 auto;
background-image: url("wallpaper2.jpg");
background-repeat: repeat;
}
.header {
width: 100%;
background: black;
height: 100px;
color: white;
}
h1 {
margin: 0 auto;
position: relative;
top: 30px;
left: 15px;
font-size: 2em;
color: #f0c330;
}
ul {
list-style-type: none;
float: right;
margin: auto;
position: relative;
}
li {
display: inline-block;
padding-left: 20px;
font-size: 1.4em;
}
.lastlist {
padding-right: 65px;
}
.nav {
text-decoration: none;
color: #f0c330;
}
.nav:hover {
color: white;
}
.wrapper {
width: 75%;
background: pink;
margin: 0 auto;
height: max-content;
padding: 2%;
}
.row {
display: flex;
flex-wrap: wrap;
}
.column {
flex: 0 1 25%;
padding: 0 1%;
}
.gallery {
width: 100%;
margin-bottom: 4%;
max-width: 100%;
}
.column img {
width: 100%;
max-width: 100%;
border-radius: 10px;
border: solid 2px #f0c330;
vertical-align: middle;
}
<div class="header">
<nav>
<h1>Daniel Savva</h1>
<ul>
<li> <a class="nav" href="#"> Home</a> </li>
<li> <a class="nav" href="#"> Gallery</a> </li>
<li> <a class="nav" href="#"> About</a> </li>
<li class="lastlist"> <a class="nav" href="#"> Contact</a> </li>
</ul>
</nav>
</div>
<div class="wrapper">
<div class="row">
<div class="column">
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=jySfU10IQu4">
<img src="picture1.jpg" alt="Picture1">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=S-9dxYcH7sg">
<img src="picture55.jpg" alt="picture55">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=jEgX64n3T7g">
<img src="picture10.jpg" alt="picture10">
</a>
</div>
</div>
<div class="column">
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=hF-QqKiT1bI">
<img src="picture2.jpg" alt="picture2">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=QUvVdTlA23w">
<img src="picture77.jpg" alt="picture77">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=lVL-zZnD3VU">
<img src="picture11.jpg" alt="picture11">
</a>
</div>
</div>
<div class="column">
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=K_9tX4eHztY">
<img src="picture3.jpg" alt="picture3">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=Nu__TzWfpss">
<img src="picture66.jpg" alt="picture66">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=S71_vIMQ0YY">
<img src="picture12.jpg" alt="picture12">
</a>
</div>
</div>
<div class="column">
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=K_9tX4eHztY">
<img src="picture45.jpg" alt="picture45">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=Soa3gO7tL-c">
<img src="picture9.jpg" alt="picture9">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=_FrOQC-zEog">
<img src="picture13.jpg" alt="picture13">
</a>
</div>
</div>
</div>
</div>
答案 1 :(得分:0)
将元标记添加到标题中
<meta content="width=device-width, initial-scale=1" name="viewport" />
答案 2 :(得分:0)
如果我正确理解了您的问题,那么您需要添加的只是这样的媒体查询(其中的max-width
值应根据您的实际需求进行调整):
@media screen and (max-width: 600px) {
.column {
flex: 0 1 50%;
padding: 0 1%;
}
}
它将.column
的宽度设置为50%(而不是25%),这将导致两列布局,而不是600像素或更小的屏幕的四列。
* {
box-sizing: border-box;
}
@media screen and(max-width:800px) {
.column {
flex: 50%;
padding: 0 1%;
}
}
body {
width: 100%;
margin: 0 auto;
background-image: url("wallpaper2.jpg");
background-repeat: repeat;
}
.header {
width: 100%;
background: black;
height: 100px;
color: white;
}
h1 {
margin: 0 auto;
position: relative;
top: 30px;
left: 15px;
font-size: 2em;
color: #f0c330;
}
ul {
list-style-type: none;
float: right;
margin: auto;
position: relative;
}
li {
display: inline-block;
padding-left: 20px;
font-size: 1.4em;
}
.lastlist {
padding-right: 65px;
}
.nav {
text-decoration: none;
color: #f0c330;
}
.nav:hover {
color: white;
}
.wrapper {
width: 75%;
background: pink;
margin: 0 auto;
height: max-content;
padding: 2%;
}
.row {
display: flex;
flex-wrap: wrap;
}
.column {
flex: 0 1 25%;
padding: 0 1%;
}
.gallery {
width: 100%;
margin-bottom: 4%;
max-width: 100%;
}
.column img {
width: 100%;
max-width: 100%;
border-radius: 10px;
border: solid 2px #f0c330;
vertical-align: middle;
}
@media screen and (max-width: 600px) {
.column {
flex: 0 1 50%;
padding: 0 1%;
}
}
<div class="header">
<nav>
<h1>Daniel Savva</h1>
<ul>
<li> <a class="nav" href="#"> Home</a> </li>
<li> <a class="nav" href="#"> Gallery</a> </li>
<li> <a class="nav" href="#"> About</a> </li>
<li class="lastlist"> <a class="nav" href="#"> Contact</a> </li>
</ul>
</nav>
</div>
<div class="wrapper">
<div class="row">
<div class="column">
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=jySfU10IQu4">
<img src="picture1.jpg" alt="Picture1">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=S-9dxYcH7sg">
<img src="picture55.jpg" alt="picture55">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=jEgX64n3T7g">
<img src="picture10.jpg" alt="picture10">
</a>
</div>
</div>
<div class="column">
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=hF-QqKiT1bI">
<img src="picture2.jpg" alt="picture2">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=QUvVdTlA23w">
<img src="picture77.jpg" alt="picture77">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=lVL-zZnD3VU">
<img src="picture11.jpg" alt="picture11">
</a>
</div>
</div>
<div class="column">
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=K_9tX4eHztY">
<img src="picture3.jpg" alt="picture3">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=Nu__TzWfpss">
<img src="picture66.jpg" alt="picture66">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=S71_vIMQ0YY">
<img src="picture12.jpg" alt="picture12">
</a>
</div>
</div>
<div class="column">
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=K_9tX4eHztY">
<img src="picture45.jpg" alt="picture45">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=Soa3gO7tL-c">
<img src="picture9.jpg" alt="picture9">
</a>
</div>
<div class="gallery">
<a target="_blank" href="https://www.youtube.com/watch?v=_FrOQC-zEog">
<img src="picture13.jpg" alt="picture13">
</a>
</div>
</div>
</div>
</div>