【问题标题】:Why is the background color of my DIV element not visible? [duplicate]为什么我的 DIV 元素的背景颜色不可见? [复制]
【发布时间】:2021-11-28 18:09:05
【问题描述】:

/**This code doesnt change the color of the class row1**/

html, body {
  font-family :'montserrat',sans-serif;
  }
h1 {
  border-left  : 2px solid #00f28f;
  font-size    : 48px;
  font-weight  : 400;
  padding-left : 20px;
  }
.main {
  margin-top : 80px;
  }
form input {
  background : #F0f0f0;
  border     : none;
  font-size  : 36px;
  padding    : 20px;
  width      : 100%;
  transition : background 0s, border-left 0s;
  }
form input:focus {
  background  : #fff;
  border-left : 2pxsolid #000;
  box-shadow  : none;
  outline     : none;
  }
button.button {
  background : transparent;
  border     : none;
  color      : #00f2bf;
  cursor     : pointer;
  font-size  : 36px;
  padding    : 20px 24px;
  transition : background 0s, border-left 0s;
  }
button.button:hover {
  background : #00f2bf;
  color      :  #fff;
  }
.row1 {
  background : yellowgreen;
  }
<link href="https://fonts.google.com/specimen/Poppins?preview.size=20" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="C:\Users\kalyanasundar.s\OneDrive - HCL Technologies Ltd\Desktop\proj\proj.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>


<header class="header">
  <div class="container">
    <div class="row">
      <div class="col-xs-2 col-md-2">
        <h1>Kalyan The Coder</h1>
      </div>
    </div>
  </div>
</header>
<div class="main">
  <div class="container2">
    <div class="row1">
      <form class="form">
        <div class="col-xs-8 col-md-4">
          <input type="text" id="TextBox1" placeholder="Enter your query">
        </div>
        <div class="col-xs-4 col-md-2">
          <button type="submit" class="button">post</button>
        </div>

这个命令工作正常,只是第 1 行的颜色没有改变,不知道为什么。
如果我在 CSS 上将 row1 更改为 row,标题的颜色会发生变化。我不确定如何更改 CSS 样式表中的类。
我是初学者,想对此进行更多探索。

【问题讨论】:

  • 不,这很正常,虚构的命令总是做任何事情
  • @MisterJojo 想说的欢迎来到 Stack Overflow!不幸的是,我们需要查看您的代码才能提供任何帮助。就目前而言,您的问题可能会因“需要改进”而被关闭。
  • 有了那个编辑,你变得暖和起来了!现在,为什么我们应该期待row1 的颜色发生变化?我知道我们可以从您的代码中推断出它,但为什么认为它应该如此?这只是想要的效果,它不起作用吗?
  • html中有一个类叫row1。在 css 表中有对该类的引用。现在 row1 必须是黄绿色,这是我所要求的。这就是原因
  • use class="row row1 遵循引导规则

标签: html css row background-color


【解决方案1】:

你需要给你的元素高度。


它有助于简化问题。目前,如果您将height: 100px 添加到您的row1 类中,则背景会根据需要变为可见的yellowgreen

.row1 {
    background: yellowgreen;
    height: 100px;
}

让我们用一个简化的 sn-p 来展示区别:

.row-1 { background-color: yellowgreen; }
.row-2 {
  height: 100px;
  background-color: red;
}
<div class="row-1"></div>
<div class="row-2"></div>

注意你怎么在这里看不到row-1?添加高度、内边距或任何其他扩展 div 区域的内容都会使其显示:

.row-1 { background-color: yellowgreen; }
<div class="row-1">
  <p>This should make the row visible.</p>
</div>

【讨论】:

  • 非常感谢 Taco。我会很感激你的这种善意的举动。很高兴向你学习。这有助于理解代码在我的编码生涯中意义重大。 :-)
  • 另外,当我添加一个类“.row”并为其添加颜色而不提及高度时,它可以工作。为什么我需要单独为一行添加高度?
  • @KalyanSundar 正如我所提到的,div 默认没有高度。没有高度,您将看不到背景颜色。
  • 抱歉再次打扰您。就像你说的 div 没有高度。在同一个 sn-p 中,我有一个名为 row 的类,我在其中添加颜色而不提及高度。它是否有效,因为它在标题中?
  • @KalyanSundar 不,正如我之前提到的;它显示颜色是因为它有 content.
猜你喜欢
  • 2018-03-21
  • 2019-08-29
  • 2019-10-31
  • 1970-01-01
  • 2020-03-15
  • 1970-01-01
  • 2016-05-28
  • 2012-07-01
  • 2016-07-22
相关资源
最近更新 更多