可以通过以下步骤实现在搜索框中水平居中:

  1. 在搜索框的父元素(通常是一个表单或一个div)中设置display:flex; 使其成为一个弹性容器。
  2. 在搜索框的父元素中使用justify-content:center; 属性来使搜索框水平居中。
  3. 如果搜索框的宽度不是100%,可以设置搜索框的宽度为100%或使用flex-grow:1; 来填充父元素的剩余空间。

以下是一个示例代码:

HTML:

<form class="search-form">
  <input type="text" placeholder="Search">
  <button type="submit">Go</button>
</form>

CSS:

.search-form {
  display: flex;
  justify-content: center;
}

.search-form input[type="text"] {
  width: 100%;
  max-width: 300px;
  padding: 10px;
  border: 1px solid #ccc;
}

.search-form button {
  padding: 10px;
  border: none;
  background-color: #007bff;
  color: #fff;
}

在上面的示例中,搜索框和按钮被包含在一个表单元素中,并且表单元素被设置为一个flex弹性容器。搜索框被设置为宽度为100%,最大宽度为300px,并且具有一些基本样式。按钮具有一些基本样式,并且不是必需的。通过使用justify-content:center; 属性,搜索框在其父元素中水平居中。

如何在搜素框中水平居中

原文地址: https://www.cveoy.top/t/topic/bSHs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录