html - 内部元素的垂直对齐

标签 html css

我在 <td> 中有两个元素一个<div>和一些文字。我想对齐 <div>到顶部,文本到 <td> 的中间我该怎么做?

编辑:应该垂直顶部对齐,而不是 div 内的文本。

最佳答案

试试这个:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title></title>
    <style type="text/css">

        html,body,h1,h2,h3,h4,h5,p,ul,li,form,button { margin:0; padding:0 }
        body { font:normal 62.5% tahoma }

        .my-table  { height:300px }
        .my-cell { position:relative; border:1px solid green }
        .my-div { position:absolute; top:0; left:0; border:1px solid red }

    </style>
</head>
<body>

    <table class="my-table">
        <tr>
            <td class="my-cell" align="center">

                <div class="my-div">
                    I'm aligned to the top
                </div>

                This text is vertically centered.

            </td>
        </tr>
    </table>

</body>
</html>

关于html - 内部元素的垂直对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3665422/

相关文章:

javascript - 网站图书阅读器

javascript - 为什么自动高度不适用于图像?

javascript - 增加任意位置固定

html - 如何在同一行对齐两列内容

javascript - swisnl 的 jquery contextMenu 根据单击的元素将自定义数据添加到名称

jquery - 单击正文 html 时隐藏元素

jquery - jQuery 动画的一些问题

html - 我无法让我的导航栏子菜单垂直对齐

html - 路径内的 SVG flex 文本

javascript - 均匀分布旋转文本