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>
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!