【问题标题】:Import existing CSS class in new DIV HTML tag在新的 DIV HTML 标签中导入现有的 CSS 类
【发布时间】:2012-10-28 07:45:24
【问题描述】:

假设有

.myButtonClass{
/*#style;*/
}

现在我有了新的

<div id="myid">

一种方法是我可以拥有

<div id="myid" class="myButtonClass">

如果在我的 CSS 中我想将这个类分配给 div 怎么办

div.myid{
/* assign existing class */
.myButtonClass
}

如何做到这一点?

【问题讨论】:

  • 你可以使用像$('div#myid').addClass('myButtonClass')这样的jquery来做到这一点

标签: html css class stylesheet


【解决方案1】:

Live Demo

如果您在 Div 中应用 ID 或 Class,您现在习惯 both Css 中的 ID 或 Class >

像这样

#myid.myButtonClass{
// style here
}

HTML

<div id="myid" class="myButtonClass">
// Some text here 
</div>

更多信息关于多个Class或idClick here

LIve Demo

【讨论】:

    【解决方案2】:

    使用这个..

     div#myid.myButtonClass{
       //your code should be here 
    }
    

    use也可以用这个。

     div#myid{
       //your code should be here
     }
    
    div.myButtonClass{
       //your code should be here
     }
    

    您可以使用任何一种 about 代码。这两个东西的区别在于,第一个是highly specified the particular div.

    【讨论】:

    • 感谢您的回复....是的,您绝对正确,但是问题是我的课程已经在 css 的某个地方定义了,我必须在 Google 表单 css 中应用它。所以有两种方法,要么我为该 div 正确的每种样式,要么我可以在 CSS 文件中分配我现有的类。所以第一个问题我可以将我现有的样式表类用于另一个 div 元素吗?如果是怎么办?
    • 是的,你可以。最好为每个div 提供单独的课程。这个例子可能对你有帮助。 jsfiddle.net/K5aHT
    【解决方案3】:

    如果没有第三方,你不能这样做,使用 LESS (http://lesscss.org/) 或 Sass (http://sass-lang.com/)

    【讨论】:

      猜你喜欢
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-27
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      相关资源
      最近更新 更多