【问题标题】:position search bar 100% width in CSSCSS中的位置搜索栏100%宽度
【发布时间】:2019-10-25 17:17:49
【问题描述】:

.brand-container3 {
  background-image: url("media/wysiwyg/576_1.JPG");
  background-size: cover;
  position: relative;
  height: 200px;
}

.search-container {
  text-align: center;
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  display: inline-flex;
}

.searchTerm {
  border: 3px solid #556B2F;
  border-right: none;
  padding: 5px;
  height: 60px;
  border-radius: 5px 0 0 5px;
  outline: none;
  color: #9DBFAC;
  font-size: 30px;
  cursor: pointer;
}

.searchTerm:focus{
  color: #12291F;
}

.centersearchButton {
  width: 70px;
  height: 60px;
  border: 1px solid #556B2F;
  background: #556B2F;
  text-align: center;
  color: #fff;
  border-radius: 0 5px 5px 0;
  cursor: pointer;
  font-size: 35px;
}
    
.centersearchButton:hover {
  background: #6B8E23;  
  border: 1px solid #6B8E23;
}

.centersearchButton:active {
  background: #6B8E23;  
  border: 1px solid #6B8E23;
  box-shadow: inset 0 0 5px #000000;  
}

.centersearchButton:focus, .loader:focus {
    outline: 0;
}    
    
/*Resize the wrap to see the search bar change!*/
.wrap{
  position: relative;
  top: 50%;
  left: 50%;
}

.loader {
  border: 10px solid #FFFFFF;
  border-top: 10px solid #ffb677; /* #3498db */
  border-radius: 50%;
  width: 25px;
  height: 25px;
  animation: spin 0.5s linear infinite;
  margin: 0 auto; 
  display: none;   
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
} 

input {
font-family: 'Open Sans', sans-serif;
}
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<div class="brand-container3">
  <form id="search_mini_form" action="https://www.cannockgates.co.uk/catalogsearch/result/" method="get" class="wrap" onsubmit="return validateCenterSearch()">
    <div class="search-container">
      <input type="search" class="searchTerm" placeholder="Search Products..." id="centersearch" name="q" maxlength="128" value="" autocomplete="off" spellcheck="true" title="Search by Keyword or Product Code">
      <button type="submit" class="centersearchButton" title="Search"><i class="fa fa-search fa-fw"></i><div class="loader"></div>
</button>
    </div>
  </form>
</div>

亲爱的stackoverflow,

我试图让搜索栏填满屏幕的宽度。搜索栏当前位于背景图像 div 容器的顶部,我希望搜索栏位于父 div 容器的中心。

我试过 width: 100%;弄乱了填充和边距。仍然无法得到我想要的结果。

也许这是一个简单的修复,但是 atm.搜索栏的大小是固定的。

【问题讨论】:

  • 你应该添加 ..search-container width:100%.. 因为 flex 总是得到自动宽度
  • .search-container input[type="search"] { width: calc(100vw - 80px); } 这也行,该死的 CSS 对我来说仍然非常困难 lmao
  • 为什么要使用 calc 方法,请解释更多。 @UrbanOzzy
  • 老实说,我不太确定自己,我可以用上面的代码弄乱搜索容器。或者我必须为 .searchterm 和 .search-container 设置 width: 100%
  • 你能检查一下你的输出吗.. 左侧搜索栏有点隐藏这是计算问题.. 检查并告诉我

标签: html css


【解决方案1】:

你应该添加宽度:100%;对于下面的课程。因为 flex 和 inputs 总是得到自动宽度

CSS

.searchTerm {
  border: 3px solid #556B2F;
  border-right: none;
  padding: 5px;
  height: 60px;
  border-radius: 5px 0 0 5px;
  outline: none;
  color: #9DBFAC;
  font-size: 30px;
  cursor: pointer;
  width:100%;
}
.search-container {
  text-align: center;
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  display: inline-flex;
  width:100%;
}

【讨论】:

  • 哇,我不知道这是一个如此简单的修复!谢谢大家的帮助。我认为问题在于,因为我正在单独更改每个班级。当它不起作用时,我只是 ctrl-z,然后尝试更改另一个类。下次我会记住的
  • 是的,很酷...您应该始终将宽度 100% 用于输入类型
【解决方案2】:

请查看更新后的代码。为输入指定宽度。

.brand-container3 {
  background-image: url("media/wysiwyg/576_1.JPG");
  background-size: cover;
  position: relative;
  height: 200px;
}

.search-container {
  text-align: center;
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  display: inline-flex;
}

.search-container input[type="search"] {
width: calc(100vw - 80px);
}

.searchTerm {
  border: 3px solid #556B2F;
  border-right: none;
  padding: 5px;
  height: 60px;
  border-radius: 5px 0 0 5px;
  outline: none;
  color: #9DBFAC;
  font-size: 30px;
  cursor: pointer;
}

.searchTerm:focus{
  color: #12291F;
}

.centersearchButton {
  width: 70px;
  height: 60px;
  border: 1px solid #556B2F;
  background: #556B2F;
  text-align: center;
  color: #fff;
  border-radius: 0 5px 5px 0;
  cursor: pointer;
  font-size: 35px;
}
    
.centersearchButton:hover {
  background: #6B8E23;  
  border: 1px solid #6B8E23;
}

.centersearchButton:active {
  background: #6B8E23;  
  border: 1px solid #6B8E23;
  box-shadow: inset 0 0 5px #000000;  
}

.centersearchButton:focus, .loader:focus {
    outline: 0;
}    
    
/*Resize the wrap to see the search bar change!*/
.wrap{
  position: relative;
  top: 50%;
  left: 50%;
}

.loader {
  border: 10px solid #FFFFFF;
  border-top: 10px solid #ffb677; /* #3498db */
  border-radius: 50%;
  width: 25px;
  height: 25px;
  animation: spin 0.5s linear infinite;
  margin: 0 auto; 
  display: none;   
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
} 

input {
font-family: 'Open Sans', sans-serif;
}
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<div class="brand-container3">
  <form id="search_mini_form" action="https://www.cannockgates.co.uk/catalogsearch/result/" method="get" class="wrap" onsubmit="return validateCenterSearch()">
    <div class="search-container">
      <input type="search" class="searchTerm" placeholder="Search Products..." id="centersearch" name="q" maxlength="128" value="" autocomplete="off" spellcheck="true" title="Search by Keyword or Product Code">
      <button type="submit" class="centersearchButton" title="Search"><i class="fa fa-search fa-fw"></i><div class="loader"></div>
</button>
    </div>
  </form>
</div>

【讨论】:

  • 哇,这也很好用,你能解释一下.search-container input[type="search"] { width: calc(80vw - 80px); } 我知道我们弄乱了类 .search 容器,但它后面的代码是什么。对不起,我是新手x
猜你喜欢
  • 2021-09-15
  • 2013-08-28
  • 2011-12-20
  • 2018-01-02
  • 2014-11-18
  • 2020-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多