html - 将此 h1 居中 'button'

标签 html css twitter-bootstrap center

这是一个非常基本的问题,但我就是无法让它发挥作用。我在 Bootstrap 框架中设计了一个 h1 标题,但无法将其设置为: - 居中 -在较小的屏幕上缩小

任何帮助将不胜感激!

我的CSS:

    .itsthisone h1 {
  background-color: #fff;
  border: 6px solid #dfdfdd;
  color: #f47d41;
  font-size: 18px;
  margin-left: auto ;
  margin-right: auto ;
  padding: 20px 0;
  position: absolute;
  top: -34px;
  text-align: center;
  width: 720px;
}

@media (max-width: 479px) {
  .itsthisone h1 {
font-size: 14px;
  top: -15px;
  margin-left: auto ;
  margin-right: auto ;
  padding: 10px 0;
  }
}

@media (max-width: 768px) {
  .itsthisone h1 {
    font-size: 14px;
    width: 360px;
  }
}

我的 HTML:

<section class="itsthisone">
    <h1>keep it in center!</h1>

提前致谢! ( 似乎从代码中消失了)

最佳答案

你可以这样做:

逻辑:移动绝对容器的 50% 并放置容器宽度的负边距的一半,然后得到容器相对于其父容器的中心。 (静态方法)

CSS

.itsthisone h1 {
  background-color: #fff;
  border: 6px solid #dfdfdd;
  color: #f47d41;
  font-size: 18px;
  padding: 20px 0;
  position: absolute;
  top: -34px;
  text-align: center;
  width: 720px;
  left: 50%;
  margin-left: -360px;
}

@media (max-width: 479px) {
  .itsthisone h1 {
    font-size: 14px;
    top: -15px;
    margin-left: auto;
    margin-right: auto;
    padding: 10px 0;
  }
}

@media (max-width: 768px) {
  .itsthisone h1 {
    font-size: 14px;
    width: 360px;
    left: 50%;
    margin-left: -180px;
  }
}

DEMO HERE

关于html - 将此 h1 居中 'button',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34227988/

相关文章:

html - 使 div 高度为 100%

java - 隐藏 p :schedule with css 中的事件

html - 重复背景图像 - 不裁剪或使其对称

CSS bootstrap 覆盖了我自己的 CSS 代码

html - 当标签达到 2 行时如何修复布局

javascript - 有没有办法使用 [javascript] 防止多点 html 输入标签按键事件

javascript - 向下滚动时如何使图像移动?

javascript - 从客户端隐藏 loggly 输入 token

css - SASS 复制嵌套选择器

javascript - 如何获取覆盖div下元素的鼠标悬停事件