2017-07-19 84 views
0

我一直在一个项目工作,并有一个页面,它会显示几个div(使用ejs),但如果一个div有3行另一个有2个,一个会比另一个大很多。是否有可能使用CSS缩小文本自动适合div的大小

enter image description here

这里是我的HTML/ejs文件代码:

<div id = "home" class = "container"> 
<h1 class="polls-head">Polls</h1> 
<div class="main-aligner">   
    <% for(var i = 0; i < allPolls.length; i++){ %> 
    <div class="title" id="dynamicDiv"> 
     <div class="poll-name" id="dynamicSpan"> 
     <%= allPolls[i].title %> 
    </div> 
    <div class="poll-link"> 
     <div class= "poll-view"> 
      <a href="polls/<%= allPolls[i]._id %>" class="btn button view">View Poll</a> 
     </div> 
     <div class="createdBy"> 
      <p>Created by: <%=allPolls[i].createdBy %></p> 
     </div> 
    </div> 
    </div> 
    <%}%> 
</div> 
</div> 

而对于CSS文件

.title 
{ 
    font-family: "Lato"; 
    width: 365px; 
    height: auto; 
    min-height: 150%; 
    background-color: #dfdce3; 
    display: inline-block; 
    margin-top: 10px; 
    margin-right: 5px; 
    margin-left: 5px; 
    text-align: center; 
} 
.poll-name 
{ 
    font-size: 50px; 
} 

是如果长度超过2行,可以自动缩小文本的大小?或者有什么办法可以让所有的盒子尺寸相同,所以盒子的尺寸总是和最多线条的盒子尺寸相同?

+0

这听起来像为JavaScript的作业。我不熟悉用CSS来做到这一点。你可以掩盖文本,但实际上缩小它,你将需要JS。 –

回答

0

您可以尝试使用下面

.poll-name { 
    overflow: hidden; 
    text-overflow: ellipsis; 
    word-wrap: break-word; 
    display: block; 
    line-height: 1em; /* a */ 
    max-height: 2em; /* a x number of line to show (ex : 2 line) */ 
} 

干杯这个代码!

+1

谢谢阿德克牧师!这些代码行与我的网站非常吻合! –

+0

很高兴听到这个消息,无论如何,您可能需要在跨浏览器上检查这些代码。 – Spartan

0

您可以使用em或rem而不是像素来确定字体的大小。它会响应并随着它所在元素的大小而变化。

相关问题