简体   繁体   English

如何删除空div

[英]How to strikethrough empty div

I was making a light saber in CSS. 我在用CSS制作军刀。 The code is 该代码是

 div.handle { height:10px; width:100px; background-color:black; display: inline-block; border-radius: 0 4px 4px 0; border-spacing: 0; border-collapse: collapse; } div.sword { height:6px; width:400px; background-color:white; display: inline-block; border-radius: 3px; border-spacing: 0; border-collapse: collapse; margin-top: 1px; margin-bottom: 1px; text-decoration: line-through; } div.red { box-shadow: 0 0 10px red; border:1px solid red; } 
 <div class = "handle"></div><div class = "sword red strike"></div> 

I want to strikethrough the light saber completely. 我想彻底删除轻剑。

The text-decoration: line-through; text-decoration: line-through; property holds good only for text inside the div and not for the div only. 该属性仅对div中的文本有效,而对div无效。

I saw Linethrough/strikethrough a whole HTML table row And tried it. 我看到在整个HTML表格行中都有Linethrough /删除线并进行了尝试。 But it doesn't work properly. 但是它不能正常工作。 So my question is "How do I strikethrough an empty div" using CSS. 所以我的问题是使用CSS“如何删除空的div”。

You can do something similar using border-bottom property as in the linked question . 您可以使用border-bottom属性执行类似的操作,如链接的问题所示 Like so: 像这样:

div.strike {
  position: relative;
}
div.strike:before {
  content: " ";
  position: absolute;
  top: 50%;
  left: 0;
  border-bottom: 1px solid #111;
  width: 100%;
}

You can change the color just by changing the value of the border-bottom color value. 您可以仅通过更改border-bottom颜色值的值来更改颜色。 Here is the Jsfiddle for it. 这是Jsfiddle

I suppose that you want a light saber like in this image here below with CSS only ? 我想您是否只想像下面这张图片中的军刀一样使用CSS?

在此处输入图片说明

I was thinking to have an approach without using a strike-through. 我当时在想一种不使用删除线的方法。 Then i realized that you could use the linear-gradient instead. 然后我意识到您可以改用linear-gradient You can improve the aligning/color of it of course. 您当然可以改善其对齐方式/颜色。

I have added the following two lines in the code example here below 我在下面的代码示例中添加了以下两行

/* added these 2 lines */
background: #FF0000;
background: linear-gradient(to bottom,  #ff0000 0%,#ffdddd 50%,#ff0000 99%);

This allows you to have that red → white → red effect without the use of a strike-through. 这使您可以在不使用删除线的情况下获得红色→白色→红色的效果。 As result of this, you get a nice red saber. 结果,您得到了一个不错的红色军刀。

 div.handle { height:10px; width:100px; background-color:black; display: inline-block; border-radius: 0 4px 4px 0; border-spacing: 0; border-collapse: collapse; } div.sword { height:6px; width:400px; /* added these 2 lines */ background: #FF0000; background: linear-gradient(to bottom, #ff0000 0%,#ffdddd 50%,#ff0000 99%); display: inline-block; border-radius: 3px; border-spacing: 0; border-collapse: collapse; margin-top: 1px; margin-bottom: 1px; text-decoration: line-through; } div.red { box-shadow: 0 0 10px red; border:1px solid red; } 
 <div class = "handle"></div><div class = "sword red strike"></div> 

Add a :after css element to the div.sword , that will be the line you want. div.sword添加:after css元素,这将是您想要的行。 text-decoration is only meant to decorate... well... text :-) text-decoration仅用于装饰...好吧...文本:-)

 div.handle { height:10px; width:100px; background-color:black; display: inline-block; border-radius: 0 4px 4px 0; border-spacing: 0; border-collapse: collapse; } div.sword { height:6px; width:400px; background-color:white; display: inline-block; border-radius: 3px; border-spacing: 0; border-collapse: collapse; margin-top: 1px; margin-bottom: 1px; text-decoration: line-through; position: relative; } div.sword:after { content: ""; position: absolute; width: 100%; height: 1px; top: 40%; background-color: black; } div.red { box-shadow: 0 0 10px red; border:1px solid red; } 
 <div class = "handle"></div><div class = "sword red strike"></div> 

Another posibility is to add a background with a gradient into it 另一个可能性是在其中添加带有渐变的背景

You can also animate it 您也可以为其设置动画

background: linear-gradient(white 2px, black 2px, black 4px, white 4px);

 div.handle { height:10px; width:100px; background-color:black; display: inline-block; border-radius: 0 4px 4px 0; border-spacing: 0; border-collapse: collapse; } div.sword { height:6px; width:400px; display: inline-block; border-radius: 3px; border-spacing: 0; border-collapse: collapse; margin-top: 1px; margin-bottom: 1px; text-decoration: line-through; background: linear-gradient(to right, red 25%, white 50%, grey 75%, black 100%); background-size: 400% 2px; background-repeat: no-repeat; background-position: 0% 2px; animation: pulse 300ms infinite; } @keyframes pulse { from {background-position: 0% 2px;} to {background-position: 100% 2px;} } div.red { box-shadow: 0 0 10px red; border:1px solid red; } 
 <div class = "handle"></div><div class = "sword red strike"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM