如何用纯色填充背景的其余部分

时间:2015-06-20 16:18:19

标签: html css

查看我的代码,看看我的意思。我希望整个背景都是黑色的。但是,背景只有黑色,有文字。

这是图片:example

<div style="background-color:black">
    <img src="http://csc2.madonna.edu/~brabahy/hw5/images/Mercury1.jpg" style="float:left; margin-right:10px" height="400" width="400">
    <p style="color:brown">Mercury is the smallest planet, and it's closest to the Sun of the eight planets in the Solar System. It has an orbital period of about 88 Earth days.</p>
</div>

2 个答案:

答案 0 :(得分:1)

<强>的index.html

<html>

  <head>
    <link rel="stylesheet" href="style.css">
  </head>

  <body>
    <img src="http://csc2.madonna.edu/~brabahy/hw5/images/Mercury1.jpg" style="float:left; margin-right:10px" height="400" width="400">
    <p style="color:brown">Mercury is the smallest planet, and it's closest to the Sun of the eight planets in the Solar System. It has an orbital period of about 88 Earth days.</p>
  </body>

</html>

<强>的style.css

body {background-color: black;}

答案 1 :(得分:1)

当您尝试设置

时发生的问题
background-color: black;
div的

是div取文本的尺寸而不是图像的尺寸。正确设置了背景颜色属性,但未设置div的尺寸。由于图像高度为400,请尝试

style="background-color: black; height: 400px;"

这适用于div,并允许您向身体添加其他元素,因为整个身体不会是黑色。

<强>增加:

根据添加的图像,为body背景着色不会产生所需的输出。

相关问题