首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何做一个居中的标题风格,其中划线安排最长的线在底部。

如何做一个居中的标题风格,其中划线安排最长的线在底部。
EN

Stack Overflow用户
提问于 2022-04-03 13:48:33
回答 3查看 226关注 0票数 9

我有一个项目,其中一些网页有一个大字体大小的标题。标题是动态的,可以是一行或3-4行.出于美学上的原因,我希望标题既要居中,又要排列线条,这样最长(最宽)的线才能到达底部,最短的线才能到达顶部。就像金字塔的形状。

我不知道这在纯CSS中是否可行,或者我应该如何解决这个问题?

有什么建议吗?-)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-04-06 19:57:48

我并不是JQuery-惊奇,但我已经提出了这个解决方案,满足了我对我所要求的对齐的期望。如果你的眼睛伤害了这个片段,请给出一些优化建议。我已经知道,在调整灵活元素的大小后,这是行不通的。

当然,我更喜欢使用纯CSS制作的解决方案,因此如果您要列出W3C,则现在正式请求text-align: pyramid;,甚至是text-align: simon; (wink)。

代码语言:javascript
运行
复制
$('#title').each(function(){

    var words = $(this).text().split(/\s+/);
    var total = words.length;
    var originalWidth = $(this).css("width");
    var maxWidth = $(this).width();
    var tempText = $(this);
    var string = "";
    var currentLineString = "";

    $(this).empty();
    tempText.css("width", "auto");

    for (i = 0; i < total; i++){

        var newString = words[total-i-1] + " " + currentLineString;
        tempText.text(newString);

        if (tempText.width() >= maxWidth) {
            tempText.text(currentLineString);
            maxWidth = tempText.width();
            string = currentLineString + "<br>" + string;
            currentLineString = "";
            i--;
        } else {
            currentLineString = newString;
        }
    }
    string = currentLineString + "<br>" + string;
                
    tempText.css("width", originalWidth);
    $(this).html(string);
});
票数 0
EN

Stack Overflow用户

发布于 2022-04-03 14:26:23

外型怎么样?我们可以用两个三角形来限制文本的边界,一个在左边,另一个在右边。

注意:尽管以文本为中心的边界不产生期望的结果,但相反,证明文本的合理性也不理想--最后一行没有居中

代码语言:javascript
运行
复制
div{
  text-align:justify;
  font-size:20px;
  padding:15px;
  background:rgba(200,200,100,0.5);
}

left-shape {
  shape-outside: polygon(0 0, 100% 0, 0 50%);
  width: 250px;
  height: 500px;
  float:left;
}

right-shape {
  shape-outside: polygon(100% 0, 0% 0, 100% 50%);
  width: 250px;
  height: 500px;
  float:right;

}
代码语言:javascript
运行
复制
<left-shape></left-shape> <right-shape></right-shape>

<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>

票数 4
EN

Stack Overflow用户

发布于 2022-04-05 21:06:15

可能的javascript解决方案

如果您找不到CSS解决方案,这里有一个使用javascript的“某种解决方案”。这可能不是最好的方法,如果屏幕宽度减少,可能会出现问题,因为我们在最后使用断点拆分了两个字符串。因此,如果标题有足够的间距,此解决方案可能适用于您,但您可能需要考虑在此解决方案中使用whitespace: nowrap;

试着改变偏移量,为自己找到最好的结果。

代码语言:javascript
运行
复制
// Get title element by ID:
var title = document.getElementById('title');

// Get content of the title:
var content = title.innerText;

// Where you want the text to be splitted from,
// 2 = middle, 3 = one third;
var offset = 2.5;

// Divide content by previously set amount:
var divided = Math.floor(content.length / offset);

// This gives the number of characters
// in the top part of the title
var count = content.indexOf(' ', divided);

// First part of the string,
// goes from start to "count" amount
var x = content.substring(0, count);

// Second part of the string, after count amount:
var y = content.substring(count);

// Place text back to the title,
// Using <br> might cause issues on mobile,
// but it's a rough idea what could be done.
title.innerHTML = x + "<br>" + y;
代码语言:javascript
运行
复制
#title{
  text-align: center;
}
代码语言:javascript
运行
复制
<h1 id="title">This is an example of the line break I want</h1>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71726511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档