html - 输入字段宽无法正常工作

标签 html css forms

我的目标输出想要像打击图像一样。

enter image description here

我的 CSS 和 HTML:

header {
  height: 40px;
  width: 100%;
  position: fixed;
  background: #00A6E3;
  top: 0;
  left: 0;
  border-radius: 0 0 10px 10px;
  padding: 15px;
  box-shadow: 0 2px 5px rgba(44, 62, 88, 0.15);
  display: block;
  z-index: 5;
}
header .todoitem {
  width: 100%;
  height: 100%;
  float: left;
  border: none;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.2);
  text-indent: 20px;
}
header input::-webkit-input-placeholder {
  color: #fff;
}
<header>
  <input type="text" name="todoitem" class="todoitem" placeholder="Enter your activity...">
</header>

在编写我的代码输入后,左侧显示正确但右侧显示不正确。据我所知,右侧需要显示 15px 的填充。但是我不明白我的代码有什么问题。

最佳答案

两种可能的解决方案:

首先,您可以将 box-sizing: border-box 添加到您的样式中。事实上,您的 header 被设置为匹配其容器的整个宽度,然后在外部应用填充,这是 box 的默认行为-sizing,这使得填充超出了容器的限制。

更多阅读 on css-tricks.com .

您还需要调整高度以适应新的盒子模型(总高度现在包括内边距的高度)- 这是您更新后的代码:

header {
  box-sizing: border-box;
  height: 70px;
  width: 100%;
  position: fixed;
  background: #00A6E3;
  top: 0;
  left: 0;
  border-radius: 0 0 10px 10px;
  padding: 15px;
  box-shadow: 0 2px 5px rgba(44, 62, 88, 0.15);
  display: block;
  z-index: 5;
}
header .todoitem {
  width: 100%;
  height: 100%;
  float: left;
  border: none;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.2);
  text-indent: 20px;
}
header input::-webkit-input-placeholder {
  color: #fff;
}
<header>
  <input type="text" name="todoitem" class="todoitem" placeholder="Enter your activity...">
</header>

选项 2:如果您希望保留默认框模型,则可以使用计算出的宽度来补偿填充:将 headerwidth 设置为 calc(100% - 30px)(如果选择此选项,请注意保留 - 周围的空格)。

关于html - 输入字段宽无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41522626/

相关文章:

jquery - Bootstrap 侧边栏贴在滚动条上闪烁

html - 使 <aside> 元素成为页面的特定部分?

jquery - 使用 jquery 或 javascript 改变变换 Angular

javascript - 查询/JavaScript : Button disappears after being clicked

javascript - 为什么 pdffiller 不选中我的 PDF 复选框?

javascript - 如何计算 "check-in date"和 "check-out date"之间有多少天?

html - 无法使用 Angular 带将 svg Logo 添加到导航栏

javascript - 使用鼠标悬停从左到右、从右到左滚动

JavaScript:使用 localStorage 登录/注册

javascript - 表单在 IE8 中滚出页面