Javascript将居中文本放在页面底部

标签 javascript html ios css

我正在尝试使用 javascript 在屏幕底部插入一些居中对齐的文本。

目前为止

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

这会按照我的意愿将它放在屏幕底部,但是当我尝试使用它时

    toInsert.style.textAlign="center";

居中对齐似乎被上面的“绝对”属性覆盖,文本左对齐。

我怎样才能将文本放在屏幕底部,并使其居中? 谢谢。

附言。 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);

关于Javascript将居中文本放在页面底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26981201/

相关文章:

iphone - EXC_BAD_ACCESS 错误发生在具有子类 UITableViewCell 的 UITableViewController 中

javascript - 如何在 OpenLayers 上单击标记

javascript - 如何跨域使用Window.postMessage()?

html - 悬停时不显示工具提示文本

javascript - 同一元素上的 Twitter Bootstrap Popover 和 Tooltip

ios - 如何设置 UICollectionViewCell 大小?

java - 尝试从构建多部分的 Objective-C 文本在 Java 中构建多部分实体

javascript - D3 将鼠标位置反转为 X 轴上最接近的刻度值

javascript - 删除移动尺寸屏幕上的脚本操作

javascript - 如何居中引导行元素?