dqhdpppm02183 2016-12-30 17:56
浏览 61

用HTML缩小PHP后,CSS字间距属性不起作用

I have something similar to the following in my document:

.batman {
  word-spacing: 100px;
}
.batman > div {
  display: inline-block;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
<div class="batman">
  <div>hello</div>
  <div>world</div>
</div>

Now, I run the html through the following php code to minify it before output:

ob_start(function($html) {return preg_replace('/>\s+</','><',$html);});

...html goes here...

ob_end_flush();

which, returns this to the browser:

.batman {
  word-spacing: 100px;
}
.batman > div {
  display: inline-block;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
<div class="batman"><div>hello</div><div>world</div></div>

So here is the problem:

When the html is on one line, the word-spacing css is gone, or should I say, ignored.

There is no 100px space between my two div's with "hello" and "world".

How can I preserve the word-spacing while also being able to keep code on one line, either actually typing it on one line myself, or running it through a minifier?

Or what change can be done to my simple minify script to make sure word-spacing still works.

Conditions: I do! need the divs to be inline-block.

</div>
  • 写回答

2条回答

  • duanjian4331 2016-12-30 18:56
    关注

    You need to add a single space character after the first div for word-spacing to work in your minified HTML. No need to alter the PHP.

    Just use a CSS pseudo-element with an escaped Unicode sequence for the space.

    .batman {
        word-spacing: 100px;
    }
    .batman > div {
        display:inline-block;
        box-sizing: border-box;
    }
    .batman > div:first-child::after {
      content: "\00a0";
    } 
    <div class="batman"><div>hello</div><div>world</div></div>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?