html - 为什么我的搜索按钮在我的搜索栏下方
问题描述
我正在为我的一个想法制作一个搜索栏,但我对 CSS 还是很陌生。我的搜索按钮在我的文本栏和悬停按钮下方,我无法找出解决方案或找到原因!
我尝试更改边距、填充和顶部/底部百分比,但无济于事
HTML
<div class="search-box">
<div class="location">
<input name="location" type="text"
placeholder="location"/>
</div>
<div class="dropdwn">
<button class="dropbtn">dropdown</button>
<div class="dropdwn-content">
<img src="../Media/sunny.png" alt="Sunny"
width="50" height="50">
<img src="../Media/partly_cloudy.png"
alt="partly_cloudy" width="50"height="50">
<img src="../Media/rain_s_cloudy.png" alt="rain_s_cloudy"
width="50"height="50">
</div>
</div>
<div class="searchbtn">
<button type="button">Search</button>
</div>
</div>
CSS
.search-box
{
width: 40%;
box-sizing: border-box;
border-radius: 3px;
border-style: solid;
position: absolute;
left: 35%;
top: 20%;
}
.location input
{
box-sizing: inherit;
width: 50%;
float:left;
}
.dropdwn
{
position: relative;
display: inline-block;
}
.searchbtn button
{
float: right;
}
.dropdwn-content
{
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
我希望我的 searchbtn 位于框的右上角而不是右下角
解决方案
使用display: inline-block
时,空格很重要。这可能会使%
基于 - 的布局和缩进有点混乱(见下文)。
我看到的另一个问题是关于设置input
宽度的规则,因为这会影响子元素——告诉它填充 50% 的.location
而不是.search-box
.
我已经简化了您的代码以突出显示这些更改,并添加了一些 % 以便输入和按钮将拉伸(或缩小)以填充其父代,而后者将依次填充.search-box
. float: right
元素上的简单.searchbtn
现在将其发送到右侧,没有新行。
* {
box-sizing: border-box;
}
.search-box
{
box-sizing: border-box;
border-radius: 3px;
border-style: solid;
left: 35%;
top: 20%;
width: 60%;
}
.search-box > div {
display: inline-block;
}
.search-box button, input {
width: 100%;
}
.location {
width: 50%;
}
.searchbtn {
float: right;
}
<div class="search-box">
<div class="location">
<input name="location" type="text" placeholder="location"/>
</div><div class="dropdwn">
<button class="dropbtn">dropdown</button>
</div><div class="searchbtn">
<button type="button">Search</button>
</div>
</div>
推荐阅读
- extentreports - 范围报告:无法为多个套件生成报告。
- machine-learning - 高成本的后果是什么?
- geometry - 在矩形域中生成二维七边形有限元网格
- c++ - 具有内存顺序的原子加载存储
- javascript - 使用 jquery替换我的链接的 href
- angular - 角色保护有时允许进入本地主机上的安全组件
- kubernetes-helm - helm 未加载 setenv 的值
- javascript - Package-lock.json - 需要 vs 依赖项
- node.js - nodejs 找不到我自己的模块关联到父路径
- android - gradle 升级到 3.0.0+ 后子模块资源不渲染