HTML

Boldewyn / Proposal for CSS :for() pseudo-class.md

elements are notoriously hard to select with CSS in any non-trivial mark-up structure, when the state of their control is to be taken into account. Consider these frequent cases, where it is not possible to reach the label element with CSS selectors, when the corresponding input state is relevant:

1. label>input> Input inside labellabel> 2. tr> td>input>td> td>label>Good ol' tablestd> tr> 3. label>Label before inputlabel>input>

If you want to make the label react to the state of its associated control, you need to rely on JavaScript to generate classes to «listen» to. Only (1) would be addressed by a parent selector, e.g., $label input . Reference combinators only work the other way round, to address the control from its label: label /for/ input .

A clunky way to address a label from its input is to use :has() :

:root:has(input#foo:invalid) label[for="foo"]

But it relies on the knowledge of the ID of the input element, if it has any. Case (1) above is then ruled out.

In many cases, the control element is partially or fully hidden, and the label acts as proxy to interact with the control, loved by designers for its independence from OS styles and ability to use ::before and ::after . In certain constellations handling this situation is already possible today with CSS only. Matt Mastracci played a bit with the thought of using CSS Extensions to expand on this. However, robust solutions would most probably still rely on JavaScript.

A pseudo-class :for() can provide better control over addressing label elements. Its content must be a single selector or selector list (mirroring :not() ), that is matched against label.control . The specificity of :for() is the specificity of its arguments. Examples:

label:for([type="text"]) /* label.control.type === "text" */ label:for(:invalid) /* label.control.invalid === true */ label:for(:focus) /* label.control == document.activeElement */ label:for(:disabled):for(read-only) /* chaining :for() works as expected */ 

If a label has no corresponding control , it will never match a :for() selector.

Источник

: The Label element

The HTML element represents a caption for an item in a user interface.

Try it

Associating a with a form control, such as or offers some major advantages:

  • The label text is not only visually associated with its corresponding text input; it is programmatically associated with it too. This means that, for example, a screen reader will read out the label when the user is focused on the form input, making it easier for an assistive technology user to understand what data should be entered.
  • When a user clicks or touches/taps a label, the browser passes the focus to its associated input (the resulting event is also raised for the input). That increased hit area for focusing the input provides an advantage to anyone trying to activate it — including those using a touch-screen device.

To explicitly associate a element with an element, you first need to add the id attribute to the element. Next, you add the for attribute to the element, where the value of for is the same as the id in the element.

Alternatively, you can nest the directly inside the , in which case the for and id attributes are not needed because the association is implicit:

label> Do you like peas? input type="checkbox" name="peas" /> label> 

The form control that a label is labeling is called the labeled control of the label element. Multiple labels can be associated with the same form control:

label for="username">Enter your username:label> input id="username" name="username" type="text" /> label for="username">Forgot your username?label> 

Elements that can be associated with a element include , (except for type=»hidden» ), , , , and .

Attributes

This element includes the global attributes.

The value of the for attribute must be a single id for a labelable form-related element in the same document as the element. So, any given label element can be associated with only one form control.

Note: To programmatically set the for attribute, use htmlFor .

The first element in the document with an id attribute matching the value of the for attribute is the labeled control for this label element — if the element with that id is actually a labelable element. If it is not a labelable element, then the for attribute has no effect. If there are other elements that also match the id value, later in the document, they are not considered.

Multiple label elements can be given the same value for their for attribute; doing so causes the associated form control (the form control that for value references) to have multiple labels.

Note: A element can have both a for attribute and a contained control element, as long as the for attribute points to the contained control element.

Styling with CSS

There are no special styling considerations for elements — structurally they are simple inline elements, and so can be styled in much the same way as a or element. You can apply styling to them in any way you want, as long as you don’t cause the text to become difficult to read.

Examples

Defining an implicit label

label>Click me input type="text" />label> 

Источник

How to style labels with CSS?

The labels are used to define or set captions for any element on the web. It conveys useful information to the user about any element or maybe any section of the website.

The labels should be designed that it adds up the semantic meaning to it. Here, we will learn to style labels with CSS.

Styling the labels

The labels can be styled with some basic CSS properties like background-color , color , padding , font-size , etc.

Example: Styling the labels with CSS

Here, we have added three level topics. It has been added with some semantic background color.

      span < padding: 16px; height: 10px; width: 100px; margin: 2px; >.label-1 < background-color: green; >.label-2 < background-color: yellow; >.label-3  

Study Topics

Easy Medium Difficult

Output

Here is the output of the above program.

labels

Example: styling the labels with CSS

In the next example, we have taken the same example and changed the background-color on :hover . Also added box-shadow property to each label.

Conclusion

In this tutorial, we have learned to style labels with CSS. We can use basic CSS property to do so. Here, we have added examples to demonstrate it.

Источник

How to style labels with CSS?

The labels are used to define or set captions for any element on the web. It conveys useful information to the user about any element or maybe any section of the website.

The labels should be designed that it adds up the semantic meaning to it. Here, we will learn to style labels with CSS.

Styling the labels

The labels can be styled with some basic CSS properties like background-color , color , padding , font-size , etc.

Example: Styling the labels with CSS

Here, we have added three level topics. It has been added with some semantic background color.

      span < padding: 16px; height: 10px; width: 100px; margin: 2px; >.label-1 < background-color: green; >.label-2 < background-color: yellow; >.label-3  

Study Topics

Easy Medium Difficult

Output

Here is the output of the above program.

labels

Example: styling the labels with CSS

In the next example, we have taken the same example and changed the background-color on :hover . Also added box-shadow property to each label.

Conclusion

In this tutorial, we have learned to style labels with CSS. We can use basic CSS property to do so. Here, we have added examples to demonstrate it.

Источник

Читайте также:  Python with command line arguments
Оцените статью