无论如何要有一个标签响应 :focus CSS

发布于 2024-11-06 08:53:59 字数 978 浏览 0 评论 0原文

有没有办法让标签响应焦点。我有一些代码,其中文本字段的焦点具有不同的样式。然而,该标签也需要稍微不同的风格。我尝试了这个,但它没有影响标签。

#header .search label {
    background:url(http://www.golfbrowser.com/images/icons/search.png) left top no-repeat;
    padding-left:20px;
    height:20px;
    float:right;
}
#header .search label:focus {
    background:url(http://www.golfbrowser.com/images/icons/search-z.png) left top no-repeat;
    padding-left:20px;
    height:20px;
    float:right;
}
#header .search input {
    padding:0px;
    border:0px;
    width:140px;
    height:20px;
    float:left;
    padding-right:10px;
    background:url(http://www.golfbrowser.com/images/icons/searchbar.png) right top no-repeat;
}
#header .search input:focus {
    padding:0px;
    width:190px;
    height:20px;
    float:left;
    padding-right:10px;
    background:url(http://www.golfbrowser.com/images/icons/searchbar-z.png) right top no-repeat;
}

标签包含图像和圆角的其他部分,并且它也必须改变颜色才能使字段看起来正确。

任何想法,

太棒了

Is there any way to have a label respond to focus. I have some code where the textfield has a different style on focus. The label also however needs a slightly different style. I tried this but it did not effect the label.

#header .search label {
    background:url(http://www.golfbrowser.com/images/icons/search.png) left top no-repeat;
    padding-left:20px;
    height:20px;
    float:right;
}
#header .search label:focus {
    background:url(http://www.golfbrowser.com/images/icons/search-z.png) left top no-repeat;
    padding-left:20px;
    height:20px;
    float:right;
}
#header .search input {
    padding:0px;
    border:0px;
    width:140px;
    height:20px;
    float:left;
    padding-right:10px;
    background:url(http://www.golfbrowser.com/images/icons/searchbar.png) right top no-repeat;
}
#header .search input:focus {
    padding:0px;
    width:190px;
    height:20px;
    float:left;
    padding-right:10px;
    background:url(http://www.golfbrowser.com/images/icons/searchbar-z.png) right top no-repeat;
}

The label contains an image and the other part of a round corner and it too must change colour in order for the field to look correct.

Any ideas,

Marvellous

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

∝单色的世界 2024-11-13 08:54:00

您实际上无法将焦点放在标签上。它不是可聚焦的表单元素。此外,即使您可以这样做,那么之前拥有焦点的任何内容(这意味着您的输入)无论如何都会将其丢失给标签。

您可能需要重新构建您的 HTML(并相应地修改您的 CSS),以便您可以选择 input:focus,然后该输入的相应标签。例如,如果您在输入后移动了标签并为标签使用了以下 CSS 选择器,那么您应该能够完成您想要的任务。

#header .search input:focus + label

You can't actually give focus to a label. It's not a focusable form element. Besides, even if you could do that, then whatever previously had focus (that means your input) would lose it to the label anyway.

You may have to restructure your HTML (and modify your CSS accordingly) so that you can select input:focus and then that input's corresponding label. For instance, if you moved your label after your input and used the following CSS selector for your label, you should be able to accomplish what you want.

#header .search input:focus + label
随遇而安 2024-11-13 08:54:00

BoltClock 的答案是通过更语义化、轻量级的方式来实现此功能。然而,并不总是能够拥有特定的 HTML 结构(尤其是为了促进像这样的小功能),并且 CSS 缺乏遍历 HTML 层次结构的能力。为了解决这个问题,这里有两种选择。第一个即将推出(CSS 规范 4),第二个是我们的老伙伴 Javascript。

首先,CSS4 的 :focus-within 伪选择器。这正是它在锡上所说的(范围基于任何子元素的活动焦点状态)。 此处了解有关该规范的更多信息。因此,假设您的 HTML 结构为:

<div class="input-wrapper">
    <label for="elem">Label Text
      <input name="elem" id="elem" type="text" />
    </label>
</div>

您可以简单地通过以下方式确定“focussed”标签的范围:

label:focus-within{}

同样,您还可以使用以下方式确定父 div 的范围:

div.input-wrapper:focus-within{}

Magical。但今天不适合使用:(

其次,如果您已经在使用 JS 选择器引擎(即 jQuery、Sizzle 等),您还可以执行以下操作:

$('input').on("focus", function(){
  var input = $(this);
  // assuming label is the parent, i.e. <label><input /></label>
  var label = $(this).parent();
  label.addClass('focussed');
  input.on("blur", function(){
    label.removeClass('focussed');
    input.off("blur");
  });
});

这是未经测试但其实现的本质是使用类而不是 :focus 伪选择器,因此您可以将焦点样式添加到我已经的 label.focussed{} 中。添加(并自行删除)模糊处理程序以方便删除该类。

BoltClock's answer is the more semantic, lightweight way of achieving this functionality. However it's not always possible to have a specific HTML structure (especially to facilitate a minor feature like this) and CSS lacks the ability to traverse up the HTML hierarchy. To get around that, here are two alternatives. The first is coming soon (CSS spec 4) and the second is our old mate Javascript.

First up, CSS4's :focus-within pseudo selector. This does exactly what it says on the tin (scopes based on any child element's active focus state). Read more about the spec here. So assuming you have a HTML structure of:

<div class="input-wrapper">
    <label for="elem">Label Text
      <input name="elem" id="elem" type="text" />
    </label>
</div>

you could scope the 'focussed' label by simply:

label:focus-within{}

by the same token, you could also scope the parent div with:

div.input-wrapper:focus-within{}

Magical. But not for use today :(

Second up, if you're already using a JS selector engine (i.e. jQuery, Sizzle, etc.), you could also do something along the lines of:

$('input').on("focus", function(){
  var input = $(this);
  // assuming label is the parent, i.e. <label><input /></label>
  var label = $(this).parent();
  label.addClass('focussed');
  input.on("blur", function(){
    label.removeClass('focussed');
    input.off("blur");
  });
});

This is untested but the essence of what this achieves is using a class rather than the :focus pseudo selector. So you can add your focussed styles to label.focussed{}. I've added (and self-removed) the blur handler to facilitate removing the class.

握住我的手 2024-11-13 08:54:00

现在使用 Flex box 可以解决这个问题。让 label 元素位于 HTML 中的 input 元素之后。然后使用 flex-direction: column-reverse 更改其位置以显示在输入元素上方。然后,您可以使用 input:focus + label: {} 来定位标签。

.input-container {
  display: flex;
  flex-direction: column-reverse;
}

  .input-container > input {
  /* your input styles */
}

 .input-container > input:focus + label {
  /* targets the label when the input receives focus */
  color: red;
 }
<div class="input-container">
  <input type='email' />
  <label>Email</label>
</div>

Now using Flex box will solve this. Have the label element follow the input element in the HTML. Then use flex-direction: column-reverse to change its position to appear above the input element. You can then use the input:focus + label: {} to target the label.

.input-container {
  display: flex;
  flex-direction: column-reverse;
}

  .input-container > input {
  /* your input styles */
}

 .input-container > input:focus + label {
  /* targets the label when the input receives focus */
  color: red;
 }
<div class="input-container">
  <input type='email' />
  <label>Email</label>
</div>

我纯我任性 2024-11-13 08:54:00
use:checked instead of :focus and you must give id,name,value into 'input'.
use:checked instead of :focus and you must give id,name,value into 'input'.
甜扑 2024-11-13 08:54:00

找到了一个很好的解决方案 - order 属性做了一个技巧:

input:focus {
        background-color: #F2FFF0;
    }
    
    * {
        font-family: "Arial";
        font-size: 13px;
    }
    
    div.settings {
        display:grid;
        grid-template-columns: max-content max-content;
        grid-gap: 7px
    }
    div.settings label {
        text-align:right;
        padding-top: 3px
    }
    
    div.settings label:after {
        content: ":";
    }
    
    div.settings input:focus  + label:before  {
        content: "\25B6  ";
        font-size: 12px;
        color: red;
    }
    
    input {
        border: 1px solid #ccc;
        padding: 2px 4px;
        font-size: 13px;
    }
<div class="settings">
<input style="order:2" type="text" title="(vardas, pavardė ir pan.)" autocomplete="off" id="name" name="name" required minlength="4" maxlength="128" size="50"><label style="order:1" for="name">Pirkėjas</label>
<input style="order:4" type="text" title="" autocomplete="off" id="company" name="company" required minlength="4" maxlength="128"><label style="order:3" for="company">Įmonės pavadinimas</label>
<input style="order:6" type="text" title="" autocomplete="off" id="companyCode" name="companyCode" required minlength="4" maxlength="128"><label style="order:5; min-width: 160px" for="companyCode">Įmonės (asmens) kodas</label>
</div>

Found a good solution - order property made a trick:

input:focus {
        background-color: #F2FFF0;
    }
    
    * {
        font-family: "Arial";
        font-size: 13px;
    }
    
    div.settings {
        display:grid;
        grid-template-columns: max-content max-content;
        grid-gap: 7px
    }
    div.settings label {
        text-align:right;
        padding-top: 3px
    }
    
    div.settings label:after {
        content: ":";
    }
    
    div.settings input:focus  + label:before  {
        content: "\25B6  ";
        font-size: 12px;
        color: red;
    }
    
    input {
        border: 1px solid #ccc;
        padding: 2px 4px;
        font-size: 13px;
    }
<div class="settings">
<input style="order:2" type="text" title="(vardas, pavardė ir pan.)" autocomplete="off" id="name" name="name" required minlength="4" maxlength="128" size="50"><label style="order:1" for="name">Pirkėjas</label>
<input style="order:4" type="text" title="" autocomplete="off" id="company" name="company" required minlength="4" maxlength="128"><label style="order:3" for="company">Įmonės pavadinimas</label>
<input style="order:6" type="text" title="" autocomplete="off" id="companyCode" name="companyCode" required minlength="4" maxlength="128"><label style="order:5; min-width: 160px" for="companyCode">Įmonės (asmens) kodas</label>
</div>

指尖上的星空 2024-11-13 08:54:00

我想我对此有一个更简单的答案,那就是添加 tabindex="0"

I think I have a simpler answer to this and that is just to add tabindex="0".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文