繁体   English   中英

将div / span对齐到表格单元格的左上角

[英]Align div/span to top left of table cell

我有一个代表日历月的表。 也就是说,每行是一周,每个单元格是一天。 我想在每个单元格的左上角显示日期编号,就像在大多数日历中一样(例如, 这里 ),同时仍允许其他内容(任意数量的单词/行)在单元格中,但是我不能让它对齐。 关于某些CSS(或HTML结构的更改,如果需要)可以使这个工作的任何想法? 这是一些示例代码

谢谢!

您可以通过使用position:relative设置父元素( td )来轻松完成此操作,这会导致子元素相对于相关元素绝对定位,因为任何绝对定位的元素都是根据它最接近的位置定位的 - 绝对的,相对的,或固定的 - 父母 正如Gaby aka G. Petrioli所正确指出的那样,我链接到的规范没有定义table-cells在设置到position:relative时应该如何发挥作用的行为position:relative但是所有浏览器实现确实始终如一地实现这一点,在这种情况下,我相信适合使用此属性。

所以你最终需要的是.dayposition:relative.date_num_container设置为position:absolute; top:0px; left:0px; position:absolute; top:0px; left:0px; 它开始工作,你可以在这里看到: http//cssdesk.com/ERpLC

试试这个

td.day {position: relative;}
.date_num_container {position:absolute; top:0; left:0;}

无需绝对定位

td.day
{
    height:15%;
    width:14%;
    background:#ccc;
    padding:0;
    vertical-align:top;
}

div.date_num_container{
  text-align:center;
}

演示 http://cssdesk.com/WMFrA

在你的情况下,我认为不需要绝对定位。 你可以简单地说:

div.date_num_container {
height:100%;
//remove absolute positionning
}

.other_stuff {
margin-top:25px;
text-align:center
}

<div align="left">不工作</div><div id="text_translate"><p>当我使用它时,将 div 放在顶部和中心</p><p> HTML </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> <div align="center"> <div style="max-width:800px"> <div> <div> <div style="CLEAR: none; BORDER-RIGHT: black 1px solid; BORDER-LEFT: black 1px solid" class="Header" align="center"> <div class="Banner"> <div style="max-width:100%"> <div> <div> <div align="top" align="left"> <a class="BannerText" href="https://web.archive.org/web/20060528010436/http://www.roblox.com:80/Default.aspx">ROBLOX.com</a></div> <div align="center" style="max-width:300px"> <a href="https://web.archive.org/web/20060528010436/http://www.roblox.com/Default.aspx" blankurl="/Thumbs/Blank.ashx?x=311&amp;y=45" imageurl="/images/logo_pure_alpha.png" style="display:inline-block;BEHAVIOR:url(/web/20060528010436im_/http://www.roblox.com/UI/Image.htc);cursor:hand;"></a> </div> <div align="right"><span class="BannerText"> <a class="BannerText" href="https://web.archive.org/web/20060528010436/http://www.roblox.com:80/Login/New.aspx">Sign Up</a></span></div> </div> </div> </div> </div> </div> </div></pre></div></div><p></p><p> CSS</p><pre> .Header { font-size: 14px; }.Banner { PADDING: 8px; }.BannerText { font-weight: bold; color: white; } a.BannerText:link, a.BannerText:visited, a.BannerText:active { text-decoration: none; COLOR: white; } a.BannerText:hover { text-decoration: underline; COLOR: white; }</pre><p> 我尝试对其进行编辑和谷歌搜索,但一无所获。 这是我在 wayback 机器<a href="https://web.archive.org/web/20060528010436/http://www.roblox.com:80/" rel="nofollow noreferrer">https://web.archive.org/web/20060528010436/http://www.roblox.com:80/</a>上找到的旧 HTML 代码我发现旧的 header 很有趣,想用它我的项目。 即使我将其更新为 HTML5 代码,我也不知道使用表格而不是 header 标签是否会损害站点优化/seo</p></div>

[英]<div align="top" align="left"> not working

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在顶部的Div内对齐跨度 在表格单元格内将div居中,但将文本向左对齐 <div align="left">不工作</div><div id="text_translate"><p>当我使用它时,将 div 放在顶部和中心</p><p> HTML </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> <div align="center"> <div style="max-width:800px"> <div> <div> <div style="CLEAR: none; BORDER-RIGHT: black 1px solid; BORDER-LEFT: black 1px solid" class="Header" align="center"> <div class="Banner"> <div style="max-width:100%"> <div> <div> <div align="top" align="left"> <a class="BannerText" href="https://web.archive.org/web/20060528010436/http://www.roblox.com:80/Default.aspx">ROBLOX.com</a></div> <div align="center" style="max-width:300px"> <a href="https://web.archive.org/web/20060528010436/http://www.roblox.com/Default.aspx" blankurl="/Thumbs/Blank.ashx?x=311&amp;y=45" imageurl="/images/logo_pure_alpha.png" style="display:inline-block;BEHAVIOR:url(/web/20060528010436im_/http://www.roblox.com/UI/Image.htc);cursor:hand;"></a> </div> <div align="right"><span class="BannerText"> <a class="BannerText" href="https://web.archive.org/web/20060528010436/http://www.roblox.com:80/Login/New.aspx">Sign Up</a></span></div> </div> </div> </div> </div> </div> </div></pre></div></div><p></p><p> CSS</p><pre> .Header { font-size: 14px; }.Banner { PADDING: 8px; }.BannerText { font-weight: bold; color: white; } a.BannerText:link, a.BannerText:visited, a.BannerText:active { text-decoration: none; COLOR: white; } a.BannerText:hover { text-decoration: underline; COLOR: white; }</pre><p> 我尝试对其进行编辑和谷歌搜索,但一无所获。 这是我在 wayback 机器<a href="https://web.archive.org/web/20060528010436/http://www.roblox.com:80/" rel="nofollow noreferrer">https://web.archive.org/web/20060528010436/http://www.roblox.com:80/</a>上找到的旧 HTML 代码我发现旧的 header 很有趣,想用它我的项目。 即使我将其更新为 HTML5 代码,我也不知道使用表格而不是 header 标签是否会损害站点优化/seo</p></div> 如何将 div 对齐到 flexed div 的左上角 将跨度内的图像对齐到表格单元格的右侧 在表格单元格中垂直对齐文本跨度 将div左上方的一组图像与CSS对齐 当一个单元格div包含绝对定位的div时,如何在表格div内使两个单元格div顶部对齐? 表格单元格内容:在HTML中将文本的左上角和中间对齐 CSS将对齐范围向右对齐的div对齐
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM