sol*_*yon 2 html css position css-position
目标:将button元素固定到元素的底部main.我试着用相对定位定位它的容器,所以它粘在底部:
/* POSITIONING NOT WORKING. I WANT THIS DIV FIXED TO BOTTOM OF PARENT */
.wrapper:nth-child( 4 ) {
background-color: #bbb;
position: relative;
bottom: 0;
}
Run Code Online (Sandbox Code Playgroud)
这根本不是移动.wrapperdiv.想法?
@import url( "https://necolas.github.io/normalize.css/latest/normalize.css" );
* {
box-sizing: border-box;
}
main {
background-color: #eee;
}
main, input {
padding: 2%;
}
main input {
width: 100%;
margin: 5% 0;
border: 0;
}
.clr-fix::after {
content: "";
display: block;
clear: both;
}
.wrapper {
width: 23%;
margin: 1%;
padding: 2%;
background-color: #ddd;
float: left;
}
/* POSITIONING NOT WORKING. I WANT THIS DIV FIXED TO BOTTOM OF PARENT */
.wrapper:nth-child( 4 ) {
background-color: #bbb;
position: relative;
bottom: 0;
}Run Code Online (Sandbox Code Playgroud)
<main class="clr-fix">
<div class="wrapper">
<input type="text" value="position:bottom">
<input type="text">
<input type="text">
<input type="text">
</div>
<div class="wrapper">
<input type="text">
<input type="text" value="Isn't working">
<input type="text">
<input type="text">
</div>
<div class="wrapper">
<input type="text">
<input type="text">
<input type="text" value="On 'A button'">
<input type="text">
</div>
<div class="wrapper">
<button>A button</button>
</div>
</main>Run Code Online (Sandbox Code Playgroud)
小智 9
相对定位是相对于元素已经定位的点的变化.因此,如果position:relative,bottom:0(或top:0,left:0,right:0等)表示将其保留在当前位置.
如果要将其定位到元素的底部,则需要将父元素位置设置为relative,并将要固定的元素固定到底部位置:absolute(底部:0).绝对定位的元素将跳出DOM流,而不是相对于它最接近的相对父元素.
基本上你想要:
.wrapper {
position: relative;
}
.wrapper:nth-child(4){
position: absolute;
bottom: 0;
}
Run Code Online (Sandbox Code Playgroud)