html - 在 CSS 中以不同文本大小居中 HTML 正文元素

标签 html css

我在尝试使用 CSS 将 body 元素居中时遇到了问题。我希望它在页面上水平和垂直居中。问题在于它现在的编写方式,它将使短消息的文本居中(即在三个词以下),但较长的消息会把它搞砸。

这是我的 CSS:(它是 Sinatra 的 erb 文件,但我认为这不会影响 CSS 的解释方式)

body {
  background: white;

  /*TODO: still not perfect; changes based on length of message */
  position: fixed;
  top: 50%;
  left: 50%;
  margin-top: -100px;
  margin-left: -100px;
}
.message {
  color: #2f4f4f;
  font-family: 'Roboto', sans-serif;
  font-size: 36;
  text-align: center;
}
.form {
  text-align: center;
}
input[type=number]{
  width: 100%;
  border: none;
  border-bottom: 6px solid #2f4f4f;
  background: transparent;
  text-align: center;
  color: #2f4f4f;
  font-size: 25px;
  font-family: 'Roboto';
}
input[type=number]:focus {
  outline: none;
}
#btn{
  display: none;
}

最佳答案

body {
    /* ... */
    position: fixed;
    left: 50%;
    top: 50%;
    -webkit-transform: translate(-50%, -50%);
    -moz-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    -o-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}

请确保移除边距。

关于html - 在 CSS 中以不同文本大小居中 HTML 正文元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42197452/

相关文章:

html - 网站在手机上看起来不一样

java - 带进度条的文件上传,或从 Google Web Toolkit 访问 Html 5 File Api?

javascript - 尽管有 z-index,但动画 DIV 隐藏的跨度文本

html - 即使给定相同的属性和值,按钮大小也不同

jquery - 禁用父 jquery 上的点击事件

javascript - div 删除/显示而不影响内容

javascript - WordPress 主题使用 Jquery 移动对象

javascript - 自动调整 Canvas 中的图像,如果图像分辨率大于 Html5 中的 Canvas 大小

html - css 悬停在 IE 中的元素问题上

html - 无序列表文本的顶部对齐