【问题标题】:Underline only final line of text when centered居中时仅在文本的最后一行下划线
【发布时间】:2017-08-07 11:53:31
【问题描述】:

我只想在某些文本的最后一行加下划线。当文本换行到更多行时,仍然只有最后一行需要下划线。

我发现了这个Solutions。但是当文本居中时,这不起作用。因为当文本被换行时,该行一直延伸到最后一行的左侧。

p{
  position: relative;
  display: inline
}
p:after {
  position: absolute;
  left: 0;
  bottom: -15px;
  width: 100%;
  height: 1px;
  border-bottom: 10px solid #000;
  content: ""
}
<div style="text-align:center;">
  <p>Een lijn onder alleen de laatste regel, werkt ook op mobiel als de tekst over meerdere regels valt</p>
</div>

Jsfiddle

有人有想法吗?

谢谢!

【问题讨论】:

  • 我尝试使用一些代码但没有成功。您是否对底部宽度减小的边框感到满意,该边框与最后一行的宽度无关?

标签: html css layout


【解决方案1】:

我猜这就是 OP 想要的:

.underlined {
  position: relative;
}

.text {
  display: inline-block;
  text-align: center;
}

.line {
  color: transparent;
  display: inline;
  position: relative;
  left: 50%;
}

.line:after {
  content: "";
  display: block;
  width: 100%;
  height: 1px;
  border-bottom: 10px solid black;
  position: absolute;
  left: -50%;
  top: 0;
}
<p class="underlined">

  <span class="text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui sed ratione voluptatum ducimus unde velit debitis asperiores expedita, a deleniti repellat quis officia. Voluptate, earum rerum itaque, iste eligendi velit!</span>

  <span class="line">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui sed ratione voluptatum ducimus unde velit debitis asperiores expedita, a deleniti repellat quis officia. Voluptate, earum rerum itaque, iste eligendi velit!</span>

</p>

我不喜欢这个解决方案,因为它需要复制内容,但也许有人有改进它的想法......

JSFiddle


编辑:添加我的结果截图:

在 Firefox 50.0 中不起作用

【讨论】:

  • 那也是全宽的。与只有边框没有什么不同。
  • 刚刚检查过。 Chrome 没问题
  • 谢谢,Jordi,但就像 R Reveley 所说,它在 Firefox 中不起作用
【解决方案2】:

我回答了类似的问题。
它不能在纯 css 中完成。 我已经使用 javascript 创建了小提琴。
http://jsfiddle.net/VHdyf/89/

Javascript 部分

var parentEl = document.getElementsByClassName('customBtn');

for(var i=0;i<parentEl.length;i++){
var currentEl = parentEl[i];
var button = currentEl.childNodes[1];
var words = button.innerText.split(/[\s]+/); // An array of allthe words split by spaces, since that's where text breaks by default. var
var lastLine = []; // Putall words that don't change the height here.
var currentHeight = currentEl.clientHeight; // The starting height.
while(1){
  lastLine.push(words.pop());
  button.innerText = words.join(' ');
  if (currentEl.clientHeight < currentHeight) {
    var span = document.createElement('span');
    span.classList=['underline'];
    span.innerText = ' '+lastLine.reverse().join(' ');
    button.appendChild(span);
    break;
  }
  currentHeight = parentEl[i].clientHeight;
  if(!words.length){
    break;
  }
}

}

【讨论】:

    【解决方案3】:

    试试下面的 CSS

    div > p:last-child:after {
        position: absolute;
        left: 0;
        bottom: -15px;
        width: 100%;
        height: 1px;
        border-bottom: 10px solid #000;
        content: ""
    }
    

    这里更新JSfiddle

    【讨论】:

    • 谢谢,但它不起作用。这条线延伸到 div 的最左边,即使底线比上面的线短。
    • 检查上面的 JSfiddle 作为答案,它在那里工作
    • 感谢您的快速回复,但无法正常工作。该行比文本的最后一个单词长。它必须只在最后的话
    • 您能否解释更多或更新您的 JS Fiddle 但您遇到的确切问题?
    • 我已更新:jsfiddle.net/VHdyf/69 当您缩小屏幕时,您会看到文本被换行,并且该行从 div 的最左侧开始,而不仅仅是在最后一个单词下方。跨度>
    猜你喜欢
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 2014-10-31
    • 2013-08-22
    • 1970-01-01
    相关资源
    最近更新 更多