【问题标题】:CSS properties don't work in child diffCSS 属性在子 diff 中不起作用
【发布时间】:2013-09-20 08:43:57
【问题描述】:

我制作了一个父 DIV 和一个子 DIV,子 DIV 不居中,宽度也不起作用。 同样,当我在子 DIV 中添加 IMG 时,这些属性也不起作用。

例如:

HTML:

<div id="parent">
 <div id="child">
  <img id="big" .. />
 </div>

</div>

CSS:

#parent {
  background: #999;
  margin: auto;
  width: 100%;

}

#parent #child {
  width: 80%;
  margin: auto;
  text-align: center;
}

#big {
  display: inline;
  max-width: 100%;
  margin: auto;
}

(我使用 HTML5 Boilerplate http://html5boilerplate.com/ 制作了测试站点并且响应迅速)

我花了几个小时解决这个问题,但没有结果:-(

【问题讨论】:

  • 这里看起来不错:jsfiddle.net/Y5WfH
  • 谢谢这个例子。帮助解决问题非常有用

标签: css html html5boilerplate


【解决方案1】:

使用类。

<div id="parent">
 <div class="child">
  <img id="big" .. />
 </div>
</div>

#parent {
  background: #999;
  margin: auto;
  width: 100%;

}

.child {
  width: 80%;
  margin: auto;
  text-align: center;
}

#big {
  display: inline;
  max-width: 100%;
  margin: auto;
}

【讨论】:

  • 也许我遗漏了一些东西,但这将如何解决他的问题?尽管我确实同意它是更好的编码标准,但我看不出问题出在 id 中
  • 据我所知,他想从“父”中获取设置并将它们应用到其他位置,但在这个特定的位置中,他将使用子来分配其他设置。因此,他可以使用子类来代替手动覆盖等。那确实是更好的标准。如果我错了,请纠正我:P 这都是我脑海中的假设
猜你喜欢
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 2016-08-02
  • 2014-05-22
  • 2012-01-06
  • 2013-11-21
  • 2016-10-11
相关资源
最近更新 更多