【问题标题】:sidebar moving to the bottom侧边栏移动到底部
【发布时间】:2017-05-30 23:41:30
【问题描述】:

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

这里有一些图片:



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 文件,请告诉我。

我真的很感激!

【问题讨论】:

  • CSS 会很有用。你有围绕 Sidebar.php 的包装器吗?
  • 添加了 CSS。我有一个名为“sidebar”的包装器,它没有显示在帖子中,但我修复了它:)
  • 我认为添加时应该没问题:.sidebar{float:right;}
  • ![floatright.png](postimg.org/image/7o40epmjv) 这是浮动权利的情况。我试图清除:两者也是

标签: php html css


【解决方案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 } ?>

【讨论】:

  • 对不起,我没有缩进最后几行。 div 已关闭。我更新了代码
  • 您必须将此作为评论发布:-)
  • 你能试试这个吗?
  • 不幸的是,它没有用。不过还是谢谢你的建议!
  • 我知道 col-lg-8s 在您的 Posts.php 中水平排序。您必须将它们分组到一个 div 中才能垂直排列帖子 div。这就是重点……不客气!
【解决方案2】:

请在下面找到一个工作示例。小提琴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>

【讨论】:

  • 谢谢!我又看了看,发现引导框架中有一些媒体查询用于“col-lg-3”,这使得它们变得非常小。我刚刚将每个侧边栏块的宽度更改为 width:auto !important; 并且它起作用了!感谢您的宝贵时间
  • 对不起,你解决了原来的问题!我想知道...我偶然发现了另一个问题(上面的那个)并忘记了我的老问题:-))
  • 很高兴它已修复:)
猜你喜欢
  • 2021-01-03
  • 1970-01-01
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多