如何在不对所有内容着色的情况下为文本div添加背景颜色?

时间:2014-04-14 21:05:16

标签: html css

我的导航div位于页面顶部。 div完全是文本,并且工作正常,但是当滚动图像或文本时,它无法清晰地看到。我尝试通过在div标签中添加背景颜色来解决这个问题,但由于某种原因,背景颜色会改变整个页面的颜色,覆盖所有内容。

这是因为div被固定在顶部吗?有没有办法清除它?

之前缺乏代码的道歉。这基本上就是我在做的事情:

<html>
<head>
<title>stack overflow</title>
<style type="text/css">
    #nav {
    position: fixed;
    left: 0px;
    top: 0px;
    right: 0px;
    bottom: 0px;
    background-color: #0000FF;
}
</style>
<link href="yotb.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="nav"><center>HEADER</center></div>
<div id="container">
<div id="content">
PAGE CONTENT
</div>
</div>
</div>
编辑:卡里姆的答案修正了它。感谢大家的帮助。

3 个答案:

答案 0 :(得分:1)

您在此处所做的是,您将position:fixed;设置为元素,并将其声明为top:0 bottom:0的边界,以便它可以从上到下传播,并left:0 right:0所以它会从左向右传播,只需移除右下角(你真的不需要那些兄弟)。

请查看 FIDDLE

答案 1 :(得分:0)

<style type="text/css">
    .anchored-top
    {
        float:left;
        position:relative; /* Highlight only text in the div by wrapping objects in a span */
    }
    .anchored-top span
    {
        background-color: blue;
    }
    .clear
    {
        clear:both; /* use an empty div with a class of clear to clear the floats */
    }
</style>
<div class="anchored-top">
   <span>Stuff in Here</span>   
</div>
<div class="clear" />

答案 2 :(得分:0)

如果没有看到你已经拥有的东西就很难提出问题,但我会首先推荐以下内容。 (如果您已熟悉HTML,请先告诉我)

  1. 检查div是否已关闭
  2. 将宽度和高度设置为div
  3. 确保您只更改该div的背景颜色而不是其他元素
  4. 我希望这有帮助!