簡體   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