我想在文本输入框中放置一张图片,并尝试通过将图像标签嵌套在输入标签内,对输入使用相对定位,对图像使用绝对定位,并将图像的“right”和“top”值设置为0来定位图像。 我认为这会将图像放在文本框的右侧。但是它实际上将图像放在了网页的右侧。我不确定为什么会出现这种情况,我通过将“top”和“right”值更改为定位到所需位置来找到了解决方法,但似乎第一种方式应该可以工作,有人能解释一下为什么不行吗?
以下是文本输入框和图像的HTML代码:
这是我认为会起作用的CSS。
以下是文本输入框和图像的HTML代码:
<input = "text" id="searchbar">
<a href="#voice"> <img src ="microphone.png" id="voicebutton"/> </a>
</input>
这是我认为会起作用的CSS。
#searchbar{
border: 0.6px solid #dbdbdb;
display:block;
margin-left:auto;
margin-right:auto;
width:600px;
height:37.5px;
position:relative;
}
#voicebutton{
width:16px;
height:23px;
position:absolute;
right:0;
top:0;
}
这是有效的CSS代码。
#searchbar{
border: 0.6px solid #dbdbdb;
display:block;
margin-left:auto;
margin-right:auto;
width:600px;
height:37.5px;
position:relative;
}
#voicebutton{
width:16px;
height:23px;
position:absolute;
right:395;
top:207;
}