html - background-image appearing in the body margin -


i have thought background-image of element appears in element itself, padding , border not margin. however, while trying else work discovered seems not case , background-image appearing in margin.

body {      background: url("https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png");      background-size: contain;      background-repeat: no-repeat;      border-width: 1px;      border-style: solid;      border-color:red;      margin: 50px;  }
<p>lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>

https://jsfiddle.net/rz52z14r/

as can seen, background appearing in margin. additionally, if add in overflow: hidden overflow not hidden. again, under impression overflow outside bounds of element, i.e., element , padding not including margin.

so, overall: why background image appearing in margin? , why overflow not hidden?

edit: there have been few similar answers question; of provide solution situation. however, situation fictional. know why behaviour occur on the <body> tag not other tag?

the reason why occurs because root element treated special case when rendering background. in effect, background not being applied body canvas:

the background of root element becomes background of canvas , covers entire canvas, anchored (for 'background-position') @ same point if painted root element itself. root element not paint background again.

the background (http://www.w3.org/tr/css2/colors.html#background)

but root element in case html element wouldn't it? well, following states preferred apply background body rather html element:

for html documents, however, recommend authors specify background body element rather html element.

the background (http://www.w3.org/tr/css2/colors.html#background)

if background-color , background-image of html element transparent , none respectively rules regarding canvas apply body element instead:

for documents root element html "html" element or xhtml "html" element has computed values of 'transparent' 'background-color' , 'none' 'background-image', user agents must instead use computed value of background properties element's first html "body" element or xhtml "body" element child when painting backgrounds canvas, , must not paint background child element. such backgrounds must anchored @ same point if painted root element.

the background (http://www.w3.org/tr/css2/colors.html#background)

so, make background on body respect margins add background-color html element in css:

html {      background-color: red;  }  body {      background: url("https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png");      background-size: contain;      background-repeat: no-repeat;      border-width: 1px;      border-style: solid;      border-color:red;      margin: 50px;  }
<p>lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -