1

I tried to arrange pre[data-code]:before a width of 100%, but
when I enter a line of code that is long in the pre element and rolled over to the side, I saw the pre[data-code]:before cut off and it does not look good.

Here is the code:

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>

How to solve this problem?

Nathan Arthur
  • 8,287
  • 7
  • 55
  • 80
Dyana Putry
  • 97
  • 1
  • 8

1 Answers1

2

You could cheat.

Don't set a background color on the pre tag at all, use a vertical gradient instead that simulates that background.

* {
  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>
Paulie_D
  • 107,962
  • 13
  • 142
  • 161