带边框半径的css输入角三角标签

时间:2017-12-07 12:30:06

标签: html css forms input

我想在input(text)元素中创建一个三角形标签,如下所示: input triangle corner with border radius

到目前为止我能做到的是: input triangle corner without border radius

我怎样才能将这个三角形“放在”输入边界下,让我们说,保持边界半径不受输入切割这个三角形的边缘?

这是我的代码:

<div class="input">
   <label>email adress</label>
   <div class="note">
      <p>*</p>
      <input type="text" placeholder="Enter your email adress">
   </div>
</div>

.input {
   display: flex;
   flex-direction: column;
   width: 100%;
}
.note > input {
   width: 515px;
   height: 48px;
   border-radius: 5px;
   border: 1px solid #7d7d7d;
   box-shadow:inset 0 0 5px 1px lightgray;
   padding-left: 20px;
   color: #acacac;
   -webkit-color: #acacac;
   -moz-color: #acacac;
   -ms-color: #acacac;
   -o-color: #acacac;
   font-size: 16px;
   background-color: rgba(211, 211, 211, 0.50);
}

.note > input:focus {
   outline: none;
   background-color: #f9f9f9;
}

.note {
   position: relative;
   display: inline-block;
}
.note:after {
   content: "";
   position: absolute;
   top: 0;
   right: 0;
   width: 0; 
   height: 0; 
   display: block;
   border-left: 35px solid transparent;
   border-bottom: 35px solid transparent;
   border-top: 35px solid #0094bb;
}

我想在没有失去形状的情况下,没有办法给这个三角形本身提供边界半径,我是对的吗?

3 个答案:

答案 0 :(得分:3)

我为你的P标签添加了绝对位置,并将其置于角落。 边界半径是通过向.note包装器添加边框半径并设置overflow:hidden。

来实现的

&#13;
&#13;
.note > input {
   width: 515px;
   height: 48px;
   border-radius: 5px;
   border: 1px solid #7d7d7d;
   box-shadow:inset 0 0 5px 1px lightgray;
   padding-left: 20px;
   color: #acacac;
   -webkit-color: #acacac;
   -moz-color: #acacac;
   -ms-color: #acacac;
   -o-color: #acacac;
   font-size: 16px;
   background-color: rgba(211, 211, 211, 0.50);
}

.note > input:focus {
   outline: none;
   background-color: #f9f9f9;
}

.note {
   position: relative;
   display: inline-block;
   border-radius: 5px;
   overflow:hidden;
}
.note:after {
   content: "";
   position: absolute;
   top: 0;
   right: 0;
   width: 0; 
   height: 0; 
   display: block;
   border-left: 35px solid transparent;
   border-bottom: 35px solid transparent;
   border-top: 35px solid #0094bb;
   z-index:0;
   
}
.note p{
  position: absolute;
  right: 5px;
  top: 5px;
  color:white;
  z-index:1;
  margin:0;
  padding:0;
}

label { 
  display:block;
}
&#13;
<div class="input">
   <label>email address</label>
   <div class="note">
      <p>*</p>
      <input type="text" placeholder="Enter your email adress">
   </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

考虑将伪元素声明为.note的嵌套元素。

这将允许您在包含元素而不是伪元素上指定所需的border-radius属性。此外,在此新的包含元素上声明了overflow: hidden规则,伪元素的右上角将是&#34; cut-off&#34;从视图中,传达的印象是这是一个嵌套在输入字段中的元素。

代码段示范:

&#13;
&#13;
.note > input {
   width: 515px;
   height: 48px;
   border-radius: 5px;
   border: 1px solid #7d7d7d;
   box-shadow:inset 0 0 5px 1px lightgray;
   padding-left: 20px;
   color: #acacac;
   -webkit-color: #acacac;
   -moz-color: #acacac;
   -ms-color: #acacac;
   -o-color: #acacac;
   font-size: 16px;
   background-color: rgba(211, 211, 211, 0.50);
}

.note > input:focus {
   outline: none;
   background-color: #f9f9f9;
}

.note {
   position: relative;
   display: inline-block;
}

/* additional */
.note .required {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 1;
    color: white;
    padding: 5px;
    box-sizing: border-box;
    border-top-right-radius: 5px;
    overflow: hidden;
    width: 35px;
    height: 35px;
    text-align: right;
}

.note .required:after {
    content: "";
    position: absolute;
    top: 0;
    right: 0;
    width: 0;
    height: 0;
    display: block;
    border-left: 35px solid transparent;
    border-bottom: 35px solid transparent;
    border-top: 35px solid #0094bb;
    z-index: -1;
}
&#13;
<div class="input">
   <label>email adress</label>
   <div class="note">
      <span class="required">*</span>
      <input type="text" placeholder="Enter your email adress">
   </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

我最接近您的解决方案。但我找到了其他方式,你对此感到满意......

&#13;
&#13;
.note > input {
   width: 515px;
   height: 48px;
   border-radius: 5px;
   border: 1px solid #7d7d7d;
   box-shadow:inset 0 0 5px 1px lightgray;
   padding-left: 20px;
   color: #acacac;
   -webkit-color: #acacac;
   -moz-color: #acacac;
   -ms-color: #acacac;
   -o-color: #acacac;
   font-size: 16px;
   background-color: rgba(211, 211, 211, 0.50);
}

.note > input:focus {
   outline: none;
   background-color: #f9f9f9;
}

.note {
   position: relative;
   display: inline-block;
}
.note:after {
   content: "";
   position: absolute;
   top: 0;
   right: 0;
   width: 0; 
   height: 0; 
   display: block;
   line-height: 48px;
   text-align: top;
   border-left: 35px solid transparent;
   border-bottom: 35px solid transparent;
   border-top: 35px solid #0094bb;
}
.note::before {
   content: "*";
   position: absolute;
   right: 7px;
   z-index: 1;
   top: 5px;
   color: white;
}
&#13;
<div class="input">
   <label>email adress</label>
   <div class="note">
      <input type="text" placeholder="Enter your email adress">
   </div>
</div>
&#13;
&#13;
&#13;