一个空间等于多少个像素?
这是我的代码:
<div style="position:absolute;top:300px;width:50px;height:50px;background:red;color:black;word-wrap:break-word;">
<div contenteditable=true>
aaaaa
bbbbbbbbbbb
</div>
</div>
我想填充 div 中的所有空间。
有多少像素?
This is my code:
<div style="position:absolute;top:300px;width:50px;height:50px;background:red;color:black;word-wrap:break-word;">
<div contenteditable=true>
aaaaa
bbbbbbbbbbb
</div>
</div>
I want to fill all of the space in the div.
How many pixels is the
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以创建一个包含
的
并分配一个
id
。设置字体大小和属性。然后使用以下命令读取当前宽度和高度:You can create a
and assign an
<div>
containingid
. Set the font size and attributes. Then read the current width and height using:这取决于字体、大小等。空间到像素没有直接的方程式/数字。
It depends on the font, size, etc. There's no direct equation/number for spaces to pixels.
它取决于字体大小,因为
是空白字符。
It's dependent on the font size because
is a white-space character.
“em”(1 个字符宽度)通常被认为是 16 x 16 像素。当然,如果查看者调整文本大小,情况就会改变。
我不确定你想做什么,但如果你只想在文本上方留出空白,你可以在你的 div 中放置另一个 div (用于文本),然后设置一个“ margin-top: 10em; ” (或者无论你想要多少个 em)来提供空白。
An "em" (1 character width) is often considered to be 16 x 16 pixels. Of course, if a viewer resizes text, that would change.
I'm not sure what you're trying to do, but if you just want empty space above your text, you could put another div (for the text) inside your div, and then set a " margin-top: 10em; " (or however many ems you want) to provide the blank space.
没有办法诚实地回答这个问题,因为一个空格的大小会根据字体大小而变化。
试试这个,将“30px”更改为您想要的任何值。
There is no way to answer this question honesty, as the size of one space changes depending on the font size.
Try this, changing the "30px" to whatever you want.
答案将取决于您使用的字体及其指定的样式。因此,您需要在页面中动态计算它。
使用 Javascript 创建一个在页面外显示的 div(即 left:-4000px;},其中包含一个 字符。确保该元素与您的 contentEditable div 具有相同的文本样式。该元素的宽度应为空格字符的宽度(确保不包含任何填充或边框等)
我没有尝试过这个,但这是我首先尝试的。
The answer will depend on the font you are using and the styles it has been given. So you'll need to calculate it dynamically in the page.
Use Javascript to create an div that is displayed off the page (i.e left:-4000px;} that contains one character. Make sure this element has the same text styling as the your contentEditable div. The width of this element should be the width of the space character (make sure you're not including any padding or borders etc.)
I've not tried this, but that's what I'd try first.
您可以使用“em”而不是“px”。第一个关于“em”的谷歌搜索链接:http://www.xs4all.nl/~sbpoley /webmatters/emex.html
尝试这样的构造
,但最好忘记这个想法并使用本主题中其他评论中的 CSS 规则。
You may use "em" instead of "px". First googled link about "em": http://www.xs4all.nl/~sbpoley/webmatters/emex.html
Try such construction
But better to forget this idea and use CSS rules from other comments in this topic.
Unicode 为许多空格字符定义了固定宽度。 此处的下表是 Unicode 规范的摘要 (Unicode 14,第 266 页):
不幸的是,公共空间没有定义为具有固定宽度。它不仅取决于字体大小和明确的样式,还取决于缩放级别和系统显示缩放比例等。
然而,
0.25em
通常会让您关闭。以像素为单位,它是字体高度的 1/4;但如果您尝试将 em 单位调整为 px 尺寸,则最好使用calc()
,或者仅使用 em。或者,考虑不需要您知道空间有多宽的样式策略。例如,对于 OP 的“填充 div 中的所有空间”的目标,父
div
的width
和height
可以是设置为max-content
,或者子 div 的样式可以设置为white-space:nowrap
。或者,例如,可以用伪元素替换 Flex 布局中的间隙,例如,可以插入包含带有
white-space: pre
的空格的元素来创建正确大小的间隙:ETC。
Unicode defines fixed widths for a number of space characters. The following table from here is a summary of Unicode's specs (Unicode 14, p. 266):
Unfortunately the common space is not defined as having a fixed width. It depends not only on font size and explicit styling, but also on zoom level and system display scaling, among other things.
However,
0.25em
will generally get you close. In pixels it would be 1/4 the font height; but you'd be better off usingcalc()
if you are trying to fit em units to px dimensions, or just use em.Alternatively, consider styling strategies that don't require you to know how wide a space is. For the OP's goal of "fill all of the space in the div", for example, the
width
andheight
of the parentdiv
could be set tomax-content
, or the child div could be styled aswhite-space:nowrap
.Or, for example, gaps in flex layouts could be replaced with pseudo elements, e.g. here an element containing a space with
white-space: pre
can be inserted to create a correctly sized gap:Etc.