2

Sorry about not having an example, but basically I want to give an effect of having a text box crossed out, like being cancelled, etc.

Anyone got any ideas?

Lucas
  • 16,930
  • 31
  • 110
  • 182

9 Answers9

4

Alternatively, here is a pretty solution using SVG lines (no JS), which automatically scales to the dimensions of your text-area. It can also be applied over img elements for example.

JSFiddle: http://jsfiddle.net/xbbzcsrk/

HTML:

<div class="crossed">
    <textarea>This is a test</textarea>
    <svg>
        <line x1="0" y1="100%" x2="100%" y2="0" />
        <line x1="0" y1="0" x2="100%" y2="100%" />
    </svg>
</div>

CSS:

.crossed {
    position: relative;
    width: 200px;
    height: 80px;
}
.crossed svg {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
}
.crossed svg line {
    stroke: rgb(255, 0, 0);
    stroke-width: 2;
}
.crossed textarea {
    width: 100%;
    height: 100%;
    box-sizing:border-box;
}
Tip-Sy
  • 810
  • 10
  • 18
1

the screenshot: enter image description here

html

<div class="con">
        <div class="input-con"><input type="text" value="text example" /></div>
        <div class="strip top-bottom"></div>
        <div class="strip bottom-top"></div>
     </div>

css

.con {
         position: relative;
      }
      .strip {
        margin-left:2px;
         position: absolute;
         left: 0px;
         top: 0px;
         z-index: 10;
         border-width: 0 0 1px 0;
         border-color: red;
         border-style: solid;
         width: 145px;
         transform-origin:top left;
         -ms-transform-origin:top left;
         -moz-transform-origin:top left;
         -webkit-transform-origin:top left;
      }
      .top-bottom {
         margin-top: 2px;
         transform:rotate(8deg);
         -ms-transform:rotate(8deg);
         -moz-transform:rotate(8deg);
         -webkit-transform:rotate(8deg);
      }
      .bottom-top {
         margin-top: 1.2em;
         transform:rotate(-8deg);
         -ms-transform:rotate(-8deg);
         -moz-transform:rotate(-8deg);
         -webkit-transform:rotate(-8deg);
      }
      .input-con > input {
          line-height:1.2em;
          width:146px;
      }
tristan
  • 399
  • 2
  • 7
  • could you provide an example on JSFiddle? – Lucas Jan 18 '13 at 07:10
  • http://jsfiddle.net/Tristan_luo/92nXR/18/ ; I use the CSS3 transform property to draw the diagonal line; and this should work on IE9+ / fixfox / chrome. also you could write it as jquery plugin to dynamically determine the diagonal line's width and angle according to textbox's width and line-height. – tristan Jan 20 '13 at 09:54
1

Here's another possible method, this one using the HTML5 canvas element to draw an 'x' over the textarea.

http://jsfiddle.net/rmqJf/

Since I started working on it a bunch of other, answers popped up, some of them pretty similar. Lots of options to go with!

I place the textarea directly on top of the canvas (of the same size), then use rgba() with alpha 0 on the background of the textarea to make the background transparent so you can see the canvas underneath.

Looking through these though, I'm inclined to feel like the background image solution suggested by @Ragnarokkr and sussed out by @kalpesh patel may be the simplest solution, if executed right.

The code for mine:

HTML:

    <canvas id="myCanvas" width="200" height="100"></canvas>
    <textarea id="myTextArea"></textarea>

JS:

    var c = document.getElementById("myCanvas");
    var ctx = c.getContext("2d");
    ctx.strokeStyle="red";
    ctx.moveTo(0,100);
    ctx.lineTo(200,0);
    ctx.stroke();
    ctx.moveTo(0,0);
    ctx.lineTo(200,100);
    ctx.stroke();

CSS:

    html, body { 
      margin: 0;
      padding: 0;
    }

    #myCanvas {
      padding: 0;
      margin: 0;
      width: 200px;
      height: 100px;
    }

    #myTextArea {
      position: absolute;
      left: 0px;
      right: 0;
      height: 102px;
      width: 202px;
      background: rgba(255,255,255,0);
      padding: 0;
      margin: 0;
    }
Nik
  • 312
  • 1
  • 4
  • 12
  • I really like it, but if you could make it to resize the textarea, that would be awesome. – Lucas Jan 18 '13 at 07:14
  • Alright after thinking a bit I came up with a much simpler method that also enables re-sizing the text area: [http://jsfiddle.net/QrLLA/](http://jsfiddle.net/QrLLA/). It uses a background image in the textArea and the CSS3 property `background-size: 100% 100%;` to keep the image the right size and shape as the text area changes. – Nik Jan 18 '13 at 09:28
  • In theory would probably be able to write a script to redraw the canvas elements in my initial solution each time the user changes the textarea size, but that would be craziness. This way is way less code, and is probably more widely supported by browsers (although it's still a CSS3 solution, so it won't work on older browsers). – Nik Jan 18 '13 at 09:31
1

Adding this one as a new answer because I think it works better than my initial response:

http://jsfiddle.net/QrLLA/

only a few lines of code this time. The HTML:

    <textarea id="myTextArea"></textarea>

The CSS:

    #myTextArea {
      display: block;
      background-image: url('http://i.imgur.com/4zKm6.png');
      background-size: 100% 100%;
      width: 200px;
      height: 100px;
    }

Just uses a image of an 'x' that I made in MS Paint as the background image for the textarea; the background-size: 100% 100%; property allows for re-sizing.

Screenshot:

screenshot:

This still enables the textarea to be written in; I'm not sure if that would be desired behavior in your case or not.

Nik
  • 312
  • 1
  • 4
  • 12
0

You could create an image (one diagonal line) and set the textbox's background with that image (horizontally repeating if you want):

Ragnarokkr
  • 2,328
  • 2
  • 21
  • 31
  • Repeating the image would end up making it look slashed (`///////`), though, not a single line. –  Jan 18 '13 at 04:22
  • Yes, it was only an idea (as I said, **if you want**). I guess this is the simplest way. Other ways could be done with an overlapping `
    ` using `:before` or `:after` but I never tried to do it, so it's just my supposition.
    – Ragnarokkr Jan 18 '13 at 04:24
0

You can try out this:

Markup:

<div class='canceled_input_container'>
  <input type='text'/>
  <span></span> 
</div>

CSS:

div.canceled_input_container {
   position:relative;
   height:30px;
}

div.canceled_input_container span{
   position:absolute;
   background-image: url("/path/to/image");
   background-repeat: no-repeat;
   height:/*background image height*/
   width:/*background image width*/
   top:-15px;
   z-index:1;
}

This is just to guide you and does not contain final solution, you have to set position and other properties as per your requirement.

Kalpesh Patel
  • 2,772
  • 2
  • 25
  • 52
0

Well, this is will work in most browsers:

Empty: ☐ &#9744 ;

Checked: ☑ &#9745 ;

Disabled: ☒ &#9746 ;

Add colors to make it looks even more disabled.

Nabil Kadimi
  • 10,078
  • 2
  • 51
  • 58
0

http://jsfiddle.net/tylerbrownhenry/NRHY5/

Here you go. For the markup...

<input type='text'/>

Then using jquery, you can either make this a function or a callback, but this is what you should run to add the 'X' to the input field. I'm just using 'input' as the selector but you can change it to fit your need.

I just wrapped the input with a div, and then put another div inside that div. The z-index of the child div should be higher than the input field, so that it will lay on top.

var input = $('input'),
divWidth = input.width();
  input.wrap('<div class="ex" style="width:'+divWidth+';"></div>').before('<div class="exMark exImage"></div>');

Then I don't want to post the entire css that was in the jsFiddle. Because I used a dataUri so I didn't have to upload an an image, but you can just make the background-image whatever 'X' image you want.

.ex{
  z-index:10000;
width:0px; /* This will get overwritten by the javascript */
}

.exMark{
 width:150px; 
  z-index:1000;
  height:2px;
  position:absolute;
}

.exImage{
  position:absolute;
  width:150px;
  height:50px;
  background-repeat:no-repeat;
  background-image:url('x.jpg');  
}

Hope that helps!

THenry
  • 411
  • 4
  • 5
-1
<hr id="linethru" width="100%" color="black" >
  • That cannot be applied to a text field. Also, the `
    ` element does not support `color` or `width` attributes.
    –  Jan 18 '13 at 04:21
  • Again: This cannot be applied to a text field. It only creates a "standalone" horizontal divider. –  Jan 18 '13 at 04:23
  • this doesnt answer the question. – Mithun Satheesh Jan 18 '13 at 04:34
  • @duskwuff: [It does, actually](http://www.w3.org/TR/html5/rendering.html#the-hr-element-0), although [they're obsolete](http://www.w3.org/TR/html5/obsolete.html#attr-hr-color). [Try it.](http://jsfiddle.net/cCw5P/) – icktoofay Jan 18 '13 at 05:51