Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Less have a feature to shorten input type selectors?

Tags:

css

less

I have written this css:

.form-group > input[type="text"],
.form-group > input[type="text"]:hover,
.form-group > input[type="text"]:active,
.form-group > input[type="password"],
.form-group > input[type="password"]:hover,
.form-group > input[type="password"]:active,
.form-group > input[type="email"],
.form-group > input[type="email"]:hover,
.form-group > input[type="email"]:active {
    max-width: 400px;
}

and i know that i can shorten this by writing

.form-group > input[type="text"],
.form-group > input[type="password"],
.form-group > input[type="email"] {
    max-width: 400px;
    &:hover,
    &:active {
    }
}

Well, the second code part is what i really did write, the first one is just for the dramaturgy of the question, i guess ;)

Now i wonder if there is a feature that allows to group the input type selectors as well, something like this:

.form-group > input {
        $:text,password,email
    max-width: 400px;
    &:hover,
    &:active {
    }
}
like image 968
marue Avatar asked Dec 19 '22 18:12

marue


2 Answers

No, Less doesn't provide a function like this.

SCSS has a more "programmatic" approach, so there you can do what you wanted using the @each direcive.

like image 20
markusthoemmes Avatar answered Feb 05 '23 00:02

markusthoemmes


You can use less like regular CSS applies:

.form-group > input {
  &[type="text"], &[type="password"], &[type="email"] {
     &:hover, &:active {
       max-width: 400px;
     }
  }
}

The ampersand (&) references the input element, and you just add a rule for the type attribute

like image 135
yoavmatchulsky Avatar answered Feb 04 '23 23:02

yoavmatchulsky