javascript - 如何定位 innerHTML 纯文本?
问题描述
简单的问题,我如何格式化display.innerHTML的输出文本,以便我可以将它定位在确定的高度内?
var display = document.getElementById("display");
display.innerHTML = "RGB (" + red + ", " + green + ", " + blue + ")";
输出将是一个文本“ RGB(red,green,blue) ”。
这是 CSS :
#display {
height: 100px;
background: #425779;
transition: background 100ms;
margin-top: 30px;
border: 1px solid #000;
text-align: center;
}
只想显示它垂直和水平居中。
解决方案
在 2020 年,我最喜欢在容器中居中元素的方式是使用display: grid
和place-items: center
#display {
display: grid;
place-items: center;
height: 100px;
background: #425779;
transition: background 100ms;
margin-top: 30px;
border: 1px solid #000;
}
<div id="display">RGB(x, y, z)</div>
推荐阅读
- function - 当仅显式返回一个参数时,Powershell 函数返回所有参数
- c - 如何在C中询问输入直到ctrl + D
- typescript - 打字稿只使用第一个函数覆盖
- tensorflow - 如何在keras自定义层中循环batch_size
- python - RHEL w/Tkinter & Python3 - 从“Tk”更改活动名称
- javascript - 我可以使用 javascript 库/插件在后台创建没有“地图”的热图吗?
- java - 使用文件java代码中的arraylist登录?
- reactjs - 新创建的 React 应用程序在实时服务器上崩溃
- sql - 将旧数据库中的表和文件组复制到新数据库
- r - 循环变量是r中的正则表达式