我有一个项目,其中一些网页有一个大字体大小的标题。标题是动态的,可以是一行或3-4行.出于美学上的原因,我希望标题既要居中,又要排列线条,这样最长(最宽)的线才能到达底部,最短的线才能到达顶部。就像金字塔的形状。
我不知道这在纯CSS中是否可行,或者我应该如何解决这个问题?
有什么建议吗?-)

发布于 2022-04-06 19:57:48
我并不是JQuery-惊奇,但我已经提出了这个解决方案,满足了我对我所要求的对齐的期望。如果你的眼睛伤害了这个片段,请给出一些优化建议。我已经知道,在调整灵活元素的大小后,这是行不通的。
当然,我更喜欢使用纯CSS制作的解决方案,因此如果您要列出W3C,则现在正式请求text-align: pyramid;,甚至是text-align: simon; (wink)。
$('#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);
});发布于 2022-04-03 14:26:23
外型怎么样?我们可以用两个三角形来限制文本的边界,一个在左边,另一个在右边。
注意:尽管以文本为中心的边界不产生期望的结果,但相反,证明文本的合理性也不理想--最后一行没有居中
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;
}<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>
发布于 2022-04-05 21:06:15
可能的javascript解决方案
如果您找不到CSS解决方案,这里有一个使用javascript的“某种解决方案”。这可能不是最好的方法,如果屏幕宽度减少,可能会出现问题,因为我们在最后使用断点拆分了两个字符串。因此,如果标题有足够的间距,此解决方案可能适用于您,但您可能需要考虑在此解决方案中使用whitespace: nowrap;。
试着改变偏移量,为自己找到最好的结果。
// 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;#title{
text-align: center;
}<h1 id="title">This is an example of the line break I want</h1>
https://stackoverflow.com/questions/71726511
复制相似问题