dongshipang8094 2012-03-08 14:10
浏览 158
已采纳

设置表列宽

I am trying to setup a table column width to only 200px or 10%. However, when I populate the data from the database, the column seems to ignore the column width and expand it as much as it can. Here is my table html.

<table>
<thead>
    <tr>
        <th><a href='#'>City</a></th>   
        <th width='200' class='table_width'><a href='#'>name</a></th> 
        <th><a href='#'>Agent</a></th>
        <th>... 
    //I have tried class and width attribute. both don't work. 
    </tr>
</thead>
<tbody>
        <tr>
            //these column data were popluated from database
            <td><?= $row->city; ?></td>
            <td width='200' class='table_width'><?= $row->_name; ?></td>  
            //ignore the width and expand the table cell. 
            <td><?= $row->agent; ?></td> 
            <td>...

        </tr>
  • 写回答

3条回答 默认 最新

  • dreamworld2007 2012-03-08 14:30
    关注

    You want to use word-wrap:break-word;.

    http://jsfiddle.net/RF4F6/1/

    HTML

    <table>
        <thead>
            <tr>
                <th>Col 1</th>
                <th>Col 2</th> 
                <th>Col 3</th> 
                <th>Col 4</th> 
                <th>Col 5</th> 
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Normal Width</td>
                <td>Normal Width</td>
                <td class="forcedWidth">hiThisIsAreallyLongStringThatWillHaveToWrapAt200PixelsOrI'llBeUnhappyAndYouWon'tLikeMeWhenI'mUnhappy.</td>
                <td>Normal Width</td>
                <td>NormalWidth</td>
            </tr>
        </tbody>
    </table>
    

    CSS

    table td{
         padding:.5em; /* Not essential for this answer, just a little buffer for the jsfiddle */
    }
    
    .forcedWidth{
        width:200px;
        word-wrap:break-word;
        display:inline-block;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?