2

我已经花了几个小时,我认为这样做要简单得多。我试图将 3 个 div 水平居中,同时保持它们完全可链接,我最终放弃了并尝试了表格(:-o )

第一个显示我尝试链接 div 失败。

 <center><table>
    <tr>

    <td>
    <a href="http://google.com" style="display:block;height:100%;width:100%">
    <div>
    a
    </div>
    </a>
    </td>
    <td>b</td>
    <td>c</td>
    </tr>
    </table>

使用 CSS

tbody tr td{
width: 300px;
height: 200px;
border: 2px solid #000;
background-color: #000;
opacity: 0.7;
color: #fff;
font-size: 30px;
font-family: 'calibri'; //temporary
padding: 30px;
}
body center table
{
border-spacing: 20px;
margin-top: -90px;
}
tr td a{
height:150%;
width:150%;
}

如果有人知道如何使用 div 或表格执行此操作,我们将不胜感激!

4

3 回答 3

8

根本不需要使用tables 。这里的关键是display: inline-block;。在这里查看它的实际效果:小链接。HTML:

<div class = "onediv"><a href = "#">Glee is awesome!</a></div>
<div class = "onediv"><a href = "#">Glee is awesome!</a></div>
<div class = "onediv"><a href = "#">Glee is awesome!</a></div>​

CSS:

body { /*this should be whatever is containing your three divs*/
    text-align: center; /*center them*/
    white-space: nowrap; /*make sure they're all on the same line*/
}
.onediv {
    display: inline-block; /*magic*/
    height: 200px; /*or whatever you want*/
    width: 150px;
    /*make it look pretty*/
    background: rgb(0, 162, 232);
    color: white;
}
a {
    color: white;
    height: 100%; /*spans the whole div vertically*/
    width: 100%; /*and horizontally (not necessary, but can't hurt!)*/
    display: block; /*otherwise the height and width definitions won't work*/
}​
于 2012-10-06T12:25:52.070 回答
2

你的意思是这样的吗?

演示

HTML:

<div id="wrapper">
<div><a href="#">a</a></div>
<div><a href="#">b</a></div>
<div><a href="#">c</a></div>
</div>

CSS:

#wrapper, #wrapper * {
    -webkit-box-sizing:border-box;
    -moz-box-sizing:border-box;
    box-sizing:border-box;
}
#wrapper {
    margin:0 auto;
    width:1020px; /* width of divs + margin */
}
#wrapper > div {
    float:left;
    width:300px;
    height: 200px;
    text-align:center;
    margin:20px;
    border: 2px solid #000;
    line-height:140px; /* optional, will center the text vertically */
    background-color: #000;
    opacity: 0.7;
    font-size: 30px;
    font-family: 'calibri';
    padding: 30px;
}
#wrapper > div a {
    display:block;
    color: #fff;
}

​​<strong>编辑:根据您的样式更新

于 2012-10-06T12:20:25.820 回答
1
<td>
<a href="http://google.com" style="display:block;height:100%;width:100%">
<div>
a
</div>
</a>
</td>

你做所有tddiv衬里。

于 2012-12-27T10:33:28.423 回答