css - 使用 CSS 定位第一个标签

标签 css

我正在设计一个门户,除了 CSS 之外我无法更改任何 HTML,并且遇到了以下代码,我想定位第一个标签(您的名字)。

<form class="no-tables" name="" action="" method="post">
    <div>
        <input type="hidden" name="" value="1">
        <label>Your Name</label>
        <label>Your Age</label>
    </div>
</form>

我尝试了 V 形选择器和第一个子选择器,但似乎都不起作用。

最佳答案

尝试使用first-of-type:

/* CSS3 */
form.no-tables > div > label:first-of-type {
  background: red;
}

/* or with CSS2 */
form.no-tables > div > input + label {
  border: 1px solid black;
}
<form class="no-tables" name="" action="" method="post">
  <div>
    <input type="hidden" name="" value="1">
    <label>Your Name</label>
    <label>Your Age</label>
  </div>
</form>

关于css - 使用 CSS 定位第一个标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35897634/

相关文章:

javascript - 如果数组长度为奇数或偶数,则将元素的宽度从 50% 更改为 100%

jquery - 我的导航有点滑稽 : setting a width won't work and it jumps

jquery - 使用 BackStretch.js 自动拉伸(stretch)现有背景图像?

jQuery Mobile – 具有相对高度的内容网格

css - Less 文件编译后的 CSS 存储在哪里?

html - CSS:在放大字体大小时保持div大小相同

html - 如何制作带有背景颜色的顶部 wrapper (?)

图像最大宽度(非 float )在 float div 旁边使用空间?

css - 如何更改语义 UI 图标的颜色?

css 网格媒体查询