我该如何使用类似这样的方法,在CSS中替换文本:
.pvw-title img[src*="IKON.img"] { visibility:hidden; }
我需要使用取代文本的方法,而不是使用 ( img[src*="IKON.img"]
)。
我必须使用 [
]
才能使其有效。
<div class="pvw-title">事实</div>
我需要替换 "事实"。
我该如何使用类似这样的方法,在CSS中替换文本:
.pvw-title img[src*="IKON.img"] { visibility:hidden; }
我需要使用取代文本的方法,而不是使用 ( img[src*="IKON.img"]
)。
我必须使用 [
]
才能使其有效。
<div class="pvw-title">事实</div>
我需要替换 "事实"。
或者你可以以以下方式将“Facts”包装在 <span>
中:
.pvw-title span {
display: none;
}
.pvw-title:after {
content: 'whatever it is you want to add';
}
<div class="pvw-title"><span>Facts</span></div>
必要的:这是一种hack方法:CSS不是正确的应用位置,但在某些情况下 - 例如,在iframe中有一个只能通过CSS定制的第三方库时 - 这种hack方法是唯一的选择。
您可以通过CSS替换文本。 让我们用CSS将带有单词“hello”的绿色按钮替换为带有单词“goodbye”的红色按钮。
之前:
之后:
请参见http://jsfiddle.net/ZBj2m/274/以获取实时演示:
这是我们的绿色按钮:
<button>Hello</button>
button {
background-color: green;
color: black;
padding: 5px;
}
现在让我们隐藏原始元素,但在后面添加另一个块级元素:
button {
visibility: hidden;
}
button:after {
content:'goodbye';
visibility: visible;
display: block;
position: absolute;
background-color: red;
padding: 5px;
top: 2px;
}
注意:
visibility
隐藏原始元素并显示伪元素。请注意,对原始元素使用display: none
无效。display: none;
不起作用是因为 ::after
的实际意思是“在此元素内部的末尾位置”,并非在元素外隐藏。 - Ry-如果你愿意使用伪元素并让它们插入内容,你可以按照以下方式操作。它不需要原始元素的知识,也不需要额外的标记。
.element {
text-indent: -9999px;
line-height: 0; /* Collapse the original line */
}
.element::after {
content: "New text";
text-indent: 0;
display: block;
line-height: initial; /* New content takes up original line height */
}
<th>
元素内部的文本节点。 - Chris Kerekesline-height: 0
和 color: transparent
设置,并在伪元素上重置这些值就可以完成工作(不需要调整 text-indent
)。 - dontGoPlasticline-height: normal
而不是 initial
吗? - BenMorel根据mikemaccana的答复,这对我有用。
button {
position: absolute;
visibility: hidden;
}
button:before {
content: "goodbye";
visibility: visible;
}
<button>original</button>
绝对定位的元素脱离文档流,在放置其他元素时不占据空间。
这很简单,短小精悍,而且有效。不需要额外的HTML。
.pvw-title { color: transparent; }
.pvw-title:after {
content: "New Text To Replace Old";
color: black; /* set color to original text color */
margin-left: -30px;
/* margin-left equals length of text we're replacing */
}
我需要替换链接文本,除了主页之外,用于WooCommerce 面包屑导航
body.woocommerce .woocommerce-breadcrumb > a[href$="/shop/"] {
color: transparent;
&:after {
content: "Store";
color: grey;
margin-left: -30px;
}
}
body.woocommerce .woocommerce-breadcrumb > a[href$="/shop/"] {
color: transparent;
}
body.woocommerce .woocommerce-breadcrumb > a[href$="/shop/"]&:after {
content: "Store";
color: @child-color-grey;
margin-left: -30px;
}
使用以下Hack:
.pvw-title {
font-size: 0;
}
.pvw-title:after {
font-size: 1rem;
content: 'I am a totally different piece of text!';
}
<div class="pvw-title">Facts</div>
将font-size
设置为0可使文本消失,但不会将实际元素从视口中移除。因此,:after
选择器有效,并且应在所有浏览器上显示。
你不能,嗯,你可以。
.pvw-title:after {
content: "Test";
}
这将在元素当前内容之后插入内容,而不是实际替换它,但您可以选择使用空的div,并使用CSS添加所有内容。
但是,虽然你多少可以这样做,但你不应该这样做。实际内容应该放在文档中。content属性主要用于小的标记,如应出现引用的文本周围的引号。
尝试使用:before
和:after
。一个在HTML渲染后插入文本,另一个在HTML渲染前插入文本。如果您想替换文本,请将按钮内容留空。
此示例根据屏幕宽度设置按钮文本。
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
button:before {
content: 'small screen';
}
@media screen and (min-width: 480px) {
button:before {
content: 'big screen';
}
}
</style>
<body>
<button type="button">xxx</button>
<button type="button"></button>
</body>
按钮文本:
使用:before
大屏幕xxx
大屏幕
使用:after
xxx大屏幕
大屏幕
我发现最简单的方法是将元素的 font-size
设为 0 像素,然后在创建 :after
伪元素时覆盖它并设置任何字体大小。以下是示例:
.pvw-title {
font-size:0px;
}
.pvw-title:after {
content: "Hello";
font-size:15px !important;
}
HTML
<p class="replaced">Original Text</p>
CSS.replaced {
visibility: hidden;
position: relative;
}
.replaced:after {
visibility: visible;
position: absolute;
top: 0;
left: 0;
content: "This text replaces the original.";
}