2016-09-14 4 views
0

각 행에는 하나의 셀만있는 테이블이 있습니다. 각 셀에 추가되는 단어는 다양합니다. 일부는 길지만 일부는 길지만 모든 셀의 너비는 가장 긴 내용이있는 셀의 너비와 같습니다.모든 <td>의 너비가 <td> 대신 가장 똑같은 내용으로 만들어야합니다.

CSS/HTML에서 각 셀 너비를 가장 긴 내용이있는 셀 대신 자체 내용에 맞출 수 있습니까?

$("#chatdisplay").append("<tr bgcolor='#ccffcc'><td><b>" + arr[i].date_time_sent + "</b><br/>" + arr[i].sender + ":" + "<br>" + arr[i].message + "<br></td></tr>"); 

$("#chatdisplay").append("&nbsp;"); 

CSS

table,th,td,tr { 
    padding: 20px; 
    border-radius: 20px; 
    padding-top: 8px; 
    padding-bottom: 8px; 
} 
table { 
    overflow-y: scroll; 
    height: 400px; 
    width: 100%; 
    display: block; 
    background-color: #ffffff; 
} 

enter image description here

+3

아니,'table' 그 유연성이 없습니다. 'div' 시도해보십시오 –

+1

각 메시지는 자체 테이블이어야합니다 ... 그러면 작동합니다, –

+1

@RiotZeastCaptain, 가능하지만 실제로는 아닙니다. 예제를 사용하여 내 대답을 확인하십시오 :) – Dekel

답변

0

당신이 정말하고 싶은 경우 - 당신은 그러나이 정말 을하지 않는 것이 좋습니다의 td 요소에 display: inline-block를 사용하여, 수 및 이것을하는 방법이 아닙니다.

구조체에 테이블을 사용할 수 있으며 (실제로는 필요하지 않지만) div 블록을 사용할 수 있습니다. 여기

는 3 옵션에 대한 예입니다

table,th,td,tr { 
 
    padding: 20px; 
 
    padding-top: 8px; 
 
    padding-bottom: 8px; 
 
} 
 
table { 
 
    overflow-y: scroll; 
 
    width: 100%; 
 
    display: block; 
 
    background-color: #ffffff; 
 
} 
 
#tbl1 td { 
 
    border-radius: 20px; 
 
    background: #ccffcc; 
 
    display: inline-block; 
 
} 
 
#tbl2 div.container { 
 
    border-radius: 20px; 
 
    background: #ccffcc; 
 
    display: inline-block; 
 
    padding: 8px 20px; 
 
} 
 
#tbl2 tr, #tbl2 td { 
 
    padding: 0; 
 
} 
 
div.blocks div.container { 
 
    padding: 4px 20px; 
 
} 
 
div.blocks div.container div.content { 
 
    border-radius: 20px; 
 
    background: #ccffcc; 
 
    display: inline-block; 
 
    padding: 8px 20px; 
 
}
td with display: inline-block:<br /> 
 
<table id="tbl1"> 
 
    <tr> 
 
    <td>This is some content</td> 
 
    </tr><tr> 
 
    <td>text</td> 
 
    </tr><tr> 
 
    <td>This is a cell with much more content</td> 
 
    </tr> 
 
</table> 
 
<br /> 
 
div's insinde td<br /> 
 
<table id="tbl2"> 
 
    <tr> 
 
    <td> 
 
     <div class="container">This is some content</div> 
 
    </td> 
 
    </tr><tr> 
 
    <td> 
 
     <div class="container">text</div> 
 
    </td> 
 
    </tr><tr> 
 
    <td> 
 
     <div class="container">This is a cell with much more content</div> 
 
    </td> 
 
    </tr> 
 
</table> 
 

 
<br /> 
 
Div's only:<br /> 
 
<div class="blocks"> 
 
    <div class="container"> 
 
    <div class="content">This is some content</div> 
 
    </div> 
 
    <div class="container"> 
 
    <div class="content">text</div> 
 
    </div> 
 
    <div class="container"> 
 
    <div class="content">This is a cell with much more contentThis is a cell with much more contentThis is a cell with much more contentThis is a cell with much more contentThis is a cell with much more contentThis is a cell with much more contentThis is a cell with much more contentThis is a cell with much more contentThis is a cell with much more content</div> 
 
    </div> 
 
</div>

+0

안녕하세요 Dekel, 귀하의 제안에 감사드립니다! 제안 사항에 따라 각 루프마다

*array items*
을 추가했습니다. 이제 각 "강조 표시된 버블"의 길이는 해당 내용의 길이만큼 길어집니다. 당신의 멋진 예제와 유용한 게시물입니다! –

+0

당신은'tr' 요소를'div'와 함께 포장하는 이유를 정말로 이해하지 못합니다. 이것은 정확한 구조가 아닙니다 ... – Dekel

+0

테이블 셀 내부에 div를 추가하고 그 반대의 경우, 현명한 외관을 보였습니다. 처음에는 채팅 메시지 디스플레이에서 찾고있는 것처럼 보였습니다. –