CSS - 将文本保留在图像下

时间:2013-11-13 16:15:39

标签: html css image text move

我正在尝试创建一个简单的图片库,我被告知要使用“float:left”,但是当我从我的页脚执行所有文本拍摄到第一张图像时。我一直在寻找一个小时试图寻找修复但我找不到任何东西。我尝试过使用边距,边框,不同的对齐以及各种不同的小东西,但没有任何作用。这是网站代码:

的header.php

<!DOCTYPE html>
<html>

<head>
    <style type="text/css" media="all">@import "./includes/layout.css";</style>
</head>
    <body>
        <a href="index.html" class="logo"> <img src="includes/images/mockwebsitelogo.png" alt="Logo" width="427" height="172" /></a>

        <div id="nav">
            <a href=index.php class="navBarLink">Home</a>
            <a href=about.php class="navBarLink">About</a>   
            <a href=gallery.php class="navBarLink">Gallery</a>   
            <a href=programs.php class="navBarLink">Programs</a>    
        </div>

gallery.php

<?php
include('includes/header.php');
?>
<div id="txt1">
    <h1>Gallery</h1>
    <p>This is the gallery. Enter pictures or screenshots for the user to view</p>       

    <p class="image"><a href="includes/images/mwt.png">
        <img src="includes/images/mwt.png" alt="tst" title="tst" width="175" height="175" /></a></p>
    <p class="image"><a href="includes/images/mwt.png">
        <img src="includes/images/mwt.png" alt="tst" title="tst" width="175" height="175" /></a></p>
    <p class="image"><a href="includes/images/mwt.png">
        <img src="includes/images/mwt.png" alt="tst" title="tst" width="175" height="175" /></a></p>
    <p class="image"><a href="includes/images/mwt.png">
        <img src="includes/images/mwt.png" alt="tst" title="tst" width="175" height="175" /></a></p>
    <p class="image"><a href="includes/images/mwt.png">
        <img src="includes/images/mwt.png" alt="tst" title="tst" width="175" height="175" /></a></p>
</div>

<?php
include('includes/footer.php');
?>

footer.php

<div id="footer">
    <p>Mock Website created on 12th November 2013</p>
</div>

</body>

layout.css中

body 
{
background-color: #e1e1e1;
background-image:url('/includes/images/mwbb.png');
background-repeat: no-repeat;    
background-position: 50%;
}

#nav
{
border: 1px solid;
border-radius: 25px;
border-color: #e1e1e1;
background-color: #e1e1e1;
border-color: #000;
margin-left: auto;
margin-right: auto;
text-align: center;
width: 850px;
}

#footer
{
border-top: 1px solid;
margin-left: auto;
margin-right: auto;
width: 700px;
padding-top: 10px;
padding-bottom: 10px;
text-align: center;
white-space: nowrap;
}

#txt1
{
width: 750px;
margin-left: auto;
margin-right: auto;
}

.navBarLink
{
margin-left: 50px;
margin-right: 50px;
color: #000;
text-decoration: none;
}

.navBarLink:hover
{
color: #878787;
}

.logo
{
display: block;
text-align: center;
}

.image
{
margin: 5px 5px 5px 5px;
border: 1px solid #000;
padding: 5px 5px 5px 5px;
width: 175px;
height: 175px;
text-align: center;
float: left;
background-color: #323232;
display:  inline;
}

.image:hover
{
background-color: #4d4d4d;
}

我认为这是一个常见的初学者错误,但我找不到任何可以解决它的问题,谢谢任何试图提供帮助的人。

2 个答案:

答案 0 :(得分:2)

浮动元素会将其从页面的“流程”中取出。你的页脚会因为没有注意到浮动的元素而上升。这是clear的来源,它指定元素是否可以在浮动元素的旁边(或与其一致)。将clear:both添加到您的页脚,您应该得到您想要的结果:

#footer {
  border-top: 1px solid;
  margin-left: auto;
  margin-right: auto;
  width: 700px;
  padding-top: 10px;
  padding-bottom: 10px;
  text-align: center;
  white-space: nowrap;
  clear:both;
}

JSFiddle

答案 1 :(得分:1)

您需要清除任何花车。因此,在浮动元素之后,您可以将此类添加到页脚和浮动div的父级, #txt1 .clearfix:after

.clearfix:after {
   clear: both;
   content: ".";
   display: block;
   height: 0;
   visibility: hidden;
}

或者你可以添加一个明确的div:两者都在你的footer.php包含之上:

<div style="clear:both;"></div>

<?php
include('includes/footer.php');
?>
相关问题