将 h2 垂直底部对齐
Align h2 vertically bottom
我正在从基础开始学习 html 和 css。我正在编写一些简单的网站。我在将 h2 垂直对齐到底部时遇到问题。
我想要实现的是带有背景图像(高度 240 像素)的页眉,其中有一个带有文本的黑条:"love cucumbers" 与它的底部对齐。
我尝试了多种解决方案,但都没有用。
文本应该是“你应该喜欢黄瓜的 9 个理由”。
这里是相关的 css 和 html:
.body {
clear: both;
width: 800px;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
}
header {
height: 240px;
margin: 1em auto 3em;
background-image: url("https://media.mercola.com/ImageServer/Public/2014/August/cucumber-health-benefits-fb.jpg");
background-size: cover;
text-align: center;
font-family: 'Lobster', Georgia, Times, serif;
}
#caption {
width: 100%;
height: 70px;
line-height: 70px;
background: #191919;
color: #ffffff;
font-size: 2em;
letter-spacing: 1px;
border-bottom-left-radius: 15px;
-webkit-border-bottom-left-radius: 15px;
-moz-border-radius-bottomleft: 15px;
border-bottom-right-radius: 15px;
-webkit-border-bottom-right-radius: 15px;
-moz-border-radius-bottomright: 15px;
<header class="body">
<h2 id="caption">Love cucumbers <3</h2>
</header>
您必须像这样将标题的 line-height 与 header 高度相同。
#caption {
width: 100%;
height: 100%;
line-height: 240px;
}
您可以使用 header 的相对位置和 h2 的绝对位置。请看下面更新的代码
.body {
clear: both;
width: 800px;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
}
header {
height: 240px;
margin: 1em auto 3em;
background-image: url("https://media.mercola.com/ImageServer/Public/2014/August/cucumber-health-benefits-fb.jpg");
background-size: cover;
text-align: center;
font-family: 'Lobster', Georgia, Times, serif;
position:relative;
}
#caption {
width: 100%;
height: 70px;
line-height: 70px;
background: #191919;
color: #ffffff;
font-size: 2em;
letter-spacing: 1px;
position:absolute;
bottom:0px;
margin-bottom:0px;
border-bottom-left-radius: 15px;
-webkit-border-bottom-left-radius: 15px;
-moz-border-radius-bottomleft: 15px;
border-bottom-right-radius: 15px;
-webkit-border-bottom-right-radius: 15px;
-moz-border-radius-bottomright: 15px; }
<header class="body">
<h2 id="caption">Love cucumbers <3</h2>
</header>
这里是working fiddle.
我基本上将这些属性添加到 .body class
display: table;
position: relative;
height: 200px; /* Edit this with as you wish */
然后 #caption id
display: table-cell;
vertical-align: bottom;
PS:背景图像 属性 不适用于 table-cell
元素,因此我将背景图像应用于 body tag
。
我正在从基础开始学习 html 和 css。我正在编写一些简单的网站。我在将 h2 垂直对齐到底部时遇到问题。
我想要实现的是带有背景图像(高度 240 像素)的页眉,其中有一个带有文本的黑条:"love cucumbers" 与它的底部对齐。 我尝试了多种解决方案,但都没有用。
文本应该是“你应该喜欢黄瓜的 9 个理由”。
这里是相关的 css 和 html:
.body {
clear: both;
width: 800px;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
}
header {
height: 240px;
margin: 1em auto 3em;
background-image: url("https://media.mercola.com/ImageServer/Public/2014/August/cucumber-health-benefits-fb.jpg");
background-size: cover;
text-align: center;
font-family: 'Lobster', Georgia, Times, serif;
}
#caption {
width: 100%;
height: 70px;
line-height: 70px;
background: #191919;
color: #ffffff;
font-size: 2em;
letter-spacing: 1px;
border-bottom-left-radius: 15px;
-webkit-border-bottom-left-radius: 15px;
-moz-border-radius-bottomleft: 15px;
border-bottom-right-radius: 15px;
-webkit-border-bottom-right-radius: 15px;
-moz-border-radius-bottomright: 15px;
<header class="body">
<h2 id="caption">Love cucumbers <3</h2>
</header>
您必须像这样将标题的 line-height 与 header 高度相同。
#caption {
width: 100%;
height: 100%;
line-height: 240px;
}
您可以使用 header 的相对位置和 h2 的绝对位置。请看下面更新的代码
.body {
clear: both;
width: 800px;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
}
header {
height: 240px;
margin: 1em auto 3em;
background-image: url("https://media.mercola.com/ImageServer/Public/2014/August/cucumber-health-benefits-fb.jpg");
background-size: cover;
text-align: center;
font-family: 'Lobster', Georgia, Times, serif;
position:relative;
}
#caption {
width: 100%;
height: 70px;
line-height: 70px;
background: #191919;
color: #ffffff;
font-size: 2em;
letter-spacing: 1px;
position:absolute;
bottom:0px;
margin-bottom:0px;
border-bottom-left-radius: 15px;
-webkit-border-bottom-left-radius: 15px;
-moz-border-radius-bottomleft: 15px;
border-bottom-right-radius: 15px;
-webkit-border-bottom-right-radius: 15px;
-moz-border-radius-bottomright: 15px; }
<header class="body">
<h2 id="caption">Love cucumbers <3</h2>
</header>
这里是working fiddle.
我基本上将这些属性添加到 .body class
display: table;
position: relative;
height: 200px; /* Edit this with as you wish */
然后 #caption id
display: table-cell;
vertical-align: bottom;
PS:背景图像 属性 不适用于 table-cell
元素,因此我将背景图像应用于 body tag
。