简体   繁体   English

强制 h1 保持在 1 行

[英]forcing a h1 to stay in 1 line

I have a div with width and height at 250px.我有一个宽度和高度为 250px 的 div。 the h1 that is inside the div has a 50px height. div 内的 h1 的高度为 50px。 People who add new stuff to my website can fill in whatever they want inside the box.向我的网站添加新内容的人可以在框中填写他们想要的任何内容。

When the text at the h1 is too long the text goes to line 2 (see the picture and the box on the right).当 h1 处的文本太长时,文本会转到第 2 行(参见图片和右侧的框)。

my question is how can I force the h1 to stay within the box like the left box on the picture.我的问题是如何强制 h1 像图片上的左框一样留在框内。

图片

我认为您正在寻找的是 CSS 属性white-space: nowrap;

you cant stay h in one line because parent has only width:250px;您不能将h留在一行中,因为parent只有width:250px; . . So if you assign width:100% to children it takes only width:250px;因此,如果您将width:100%分配给 children 只需要width:250px; only.只要。 Try using white-space:nowrap property if you requried如果您需要,请尝试使用white-space:nowrap属性

Check here在这里查看

If you want the text to fit into the line without having to scroll or expanding the width, your only option is reducing the font-size on the H1.如果您希望文本适合行而不必滚动或扩展宽度,您唯一的选择是减小 H1 上的font-size

To do that automatically, you can use a JS plugin like FITTEXT: http://fittextjs.com/要自动执行此操作,您可以使用像 FITTEXT 这样的 JS 插件: http ://fittextjs.com/

You can use white-space: nowrap to prevent the text from wrapping.您可以使用white-space: nowrap来防止文本换行。 Optionally use overflow and text-overflow to control the behavior.可以选择使用overflowtext-overflow来控制行为。

 h1 { width: 100px; background: salmon; } .nowrap { white-space: nowrap; } .overflow { white-space: nowrap; overflow: hidden; } .text-overflow { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
 <h1 class="nowrap">Using white-space: nowrap</h1> <h1 class="overflow">Using overflow: hidden</h1> <h1 class="text-overflow">Using text-overflow: ellipsis</h1>

This is work around using javascript.这是使用 javascript 的解决方法。 What I did here is taking the height of the using script and from that we get the number of lines of content.我在这里所做的是获取 using 脚本的高度,并从中获得内容的行数。 Based on that value we are adjusting the font size.根据该值,我们正在调整字体大小。

Base font I applied here is 20px and line height is 50px.我在这里应用的基本字体是 20 像素,行高是 50 像素。 if the height we are getting is 100, there will be 2 lines of text and I reduced the font size to 20/2 and with this value, the text can be accommodated in one line.如果我们得到的高度是 100,将有 2 行文本,我将字体大小减小到 20/2,使用这个值,文本可以容纳在一行中。

If you find it helpful, you may fine tune.如果你觉得它有帮助,你可以微调。 Cheers!干杯!

 $(document).ready(function () { var actualHeight = $("h1 span").height(); var fontSize = 20; var fontRatio; if(actualHeight > 50){ fontRatio = actualHeight/50; fontSize = 20/fontRatio; $("h1 span").css('font-size', fontSize); } });
 h1{ width: 300px; height: 50px; line-height: 50px; color: #000; background: #999; font-size: 20px; position: relative; } h1 span{ position: absolute; top: 0; left: 0; width: auto; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h1> <span> orem ipsum dolor sit amet, orem ipsum dolor sit amet, consectetur adipiscing elitorem ipsum dolor sit amet, consectetur adipiscing elit </span> </h1>

元素(空白:nowrap;溢出:隐藏;)

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

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