Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css Can';t中心文本?_Css_Html_Text_Center - Fatal编程技术网

Css Can';t中心文本?

Css Can';t中心文本?,css,html,text,center,Css,Html,Text,Center,我一直在尝试使用top:50%将一段文本放在登录页的中心位置;左:50%在我的html中,但由于某种原因它不在中心?下面是我在标记后使用的html- <div style="position: absolute; top:50%; left:50%; width:500px; height:25px"><font size="4" color="white">My Text Here</font></div> 这就是我网站上的样子- 另外,我没

我一直在尝试使用
top:50%将一段文本放在登录页的中心位置;左:50%在我的html中,但由于某种原因它不在中心?下面是我在
标记后使用的html-

<div style="position: absolute; top:50%; left:50%; width:500px; height:25px"><font size="4" color="white">My Text Here</font></div>
这就是我网站上的样子-

另外,我没有那么多的编码知识,所以如果我说的没有意义,就说吧


谢谢

您可以尝试以下代码:


我的文本在这里
您只需设置:

文本对齐:居中

 <div style="position: absolute; top:50%; left:50%; width:500px; height:25px;text-align:center;border:1px solid"><font size="4" color="white">My Text Here</font></div>
我的文本在这里

如果这有助于你,不要忘记接受答案;)

试试这个。更好的兼容性和更强的健壮性

<div style="position: absolute; top:50%; width:100%; height:1em; margin-top: -.5em; color: white; font-size: 10px; text-align: center;">My Text Here</div>
我的文本在这里
上面的文字应该在页面的正中央对齐。诀窍是“文本对齐:居中;”。浏览器将自动将标记的所有子文本居中


另外,请注意:使用不推荐的标记(如字体标记)通常被认为是不好的做法,因为它的结果可以使用CSS实现。我没有使用它,而是在CSS中使用了它的一般等价物。

你需要否定宽度的1/2和高度的1/2的边距。另外,不要使用
标记,它不推荐在你的身体标记中添加宽度和高度。Tk先生,不起作用..只是使文本消失..这只是使文本移近左侧:/仍然不起作用,只是使文本移近左侧:-/仍然向右移动了一点点!我太完美主义了!!我能做些什么把它移到那几个像素上吗?除此之外,它工作得很好:-)还有,对于css…是否有一个代码我应该添加以使它…更好?啊,我想我明白了。我猜您尚未设置“body”标记样式“margin:0;”。默认情况下,HTML页面有一点空白。我只是在测试页面上尝试了一下,得到了正确的结果。至于你的代码,我觉得一切都很好。我要说的唯一一件事是使用“样式”标记或链接到CSS文件将CSS代码与HTML标记分开。使用属性没什么错,只是比内联属性更容易阅读规则表。太棒了,全部修复:-)谢谢!
 <div style="position: absolute; top:50%; left:50%; width:500px; height:25px;text-align:center;border:1px solid"><font size="4" color="white">My Text Here</font></div>
<div style="position: absolute; top:50%; width:100%; height:1em; margin-top: -.5em; color: white; font-size: 10px; text-align: center;">My Text Here</div>