html - 为什么我的按钮标签不能像其他输入标签一样居中?

标签 html css

<分区>

我正在尝试使用 HTML 和 CSS 创建一个搜索框,其中有一个文本输入标签和一个按钮输入标签,它们都位于其中。但是 CSS 代码不适用于我的按钮标签,尽管它适用于其他标签。

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

input {
  outline: none;
}

.search-box {
  height: 50px;
  width: 280px;
  border: 1px solid #DCDCDC;
  background-color: #F5F5F5;
  margin: 10px auto;
  border-radius: 10px;
  box-shadow: 2px 2px 1px #D3D3D3, -2px 2px 1px #D3D3D3;
}

#input-search {
  width: 200px;
  height: 30px;
  border: 1px solid #DCDCDC;
  margin: 9px 10px 9px 9px;
  border-radius: 5px;
  text-indent: 3px;
}

#btn {
  height: 30px;
  width: 50px;
  font-size: 12px;
  font-weight: 600;
  color: #fff;
  border-radius: 5px;
  border: 1px solid #008080;
  display: inline-block;
  margin: 9px 9px 9px 0;
  background-image: linear-gradient(#20B2AA, #5F9EA0);
}
<div class="search-box">
  <input type="text" id="input-search" placeholder="search">
  <input type="button" value="GO" id="btn">
</div>

根据我的计算,两侧的边距应该使两个标签在 div 中完美居中,但似乎有一些填充或边距 *{margin:0; padding:0;box-sizing:border-box; 无法清除。

按钮既不能垂直居中,也不能与文本标签对齐。谁能帮我修复按钮标签?

这是完整的代码:https://codepen.io/mayiscoding/pen/qBxLbYb

最佳答案

Based on my calculation, the margin on both sides is supposed to make the two tags perfectly centred in the div

您的计算不完全正确,您所做的是 200+9+10+50+9278,这似乎适合元素宽度 278。但是您没有包括的是由新行 ( Why does the browser renders a newline as space? ) 引入的两个 input 元素之间的空格。

如果您删除该新行,它将起作用:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

input {
  outline: none;
}

.search-box {
  height: 50px;
  width: 280px;
  border: 1px solid #DCDCDC;
  background-color: #F5F5F5;
  margin: 10px auto;
  border-radius: 10px;
  box-shadow: 2px 2px 1px #D3D3D3, -2px 2px 1px #D3D3D3;
}

#input-search {
  width: 200px;
  height: 30px;
  border: 1px solid #DCDCDC;
  margin: 9px 10px 9px 9px;
  border-radius: 5px;
  text-indent: 3px;
}

#btn {
  height: 30px;
  width: 50px;
  font-size: 12px;
  font-weight: 600;
  color: #fff;
  border-radius: 5px;
  border: 1px solid #008080;
  display: inline-block;
  margin: 9px 9px 9px 0;
  background-image: linear-gradient(#20B2AA, #5F9EA0);
}
<div class="search-box">
  <input type="text" id="input-search" placeholder="search"><!--
  --><input type="button" value="GO" id="btn">
</div>

但为避免此类问题,您可能希望使用 display:flex 而不是手动计算元素的正确宽度。

  • .search-boxdisplay 设置为 flex
  • 删除两个 input 元素的 widthheight,flex 将处理它。
  • 不是将 width 设置为 #btn,而是设置所需的 padding
  • #input-search 上使用 flex: 1 1 auto; 使其拉伸(stretch)并使用可用尺寸。

这样你可以改变你的.search-boxwidth而不需要改变输入元素的宽度,你可以更新字体大小按钮和文本。您甚至可以通过将 with 更改为 max-width 来使 .search-box 响应。

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

input {
  outline: none;
}

.search-box {
  height: 50px;
  width: 280px;
  border: 1px solid #DCDCDC;
  background-color: #F5F5F5;
  margin: 10px auto;
  border-radius: 10px;
  box-shadow: 2px 2px 1px #D3D3D3, -2px 2px 1px #D3D3D3;
  display: flex;
}

#input-search {
  border: 1px solid #DCDCDC;
  margin: 9px 10px 9px 9px;
  border-radius: 5px;
  text-indent: 3px;
  flex: 1 1 auto;
}

#btn {
  font-size: 12px;
  font-weight: 600;
  color: #fff;
  border-radius: 5px;
  border: 1px solid #008080;
  display: inline-block;
  margin: 9px 9px 9px 0;
  padding-left: 15px;
  padding-right: 15px;
  background-image: linear-gradient(#20B2AA, #5F9EA0);
}
<div class="search-box">
  <input type="text" id="input-search" placeholder="search">
  <input type="button" value="GO" id="btn">
</div>

为了进一步改进 CSS,我将从 input 元素中删除 margin,并将其更改为 上的 padding .search-boxgap。对于 #btnpadding 我将使用 em 而不是 px 单位。

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

input {
  outline: none;
}

.search-box {
  height: 50px;
  width: 280px;
  border: 1px solid #DCDCDC;
  background-color: #F5F5F5;
  margin: 10px auto;
  border-radius: 10px;
  box-shadow: 2px 2px 1px #D3D3D3, -2px 2px 1px #D3D3D3;
  display: flex;
  padding: 9px;
  gap: 10px;
}

#input-search {
  border: 1px solid #DCDCDC;
  border-radius: 5px;
  text-indent: 3px;
  flex: 1 1 auto;
}

#btn {
  font-size: 12px;
  font-weight: 600;
  color: #fff;
  border-radius: 5px;
  border: 1px solid #008080;
  display: inline-block;
  padding-left: 1.2em;
  padding-right: 1.2em;
  background-image: linear-gradient(#20B2AA, #5F9EA0);
}
<div class="search-box">
  <input type="text" id="input-search" placeholder="search">
  <input type="button" value="GO" id="btn">
</div>

关于html - 为什么我的按钮标签不能像其他输入标签一样居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72593935/

相关文章:

javascript - 如何使用 Mechanize with Selenium 在 python 中单击按钮

html - Bootstrap 中的 100% 高度 div

java - 在 JSP 中创建选项卡

javascript - 显示/隐藏 div 下拉菜单

html - 大纲变成内联 - 我认为是 Chrome 错误

javascript - 防止在 HTML 中缓存某些 JS 文件。 (电话间隙)

html - 悬停时的通用兄弟选择器

html - 基础 4 中的包装器 div

php - htaccess 和两个 post 参数

javascript - 通过javascript链接href以单击选项卡中的按钮