【发布时间】:2016-02-19 16:17:13
【问题描述】:
我正在尝试为 wordpress 博客创建模板。
问题是我试图将图像作为全屏背景,但没有使用背景 css 函数 background-image {},因为我无法在CSS 代码。
所以,我将图像显示为 position: fixed; z-index:1。 我用 width: 100%; 和 height: auto; 完成代码,以使图像适合屏幕尺寸(如果屏幕和图片的比例不一样……)。
然后我显示可以具有动态高度的页脚。对于这种情况,我为整个页面使用了一个 flexbox。
我的担忧是: 背景图像可以有不同的比例,我希望它无论如何都能填满视口,但不要溢出它。
真正的问题是,因为我有一个 position: fixed 我无法获得图像的 height,这意味着我无法动态推动页脚。 p>
一种解决方案是复制图像 div 并为其赋予 visibility: hidden 以便我获得背景图像的确切大小并推动页脚。但我认为这个解决方案不优雅。而且我确信它必须使用资源......好吧,即使它没有出现,我也对复制内容感到不满意。
这里是问题的截图和视频,后面是一些代码:
HTML
<?php get_header(); ?>
<div id="single.php">
<?php include("menu.php"); ?>
<section id="content">
<div class="row">
<div class="col">
<?php if(have_posts()) :?>
<?php while(have_posts()) : the_post(); ?>
<div class="postThumbnail">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
</div>
<div class="col-1">
<div class="col-1-1">
<div id="postMetadata">
<div id="postTitle">
<h2>
<?php the_title(); ?>
</h2>
</div>
<div id="postCategory">
<p>
<?php
$category = get_the_category();
echo $category[0]->slug;
?>
</p>
</div>
<div id="postDate">
<p>
<?php the_time("d F") ?>
</p>
</div>
</div>
<div id="postContent" >
<?php the_content(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
</section>
</div>
<?php get_footer(); ?>
CSS
/* ----- ----- ----- ----- ----- */
/* html */
html {
width: 100%;
height: 100%;
}
/* ----- ----- ----- ----- ----- */
/* body */
body {
display: flex;
flex-direction: column;
width: 100%;
min-width: 640px;
height: 100%;
font-family: Open Sans, Arial;
font-size: 1em;
line-height: 1em;
}
/* ----- ----- ----- ----- ----- */
/* header */
header {
position: fixed;
z-index: 999; /* do not remove = header always on top of content */
width: 100%;
height: 48px;
font-size: 1.2em;
color: rgb(255, 255, 255);
background: rgb(0, 0, 0);
transition: all 1s ease;
box-shadow: rgb(0, 0, 0) 0px 0px 8px;
}
header:hover {
transition: all 1s ease;
box-shadow: rgb(40, 40, 40) 0px 0px 12px;
}
#headerMenu {
position: relative;
display: block;
height: 100%;
line-height: 48px;
margin: 0 32px 0 32px;
}
#siteLogo {
position: absolute;
left: 0;
top: 0;
line-height: 48px;
}
#siteLogo a img {
display: inline-block;
vertical-align: middle;
width: 24px;
height: 24px;
}
#siteLogo a h1 {
display: inline-block;
vertical-align: middle;
letter-spacing: 0.3em;
color: rgb(255,255,255);
}
#searchEngine {
position: absolute;
top: 8px;
right: 0;
}
#searchForm input {
position: relative;
width: 164px;
height: 32px;
display: block;
line-height: 32px;
vertical-align: middle;
margin: 0 32px 0 0;
padding: 0 16px 0 16px;
font-size: 1em;
color: rgb(0, 0, 0);
background: rgb(255, 255, 255);
border: none;
transition: width 0.5s ease;
}
#searchForm input:focus {
width: 226px;
}
#index\.php #searchForm input:focus {
width: 226px;
}
#search\.php #searchForm input {
transition: width 0.5s ease;
animation-name: searchFormResize;
animation-duration: 0.5s;
}
@keyframes searchFormResize {
from {
width: 226px;
}
to {
width: 164px;
}
}
/* ----- ----- ----- ----- ----- */
/* footer */
footer {
position: relative;
z-index: 998;
}
footer, footer a {
color: rgb(255, 255, 255);
line-height: 2em;
}
#twitter {
background: rgb(99, 185, 251);
background: linear-gradient(to bottom, rgb(99, 185, 251)0%,rgb(56, 110, 151) 100%);
transition: all 1s ease;
box-shadow: rgb(99, 185, 251) 0 0 4px;
}
#twitter:hover {
transition: all 1s ease;
box-shadow: rgb(99, 185, 251) 0 0 8px;
}
#twitter .twitterTimeline {
width: 520px;
margin: 0 auto;
padding: 32px 0 24px 0;
}
#twitter #twitterIcon {
display: block;
width: 32px;
height: 32px;
margin: 0 auto 16px auto;
}
#twitter #lastTweet {
min-height: 90px;
}
#infoFooter {
padding: 16px 0 16px 0;
background: rgb(40, 40, 40);
background: linear-gradient(to bottom, rgb(40, 40, 40) 0%,rgb(0, 0, 0) 100%);
transition: all 1s ease;
box-shadow: rgb(40, 40, 40) 0 0 8px;
}
#infoFooter:hover {
transition: all 1s ease;
box-shadow: rgb(40, 40, 40) 0 0 12px;
}
#siteMap {
font-size: 0.8em;
}
#siteMap li a {
color: rgb(150, 150, 150);
}
#siteMap h3, #siteMap #iconHeart, #siteMap #iconContact, #siteMap #iconCategories {
display: inline-block;
vertical-align: middle;
}
#siteMap #iconHeart, #siteMap #iconContact, #siteMap #iconCategories {
width: 16px;
height: 16px;
position: relative;
left: 8px;
top: 1px;
}
#copyright {
padding: 16px 0 0 0;
font-size: 0.7em;
}
#copyright p {
color: rgb(75, 75, 75);
text-align: center;
}
#copyright a {
color: rgb(100, 100, 100);
}
#copyright #siteName {
letter-spacing: 0.3em;
}
/* ----- ----- ----- ----- ----- */
/* misc */
#index\.php,
#single\.php,
#page\.php,
#search\.php,
#\404\.php {
flex: 1;
}
#content {
position: relative;
margin: 48px 0 0 0;
z-index: 997;
}
.row {
width: 100%;
}
.col { /* /!\ must not have width setting*/
display: block;
max-width: 960px;
height: 100%;
margin: 0 auto;
padding: 0 16px 0 16px;
}
.col-1-1 {
display: inline-block;
width: calc(100% - 32px);
margin: 0 16px 0 16px;
}
.col-1-2 {
display: inline-block;
vertical-align: top;
width: calc(50% - 32px);
margin: 0 16px 0 16px;
}
.col-1-3 {
display: inline-block;
vertical-align: top;
width: calc((100% / 3) - 32px);
margin: 0 16px 0 16px;
}
.col-2-3 {
display: inline-block;
vertical-align: top;
width: calc(((100% / 3) * 2) - 32px);
margin: 0 16px 0 16px;
}
.col-1-4 {
display: inline-block;
vertical-align: top;
width: calc(25% - 32px);
margin: 0 16px 0 16px;
}
.col-2-4 {
display: inline-block;
vertical-align: top;
width: calc(50% - 32px);
margin: 0 16px 0 16px;
}
.col-3-4 {
display: inline-block;
vertical-align: top;
width: calc(75% - 32px);
margin: 0 16px 0 16px;
}
/* ----- ----- ----- ----- ----- */
/* responsive */
@media screen and (max-width: 640px) {
#siteMap .col-1-3 {
display: inline-block;
width: calc(100% - 32px);
margin: 0 16px 16px 16px;
}
}
/* ----- ----- ----- ----- ----- */
/* single.php */
#single\.php .postThumbnail img {
position: fixed;
width: 100%;
height: auto;
z-index: 1;
}
#single\.php, #single\.php #content {
min-height: 100%;
}
#single\.php #postMetadata {
}
#single\.php #postContent {
}
【问题讨论】:
标签: html css background-image css-position