특정 너비의 div 내에서 두 줄로만 텍스트를 감싸고 싶습니다. 텍스트가 두 줄의 길이를 초과하면 생략 표시를 표시하고 싶습니다. CSS를 사용하는 방법이 있습니까?
예 :
Sample text showing wrapping
of text in only two line...
감사
답변
당신은 설정하면 텍스트의 두 줄 출력을 제한하면, CSS 가능합니다 line-height
및 height
요소의, 그리고 세트 overflow:hidden;
:
#someDiv {
line-height: 1.5em;
height: 3em; /* height is 2x line-height, so two lines will display */
overflow: hidden; /* prevents extra lines from being visible */
}
또는 CSS text-overflow
및 white-space
속성을 사용하여 타원을 추가 할 수 있지만 이는 한 줄에서만 작동하는 것처럼 보입니다.
#someDiv {
line-height: 1.5em;
height: 3em;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
width: 100%;
}
그리고 데모 :
여러 줄의 텍스트와 줄임표를 모두 달성하는 것이 자바 스크립트의 영역 인 것처럼 보입니다.
답변
또 다른 간단하고 빠른 솔루션
.giveMeEllipsis {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: N; /* number of lines to show */
line-height: X; /* fallback */
max-height: X*N; /* fallback */
}
원래 질문과 답변에 대한 참조는 여기에 있습니다.
답변
내가 본 것 중 가장 좋은 것은 CSS 전용이고 반응 형인 Mobify 개발자 블로그-CSS Ellipsis : How to Manage Multi-Line Ellipsis in Pure CSS :
CSS :
html, body, p { margin: 0; padding: 0; font-family: sans-serif;}
.ellipsis {
overflow: hidden;
height: 200px;
line-height: 25px;
margin: 20px;
border: 5px solid #AAA; }
.ellipsis:before {
content:"";
float: left;
width: 5px; height: 200px; }
.ellipsis > *:first-child {
float: right;
width: 100%;
margin-left: -5px; }
.ellipsis:after {
content: "\02026";
box-sizing: content-box;
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
float: right; position: relative;
top: -25px; left: 100%;
width: 3em; margin-left: -3em;
padding-right: 5px;
text-align: right;
background: -webkit-gradient(linear, left top, right top,
from(rgba(255, 255, 255, 0)), to(white), color-stop(50%, white));
background: -moz-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
background: -o-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
background: -ms-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
background: linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white); }
HTML :
<div class="ellipsis">
<div class="blah">
<p>Call me Ishmael. Some years ago – never mind how long precisely – having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people's hats off – then, I account it high time to get to sea as soon as I can.</p>
</div>
</div>
답변
CSS 전용 솔루션 text-overflow: ellipsis
은 한 줄에만 적용되므로이 경로를 사용할 수 없습니다.
.yourdiv {
line-height: 1.5em; /* Sets line height to 1.5 times text size */
height: 3em; /* Sets the div height to 2x line-height (3 times text size) */
width: 100%; /* Use whatever width you want */
white-space: normal; /* Wrap lines of text */
overflow: hidden; /* Hide text that goes beyond the boundaries of the div */
text-overflow: ellipsis; /* Ellipses (cross-browser) */
-o-text-overflow: ellipsis; /* Ellipses (cross-browser) */
}
jQuery에 http://tpgblog.com/threedots/ 를 사용해 보셨습니까 ?
답변
Webkit 용 CSS 전용 솔루션
// Only for DEMO
$(function() {
$('#toggleWidth').on('click', function(e) {
$('.multiLineLabel').toggleClass('maxWidth');
});
})
.multiLineLabel {
display: inline-block;
box-sizing: border-box;
white-space: pre-line;
word-wrap: break-word;
}
.multiLineLabel .textMaxLine {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
/* Only for DEMO */
.multiLineLabel.maxWidth {
width: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="multiLineLabel">
<span class="textMaxLine">This text is going to wrap automatically in 2 lines in case the width of the element is not sufficiently wide.</span>
</div>
<br/>
<button id="toggleWidth">Toggle Width</button>
답변
CSS 만
line-height: 1.5;
white-space: normal;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
답변
다음과 같이 시도하십시오. http://jsfiddle.net/6jdj3pcL/1/
<p>Here is a paragraph with a lot of text ...</p>
p {
line-height: 1.5em;
height: 3em;
overflow: hidden;
width: 300px;
}
p::before {
content: '...';
float: right;
margin-top: 1.5em;
}