如何将div粘贴到另一个div的底部?

Dee*_*Dee 10 html css positioning

好的,所以这是父div:

#left {
    width: 50%;
    height: 100%;
    background-color: #8FD1FE;
    float: left;
    opacity:0.75;
    filter:alpha(opacity=75);
    -webkit-transition: all .45s ease;  
    -moz-transition: all .45s ease; 
    transition: all .45s ease; }
Run Code Online (Sandbox Code Playgroud)

这就是div里面的div:

#reasons {
background-image:url('arrow1.png');
background-repeat: no-repeat;
height: 94px;
width: 400px;
margin: 0 auto 0 auto; }
Run Code Online (Sandbox Code Playgroud)

我已经尝试了一些不同的方法,但我似乎无法保持第二个div居中并坚持到第一个div的底部.

ick*_*fay 31

首先,使外部div布局父级:

#left {
     /* ... */
     position: relative; /* anything but static */
     /* ... */
}
Run Code Online (Sandbox Code Playgroud)

现在让我们修复内部div到底部:

#reasons {
    /* ... */
    position: absolute;
    bottom: 0;
    /* ... */
}
Run Code Online (Sandbox Code Playgroud)

现在它固定在底部,但我们需要将其集中在一起:

#reasons {
    /* ... */
    left: 50%;
    margin: 0 0 0 -200px; /* 200px is half of the width */
    /* ... */
}
Run Code Online (Sandbox Code Playgroud)

查看JSFiddle的演示.

  • 您还可以考虑使用`transform:translateX(-50%)`来居中div,这样做的好处(如果我没记错的话)就是你不需要在孩子身上加上硬编码的宽度元素(在这种情况下为400px) (3认同)