Although I've already answered here Div with height 100% , I'll try to lighten up and explain the behavior, DOCTYPE
to HTML5 as well as to HTML4 with Strict works the way, as well as in Quirks mode (Internet Explorer 10), basically the element root
(usually) the html
gets scrollbar, however this varies in Gecko, which scrollbar would be like in body
, anyway when using DOCTYPE
to HTML5
<!DOCTYPE html>
<html>
<head></head>
<body></body>
</html>
Notice the result:
I made the examples in Opera, but it's the same engine as Google Chrome, so the behavior is identical
Evenifthescrollbarispresent,theelement's"box" is with height
equivalent to the content, so even if it applies height: 100%
to your iframe this will not work:
Soifyouapplyheight100%totheelements
<html>
and
<body>
theiframewillbeabletostayat100%,howeveriftheelementisinsideaDIVthatisinsidetheBODY,youwillneedtoapply
height:100%;
forDIVtoo,because100%in
html,body
onlyaffectschildrenof
BODY
,butnot"grandchildren", you also need to apply
width: 100%;
to elements if you need to use width in percentage iframe instead of pixels, it should look like this:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html, body, .main, .main iframe {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div class="main">
<iframe src="https://pt.stackoverflow.com"></iframe></div></body></html>
Howeveryoumaynoticethatthereweresomemarginsintheiframeandyouwillalsonoticethatthebodyscrollbarshavebeengenerated:
Soyouhavetoremovethemargins:
html,body{margin:0;padding:0;}html,body,.main,.mainiframe{margin:0;width:100%;height:100%;}.mainiframe{border:none;/*removeasbordasdoiframes*/}
Howeverdependingonthebrowser(renderingengine)itmaybethattheverticalscrollbaroftheBODY,youhavetwothingsyoucando,oradjustthebox-sizing:content-box;
(itcanstillvary),butwhatprobablyitwillbemoreefficientistoremovethescrollbar,soyouwillonlyusethescrollbarofHTMLandBODYsoitwillonlyusethescrollbarfromwithintheiframe:
html,body{margin:0;padding:0;overflow:hidden;}html,body,.main,.mainiframe{margin:0;width:100%;height:100%;}.mainiframe{border:none;/*removeasbordasdoiframes*/}
Alternative
Thereisasimplealternativethatwillnotdependoncascadingheight:100%;
,justuseposition
(fixed
orabsolute
),notethatgeneratedscrollbariswithintheIFRAME,position:fixed
doesnotaffectthecontentandthereforedoesnotgeneratethescrollbar,differentfromposition:absolute;
:
.main iframe {
border: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div class="main">
<iframe src="https://pt.stackoverflow.com"></iframe></div>
PHPdoesnothaveanythingtodowithclient-side,PHPdoesnotrunatthesametimeasHTML,PHPrunsonserver-sidein>generatestheHTMLoranyothertypeofdocumentsendstotheprogramthatworkstheHTTP(Apache,IIS,Nginx,LightTTPD,etc)andthisinturnsendsasanswerbytheinternetgenerallyandisreceivedtheHTMLalreadyprocessed,IrecommendwhichreadthesepostsImadeonthesubject: