簡體   English   中英

Javascript將居中文本放在頁面底部

[英]Javascript to put centered text at the bottom of the page

我正在嘗試使用javascript在屏幕底部插入一些居中對齊的文本。

到目前為止,我有

    var toInsert = document.createElement("div");
    toInsert.innerHTML = "text to insert";
    toInsert.style.position = "absolute";     
    toInsert.style.bottom = "0px";

這會將其放置在屏幕底部,但我想要使用它時

    toInsert.style.textAlign="center";

中心對齊方式似乎被上面的“ absolute”屬性所覆蓋,並且文本左對齊。

如何將文本放在屏幕底部並居中? 謝謝。

PS。 JavaScript,而不是jquery,用於iOS設備上的UIWebView。

確保您的div為100%寬度。

toInsert.style.width = "100%";

 var toInsert = document.createElement("div"); toInsert.innerHTML = "text to insert"; toInsert.style.position = "absolute"; toInsert.style.bottom = "0px"; toInsert.style.textAlign = "center"; toInsert.style.width = "100%"; document.body.appendChild(toInsert); 

IMO會更容易使用類標識符在CSS中正確定位元素,然后使用javascript添加類。

由於您使用的是絕對定位,因此您要查找的CSS可能是:

left:50%;

我在StackOverFlow上進行了快速搜索,這是第一個出現的搜索,我想您的答案已經存在了。

使DIV居中的最佳方法

這是我的Dabblet.com示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM