我试图将pre[data-code]:before
的宽度设为100%,但是
当我输入一行在pre元素中长而且翻到一边的代码时,我看到pre[data-code]:before
被切掉了,看起来不太好。
以下是代码:
pre {
background-color: #233948;
font: bold 12px/15px Inconsolata, Monaco, Consolas, "Andale Mono", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace;
color: #333;
border: 1px solid #f1c40f;
overflow: auto;
word-wrap: normal;
white-space: pre;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
position: relative;
margin: 10px 0;
padding: 0 7px;
}
pre[data-code] {
padding: 3em 1em 7px;
}
pre[data-code]:before {
content: attr(data-code);
position: absolute;
top: 0;
left: 0;
width: 100%;
background-color: #95a5a6;
text-indent: 10px;
font: bold 12px/20px Arial, Sans-Serif;
color: #FFF;
padding: 7px 0;
}
pre[data-code="CSS"] {
color: #7DDECA;
border-color: #16a085;
}
pre[data-code="CSS"]:before {
background-color: #16a085;
}

<pre data-code="CSS">
pre{background-color:#233948;font:bold 12px/15px Inconsolata,Monaco,Consolas,"Andale Mono","Bitstream Vera Sans Mono","Courier New",Courier,monospace;color:#333;border:1px solid #f1c40f;overflow:auto;word-wrap:normal;white-space:pre;box-shadow:0 1px 2px rgba(0,0,0,0.2);position:relative;margin:10px 0;padding:0 7px}
</pre>
&#13;
如何解决这个问题?
答案 0 :(得分:2)
你可以作弊。
根本不要在pre
标签上设置背景颜色,而是使用垂直渐变来模拟该背景。
* {
box-sizing: border-box;
}
pre {
background: linear-gradient(to bottom, #95a5a6, #95a5a6 32px, #233948 32px);
font: bold 12px/15px Inconsolata, Monaco, Consolas, "Andale Mono", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace;
color: #333;
border: 1px solid #f1c40f;
overflow: auto;
word-wrap: normal;
white-space: pre;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
position: relative;
margin: 10px 0;
padding: 0 7px;
}
pre[data-code] {
padding: 3em 1em 7px;
}
pre[data-code]:before {
content: attr(data-code);
position: absolute;
top: 0;
left: 0;
text-indent: 10px;
font: bold 12px/20px Arial, Sans-Serif;
color: #FFF;
padding: 7px 0;
}
pre[data-code="CSS"] {
color: #7DDECA;
border-color: #16a085;
background: linear-gradient(to bottom, #16a085, #16a085 32px, #233948 32px)
}
<pre data-code="CSS">
pre{background-color:#233948;font:bold 12px/15px Inconsolata,Monaco,Consolas,"Andale Mono","Bitstream Vera Sans Mono","Courier New",Courier,monospace;color:#333;border:1px solid #f1c40f;overflow:auto;word-wrap:normal;white-space:pre;box-shadow:0 1px 2px rgba(0,0,0,0.2);position:relative;margin:10px 0;padding:0 7px}
</pre>