简体   繁体   English

当id引用时,CSS不应用于基页元素

[英]CSS not being applied to base page element when referenced by id

I have a Master Page which has an associated css file. 我有一个主页面,它有一个相关的css文件。 On one of the base pages I have a div to which I am trying to apply a style from this css file by id. 在其中一个基页上,我有一个div,我试图通过id从这个css文件中应用一个样式。 However, the page when rendered has a different id for this element. 但是,呈现时的页面具有此元素的不同ID。

How can I specify the correct id name in the css file? 如何在css文件中指定正确的id名称?

Is there a way to specify that I want the id of this element like there is in javascript using the <%= Element.ClientID %>? 有没有办法指定我想要使用<%= Element.ClientID%>在javascript中的这个元素的id?

As best practice you should use the CssClass property to handle the look and feel of page controls. 作为最佳实践,您应该使用CssClass属性来处理页面控件的外观。 And for the page UI use the ID property which are commonly not controls and have child elements. 对于页面UI,使用ID属性,这些属性通常不是控件并且具有子元素。

Controls 控制

<asp:Label id="lblTest" CssClass="label" runat="Server">Test</Label>

Parent Elements 父元素

<div id="someid">
      <asp:Label id="lblTest" CssClass="label" runat="Server">Test</Label>
</div>

You should be using class="someClass" for this instead of by the id. 您应该使用class =“someClass”而不是id。 If you MUST use an id, put a container around it you know will not change, and give it an id, then do this 如果你必须使用一个id,在它周围放一个你知道不会改变的容器,并给它一个id,然后这样做

#myContainer .someClass {
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM