5

I have some questions regarding the following css that I found:

html, body {
    height:100%;
    min-width:100%;
    position:absolute;
}

html {
    background: none repeat scroll 0 0 #fff;
    color:#fff;
    font-family:arial,helvetica,sans-serif;
    font-size:15px;
}


  1. is it necessary to have height and min-width to 100% on the html and body? What's the benefit?
  2. what is the reason for using position absolute?
  3. why did they set the background/color/font on the html and not the body? Is there a difference? Or is it just preference?


4

2 回答 2

4
  1. 通常是不必要的。但是,有时您可能需要它。例如,也许您的基本/站点范围的网站 css 文件指定了不同的大小(您知道那些边只是边框的站点,通常是博客?这些宽度已被缩小)。请注意,当您有百分比时,它属于父容器。所以 Div A 可能有width: 100%,但如果它的父容器有width: 500pxDiv A 将有 100% 的 500px。

  2. position: absolute我能想到的 html + body 上没有任何理由。绝对定位的一个副作用是元素不再与其他元素“浮动内联”(不确定您将如何描述/措辞)。

    例如,position: relative忽略绝对定位的元素。因此,如果您有 Image A (绝对)和 Image B (相对)并且 B 有left: 10px;,则 Image B 将从父级的左侧偏移,而不是 A 本来的位置。希望我在这里有意义。

    所以有时我只要有背景图像就设置“位置:绝对”。如果它是第一个孩子,它的所有东西都会显示在它上面(因为新元素是“添加到顶部”并忽略绝对定位的元素)。

  3. 身体将继承这些属性,所以是的,这只是偏好。

于 2012-06-12T00:30:53.413 回答
2

将元素的宽度或高度设置为 100% 仅在其父元素也为该尺寸的 100% 时才有效。这意味着如果 body 甚至 html 标签由于某种原因不是高度或宽度的 100%,则其中具有这些属性的元素将具有 0 高度或宽度。

例如:http: //jsfiddle.net/KZaum/

于 2012-06-12T00:28:45.823 回答