11

How can I put a big cross over an entire (div) element?

I have a page with a person's details on it - name, dob, address, etc. - and if the person is deceased I still want to display the contact details (address, phone number, etc.) but want to put a big cross through it all to show that it shouldn't be used.

I could only really think of using an image, but as the element could be varying in length and width I'm not sure how that would work anyway.

  • An image with `background-size:cover` would be the easiest thing to do – Zach Saucier Feb 22 '14 at 16:11
  • 2
    This question might get better answers on [User Experience](http://ux.stackexchange.com/) as it is asking more about how to convey information to the user of a form, rather than how to technically implement a means of doing so. – Jason Aller Feb 22 '14 at 16:13
  • 2
    @Jason Aller Thanks for the info, but it is about how to technically achieve it, as the decision on how to display it has already been decided. –  Feb 22 '14 at 16:38
  • related http://stackoverflow.com/questions/14593415/how-to-strike-through-obliquely-with-css – Ciro Santilli新疆棉花TRUMP BAN BAD Jul 05 '16 at 20:43

5 Answers5

21

You can use CSS and SVG inside that CSS:

  background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' version='1.1' preserveAspectRatio='none' viewBox='0 0 100 100'><path d='M100 0 L0 100 ' stroke='black' stroke-width='1'/><path d='M0 0 L100 100 ' stroke='black' stroke-width='1'/></svg>");
  background-repeat:no-repeat;
  background-position:center center;
  background-size: 100% 100%, auto;

Demo: http://jsfiddle.net/02ffvyeo/

All this, including the demo, is based on https://stackoverflow.com/a/20231370/694469. That answer draws a colored triangle, this question was about a cross

Community
  • 1
  • 1
KajMagnus
  • 10,177
  • 14
  • 69
  • 115
11

You could use :before and :after in combination with transforms to put an X over the entire div: DEMO

.container {
    position: relative;
    background: gray;
    padding: 30px;
    overflow: hidden; //hide overflow of pseudo elements
}

.container:before, .container:after {
    position: absolute;
    content: '';
    background: red;
    display: block;
    width: 100%;
    height: 30px;
    -webkit-transform: rotate(-45deg);
    transform: rotate(-45deg);
    //center the X vertically and horizontally:
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    margin: auto;
}

.container:after {
    -webkit-transform: rotate(45deg);    
    transform: rotate(45deg);
}
brouxhaha
  • 3,693
  • 1
  • 17
  • 22
  • I had to change `-webkit-transform` to `transform`, but it worked! Thanks. –  Feb 22 '14 at 18:34
  • Go ahead and leave `-webkit-transform` in there above the `transform` property so webkit browsers render it. I've edited my answer for this. – brouxhaha Feb 22 '14 at 18:38
  • classy answer. would be great if you explain transform property a bit more – Nassim Nov 02 '16 at 10:16
1

Add an absolute positioned div (100% height/width) containing a font with em size, fill it with an "X"

<div class="deceased">X</div>

Will expand properly (and save an image callback as well :) )

If you don't like the X to be clipped by setting its size very big you will need javascript to resize it when div size changes.

Even better, you can add it with :after pseudo class and save both the extra div and the "X"

.deceased:after
{ 
    content:"X";
    position:absolute;
    left:0px;
    top:0px;
    width:100%;
    height:100%;
    font-size: 1000px;      // adjust this a little though it will be clipped by the div
    text-align: center;
    line-height: 100%;
    overflow: hidden;
}

Or :before .. will put it "underneath" the existing content

.deceased:before
{ 
    content:"X";
    position:absolute;
    left:0px;
    top:0px;
    width:100%;
    height:100%;
    font-size: 1000px;      // adjust this a little though it will be clipped by the div
    text-align: center;
    line-height: 100%;
    overflow: hidden;
}

UPDATE

I did find a way to use CSS only ...

@media all and (min-width: 50px)  {  .deceased:before  {   font-size:0.1em;  } }
@media all and (min-width: 100px) {  .deceased:before  {   font-size:0.2em;  } }
@media all and (min-width: 200px) {  .deceased:before  {   font-size:0.4em;  } }
@media all and (min-width: 300px) {  .deceased:before  {   font-size:0.6em;  } }
@media all and (min-width: 400px) {  .deceased:before  {   font-size:0.8em;  } }
and so on ....

UPDATE 2

And an inline svg containing a text will scale the text to the size of the div...

.deceased:before
{ 
    content: url('data:image/svg+xml;utf8,<svg>....</svg>');
    position:absolute;
    left:0px;
    top:0px;
    width:100%;
    height:100%;
    overflow: hidden;
}
Ason
  • 79,264
  • 11
  • 79
  • 127
  • I can't get this to work. The first example just displays an `X` and the second must be erroring as the entire page is disabled (you can't click or select anything). –  Feb 22 '14 at 16:45
  • use :before instead .. then the X comes under everything – Ason Feb 22 '14 at 16:49
  • Still doesn't work - it's because the `position:absolute; height:100%; width:100%;` makes the element the full size of the page, not the element; plus doesn't actually change the size of the x anyway. –  Feb 22 '14 at 16:57
  • You need to set the class on your content div where you have your info and the font needs to be set to full size and center the text .. I updated my answer with a sample – Ason Feb 22 '14 at 17:06
  • If it doesn't work, pass the page or a add sample of the html/css – Ason Feb 22 '14 at 17:10
  • Sorry, it's been a while since I've done much with css and overlooked I needed the outer div to be `position: relative` for the `position:absolute` not to affect the whole page. This works but the only problem now is the X doesn't stretch/shrink to fit. So I might need to use javascript to find out the container's dimensions? –  Feb 22 '14 at 17:27
  • If you don't like the X to be clipped by setting its size very big you will need javascript to resize it when div size changes – Ason Feb 22 '14 at 17:30
  • You've been helpful and I've marked the answer as useful, but it doesn't really provide a proper answer to the question as it requires javascript to make it work properly, and ideally I want to avoid this. –  Feb 22 '14 at 17:55
  • I updated my answer ... and by the way, you never wrote that it needed to be a CSS-only solution, but now you have one so I am looking forward to your accept – Ason Feb 22 '14 at 17:59
  • I didn't know you could do conditions like that in css! Where is it meant to go? I just put it at the end of my css file but when I try it seems to be based on screen/page size not the element. –  Feb 22 '14 at 18:17
  • Yes, it based on that ... it calls media queries ... but you know, I did my best to be at help and know there are no other ways, and if that is not enough for you so be it – Ason Feb 22 '14 at 18:29
  • You have helped and taught me somehting new. Thanks. –  Feb 22 '14 at 18:50
0

you could use a class on your box element. Then, through that class, add your cross as a background or a pseudo-element over it , using background-size or width/height for pseudo-element and absolute positionning over it.

Anyhow, the use of a class will help you to make a specific style to that box, it could even be display:none or opacity:0.5; pointer-events:none; , or whatever you thinks is right and coherent with meaning and website design.

G-Cyrillus
  • 85,910
  • 13
  • 85
  • 110
-1

CSS isn't really designed for this sort of thing - the purpose of a div element is to divide the html into sections so you can apply formatting (CSS) to elements within the div tags. What you could try is formatting the text to have a line through it.

<p style="text-decoration: line-through">Name</p>

Its still readable but obviously defunct.

user1205406
  • 573
  • 1
  • 5
  • 15
  • It has to be a cross through the entire section. If it can't be done with css alone I will have to look at javascript. –  Feb 22 '14 at 16:31
  • document.getElementById("foo").style.textDecoration="line-through"; foo could be a div if you want to effect a large area of text – user1205406 Feb 22 '14 at 16:34
  • 1
    That just puts a line through all the text. I need it to put a cross over the entire section - not the individual characters. –  Feb 22 '14 at 16:42
  • 1
    This is invalid HTML - for inline styles you should be using quotes where you have used curly braces. – pwdst Feb 22 '14 at 17:20
  • oops - wasn't paying full attention when i posted that - serves to remind that you should always test code – user1205406 Feb 22 '14 at 18:57