3

I want to add a small triangle to the upper-right corner of an input type='text' HTML element, like Excel uses to indicate a comment.

The accepted answer to this question:

How to add triangle in table cell

shows how to do it for a table cell. I naively applied the "note" css class to my input element but it didn't work. Any suggestions for how to achieve this?

Community
  • 1
  • 1
Joe
  • 122,218
  • 32
  • 205
  • 338
  • Pls post ur code. – Alex Mar 31 '17 at 10:56
  • 3 downvotes; it would be helpful to add a comment to say what's wrong with the question. I thought it was clear enough what I want from the answer to the linked question without posting code. – Joe Mar 31 '17 at 19:13
  • 1
    Upvoted. Both the question and the answer are useful. – udog Jun 22 '18 at 00:11

3 Answers3

9

Pseudo elements can't be used on input's. You can use the same CSS as described in the linked post (here) but you would have to apply the class to an ancestor.

.note {
    position: relative;
    display: inline-block;
}
.note:after {
    content: "";
    position: absolute;
    top: 0;
    right: 0;
    width: 0; 
    height: 0; 
    display: block;
    border-left: 20px solid transparent;
    border-bottom: 20px solid transparent;
    border-top: 20px solid #f00;
}
<div class="note">
  <input type="text">
</div>
Community
  • 1
  • 1
Turnip
  • 35,836
  • 15
  • 89
  • 111
4

Given a simple input field

 <input type="text" />

and assuming we can't use or rely on a parent element for styling, you may use multiple box-shadow to create a triangle, e.g.

Codepen demo


Result (triangle outside)

enter image description here


Code

input {
  padding: 10px;
  border: 1px #ccc solid;
  margin: 15px;
  box-shadow: 0 -12px 0 #fff, 12px 0px 0 #fff, 
              1px -12px 0 #fff, 2px -11px 0 #fff, 
              3px -10px 0 #fff, 4px -9px 0 #fff, 
              5px -8px 0 #fff, 6px -7px 0 #fff, 
              7px -6px 0 #fff, 8px -5px 0 #fff, 
              9px -3px 0 #fff, 10px -2px 0 #fff, 
              11px -1px 0 #fff, 12px 0 0 #fff, 
              8px -12px 0 #9bc;
}

Of course you can change the size of the triangle playing with the offset(y/x) of the box-shadow.

If you need to place the triangle inside the input you could just add a couple of properties to the code above and use outline and outline-offset instead of the border property to create the illusion of an outer border. All the shadows don't move from their position, just give enough space to the offset in order to wrap them.

Codepen demo


Result (triangle inside)

enter image description here


Code

input {
   padding: 0;
   border: 0;
   outline: 1px #ccc solid;
   outline-offset: 15px;  
   ...
}
Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
0

To mark all text inputs with red corner, you could use

textarea, input[type="text"], input:not([type]) {
  background: linear-gradient(225deg, red 5px, white 5px);
}

If you want to mark only required fields, then use required attribute and selector:

[required] { background: linear-gradient(225deg, red 5px, white 5px); }
<input placeholder="optional field">
<input placeholder="mandatory field" required>
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169