Miggi
Miggi

Reputation: 13

SCSS - Combining/shorten/simplify code with parametres?

Is it somehow possible to combine this SCSS code with parametres or something similar? I just don't know how to do that. This code is about styling the different types of input fields in SCSS. I had do to it this way because I didn't want to delete the specific properties of the different types (e.g. type=password -> I wanted i to still mask the input in the field)

input[type=text]{
  width:100%;
  border:1px solid $nav-color;
  border-radius:4px;
  margin: 4px 0;
  padding: 5px;
  box-sizing:border-box;
  transition:.3s;
  &:focus{
    border-color: $turquoise;
    box-shadow: 0 0 4px 0 $turquoise;
  }
}

.inputWithIcon{
  position:relative;
  input[type=text]{
    padding-left:40px;
    &:focus + i{
      color:$turquoise;
    }
  }
  i{
    position:absolute;
    left:0;
    padding: 10px 7px;
    color: $nav-color;
    transition:.3s;
  }
}

input[type=date]{
  width:100%;
  border:1px solid $nav-color;
  border-radius:4px;
  margin: 4px 0;
  padding: 5px;
  box-sizing:border-box;
  transition:.3s;
  &:focus{
    border-color: $turquoise;
    box-shadow: 0 0 4px 0 $turquoise;
  }
}
.inputWithIcon{
  position:relative;
  input[type=date]{
    padding-left:40px;
    &:focus + i{
      color:$turquoise;
    }
  }
  i{
    position:absolute;
    left:0;
    padding: 10px 8px;
    color: $nav-color;
    transition:.3s;
  }
}

input[type=password]{
  width:100%;
  border:1px solid $nav-color;
  border-radius:4px;
  margin: 4px 0;
  padding: 5px;
  box-sizing:border-box;
  transition:.3s;
  &:focus{
     border-color: $turquoise;
     box-shadow: 0 0 4px 0 $turquoise;
  }
} 

Upvotes: 0

Views: 78

Answers (1)

mfluehr
mfluehr

Reputation: 3177

This looks like a good time to use a placeholder. Placeholders don't output code directly, but can be extended by other selectors to output their contents.

$nav-color: #333;
$turquoise: #48d1cc;

// Input placeholder
%input {
  width: 100%;
  border: 1px solid $nav-color;
  border-radius: 4px;
  margin: 4px 0;
  padding: 5px;
  box-sizing: border-box;
  transition: color .3s;

  &:focus {
    border-color: $turquoise;
    box-shadow: 0 0 4px 0 $turquoise;
  }
}

input[type=text],
input[type=date],
input[type=password] {
  @extend %input;
}


.inputWithIcon {
  position: relative;

  input {
    padding-left: 40px;

    &:focus + i {
      color: $turquoise;
    }
  }

  i {
    position: absolute;
    left: 0;
    padding: 10px 7px;
    color: $nav-color;
    transition: color .3s;
  }
}

Upvotes: 1

Related Questions