简体   繁体   English

如何使div中的文本位于div的底部?

[英]How do you make text in a div sit at the bottom of the div?

I have been trying to place text over a background image for a header element (id="blogheader") on my page but I can't seem to get the text to sit at the bottom of the div. 我一直在尝试在页面上的标题元素(id =“ blogheader”)的背景图像上放置文本,但似乎无法将文本放在div的底部。 I have tried a couple things including 'vertical-align: bottom' and vertical-align: text-bottom'. 我已经尝试了几件事,包括“ vertical-align:bottom”和vertical-align:text-bottom'。

I figure there is something I'm just missing or an easier way to get the result I want but I'm still learning how to code properly! 我发现有一些我只是想缺少的东西,或者是一种获得所需结果的简便方法,但是我仍在学习如何正确编写代码! :) :)

Here's the HTML: 这是HTML:

<main id="blogpage">
            <h2 id="blogheader">The Blog!</h2>

            <div id="popblogs">Most Popular Blogs</div>
            <div id="recentblogs">Most Recent Blogs</div>

            <section>
                <article id="art1">
                    <h3>Topic One</h3>
                </article>
            </section>
</main>

And the CSS: 和CSS:

#blogheader         {clear: both;
                    text-align: center;
                    background-image: url(Photos/image.jpg);
                    background-repeat: no-repeat;
                    background-size: 100%;
                    margin: 0;
                    font-size: 5em;
                    color: #00c3d5;
                    height: 400px;
                    vertical-align: text-bottom;}

Also, any code criticism would be greatly appreciated since I am in a course currently learning all this stuff and it doesn't hurt to have some expert advice! 另外,任何代码批评都将不胜感激,因为我正在学习一门有关所有这些知识的课程,因此获得一些专家建议也无害!

To put the text at the bottom of the element, you can assign display: flex; 要将文本放在元素的底部,可以分配display: flex; to #blogheader and then justify-content: center; #blogheader ,然后justify-content: center; to center the child text horizontally, and align-items: flex-end; 使子文本水平居中,并align-items: flex-end; to push the child text to the flex-end/bottom. 将子文本推送到flex-end / bottom。

 #blogheader { clear: both; text-align: center; background-image: url(Photos/image.jpg); background-repeat: no-repeat; background-size: 100%; margin: 0; font-size: 5em; color: #00c3d5; height: 400px; vertical-align: text-bottom; display: flex; justify-content: center; align-items: flex-end; } 
 <main id="blogpage"> <h2 id="blogheader">The Blog!</h2> <div id="popblogs">Most Popular Blogs</div> <div id="recentblogs">Most Recent Blogs</div> <section> <article id="art1"> <h3>Topic One</h3> </article> </section> </main> 

You may use display:table-cell or flex: 您可以使用display:table-cell或flex:

  • flex 柔性

 #blogheader { clear: both; text-align: center; background-image: url(Photos/image.jpg); background-repeat: no-repeat; background-size: 100%; margin: 0; font-size: 5em; color: #00c3d5; /* send content down */ height: 400px;display:flex; flex-flow:column; justify-content:flex-end; } 
 <main id="blogpage"> <h2 id="blogheader">The Blog!</h2> <div id="popblogs">Most Popular Blogs</div> <div id="recentblogs">Most Recent Blogs</div> <section> <article id="art1"> <h3>Topic One</h3> </article> </section> </main> 

  • table-cell 表单元格

 #blogheader { clear: both; text-align: center; background-image: url(Photos/image.jpg); background-repeat: no-repeat; background-size: 100%; margin: 0; font-size: 5em; color: #00c3d5; height: 400px; /* send content down */ display:table-cell; vertical-align:bottom; } 
 <main id="blogpage"> <h2 id="blogheader">The Blog!</h2> <div id="popblogs">Most Popular Blogs</div> <div id="recentblogs">Most Recent Blogs</div> <section> <article id="art1"> <h3>Topic One</h3> </article> </section> </main> 

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

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