简体   繁体   English

如何在CSS中的div中垂直对齐元素?

[英]How do I vertically align elements within a div in CSS?

I have the following bit on my site... 我的网站上有以下内容... 当前结果

This is the HTML output for the previous bit: 这是上一位的HTML输出:

<div class="featured" style="background: url(http://localhost/basecommand/attachments/8d7fd632111cf768ec62d5ad084d8059.jpg);">

                    <h1>This Is a Test Article</h1>

                    <div class="arrow left">

                        <i class="fi-arrow-left"></i>

                    </div>

                    <div class="arrow right">

                        <i class="fi-arrow-right"></i>

                    </div>

                    <div class="clear"></div>

                    <div class="info">

                        <p>https://www.bungie.net/pubassets/1319/Destiny_31.png

Lorem ipsum dolor sit amet, inani accusata et duo, ad sit veniam interpretaris. Sea scripta nostrum ex, liber fastidii ea duo. Id vim nobis option contentiones, mea probatus praesent ut. Sea ex ...</p>

                        <h2><a href="http://localhost/basecommand/index.php/articles/This-Is-a-Test-Article/1">Read More</a></h2>

                    </div>

                </div>

This is the CSS I have so far 这是我到目前为止的CSS

.featured {
    width: 620px;
    height: 349px;
    border: 1px solid #000;
    margin: 0px;
}

.featured h1 {
    font-size: 18px;
    color: #fff;
    background: rgba(183, 31, 47, 0.5);
    padding: 10px;
    margin: 15px 0px;
    width: 50%
}

.featured .arrow {
    font-size: 72px;
    font-weight: bold;
    padding: 10px;
    color: rgba(255, 255, 255, 0.25);
    background-color: rgba(0, 0, 0, 0.25);
    border: 1px solid rgba(255, 255, 255, 0.25);
}

.featured .info {
    color: #fff;
    background-color: rgba(0, 0, 0, 0.5);
    border-top: 1px solid rgba(255, 255, 255, 0.5);
    bottom: 0;
    left: 0;
    position: relative;
    vertical-align: bottom;
}

.featured p {
    padding: 10px;
}

.featured a {
    color: #fff;
}

.featured h2 {
    font-size: 12px;
    font-weight: bold;
    text-align: right;
    padding: 10px;
}

What modifications can I make to my css so that the description box("info" class) is aligned to the bottom of the parent div "featured" class? 我可以对CSS进行哪些修改,以使描述框(“ info”类)与父div“ featured”类的底部对齐? I would also like the arrows to be aligned in the middle, if possible. 如果可能的话,我也希望箭头在中间对齐。 How can I do this? 我怎样才能做到这一点?

Thanks in advance! 提前致谢!

To get the "featured" class on the bottom, you can use position. 要使“特色”类位于底部,您可以使用position。

.featured {
   position: relative;
}

.featured .info {
   position: absolute;
   bottom: 0px;
   left: 0px;
   right: 0px;
}

As for aligning the arrows to the center. 至于将箭头对准中心。 This works by positioning the arrow at the 50% mark, which is half. 通过将箭头定位在一半的50%标记处可以起作用。 But we then move the div up by half of the height. 但是,然后将div向上移动一半的高度。 I specified the height of 20px, and set the div to move up 10px with the negative margin-top. 我指定了20px的高度,并将div设置为以负的margin-top向上移动10px。

.arrow {
    position: absolute;
    top: 50%;
    height: 20px;
    margin-top: -10px;
}

For more information about Vertical-Align, please look at these resources. 有关Vertical-Align的更多信息,请查看这些资源。

https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align

http://css-tricks.com/almanac/properties/v/vertical-align/ http://css-tricks.com/almanac/properties/v/vertical-align/

http://phrogz.net/CSS/vertical-align/ http://phrogz.net/CSS/vertical-align/

Please refer JSFiddle 请参考JSFiddle

Use position absolute as below for your .info class and use any position other then static in . 对.info类使用以下绝对位置,并在中使用除static以外的任何位置。 featured class: 特色班:

.featured .info {
    color: #fff;
    background-color: rgba(0, 0, 0, 0.5);
    border-top: 1px solid rgba(255, 255, 255, 0.5);

    left: 0;
    position: absolute;
    bottom: 0;


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM