侧边栏移动到底部

时间:2017-05-30 14:07:07

标签: php html css

我决定今天开始一个新的个人项目,这是一个论坛类型的东西(仅供练习)。 我设置了基本布局并编写了一些PHP函数,但是当我添加一个新帖子时,侧边栏向下移动并位于帖子下方。

这里有一些照片:

sidebar with only one post :)

sidebar with more than one post :(

的sidebar.php

<div class ="sidebar">

<div class = "col-lg-3 profile-tab sidebar-nav">

    <img src="images/dummy-pic.png" alt = ""><br>
        <a href = "#">USERNAME</a><br>
        <span>Post count: x</span><br>
        <span>Likes: x</span>


</div>

<div class="col-lg-3">
    <ul class="sidebar-nav">
        <h4 class = "sb-heading">Post Categories</h4>
            <li><a href="#">Category Name</a>
            </li>
            <li><a href="#">Category Name</a>
            </li>
            <li><a href="#">Category Name</a>
            </li>
            <li><a href="#">Category Name</a>
            </li>
            </ul>
</div>

<div class = "col-lg-3">
    <ul class="sidebar-nav">
        <h4 class = "sb-heading">Announcements</h4>

            <li>interesting stuff</li>
            <li>interesting stuff</li>
            <li>interesting stuff</li>
    </ul>
</div>

Posts.php

<div class = "post col-lg-8">

<h2 class = "page-header"><?php echo $post_title; ?></h2>
<p class = "lead">By
<a href = "#"><?php echo $post_author ?></a></p>
<span class = "glyphicon glyphicon-time"><?php echo $post_date ?></span>

<img class = "img-responsive" src = "<?php echo $post_image; ?>" />
<br>
<p>    
<?php 
echo $post_content;
 ?>

</p>

<br/>
<a class = "btn btn-primary" href = "#">READ MORE</a>

</div>

<?php } ?>

相关CSS

.sidebar-nav{
background-color: #ffffff;
margin-top: 15px;
border:2px solid #1b76bc;
border-radius: 10px;
padding: 20px 0 20px  0;
text-align: center;


}

div.post.col-lg-8{

margin-left: 20px;
}

.sidebar-nav li{

    list-style: none;
}

.post{

padding:15px;
margin-top: 15px;
background-color: #ffffff;
border-radius: 10px;
border: 2px solid #1b76bc;
padding: 20px;

}

我使用col-lg-3而不是4作为侧边栏,以便我可以在帖子中添加左边距。可能有更好的方法,但只是抬头

如果您需要css文件,请告诉我。

我真的很感激!

2 个答案:

答案 0 :(得分:1)

你必须在Posts.php中关闭div。

编辑:您可以尝试以下内容:

的sidebar.php

<div class ="col-lg-3 col-md-3 col-sm-3 sidebar">

<div class = "profile-tab sidebar-nav">

    <img src="images/dummy-pic.png" alt = ""><br>
        <a href = "#">USERNAME</a><br>
        <span>Post count: x</span><br>
        <span>Likes: x</span>


</div>

<div>
    <ul class="sidebar-nav">
        <h4 class = "sb-heading">Post Categories</h4>
            <li><a href="#">Category Name</a>
            </li>
            <li><a href="#">Category Name</a>
            </li>
            <li><a href="#">Category Name</a>
            </li>
            <li><a href="#">Category Name</a>
            </li>
            </ul>
</div>

<div>
    <ul class="sidebar-nav">
        <h4 class = "sb-heading">Announcements</h4>

            <li>interesting stuff</li>
            <li>interesting stuff</li>
            <li>interesting stuff</li>
    </ul>
</div>
</div>

Posts.php

<div class = "col-lg-8 col-md-8 col-sm-8"> //New line
// php foreach etc.
<div class = "post">

<h2 class = "page-header"><?php echo $post_title; ?></h2>
<p class = "lead">By
<a href = "#"><?php echo $post_author ?></a></p>
<span class = "glyphicon glyphicon-time"><?php echo $post_date ?></span>

<img class = "img-responsive" src = "<?php echo $post_image; ?>" />
<br>
<p>    
<?php 
echo $post_content;
 ?>

</p>

<br/>
<a class = "btn btn-primary" href = "#">READ MORE</a>

</div>

<?php } ?>

答案 1 :(得分:0)

请在下面找到一个工作示例。小提琴here

.sidebar {
  float: right;
  width: 25%;
  max-width: 25%;
}

div.post.col-lg-8 {
  float: left;
  width: 70%;
  max-width: 70%;
}

.sidebar-nav {
  background-color: #ffffff;
  margin-top: 15px;
  border: 2px solid #1b76bc;
  border-radius: 10px;
  padding: 20px 0 20px 0;
  text-align: center;
}

div.post.col-lg-8 {
  margin-left: 20px;
}

.sidebar-nav li {
  list-style: none;
}

.post {
  padding: 15px;
  margin-top: 15px;
  background-color: #ffffff;
  border-radius: 10px;
  border: 2px solid #1b76bc;
  padding: 20px;
}
<div class="sidebar">

  <div class="col-lg-3 profile-tab sidebar-nav">

    <img src="images/dummy-pic.png" alt="">
    <br>
    <a href="#">USERNAME</a>
    <br>
    <span>Post count: x</span>
    <br>
    <span>Likes: x</span>


  </div>

  <div class="col-lg-3">
    <ul class="sidebar-nav">
      <h4 class="sb-heading">Post Categories</h4>
      <li><a href="#">Category Name</a>
      </li>
      <li><a href="#">Category Name</a>
      </li>
      <li><a href="#">Category Name</a>
      </li>
      <li><a href="#">Category Name</a>
      </li>
    </ul>
  </div>

  <div class="col-lg-3">
    <ul class="sidebar-nav">
      <h4 class="sb-heading">Announcements</h4>

      <li>interesting stuff</li>
      <li>interesting stuff</li>
      <li>interesting stuff</li>
    </ul>
  </div>
</div>
<div class="post col-lg-8">

  <h2 class="page-header">Post title</h2>
  <p class="lead">By
    <a href="#">
      Post author
    </a>
  </p>
  <span class="glyphicon glyphicon-time">May 30, 2017</span>

  <img class="img-responsive" src="http://placehold.it/100" />
  <br>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

  <br/>
  <a class="btn btn-primary" href="#">READ MORE</a>

</div>
<div class="post col-lg-8">

  <h2 class="page-header">Post title</h2>
  <p class="lead">By
    <a href="#">
      Post author
    </a>
  </p>
  <span class="glyphicon glyphicon-time">May 30, 2017</span>

  <img class="img-responsive" src="http://placehold.it/100" />
  <br>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

  <br/>
  <a class="btn btn-primary" href="#">READ MORE</a>

</div>