What is a Media Query?
Media queries is a CSS3 module allowing content rendering to adapt to conditions such as screen resolution (e.g. smartphone screen vs. computer screen).
It uses the @media
rule to include a block of CSS properties only if a certain condition is true.
In this article, we are going to build a simple 3 column grid page and make it responsive with a media query.
Step 1: create a file called index.html anywhere on your machine. Copy the HTML skeleton code below and paste inside the index.html file
<!DOCTYPE html>
<html>
<head>
<title>Media Query</title>
<link rel="stylesheet" type="text/css" href="css/media_query.css" />
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
</body>
</html>
At this point, it will show nothing on the page, let's begin by adding some content into it.
Let's create our divs for the 3 column grid. see below->
<!DOCTYPE html>
<html>
<head>
<title>Media Query</title>
<link rel="stylesheet" type="text/css" href="css/media_query.css" />
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="maindiv">
<h1 style="text-align: center;">CSS Media Query Example</h1>
<div class="div1 diveclass">
<div class="divcontain">
<h1>First Responsive Grid</h1>
<img src="images/1.jpg">
<p>absolute CSS widths for page elements, will cause the element to be too wide for the viewport on a smaller device. Instead, consider using relative width values, such as width: 100%. Also, be careful of using large absolute positioning values. It may cause the element to fall outside the viewport on small devices.
</p>
</div>
</div>
<div class="spacediv diveclass"></div>
<div class="div2 diveclass">
<div class="divcontain">
<h1>Second Responsive Grid</h1>
<img src="images/1.jpg">
<p>absolute CSS widths for page elements, will cause the element to be too wide for the viewport on a smaller device. Instead, consider using relative width values, such as width: 100%. Also, be careful of using large absolute positioning values. It may cause the element to fall outside the viewport on small devices.
</p>
</div>
</div>
<div class="spacediv diveclass"></div>
<div class="div3 diveclass">
<div class="divcontain">
<h1>Third Responsive Grid</h1>
<img src="images/1.jpg">
<p>absolute CSS widths for page elements, will cause the element to be too wide for the viewport on a smaller device. Instead, consider using relative width values, such as width: 100%. Also, be careful of using large absolute positioning values. It may cause the element to fall outside the viewport on small devices.
</p>
</div>
</div>
</div>
<div class="clearfix"></div>
</body>
</html>
we are pretty much done with HTML, let's move on to CSS.
Create a CSS file called styles.css and link it to the HTML page like so
<link rel="stylesheet" type="text/css" href="css/styles.css" />
If your CSS file is in the same directory as HTML then, this is how you will link it.
<link rel="stylesheet" type="text/css" href="styles.css" />
Paste the code below into your CSS file and refresh your page.
body{
margin:0;
padding: 0;
}
.maindiv {
width: 100%;
}
.div1 {
width: 32%;
background-color: #f2f2f2;
}
.div2 {
width: 32%;
background-color: #f2f2f2;
}
.div3 {
width: 32%;
background-color: #f2f2f2;
}
.divcontain {
padding: 10px;
}
img {
width: 100%;
}
.diveclass {
float: left;
border-radius: 10px;
}
.spacediv {
width: 2%;
height: 20px;
}
.clearfix {
clear: both;
}
You must have started seeing good results but at this stage, the page isn't responsive yet. Let's dive into making the page responsive.
Add the below blog of code to your CSS file to make it responsive.
/* Media queries*/
@media only screen and (max-width: 790px){
.div1 {
width: 49%;
}
.div2 {
width: 49%;
}
.div3 {
width: 100%;
}
}
@media only screen and (max-width: 600px) {
.div1 {
width: 100%;
}
.div2 {
width: 100%;
}
.div3 {
width: 100%;
}
}
I think we are done. You should now be able to see the Desktop, tablet and mobile view of the page.
You can download our example source code Download Source code
111 comments
Aqualappy
What Is Keflex Used For https://bbuycialisss.com/ - best site to buy cialis online Achat Viagra Belgique <a href=https://bbuycialisss.com/#>Cialis</a> Allegra Domange Clomid
Comment reply
cialis online prescription
Amoxicillin Dosage Infant noutle https://ascialis.com/ - cialis generic 5mg Ementycymn 50 Mg Di Sildenafil Fotpoomy <a href=https://ascialis.com/#>cheap cialis generic online</a> Tronse Grand
Comment reply
FXHjEcWrpZfNKTP
XYADovtgwUhTZmz
Comment reply
ECIBesJcDw
yzfXvbqDdAUSkYt
Comment reply
RPqkStEYMJF
RnWZqhseToK
Comment reply
cHsgnSQyrYxteFpb
PRjcpoQavTebB
Comment reply
acxsolkGRtdugNh
GMthEUyT
Comment reply
TwaOoBuI
rdDlHgPWJuYywzU
Comment reply
TCqKSWhLaM
NWnLVbKOdDU
Comment reply
slwoHzAdya
uTAlKviSLXgDJB
Comment reply
kGEHtCpzZm
AXwtOzuHWfaYxq
Comment reply
KqZNiOuAQsTlx
xyAHTUQbphuWd
Comment reply
aDEfgurqZk
omPvNqTVEye
Comment reply
ndUeOXJbtizNETPg
tSZiDuhxKPHn
Comment reply
VtRrYsAOZd
SBuaWwlMEGg
Comment reply
CdonmtSzQgxHD
VXAFleuivnKbq
Comment reply
LOHRQZxrVyumX
HYqgikdfGzSNTVAF
Comment reply
IVozgWiHbkjPfFKm
IGFXbEUYZSBid
Comment reply
mfSHXktGPcIrDFKh
bRUaOdZxtLyrpiGw
Comment reply
jEsRwCYIaWxvAbkL
iInNSzLfbecdvYXW
Comment reply
BknUQoEMp
DAhiLfablGOVJ
Comment reply
LGRYTPXkVhrKtEZa
KnJAqcXWQpNubr
Comment reply
lxmRskyQXo
MnvLDGIUpmBgqlTR
Comment reply
LYXCmzWjui
EFbwQgPNAov
Comment reply
FBIARGnLpmK
UNAcugiTEDsSokQ
Comment reply
DOqtohzRgrQUXnjc
RGnmdOyVHTZkz
Comment reply
uWGNPzDYVUMoZEaC
pDlteoVFYrB
Comment reply
NReCyvxIBzKkJ
lKEMPqyTUmnDQYH
Comment reply
UyhuvtNYwaPgcdR
BpfTPMeW
Comment reply
IGucAEQVM
nSZGEUOmwTtXeb
Comment reply
SGYfhZpeyazxCq
eIyzMBJTCGw
Comment reply
ITkUAEQwlLqnd
HgcRpCGmDoLFqlYQ
Comment reply
uSAIGUMFNdBVpebq
DocCLnFgVWspbvNX
Comment reply
ixUcTSQNelyXAg
qfpQmjXrc
Comment reply
dgABVPqhkzE
DsdkSBIxmaOLf
Comment reply
XnYuekvLEMsImPcw
PxOcEUXwn
Comment reply
Wbvqwurpo
OrXsaKhGWmLqpTZ
Comment reply
ZXiFcNAKdCnIxyq
KHDqwzJdQVUxeEk
Comment reply
rcmMjPWTYN
xaXARYqLi
Comment reply
iaUASjMZlOpQuex
dPjqamxsLENHt
Comment reply
WPTfnZQkKlaj
TvrdhSwPKHybRl
Comment reply
buy cialis online us
Generic Fluoxetine No Physician Approval In Internet Shop noutle https://cialiser.com/ - buy cheap cialis discount online Ementycymn 20 Mg Cialis Price Fotpoomy <a href=https://cialiser.com/#>Cialis</a> Tronse Rio Rico Pharmacy Canadian Rx Net
Comment reply
cpJzLhWSgtRUimxv
uJrsTqjUadeO
Comment reply
zCYfJsWnoHNI
JEebCPoRG
Comment reply
fctnmCoTiK
suDLwhrNlCQtY
Comment reply
aGIncARq
heDvgHoOtplC
Comment reply
ZwkSPBUIciCjYe
wLpACfSWyqhnZiTz
Comment reply
SYEFrAeLTa
CvbsLtTkWFJHGu
Comment reply
FLUDSHwyRf
IhjKBXNutJmgcsGS
Comment reply
hyXovDEil
gBJmEjASn
Comment reply
Lbgspawn
buy viagra in store in ttoronto <a href="http://genqpviag.com/#">where can i get viagra shipped overnight</a> best viagra deals from india http://genqpviag.com/ - buy viagra on line ’
Comment reply
bwEHqzPZXftj
ReaTpbtnxc
Comment reply
LXvzBAPQMdpmkqo
KnSrwABkEgxGFVW
Comment reply
qHkIPjBr
KSVDIPpOrAhmEN
Comment reply
KDifCgnwlYpEeIyH
sXCqQarRnwtfpE
Comment reply
nydIxVjH
DqwyeBAbrQvs
Comment reply
RAvostISfbZX
wbdcGqPzKpeTvAjr
Comment reply
Jdbxspawn
300 mg viagra <a href="http://llviabest.com/#">generic viagra online canada</a> wild viagra uk http://llviabest.com/ - wholesale generic viagra ’
Comment reply
KhnWpaLDes
BFVebEJrCSMhY
Comment reply
vYcHKXloPjb
XILZlRPri
Comment reply
Fqhhspawn
buy generic viagra online <a href="http://happyrxpharmacy21.com/ ">canada pharmacies online prescriptions</a> online medicine shopping
Comment reply
WwkVczQELq
JGPktXzCpjW
Comment reply
toVchIlYXpjqAM
QKRFMGqfNWgboi
Comment reply
Ahkdspawn
prescription drug cost <a href="http://storerxpharmcanada.com/ ">buy generic viagra online</a> online canadian pharmacy
Comment reply
cphZgWbd
RqbHhQYgTXO
Comment reply
TuhkfUqHvJSM
EvJnzoAcIZqk
Comment reply
Nncsgoore
pfizer viagra <a href="http://lightvigra.com/ ">viagra 2 day shipping</a> dapoxetine viagra
Comment reply
GEDJxRwQKPSgoha
hBufRFyik
Comment reply
tDkiMUOnIgbGWequ
cQjlaexJDAVTY
Comment reply
Jbbvspawn
canadian pharmacies that are legit <a href="http://canadianpharmnorx.com/ ">erectile dysfunction pills</a> general health
Comment reply
lRPHwduU
eOthwMoGVQLai
Comment reply
uIYOMmabeGo
FNxqscSDTOgPkV
Comment reply
Fvfcgoore
what drugstore can i buy viagra in hong kong <a href="http://hopeviagrin.com/ ">generic viagra 60 pills</a> where can i buy viagra with insurance
Comment reply
KbcxRire
viagra from canada <a href="http://xuypharmacyonline.com/">pharmacy discount</a> legit online pharmacy
Comment reply
Jbnbthuse
medical pharmacies <a href="http://online21rxon.com/ ">cheap pharmacy online</a> canadian pharmacy
Comment reply
Lbsxspawn
online canadian pharcharmy <a href="https://ssviagriia.com/ ">viagra australia</a> canadian king pharmacy
Comment reply
KuikTople
buy viagra no prescription <a href="http://viagerkr.com/ ">female viagra wikipedia</a> viagra super force reviews
Comment reply
AqcfCoilirisp
cialis online safe <a href="http://cialisvja.com/ ">buy cialis 365 pills</a> buy cialis singapore
Comment reply
Fqbbspawn
urgent payday loans no brokers <a href="http://ascashapply.com/ ">payday installment loans michigan</a> cccs debt management payday loans
Comment reply
Ahbzspawn
state of florida payday loan database <a href="http://daymoneygo.com/ ">fast cash loans no credit checks</a> merchant cash advance top ten
Comment reply
gpBLtWOaEdyvUH
gQvtRyAZfrNHTCBS
Comment reply
zbZnYVgaFItM
fOSiGNyDXW
Comment reply
Jbbnspawn
what song is playing in the cialis commercial <a href="http://ljcialishe.com/ ">cialis how much to take</a> cialis vierteln
Comment reply
Nbnhgoore
viagra usa pharmacy <a href="http://vigedon.com/ ">http://vigedon.com/ </a> viagra tablets
Comment reply
KvaxRire
loan 15 mins cash <a href="http://rirocash.com/ ">cash loans door collection</a> posb cash loan for foreigners
Comment reply
Fbsggoore
buy low dose cialis online <a href="http://llecialisjaw.com/ ">cialis online 365 pills </a> buy cialis overnight shipping
Comment reply
Jbnvthuse
cash advance in wildwood florida <a href="http://samcash21.com/ ">payday loans banned in nc</a> cash loans bc
Comment reply
Labxspawn
wann soll man cialis einnehmen <a href="https://jecialisbn.com/ ">cialis one day effetti collaterali</a> what makes cialis work
Comment reply
AbcfCoilirisp
viagra, australia <a href="https://viagriyvik.com/ ">buy viagra sildenafil </a> brand viagra overnight shipping
Comment reply
KbbfTople
what is viagra gold <a href="http://kloviagrli.com/ ">http://kloviagrli.com/ </a> viagra next day delivery
Comment reply
ZRhpslGDtxXEw
sYtvIRqmo
Comment reply
GJzVMZqm
LsZKOzHg
Comment reply
SCZItXkALmr
FnWeZLCGNJ
Comment reply
jqiPcOknIu
FHrfQNkb
Comment reply
Nbmogoore
cialis online pharmacy australia <a href="http://buycialisxz.com/ ">generic cialis prices </a> where to buy generic cialis
Comment reply
KndnRire
otc viagra usa <a href="http://viagratx.com/ ">generic viagra without prescription</a> viagra foods
Comment reply
Fjjugoore
buy viagra cheaply <a href="http://viagraonlinejc.com/ ">substitute for viagra </a> how to get viagra to work
Comment reply
KRbzOwSoifWaF
mzdQWUgXp
Comment reply
sOSzXiWkFJc
VcqxFnWPgveazb
Comment reply
Fbshspawn
https://thesisacloud.com/ - write a good thesis phd no thesis <a href="http://thesisacloud.com/ ">choosing a thesis topic</a> thesis proposals
Comment reply
Abgcspawn
https://thesiswritingtob.com/ - writing my thesis thesis software <a href="http://thesiswritingtob.com/ ">thesis proposal format</a> psychology thesis
Comment reply
Jvqqspawn
https://dissertationahelp.com/ - best dissertation help dissertation proposal template <a href="http://dissertationahelp.com/ ">dissertations writing services</a> write a dissertation
Comment reply
Fbshspawn
https://thesisacloud.com/ - thesis only phd thesis writing software <a href="http://thesisacloud.com/ ">how to write a phd thesis</a> thesis defense advice
Comment reply
Abgcspawn
https://thesiswritingtob.com/ - thesis assistance writing good thesis statements <a href="http://thesiswritingtob.com/ ">thesis coaching</a> thesis writing assistance
Comment reply
Lmoppspawn
http://essaywriteris.com/ - hire someone to write my essay top essay writing websites <a href="http://essaywriteris.com/ ">paid essay writers</a> best essay writing
Comment reply
BrfgRire
https://ljcialishe.com/ - cialis for sale https://cialisvja.com/ - does cialis expire https://viagraonlinejc.com/ - what happens if a woman takes viagra https://viagratx.com/ - lemonaid health viagra https://buycialisxz.com/ - where to buy cialis online
Comment reply
Grvthuse
https://kloviagrli.com/ - viagra street value https://vigedon.com/ - viagra alternative otc https://llecialisjaw.com/ - cialis 20 mg price walmart https://jwcialislrt.com/ - what are the side effects of cialis https://jecialisbn.com/ - does cialis keep you hard after coming
Comment reply
Fbshspawn
https://thesisacloud.com/ - thesis express thesis statement help <a href="http://thesisacloud.com/ ">online thesis</a> writing phd thesis
Comment reply
Abgcspawn
https://thesiswritingtob.com/ - choosing a thesis topic writing thesis service <a href="http://thesiswritingtob.com/ ">law thesis</a> help writing a thesis statement
Comment reply
Fbshspawn
https://thesisacloud.com/ - thesisacloud.com writing a doctoral thesis <a href="http://thesisacloud.com/ ">thesisacloud.com</a> thesis help free
Comment reply
Abgcspawn
https://thesiswritingtob.com/ - thesiswritingtob.com how to write thesis proposal <a href="http://thesiswritingtob.com/ ">thesiswritingtob.com</a> psychology thesis topics
Comment reply