html - CSS 页脚将一行左对齐,另一行右对齐页面上的内容

标签 html css formatting footer

我有一个容器 div,这个 div 里面是随机内容和一个页脚标签。页脚包含 2 个带有随机文本的跨度。我想将第一个跨度向左对齐,第二个跨度向右对齐。

问题是我的页脚位于窗口底部。因此,页脚的宽度仅与其内的文本一样宽,而不是“包含” block 的宽度(它不是真正的容器,因为显然 CSS 认为容器是第一个提供非默认位置的 block ) .

HTML

<div id="container">
    <div class="randomContent">
        <h1>Content of an arbitrarily long length will go here</h1>

        <footer>
            <span class="alignLeft">Hello world</span>
            <span class="alignRight">other text</span>
        </footer>
    </div>
</div>

CSS

#container {
    display: inline-block;

    border: 1px solid #000000;
}

footer {
    position: absolute;
    bottom: 0;
    height: 60px;           /* Height of the footer */
    border: 1px solid #000000;
}

.alignLeft {
    float: left;

}

.alignRight {
    float: right;

}

我的 JS Fiddle显示问题。我希望框的宽度相同,“hello world”文本左对齐,“其他文本”在页脚中右对齐。这应该随着“顶部”文本大小的变化而调整。

这是第二个 JSFiddle这表明我在寻找什么。我希望在没有 java 脚本的情况下做到这一点。希望仅使用 CSS 来做到这一点。

我怎样才能使这项工作?

最佳答案

抱歉,试试这个,它对我有用。

#container {
  display: inline-block;
  border: 1px solid #000000;
}
footer {
  position: absolute;
  bottom: 0;
  left: 0px;
  height: 60px;
  /* Height of the footer */
  width: 100%;
}
.footer {
  margin: 8px;
  border: 1px solid #000000;
  height: 50px;
}
.alignLeft {
  float: left;
}
.alignRight {
  float: right;
}
<div id="container">
  <div class="randomContent">
    <h1>Content of an arbitrarily long length will go here</h1>

    <footer>
      <div class="footer">
        <span class="alignLeft">Hello world</span>
        <span class="alignRight">other text</span>
      </div>
    </footer>
  </div>
</div>

关于html - CSS 页脚将一行左对齐,另一行右对齐页面上的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28461888/

相关文章:

html - 如何使 div 中的所有内容随着过渡以相同的比例扩展

java - 使用 StreamResult 打印 SOAP 消息时或修改其 DOM 结构后出现未格式化的输出

html - 如何防止在 HTML 元素的新行中只有一个悬词?

CSS 计数器无法在 <ul> 内递增

javascript - 如何在倒计时结束后显示图像?

jquery - 鼠标在其他 div 上显示的隐藏按钮

actionscript-3 - 如何按字母顺序自动格式化 AS3 变量声明?

javascript - 如果找不到内容,则折叠 HTML

html - 在响应式布局中设置边距

html - 对齐网页标题中的文本和社交媒体图标