为 div 的第一个标签应用 CSS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20782995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 01:24:59  来源:igfitidea点击:

apply CSS for the first label of div

csscss-selectors

提问by hamza437

i would like to apply a specific css to a specific label in my html this is my HTML

我想将特定的 css 应用于我的 html 中的特定标签,这是我的 HTML

<div id="registration">
    <div>
        <label>Localisation</label>**//this is the target label to apply css** 
        <div id="registration_localisation">
            <div>
                <label>Gouvernorat</label>
                <select id="registration_localisation_gouvernorat">
                  <option value="1">Ariana</option>
                  <option value="2">Ben Arous</option>
                  <option value="3">Bizerte</option>
               </select>
                </div>
                 <div>
                <label for="registration_localisation_ville">Ville</label>
                <input type="text" id="registration_localisation_ville">
                 </div>
               </div>
         </div>
         <div>
        <label>Annonceur</label>**//this is the target label to apply the css** 
        <div id="registration_annonceur">
            <div>
                <label for="registration_annonceur_Nom">Nom</label>
                <input type="text" id="registration_annonceur_Nom">
            </div>
            <div>
                <label for="registration_annonceur_Email">Email</label>
                <input type="text" id="registration_annonceur_Email">
            </div>
            <div>
                <label for="registration_Telephone" >Telephone</label>
                <input type="text" id="registration_annonceur_Telephone">
            </div>
        </div>
         </div>

</div>

i used many pseudo classes but i didn't find any solution any idea please ?

我使用了很多伪类,但我没有找到任何解决方案,请问有什么办法吗?

回答by Alessandro Minoccheri

try this way where you can style all your label inside the div contained into registration

尝试这种方式,您可以在其中包含的 div 中设置所有标签的样式 registration

#registration > div > label{
   //your css
}

DEMO

演示

回答by blasteralfred Ψ

Just give it a class. Classes may be repeated in HTML, like;

给它一个课。类可以在 HTML 中重复,例如;

<label class="class1">Localisation</label>

and in your css,

在你的 css 中,

.class1{
  //styling
}

回答by Sahil Popli

CSS3 way

CSS3方式

#registration div label:first-child {
    // specific styles for just the first label item
}

Or

或者

#registration div label:nth-first-of-type{
// specific styles for just the first label item
}

jQuery Way

jQuery方式

<script>
$( "#registration div label:first" ).css( "font-style", "italic" );
</script>

回答by myTerminal

How about

怎么样

#registration > div > label:first-of-type {}

?

?

回答by LeBen

Match labels who are the first, direct child of a div.

匹配 div 的第一个直接子代的标签。

div > label:first-child {}

Actually it would be much better if you could add some classes to your elements. Matching wide selector such as divis a bad idea for future maintainability. Changing your markup will break your style.

实际上,如果您可以为元素添加一些类会更好。匹配宽选择器div对于未来的可维护性来说是一个坏主意。改变你的标记会破坏你的风格。