造型<pre> tag

时间:2017-08-03 07:09:31

标签: html css pre

In my website documentation, I have some code that should be displayed like to suggest the user how they can change the look and feel of the website by following the displayed code. I don't want to use the background image inside this pre tag but want some styling for it. like every line of code should have an alternate background color. I've linked the inspiration image.

demo image

  pre {
  font-size: 20px;
  border: 2px solid grey;
  width: 450px;
  border-left: 12px solid green;
  border-radius: 5px;
  padding: 14px;
}
<pre>
.header-inner {
width: 1200px;
margin: 0 auto;
text-align: center;
font-size: 24px;
font-family: 'lato', sans-serif;
}
</pre>

3 个答案:

答案 0 :(得分:5)

正如@Mr Lister所说,使用渐变。

&#13;
&#13;
.my-pre{
  line-height:1.2em;
  background:linear-gradient(180deg,#ccc 0,#ccc 1.2em,#eee 0);
  background-size:2.4em 2.4em;
  background-origin:content-box;
  
  /* some extra styles*/
  padding:0 20px;
  text-align:justify;
  font-family:calibri,arial,sans-serif;
}
&#13;
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<pre class="my-pre">
  Lorem ipsum dolor sit amet, consectetur adipisicing elit.
  Voluptate ab pariatur assumenda ipsum exercitationem tempore
  architecto, adipisci, id nihil culpa molestias rerum, dolore alias?
  Fugit eos doloribus dolore, expedita officiis.
</pre>
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如上所述,您必须使用线性渐变。由于您实际上已声明了顶部和底部填充(当与行高不同时,将干扰文本相对于渐变的相对偏移),您将必须设置背景图像的y位置:

background-position: 0 14px;

...假设你的top-padding是14px(如你的例子中所述)。

解决方案1:使用linear-gradient

今天现代浏览器中的

linear-gradient is actually very widely supported(> 93%未加前缀,> 94%前缀)。对于linear-gradient,假设开始和结束颜色停止与最近的断点相同,因此您需要声明的是:

  1. 两个断点,颜色A和颜色B都在50%(中间)
  2. 将背景高度设置为2倍线高
  3. 即:

    background-image: linear-gradient(180deg, #eee 50%, #fff 50%);
    background-size: 100% 48px;
    

    pre {
      font-size: 20px;
      border: 2px solid grey;
      width: 450px;
      border-left: 12px solid green;
      border-radius: 5px;
      padding: 14px;
      
      /* Fixed line height */
      line-height: 24px;
      
      /* Use linear-gradient for background image */
      background-image: linear-gradient(180deg, #eee 50%, #fff 50%);
      
      /* Size background so that the height is 2x line-height */
      background-size: 100% 48px;
      
      /* Offset the background along the y-axis by top padding */
      background-position: 0 14px;
    }
    <pre>
    .header-inner {
    width: 1200px;
    margin: 0 auto;
    text-align: center;
    font-size: 24px;
    font-family: 'lato', sans-serif;
    }
    </pre>

    解决方案2:使用repeating-linear-gradient

    使用repeating-linear-gradientlinear-gradient非常相似,但请记住声明所有颜色停止(不假设开始和结束停止):

    1. 颜色A在0px(开始)
    2. 颜色A为24px(1×线高)
    3. 颜色B为24px(1×线高)
    4. 颜色B,48px(2×线高)
    5. 即:

      background-image: repeating-linear-gradient(
          180deg,
          #eee 0px,
          #eee 24px,
          #fff 24px,
          #fff 48px);
      

      以下是一个例子:

      pre {
        font-size: 20px;
        border: 2px solid grey;
        width: 450px;
        border-left: 12px solid green;
        border-radius: 5px;
        padding: 14px;
        
        /* Fixed line height */
        line-height: 24px;
        
        /* Use repeating-linear-gradient for background image */
        background-image: repeating-linear-gradient(
            180deg,
            #eee 0px,
            #eee 24px,
            #fff 24px,
            #fff 48px);
      
        /* Offset the background along the y-axis by top padding */
        background-position: 0 14px;
      }
      <pre>
      .header-inner {
      width: 1200px;
      margin: 0 auto;
      text-align: center;
      font-size: 24px;
      font-family: 'lato', sans-serif;
      }
      </pre>

答案 2 :(得分:0)

如其他人所述,您可以使用linear gradiant

&#13;
&#13;
pre {
  font-size: 20px;
  border: 2px solid grey;
  width: 450px;
  border-left: 12px solid green;
  border-radius: 5px;
  padding: 14px;
  background-color: #2f2f2f;
  background-image: -webkit-repeating-linear-gradient(top, #444 0px, #444 22px, #2f2f2f 22px, #2f2f2f 44px);
  background-image: -moz-repeating-linear-gradient(top, #444 0px, #444 22px, #2f2f2f 22px, #2f2f2f 44px);
  background-image: -ms-repeating-linear-gradient(top, #444 0px, #444 22px, #2f2f2f 22px, #2f2f2f 44px);
  background-image: -o-repeating-linear-gradient(top, #444 0px, #444 22px, #2f2f2f 22px, #2f2f2f 44px);
  background-image: repeating-linear-gradient(top, #444 0px, #444 22px, #2f2f2f 22px, #2f2f2f 44px);
  padding: 0em 1em;
  white-space: pre-wrap;
  word-wrap: break-word;
}
&#13;
<pre>
.header-inner {
width: 1200px;
margin: 0 auto;
text-align: center;
font-size: 24px;
font-family: 'lato', sans-serif;
}
</pre>
&#13;
&#13;
&#13;

简单的方法,

&#13;
&#13;
$("pre").html(function (index, html) {
    return html.replace(/^(.*)$/mg, "<span class=\"line\">$1</span>")
});
&#13;
pre {
  counter-reset: line-numbering;
  background: #2c3e50;
  padding: 12px 0px 14px 0;
  width: 600px;
  color: #ecf0f1;
  line-height: 100%;
}

pre .line::before {
  content: counter(line-numbering);
  counter-increment: line-numbering;
  padding-right: 1em;
  /* space after numbers */
  padding-left: 8px;
  width: 1.5em;
  text-align: right;
  opacity: 0.5;
  color: white;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre>
.header-inner {
width: 1200px;
margin: 0 auto;
text-align: center;
font-size: 24px;
font-family: 'lato', sans-serif;
}
</pre>
&#13;
&#13;
&#13;

使用行号,

&#13;
&#13;
$("pre").html(function(index, html) {
  return html.replace(/^(.*)$/mg, "<span class=\"line\">$1</span>")
});
&#13;
pre {
  counter-reset: line-numbering;
  background: #2c3e50;
  font-size: 20px;
  border: 2px solid grey;
  width: 450px;
  border-left: 12px solid green;
  border-radius: 5px;
  padding: 14px;
  background-color: #2f2f2f;
  background-image: -webkit-repeating-linear-gradient(top, #444 0px, #444 22px, #2f2f2f 22px, #2f2f2f 44px);
  background-image: -moz-repeating-linear-gradient(top, #444 0px, #444 22px, #2f2f2f 22px, #2f2f2f 44px);
  background-image: -ms-repeating-linear-gradient(top, #444 0px, #444 22px, #2f2f2f 22px, #2f2f2f 44px);
  background-image: -o-repeating-linear-gradient(top, #444 0px, #444 22px, #2f2f2f 22px, #2f2f2f 44px);
  background-image: repeating-linear-gradient(top, #444 0px, #444 22px, #2f2f2f 22px, #2f2f2f 44px);
  padding: 0em 1em;
  white-space: pre-wrap;
  word-wrap: break-word;
}

pre .line::before {
  content: counter(line-numbering);
  counter-increment: line-numbering;
  padding-right: 1em;
  /* space after numbers */
  padding-left: 8px;
  width: 1.5em;
  text-align: right;
  opacity: 0.5;
  color: white;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre>
.header-inner {
	width: 1200px;
	margin: 0 auto;
	text-align: center;
	font-size: 24px;
	font-family: 'lato', sans-serif;
}
</pre>
&#13;
&#13;
&#13;