我有一个包含所有这些元素的div:
<div id="container">
<input type="text" />
<div id="click"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
我想以某种方式设置样式,以便divwith id=click将位于input元素的右角.
我应用于文本的CSS是:
text-align: left;
width: 400px;
Run Code Online (Sandbox Code Playgroud)
在我的clickdiv中我有:
width: 30px;
height: 30px;
Run Code Online (Sandbox Code Playgroud)
基本上它看起来像这样:
[____INPUT___________________{DIV}]
Run Code Online (Sandbox Code Playgroud)
我不知道如何设置div的样式以便将它放在输入元素上.现在它直接躺在它的右边.
您可以使用position:absolute的div#click控制,但确保#container了position:relative
#container{
margin:0 auto;
width:300px;
position:relative;
outline:2px solid #060;
}
#container #click{
width:40px;
line-height:36px;
background-color:#090;
color:white;
text-align:center;
position:absolute;
right:0;
top:0;
}
#container input[type="text"]{
width:298px;
height:30px;
padding:right:40px;
}Run Code Online (Sandbox Code Playgroud)
<br>
<div id="container">
<input type="text" />
<div id="click">GO</div>
</div>Run Code Online (Sandbox Code Playgroud)
* {
box-sizing: border-box; /* gives padding and border from inside */
}
#container {
position: relative; /* for absolute child element */
display: inline-block; /* to take the width of the input */
}
input {
text-align: left;
width: 400px;
height: 30px; /* added to match the height of the #click div */
outline: 0; /* to remove outline when focused */
}
#click {
width: 30px;
height: 30px;
position: absolute; /* to align it to right and positon it over the input */
top: 0;
right: 0;
background: lightgrey;
}Run Code Online (Sandbox Code Playgroud)
<div id="container">
<input type="text" />
<div id="click"></div>
</div>Run Code Online (Sandbox Code Playgroud)