简体   繁体   English

缩放文字以适合背景图像

[英]Scale text to fit in a background image

I am trying to get longer wordpress post titles to automatically scale down their size to fit into width of the backround image (which is fixed at 986px). 我试图获得更长的wordpress帖子标题,以自动缩小其大小以适合背景图片的宽度(固定为986px)。 Currently, when a title is too long, the last words will get cut off towards the end of the sentence. 当前,当标题过长时,最后的单词将在句子结尾处被截断。 Please see the below site for example: http://www.nursingassistantdegree.com/earning-a-certified-nursing-assistant-degree-can-help-break-the-single-mom-poverty-cycle/ 请访问以下网站,例如: http : //www.nursingassistantdegree.com/earning-a-certified-nursing-assistant-degree-can-help-break-the-single-mom-poverty-cycle/

The CSS code is below. CSS代码如下。

.networkNews_bg{ background:url(images/network_news_bg.png) no-repeat 0 0; width:986px; height:82px;}
.networkNews_bg h2{ font:bold 35px/82px "Myriad Pro"; color:#2a2c2e; padding:0 20px;}

I have seen similar topics, but none that are specifically related to auto-sizing in a CSS. 我见过类似的主题,但没有一个主题与CSS中的自动调整大小特别相关。 How can I get the text to auto-scale down to fit within the 986px? 如何获得自动缩小的文本以适合986px?

Thank you in advance. 先感谢您。

You should modify your css for the background to be 您应该修改CSS以使背景为

background: url(images/network_news_bg.png) bottom no-repeat;

and then edit your image to be much taller. 然后将图片编辑得更高。 repeating the top upward if that makes sense. 如果可以的话,向上重复顶部。 Then remove the height restriction and style accordingly with line height. 然后删除高度限制并相应地设置线条高度的样式。

You can't autoscale text to fit within a specified width. 您无法自动缩放文本以使其适合指定的宽度。 You can rescale text using "em," "rem" or other relative text scaling unit but all of this changes the text height, not its width. 您可以使用“ em”,“ rem”或其他相对文本缩放单位来重新缩放文本,但是所有这些都会改变文本的高度,而不是其宽度。 Instead of scaling, how about adding an automatic ellipsis to your title? 除了缩放之外,如何在标题中添加自动省略号?

.ellipsis {
  text-overflow: ellipsis;
  overflow: hidden;
  width: 986px;
  height: 20px;
  line-height: 20px;
}

If a title heading expands past 986px, it will show "..." This is a very simple solution. 如果标题扩展到986px以上,它将显示“ ...”。这是一个非常简单的解决方案。

Decreasing the font-size is also recommended if this is a common issue. 如果这是一个常见问题,也建议减小字体大小。

EDIT: 编辑:

Here is the snapshot from FF. 这是FF的快照。 This is what I added. 这就是我添加的内容。 I did not change the font properties. 我没有更改字体属性。 But you can decrease it if you want. 但是您可以根据需要减少它。 You need 3 new properties: text-overflow, overflow, white-space . 您需要3个新属性: text-overflow, overflow, white-space The white-space is especially important. 空格特别重要。

Snapshot: 快照:

屏幕截图

Update 2: 更新2:

networkNews_bg h2 {
        text-overflow: ellipsis;
        overflow: hidden;
        wite-space: nowrap;
        color: #2A2C2E;
        font: 35px/82px "Myriad Pro";
        padding: 0 20px;
}

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

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