html - 如何使文本在一行中居中,按钮 float 到它的右边?

标签 html css

我想完成一个非常简单的布局,但正在努力。我希望标题在父 div 中居中,标题右侧有一个 float 按钮(在同一行)。下面的代码实现了这个效果,但是按钮是不可点击的,因为标题覆盖了它。关于如何实现这一点有什么建议吗?

.header {
  position: absolute;
  left: 0;
  right: 0;
  font-size: 20px;
  text-align: center;
  text-decoration: underline;
}
.parent {
  border: solid 3px #000;
  padding: 5px;
  position: relative;
}
.button {
  float: right;
}
<div class="parent">
  <div class="header">My header:</div>
  <button type="button" name="abutton" class="button" onclick="alert('a');">The button!</button>
</div>

编辑:

修正了代码错误。

最佳答案

使用 z-index css 属性(并使按钮相对定位):

.header {
  position: absolute;
  left: 0;
  right: 0;
  font-size: 20px;
  text-align: center;
  text-decoration: underline;
  z-index: 1;
}

.button {
   float: right;
   position: relative;
   z-index: 2;
}

关于html - 如何使文本在一行中居中,按钮 float 到它的右边?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30101394/

相关文章:

html - 发布响应式布局

css - 命名 BEM 子 block

jquery - JavaScript、CSS、函数仅在 Internet Explorer 中不起作用

javascript - 使用 jQuery 在特定点加载页面(没有动画!)

Jquery - 如何交替 :Odd :Even pattern every 4 <Divs>?

html - 使用 Flexbox 组织的带有 microTiles 的响应式 Div

javascript - 为多个页面和按钮添加事件监听器

javascript - 我的 AndroidManifest.xml 中是否有任何内容阻止加载外部 JS 和 CSS?

html - 宽度百分比不适用于 div

快速格式化 HTML