Reputation: 33
I tried to implement my first Media Query and I cannot understand why it has had no effect. I have 4 columns (flex items) and I am trying to reduce this to 2 columns (flex Items) on screens under 800px.
I followed the w3schools example.
I would be very grateful if somebody could look at my code and tell me what I am missing.
https://codepen.io/mrlondon/project/live/DdvONp/
I am trying to build my first static website. I am now trying to figure out how to make it responsive. It won't let me post this without adding more detail but I just want to know what's wrong with this media query.
* {
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>
Upvotes: 0
Views: 48
Reputation: 105843
You need a space in between and
& (max-width-:800px)
.
You also need to place the media querie at the end or increase the selector weight , else , the other rule for .column {flex: xx}
overides it .
example:
* {
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>
Upvotes: 1
Reputation: 67738
If I understand your question correctly, all you need to add is a media query like this (the max-width
value in it should be adjusted to your actual requirements):
@media screen and (max-width: 600px) {
.column {
flex: 0 1 50%;
padding: 0 1%;
}
}
It sets the width of .column
to 50% (instead of 25%), which results in a two-column layout instead of four columns for screens which are 600px wide or less.
* {
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>
Upvotes: 0
Reputation: 93
Add meta tag to your header
<meta content="width=device-width, initial-scale=1" name="viewport" />
Upvotes: 0