【问题标题】:Add a border only to the bottom of wrapped text仅在换行文本的底部添加边框
【发布时间】:2013-12-28 07:26:57
【问题描述】:

我正在尝试在一些适合底部文本行宽度的包装文本上添加下划线,同时仅出现在该底部线下方。图 1 说明了预期的效果

图一

使用此 HTML:

<h2><span class="inline-block">optatur, volendit inum simolor</span></h2>

并将span 设置为display:inline; 我可以让下划线与文本的宽度完美匹配,但它会为所有文本添加下划线。

或者,将span 设置为display:inline-block; 我可以让下划线仅出现在底线下方,但它会填充父级的整个宽度。

上述示例请参见此 JSfiddle:http://jsfiddle.net/PWDV7/1/

有什么办法可以达到图1的效果吗?

【问题讨论】:

  • @Pranavc u's 不应该与 html 4 一起使用,如果你正在注释某些东西,只能与 html5 一起使用,否则你应该使用 css 来下划线
  • 真的很有趣的问题。
  • 这太疯狂了。我可以只强调最后一行的长度,但不能在它居中时。我什至考虑过使用 JavaScript。
  • 嗨,乔丹!很高兴你能在头疼的情况下上船!如果它需要它,我很乐意提供 JavaScript 解决方案...
  • 我最终设法用一些 JS 解决了它! (见下文。)

标签: border inline css underline


【解决方案1】:

this answer 很好地解决了关于查找换行的问题,我设法提出了这个解决方案(简而言之,它涉及使用 JS 来查找发生换行的位置并围绕所有位于最后一行的文本):

function underLineText () {
    $underlined_text.each(function(){
        var $this = $(this);
        var text = $this.text();
        var originalText = text;
        var breakWords = new Array();

        //split the words into individual strings 
        var words = text.split(' ');

        //find the height of the first word
        $this.text(words[0]);
        var height = $this.height();

        //if there is more than one word
        if (words.length > 1) {
            //loop through all the words
            for(var i = 1; i < words.length; i++){
                $this.text($this.text() + ' ' + words[i]);

                //check if the current word has a different height from the previous word
                //if this is true, we have witnessed a word wrap!
                if($this.height() > height){
                    height = $this.height();
                    //add the first word after the wrap to a predefined array
                    breakWords.push(words[i]);
                }

                //on the last iteration on the loop...
                if (i === words.length - 1) {
                    if (breakWords.length > 0) {
                        //select the last word of the breakWords array
                        //(this is to handle cases where there there are more than one line or word wraps)
                        var breakStartAt = breakWords[breakWords.length - 1];
                        //add a span before the last word
                        var withSpan = '<span>'+breakStartAt;
                        //replace the last occurrence of this word with the span wrapped version
                        //(this is to handle cases where there are more than one occurrences of the last word)
                        originalText = originalText.replaceLast(breakStartAt,withSpan);
                        //close the last line with a span
                        originalText += "</span>";

                    }
                }
            }
        }
        //if there are no word wraps, wrap the whole text in spans
        else {
            originalText = '<span>'+originalText+'</span>';
        }

        //replace the original text with the span-wrapped mod
        $(this).html(originalText);
    });
}

你可以在这里看到它的工作原理:http://jsfiddle.net/PWDV7/5/

【讨论】:

  • 完全忘记了这个问题!这几乎也是我的想法所在。我得到的最接近的是在定位的after 伪元素上使用边框,这仅在文本左对齐时才有效。干得好!
【解决方案2】:

像这样更改代码:

HTML

<h2>optatur, volendit <span>inum simolor</span></h2>

CSS

h2 {
    width:200px;
    text-align:center;
}
h2 span {
    border-bottom:3px solid black;
    width:100%;
}

我改变的只是&lt;span&gt; 的位置,以换行您想要边框的文本。

JsFiddle

【讨论】:

  • 我猜 OP 不想在 html 中具体说明文本的哪一部分要加下划线
  • 你可以用border-bottom代替text-decoration:underline;
  • 这取决于你希望边框有多厚。
  • 感谢您的帮助 Jamie,但是是的,Danko 是对的;文本将由使用 CMS 的人创建,我不能指望他们将跨度放在正确的位置...
猜你喜欢
  • 2014-10-18
  • 1970-01-01
  • 2014-07-29
  • 1970-01-01
  • 2014-06-06
  • 2011-09-15
  • 1970-01-01
  • 2017-03-29
  • 2012-04-19
相关资源
最近更新 更多