语法:对css值使用变量 [英] Syntax: Use variable for css value

查看:138
本文介绍了语法:对css值使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在另一个框中将框保持垂直居中。我知道有css可以做到这一点,但我宁愿使用jquery,更可靠(?)。

Trying to keep a box centered vertically within another box. I know there's css that can do this, but I'd rather use jquery, more reliable(?).

var textH = $(".Text").height();
var vertAlign = ((140 - textH)/2);

$(".Text").css({
    marginTop: 'vertAlign'
});

不知道我缺少什么细节。输出应为可用垂直空间的一半,以像素为单位。

Not sure what detail I'm missing. The output should be half the available vertical space, in pixels.

编辑

最初,文本块是由div包含的span。 div有一个设置的高度(在这种情况下为140像素),并且文本块,跨度,将是基于其中有多少文本的可变高度。但是,我需要这个文本块可编辑。所以我把它改为一个文本区域。然而,文本区域的尺寸的行为是尴尬的,我不得不设置一个静态的高度和宽度。现在这个文本块的高度不是可变的,所以它和它的父对象之间的高度没有差别,可以从中导出边距顶部间距。我应该怎么办?

Originally, the text block was a span contained by a div. The div had a set height (140 px in this case), and the text block, the span, would be variable height based on how much text was in it. However, I need this text block to be editable. So I changed it to a text area. however, the behavior for the dimensions of the text area is awkward, and I had to set a static height and width to it. Now the height of this text block isn't variable, so there's no difference in height between it and it's parent with which to derive the margin-top spacing from. What should I do?

推荐答案

删除 vertAlign

$(".Text").css('margin-top', vertAlign);

尝试上述代码。它必须帮助你。

Try the above code. It must help you.

这篇关于语法:对css值使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆