CSS body element width not working

前端 未结 6 839
挽巷
挽巷 2021-01-28 14:32

HTML:


    

Some content

CSS:

body {
    margin: 0 auto;
    width: 470px;
    back         


        
6条回答
  •  不要未来只要你来
    2021-01-28 15:23

    When the element has no specified background the body background will cover the page. If the html element does have a background specified on it, the body background will behave like any other element.

    From the W3:

    For HTML documents, however, we recommend that authors specify the background for the BODY element rather than the HTML element. For documents whose root element is an HTML "HTML" element or an XHTML "html" element that has computed values of 'transparent' for 'background-color' and 'none' for 'background-image', user agents must instead use the computed value of the background properties from that element's first HTML "BODY" element or XHTML "body" element child when painting backgrounds for the canvas

    So essentially your code is fine and the content is centered, but the background you specified on the body is being applied to the as well. You can see the difference when you give the element a white background:

    jsFiddle example

提交回复
热议问题