简体   繁体   English

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

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

I have a table that represents a calendar month. 我有一个代表日历月的表。 That is, each row is a week, and each cell is a day. 也就是说,每行是一周,每个单元格是一天。 I'd like to display the date number in the top left of each cell, like it is on most calendars (eg, here ), while still allowing other content, of an arbitrary number of words/lines, to be in the cell, but I can't get it to align. 我想在每个单元格的左上角显示日期编号,就像在大多数日历中一样(例如, 这里 ),同时仍允许其他内容(任意数量的单词/行)在单元格中,但是我不能让它对齐。 Any ideas on some CSS (or changes to the HTML structure, if needed) that can make this work? 关于某些CSS(或HTML结构的更改,如果需要)可以使这个工作的任何想法? Here's some example code . 这是一些示例代码

Thanks! 谢谢!

You can easily do this by setting the parent element (the td ) with a position:relative which causes the child elements to be absolutely positioned relative to the element in question , because any absolutely positioned element is positioned according to it's closest positioned - absolute, relative, or fixed - parent . 您可以通过使用position:relative设置父元素( td )来轻松完成此操作,这会导致子元素相对于相关元素绝对定位,因为任何绝对定位的元素都是根据它最接近的位置定位的 - 绝对的,相对的,或固定的 - 父母 As correctly noted by Gaby aka G. Petrioli the spec I linked to does not define behavior of how table-cells should funciton when set to position:relative however all browser implementations do consistently implement this none the less and in a case like this I believe it appropriate to use this property. 正如Gaby aka G. Petrioli所正确指出的那样,我链接到的规范没有定义table-cells在设置到position:relative时应该如何发挥作用的行为position:relative但是所有浏览器实现确实始终如一地实现这一点,在这种情况下,我相信适合使用此属性。

So all you need in the end is have .day be position:relative and .date_num_container be set to position:absolute; top:0px; left:0px; 所以你最终需要的是.dayposition:relative.date_num_container设置为position:absolute; top:0px; left:0px; position:absolute; top:0px; left:0px; and it starts working as you can see here: http://cssdesk.com/ERpLC 它开始工作,你可以在这里看到: http//cssdesk.com/ERpLC

try this 试试这个

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

No need for absolute positioning 无需绝对定位

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

div.date_num_container{
  text-align:center;
}

Demo at http://cssdesk.com/WMFrA 演示 http://cssdesk.com/WMFrA

I don't see the need for absolute positionning in your case. 在你的情况下,我认为不需要绝对定位。 You can take it simple: 你可以简单地说:

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内对齐跨度 - Align Span inside Div on top 在表格单元格内将div居中,但将文本向左对齐 - Center div inside table cell but align text left <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 如何将 div 对齐到 flexed div 的左上角 - How to align a div to top left on flexed div 将跨度内的图像对齐到表格单元格的右侧 - Align image within a span to be to the right of a table cell 在表格单元格中垂直对齐文本跨度 - Align span of text vertically in table cell 将div左上方的一组图像与CSS对齐 - Align a set of images at top left of a div with css 当一个单元格div包含绝对定位的div时,如何在表格div内使两个单元格div顶部对齐? - How to top align two cell divs within a table div when one cell div contains an absolute positioned div? 表格单元格内容:在HTML中将文本的左上角和中间对齐 - Table cell content: align text top-left and image in the middle in HTML CSS将对齐范围向右对齐的div对齐 - CSS Align span to left bottom of div aligned to right
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM