文本换行和超出一行显示

The text-overflow property specifies how overflowed content that is not displayed should be signaled to the user. It can be clipped, display an ellipsis (...), or display a custom string.

Both of the following properties are required for text-overflow:

  • white-space: nowrap;
  • overflow: hidden;

text-overflow属性可以控制一行文字超过框以后超过的部分的显示形式。

<!DOCTYPE html>
<html>
<head>
<style> 
div.a {
    white-space: nowrap; 
    width: 50px; 
    overflow: hidden;
    text-overflow: clip; 
    border: 1px solid #000000;
}

div.b {
    white-space: nowrap; 
    width: 50px; 
    overflow: hidden;
    text-overflow: ellipsis; 
    border: 1px solid #000000;
}

div.c {
    white-space: nowrap; 
    width: 50px; 
    overflow: hidden;
    text-overflow: "----"; 
    border: 1px solid #000000;
}
</style>
</head>
<body>
<h1>The text-overflow Property</h1>

<p>The following two divs contains a text that will not fit in the box.</p>

<h2>text-overflow: clip (default):</h2>
<div class="a">Hello world!</div>

<h2>text-overflow: ellipsis:</h2>
<div class="b">Hello world!</div>

<h2>text-overflow: "----" (user defined string):</h2>
<div class="c">Hello world!</div>

<p><strong>Note:</strong> The text-overflow: "<em>string</em>" only works in 
Firefox.</p>

</body>
</html>

效果如下:

 而且

  • white-space: nowrap;
  • overflow: hidden;

而且除了text-overflow以外,这两个属性也必须同时写上。

更多内容,请参考:CSS text-overflow Property

猜你喜欢

转载自blog.csdn.net/Abudula__/article/details/81637409
今日推荐