【问题标题】:Bootstrap 3 col-sm-4 creates two columns instead of 3Bootstrap 3 col-sm-4 创建两列而不是 3
【发布时间】:2015-08-31 12:11:07
【问题描述】:

按照 TutsPlus 使用 Bootstrap 启动您的投资组合教程。

尝试使用 Bootstrap 3 的 col-sm-4 类实现 3 列组合,以获得类似:

相反,这是我得到的:

这是我缩小时得到的结果:

HTML 代码:

<!doctype html>
<html class="no-js" lang="">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="apple-touch-icon" href="apple-touch-icon.png">
        <!-- Place favicon.ico in the root directory -->
        <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
        <link rel="stylesheet" href="css/normalize.css">
        <link rel="stylesheet" href="css/style.css">

        <script src="js/vendor/modernizr-2.8.3.min.js"></script>
    </head>
    <body>
        <!--[if lt IE 8]>
            <p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
        <![endif]-->

        <!-- Add your site or application content here -->
<nav class="navbar">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Red Beluga</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav navbar-right">
        <li><a href="home.html">Home</a></li>
        <li><a href="blog.html">Blog</a></li>
        <li><a href="contact.html">Contact</a></li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

<div class="container-fluid text-center">
  <div class="jumbotron">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <h1>Hello, welcome to Red Beluga's lab.</h1>
            <p class="lead">
                We build simple web apps.
            </p>
        </div>
    </div>
  </div>
</div>

<div class="container-fluid filterable-portfolio">
    <div class="row">
        <div class="col-md-12">
            <ul class="nav nav-pills">
              <li class="portfolio-title">Filter by:</li>
              <li role="presentation" class="active"><a href="#">All</a></li>
              <li role="presentation"><a href="#">Animation</a></li>
              <li role="presentation"><a href="#">Design</a></li>
              <li role="presentation"><a href="#">Photography</a></li>
              <li role="presentation"><a href="#">Web</a></li>
            </ul>
        </div>
    </div>

    <div class="row portfolio-items">
        <figure class="portfolio-item col-sm-4">
            <a href="#">
                <img src="http://placekitten.com/700/400" class="img-responsive">
            </a>
        </figure>
        <figure class="portfolio-item col-sm-4">
            <a href="#">
                <img src="http://placekitten.com/701/400" class="img-responsive">
            </a>
        </figure>
        <figure class="portfolio-item col-sm-4">
            <a href="#">
                <img src="http://placekitten.com/700/400" class="img-responsive">
            </a>
        </figure>
        <figure class="portfolio-item col-sm-4">
            <a href="#">
                <img src="http://placekitten.com/701/400" class="img-responsive">
            </a>
        </figure>
        <figure class="portfolio-item col-sm-4">
            <a href="#">
                <img src="http://placekitten.com/700/400" class="img-responsive">
            </a>
        </figure>
        <figure class="portfolio-item col-sm-4">
            <a href="#">
                <img src="http://placekitten.com/701/400" class="img-responsive">
            </a>
        </figure>

    </div>

</div>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.11.3.min.js"><\/script>')</script>
        <script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
        <script src="js/plugins.js"></script>
        <script src="js/main.js"></script>

        <!-- Google Analytics: change UA-XXXXX-X to be your site's ID. -->
        <script>
            (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
            function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
            e=o.createElement(i);r=o.getElementsByTagName(i)[0];
            e.src='https://www.google-analytics.com/analytics.js';
            r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
            ga('create','UA-XXXXX-X','auto');ga('send','pageview');
        </script>
    </body>
</html>

CSS 代码:

@import 'main.css';
@import url(http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,700);
@import url(http://fonts.googleapis.com/css?family=Abril+Fatface);

html{
    background: #222 url(../img/abstract-bg.svg) bottom right no-repeat;
    height: 100%;
    background-attachment: fixed;
    overflow-y: scroll;
}

body{
    background: none;
    line-height: 1.5;
    color: #F5F5F5;
    font-family: 'Source Sans Pro', sans-serif;
}

h1, h2, h3, h4, h5, h6 {
    font-family: 'Abril Fatface', cursive;
    margin-top: 0;
}

p {
    margin-bottom: 1rem;
}

.navbar-brand, .navbar-nav>li>a{
    color: #F5F5F5;
    font-weight: bold;
}

.navbar-brand:hover, .navbar-nav>li>a:hover{
    color: #F5F5F5;
    font-weight: bold;
    background: none;
}

.navbar-nav>li>a:after{
    content: '/';
    margin-left: 30px;
}

.navbar-nav>li:last-child>a:after{
    content: '';
}

.navbar-toggle{
    background: #111;
}

.navbar-toggle .icon-bar{
    background: #F5F5F5;
}

@media (max-width: 768px) {
    .navbar-nav>li>a:after{
    content: '';
}
    .navbar-nav>li>a{
        background: #111;
        border-radius: 4px;
        margin-bottom: 3px;
    }
}

.container .jumbotron, .container-fluid .jumbotron{
    padding-left: 0;
    padding-right: 0;
}

.jumbotron{
    background: none;
    margin-top: 75px;
    margin-bottom: 75px;
}

.jumbotron h1 {
    font-size: 120px;
}

.jumbotron .lead {
    font-size: 32px;
}

@media (max-width: 992px) {
    .jumbotron h1{
        font-size: 72px;
    }
}

@media (max-width: 768px) {
    .jumbotron h1{
        font-size: 40px;
    }
    .jumbotron .lead {
    font-size: 20px;
}
}

.portfolio-title{
    padding: 10px 15px 10px 0px;
    font-weight: bold;
}

.nav-pills>li.active>a, .nav-pills>li.active>a:focus, .nav-pills>li.active>a:hover {
  color: #F5F5F5;
  background: #111;
}

.nav-pills a {
    color: #F5F5F5;
    margin-bottom: 1rem;
}

.nav>li>a:focus, .nav>li>a:hover{
    background: none;
}

.portfolio-item {
    margin-bottom: 1rem;
}

.portfolio-item img{
    -webkit-filter: grayscale(100%);
    filter: grayscale(100%);
    border-radius: 4px;
}

.portfolio-item:hover img{
    -webkit-filter: grayscale(50%);
    filter: grayscale(50%);
}

.filterable-portfolio{
    margin-bottom: 3rem;
}

知道我做错了什么吗?

【问题讨论】:

    标签: html css twitter-bootstrap


    【解决方案1】:

    您需要将您的col-sm-4 div 设置为另一个col-md-12 div,否则它将设置您总页面宽度的 33.3% 并使所有内容看起来很傻。

    【讨论】:

    • 感谢@Adam Buchanan Smith。我同意,这将是正确的做法,但我不明白为什么它在教程中工作,而不是在我的电脑上工作。显然只是 CSS 加载顺序的问题。无论如何,谢谢。
    【解决方案2】:

    3 列布局的标记是:

    <div class="row>
        <div class="col-md-4">...cat1...</div>
        <div class="col-md-4">...cat2...</div>
        <div class="col-md-4">...cat3...</div>
    </div>
    <div class="row>
        <div class="col-md-4">...professor puddinpop...</div>
        <div class="col-md-4">...colonel snazzypants...</div>
        <div class="col-md-4">...the good king snugglewumps...</div>
    </div>
    <div class="row">
        ...
    </div>
    

    当屏幕变得足够小时,这将确保您在堆叠之前有 3 列。

    有关更多信息,请参阅网格上的bootstrap docs

    【讨论】:

    • 你不需要
    • 感谢刘易斯和布赖恩。正如上面对亚当所说,我同意,这将是正确的做法,但我不明白为什么它在教程中工作,而不是在我的电脑上工作。显然只是 CSS 加载顺序的问题。无论如何,谢谢。
    【解决方案3】:

    您需要将col-sm-4 包装成行,

    更改此部分并使用每 3 col-sm-4 的行作为包装器;
    网格用作第 12 个系统列,检查bootstrap grid

    <div class="row portfolio-items">
            <figure class="portfolio-item col-sm-4">
                <a href="#">
                    <img src="http://placekitten.com/700/400" class="img-responsive">
                </a>
            </figure>
            <figure class="portfolio-item col-sm-4">
                <a href="#">
                    <img src="http://placekitten.com/701/400" class="img-responsive">
                </a>
            </figure>
            <figure class="portfolio-item col-sm-4">
                <a href="#">
                    <img src="http://placekitten.com/700/400" class="img-responsive">
                </a>
            </figure>
            <figure class="portfolio-item col-sm-4">
                <a href="#">
                    <img src="http://placekitten.com/701/400" class="img-responsive">
                </a>
            </figure>
            <figure class="portfolio-item col-sm-4">
                <a href="#">
                    <img src="http://placekitten.com/700/400" class="img-responsive">
                </a>
            </figure>
            <figure class="portfolio-item col-sm-4">
                <a href="#">
                    <img src="http://placekitten.com/701/400" class="img-responsive">
                </a>
            </figure>
    
        </div>
    

    到:

    <div class="row portfolio-items">
            <figure class="portfolio-item col-sm-4">
                <a href="#">
                    <img src="http://placekitten.com/700/400" class="img-responsive">
                </a>
            </figure>
            <figure class="portfolio-item col-sm-4">
                <a href="#">
                    <img src="http://placekitten.com/701/400" class="img-responsive">
                </a>
            </figure>
            <figure class="portfolio-item col-sm-4">
                <a href="#">
                    <img src="http://placekitten.com/700/400" class="img-responsive">
                </a>
            </figure>
       </div><!-- closing first row -->
      <div class="row portfolio-items">
            <figure class="portfolio-item col-sm-4">
                <a href="#">
                    <img src="http://placekitten.com/701/400" class="img-responsive">
                </a>
            </figure>
            <figure class="portfolio-item col-sm-4">
                <a href="#">
                    <img src="http://placekitten.com/700/400" class="img-responsive">
                </a>
            </figure>
            <figure class="portfolio-item col-sm-4">
                <a href="#">
                    <img src="http://placekitten.com/701/400" class="img-responsive">
                </a>
            </figure>
           </div><!-- closing sec row -->
        </div>
    

    【讨论】:

      【解决方案4】:

      Thibaud Clement,您的自定义 CSS 似乎有问题。
      当我将 cdn Bootstrap css 添加到您的代码中时,它工作正常。

      看看这里 Fiddle

       <link rel="apple-touch-icon" href="apple-touch-icon.png">
              <!-- Place favicon.ico in the root directory -->
              <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
              <link rel="stylesheet" href="css/normalize.css">
              <link rel="stylesheet" href="css/style.css">
      <!-- Bootstrap core CSS -->
          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
      

      已添加到此
      看起来您的问题可能来自另一个帖子的normalize.cssgoing。我认为这是正确的轨道。我为normalize.css添加了一个cdn,但您的代码仍然可以正常工作。
      正如您将在此 updated Fiddle 中看到的那样。

      【讨论】:

      • 这不是他的自定义 CSS,而是 normalize.css,在您更改后加载它之后,Bootstrap(巧合地来自 CDN)现在被覆盖。
      【解决方案5】:

      您的问题在于通过 normalize.css 将样式应用于 figure 标签元素(margin-top 的 1em 和 margin-right 的 40px 中的一些额外边距,导致您的内容比 Bootstrap 的 col-sm-4 div 的 33.3% 宽度更宽)。将您的 figure 元素切换为 div 或为 figure 标签提供替代值,以消除规范化应用的行为。这就是我的意思:

      [编辑] 为了更好地查看 gif,check it out in a different tab。我以比这里看起来更好的分辨率捕获它。

      这是我从上面的代码生成的the plnkr 的链接。

      AngularJR 的回答有效的原因是因为他不只是使用 Bootstrap 的 CDN,他还打乱了 CSS 加载的顺序,将 Bootstrap 的 CSS after 放在 normalize.css 之后,使其继承 Bootstrap 的最后一个(和取代)。 [/编辑]

      【讨论】:

      • 您好,埃里克,我以为您已经在这里找到了答案。所以我添加了一个cdn来拉入normalize.css。但它没有影响。在我的第一篇文章中,我只添加了一个 Bootstrap cdn,是的,在其他 css 链接下方。但是另一个 Bootstrap 的链接都不起作用,因为它们没有链接到任何地方。看看我在第二把小提琴的编辑。它在 Bootstrap 下方具有规范化。你觉得他的自定义 CSS 有什么问题?
      • 我会完全删除 normalize,因为它是导致问题的原因。 Here's a link to an SO answer on the subject(在 Bootstrap 2.3 下,但仍然有效)。它是从 CDN 加载的这一事实无关紧要。重要的是您的回答将其移到了 Bootstrap 的 CSS 之上,从而使 Bootstrap 胜出(在一场不必要的战斗中)。
      • 嗨,是的,删除规范化。 Eric,Bootstrap.css 上方的 css 链接都不起作用。因为他们也没有任何链接路径。在我的第一个小提琴中尝试这个,删除 Bootstrap css 上方的所有 css 链接。这就是为什么我看不到你发现了什么。这里一定有别的东西。
      • plnkr 是为了展示事物是如何交互的,而不是替换 OP 的代码。
      • 非常感谢@EricMcCormick。你的 gif 是一个很好的方式来说明你的意思。你是对的,只要我改变 CSS 加载的顺序,将 normalize.css 放在 bootstrap 的 css 之后,它就起作用了。
      猜你喜欢
      相关资源
      最近更新 更多
      热门标签