56

我正在开发页面,现在以我的 css 样式我有这行代码

.flex-control-thumbs li {
    width: 25%;
    float: left;
    margin: 0;
}

对于我的网页。现在,我的一些页面不需要这一行

width: 25%;
float: left;

有可能我可以在页面的内部 css 中覆盖它,这会导致原始行为被忽略吗?

4

7 回答 7

88

!important不推荐使用,但在这种情况下,我认为你应该 -

把它写在你的内部 CSS 中——

.flex-control-thumbs li {
  width: auto !important;
  float: none !important;
}
于 2012-10-29T07:00:04.457 回答
9

而不是覆盖,将其创建为不同的 css 并在您的元素中将其作为其他 css(多个 css)调用。
就像是:

.flex-control-thumbs li 
{ margin: 0; }

内部 CSS:

.additional li
{width: 25%; float: left;}

<ul class="flex-control-thumbs additional"> </ul> /* assuming parent is ul */
于 2012-10-29T06:58:07.987 回答
5

You can create one more class naming

.flex-control-thumbs-without-width li {
width: auto;
float: initial; or none
}

Add this class whenever you need to override like below,

<li class="flex-control-thumbs flex-control-thumbs-without-width"> </li>

And do remove whenever you don't need for other <li>

于 2012-10-29T07:09:46.540 回答
3

是的,你确实可以。我可以想到三种方法来实现这一点。

  1. 为元素添加内联样式。
  2. 创建并附加一个新的 <style> 元素,并添加文本以覆盖此样式。
  3. 修改css规则本身。

笔记:

  1. 有点混乱,并增加了浏览器渲染所需的解析。
  2. 也许我最喜欢的方法
  3. 不是跨浏览器,一些浏览器喜欢它以一种方式完成,另一些则以不同的方式完成,而其余的只是对这个想法犹豫不决。
于 2012-10-29T07:06:05.910 回答
3

只需添加

.flex-control-thumbs li {
width: auto; 
}
于 2012-10-29T07:02:32.023 回答
2

您可以在外部样式表之后的所需页面中添加样式,以便它们级联并覆盖第一组规则。

<link rel="stylesheet" href="allpages.css">
<style>
.flex-control-thumbs li {
  width: auto;
  float: none;
}
</style>
于 2012-10-29T07:05:47.600 回答
2

增加你的CSS 特异性

例子:

.parent-class .flex-control-thumbs li {
  width: auto;
  float: none;
}

演示:

.sample-class {
  height: 50px;
  width: 50px;
  background: red;
}

.inner-page .sample-class {
  background: green;
}
<div>
  <div class="sample-class"></div>
</div>

<div class="inner-page">
  <div class="sample-class"></div>
</div>

于 2020-09-27T01:57:16.307 回答