水平居中对齐 3 个 div 并可链接



我花了几个小时,我认为这样做要简单得多。我正在尝试水平居中 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 或表格执行此操作,非常感谢您的回复!

根本不需要使用 table s。这里的关键是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*/
}​

你的意思是这样吗?

演示

.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;
}

编辑:使用您的样式进行更新

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

你把所有的tddiv衬里。

最新更新