我在主页中有一个关闭lightbox的按钮,它的文本显示为“关闭”,并且右对齐。但是当视口宽度小于400像素时,它可能会与标题文本重叠。
因此,我想使用媒体查询将其替换为“X”。
我尝试了 .button:before,这样可以实现,但我无法通过这种方式摆脱原始的“关闭”文本。
如何只使用CSS来实现此目标?
因此,我想使用媒体查询将其替换为“X”。
我尝试了 .button:before,这样可以实现,但我无法通过这种方式摆脱原始的“关闭”文本。
如何只使用CSS来实现此目标?
使用伪元素:before
设置按钮的值,同时将其默认值设置为"关闭"。下面是一个示例:
HTML
<span class="button" title="Close"></span>
CSS
.button:before {
content: 'Close';
}
@media screen and (max-width: 200px) {
.button:before {
content: 'X';
}
}
演示
<a><span class="mob-view">X</span><span class="normal-view">close</span>
.mob-view {
display:block;
}
.normal-view {
display:none;
}
考虑你的按钮:
<button>Close</button>
button {
visibility: hidden;
}
button:after {
content:'X';
visibility:visible;
}
试试这个 http://jsfiddle.net/ZBj2m/685/
希望对你有用
visibility
。 - insertusernamehere试试这个:
HTML:
<div id="lightBox">
<h1>Heading Goes Here</h1>
<button id="close">Close</button>
</div>
CSS
#lightBox{
width:100%;
height:100%;
background:grey;
position:relative;
}
#close{
position:absolute;
right:5px;
bottom:5px;
border:none;
padding:5px;
height:25px
}
@media screen and (max-width:360px){
#close{
top:-10px !important;
right:-10px !important;
width:25px;
border-radius:25px;
height:25px;
text-indent:-99px;
background-color:red;
}
#close:before{
content:'X';
text-indent: -14px;
color: white;
float: right;
}
}
演示 Fiddle
.button:before
设置Close
,请参考此fiddle。 - insertusernamehereinsertusernamehere
给出了正确的答案! - Alexander Solonik