繁体   English   中英

在伪元素(:after)上不强制换行

[英]Force no line-break on pseudo element (:after)

http://codepen.io/ethanclevenger91/pen/vNjOyP

编辑:此CodePen现在展示了各种建议的解决方案

如果您调整这支笔的可用空间大小,您会看到在标题的顶部,您最终会到达菱形(通过伪元素创建)会断行的位置,但是最后一个单词不会断行,因此伪元素是独立的。

我想使用纯CSS来设计一种解决方案,其行为类似于第二个标题,如果伪元素中断,则最后一个单词也会中断。 目前,我已经通过Javascript实现了它,因为标题的内容将来自数据库。

HTML:

<div class="container">
<h1 class="alpha">A heading that takes up nearly the entire width</h1>
<h1 class="alpha solution1">A solution that would require Javascript</h1>
</div>

SCSS:

$brand-primary:lightblue;
$brand-secondary:darken(lightblue, 40%);
@mixin hidpi($ratio) {
  @media (-webkit-min-device-pixel-ratio: $ratio), (min-resolution: #{round($ratio*96)}dpi) {
      @content;
  }
}
body {
  position:relative;
  height:100vh;
  background:$brand-primary;
  width:100%;
}
.container {
  top:50%;
  transform:translateY(-50%);
  position:relative;
}
.alpha {
  text-align:center;
  color:$brand-secondary;
  &:not(.solution1) {
    &:after {
      content:'';
      @extend %diamond;
      margin-left:11px;
    }
  }
  &.solution1 {
    .diamond {
      @extend %diamond;
      margin-left:11px;
    }
  }
}
%diamond {
  width:13px;
  height:14px;
  display:inline-block;
  transform:rotate(-45deg);
  border:1px solid $brand-secondary;
  vertical-align:middle;
  @include hidpi(2) {
    height:13px;
  }
}

JS:

jQuery(document).ready(function($) {
  var content = $('.solution1').html();
  var lastChar = content.slice(-1);
  $('.solution1').html(content.slice(0, -1)+'<span style="white-space:nowrap">'+lastChar+'<span class="diamond"></span></span>');
});

您可以尝试使用white-space来防止h1换行,并将文本包装在span元素中,以恢复正常行为。

h1 {
  white-space: nowrap;
}
h1 > span {
  white-space: normal;
}

此外,Chrome还需要

h1 > span::after {
  content: '';
  white-space: nowrap;
}

 body { position: relative; height: 100vh; background: lightblue; width: 100%; } .container { top: 50%; -webkit-transform: translateY(-50%); -ms-transform: translateY(-50%); transform: translateY(-50%); position: relative; } .alpha { text-align: center; color: #2e7e99; } .alpha:after { content: ''; width: 13px; height: 14px; display: inline-block; -webkit-transform: rotate(-45deg); -ms-transform: rotate(-45deg); transform: rotate(-45deg); border: 1px solid #2e7e99; vertical-align: middle; margin-left: 11px; } h1 { white-space: nowrap; } h1 > span { white-space: normal; } h1 > span::after { content: ''; white-space: nowrap; } 
 <div class="container"> <div class="row"> <div class="col-xs-12"> <h1 class="alpha"> <span>A heading with a decorative pseudo-element that breaks by itself</span> </h1> </div> </div> </div> 

您也可以使用UTF8中的标准菱形:

如果让:after作为display: inline ,则不会中断,因为您没有在最后一个单词和它之间添加任何空格。

 .alpha.solution3:after { display: inline; content: "◇"; font-weight: normal; font-size: 1.2em; } 
 <h1 class="alpha solution3">A solution that does not require Javascript neither a SPAN</h1> 

如果这个字符对您来说太粗了,您可以制作自己的字体,使用font-face并搭配使用。

http://codepen.io/anon/pen/BoxjzO

这也适用于不支持transform浏览器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM