html - 如何对齐此 CodePen 中的中心元素?

标签 html css

http://codepen.io/leongaban/pen/ACJta
^ 已更新(我永远无法让 SASS 在 Codepen 中正常工作)

无法与上面我的元素居中对齐。使用 SASS,我想避免使用 position hacks

<main>
  <div class="gradient">
    <div class="hero">
      <img src="http://placehold.it/350x150" alt=""/>
    </div>
  </div>

  <section class="tagline">
    <h1>Hi there Lorem Ipsum <em>My name is Bob</em></h1>
    <button>Learn More</button>
  </section>
</main>


body {
  font-family:Arial
}

.hero {
    display: table-cell;
    //position: relative;
    margin: 0 auto;
    // top: 42%;
    // left: 10%;
    text-align: center;
    width: auto;
    height: 447px;
    vertical-align: bottom;

    img {
        width: 300px;
        height: 280px;
        text-align: center;
        vertical-align: bottom;
        border:0;
    }
}

.tagline {
    position: relative;
    margin: 0 auto;
    width: 100%;
    height: 300px;
    text-align: center;
    color: blue;
    background: gray;
    z-index: 2;

    h1 {
        margin: 0 auto;
        padding-top: 5%;
        width: 80%;
        font-size: 42px;
        text-align: center;
    }

    em {
        font-weight: bold;
    }

    button {
        margin-top: 25px;
        padding: 10px 20px;
        font-family: Arial;
        font-size: em(21);
        color: white;
        border: 0;
        background: orange;
    }
}

最佳答案

我能够将您的元素置于我执行以下操作之上:

.hero {
  margin: 0 auto;
  text-align: center;
  width: 50%;
  height: 447px;
  vertical-align: bottom;
}

它似乎不想居中,因为你指定了 width: auto

关于html - 如何对齐此 CodePen 中的中心元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22997394/

相关文章:

html - 如何根据内容在页面加载时设置 textarea 高度的样式?

javascript - 如何使用 javascript 创建电子邮件按钮

html - 为什么悬停输入会在 CSS 中的相应标签上触发?

css - 内容 CSS 在 Safari 浏览器中不起作用

html - 如何使用 Bootstrap 表单控件类自动调整内容大小来制作 contenteditable div?

javascript - CSS 图像加载时翻转

html - CSS 链接背景图片不显示

html - css 样式列表中有一个漏洞,但为什么呢?

css - 如何删除两个对 Angular 相对的 css 斜 Angular 三 Angular 形之间的线?

html - ios 用新的子字符串替换子字符串