2016-02-11 49 views
0
之上啓動了一個列太遠了

我做了一個簡單的登錄頁面,以便可以使用響應式設計。我正在使用Bootstrap。Bootstrap網格行在

我有4個項目的網格。每個項目都有一個圖像,一個標題,一個描述和一個鏈接。在所有設備上,網格的行應該包含兩列,除了超小的,在這種情況下,它應該包含一列。

這一切似乎工作正常,經過全部4個Bootstrap尺寸的測試。但是,當我將它插入Codepen時,其中一列開始於行的中間,並強制下一列溢出到下一行。這隻發生在小屏幕上(768px到991px)。

我試圖改變某些樣式,如減少容器寬度和文本大小,但沒有產生所需的效果,同時保持設計。

這裏是正在發生的一個畫面: enter image description here

的「Nullam」格應該是在同一行的左側。

這裏是對Codepen鏈接:http://codepen.io/alanbuchanan/pen/PZXRPr

這裏是我的HTML:

<div class="bg-brown"> 
    <div class="container"> 
     <div class="row top-section"> 
      <img class="lorem-image" src="http://i.imgur.com/7Eiswkk.png" alt=""> 
      <h1>Ipsum Dolor sit Amet.</h1> 
      <p class="col-lg-offset-2 col-lg-8">Consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p> 
      <div class="col-lg-12"> 
       <button>Donec quam felis!</button> 
      </div> 
     </div> 
    </div> 
</div> 

<div class="bg-white"> 
    <div class="container"> 
     <div class="row content-section-1"> 

      <div class="col-sm-6 col-xs-12"> 
       <img class="img-responsive" src="http://lorempixel.com/400/250/cats/1" alt=""> 
       <h2 class="thing-header">Ultricies nec.</h2> 
       <p class="thing-description">Pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.</p> 
       <p class="thing-link"><span>Donec pede justo!</span></p> 
      </div> 

      <div class="col-sm-6 col-xs-12"> 
       <img class="img-responsive" src="http://lorempixel.com/400/250/cats/2" alt=""> 
       <h2 class="thing-header">Fringilla vel.</h2> 
       <p class="thing-description">Aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut.</p> 
       <p class="thing-link"><span>Imperdiet a, venenatis vitae, justo!</span></p> 
      </div> 

      <div class="col-sm-6 col-xs-12"> 
       <img class="img-responsive" src="http://lorempixel.com/400/250/cats/3" alt=""> 
       <h2 class="thing-header">Nullam.</h2> 
       <p class="thing-description">Dictum felis eu pede mollis pretium. Integer tincidunt. </p> 
       <p class="thing-link"><span>Cras dapibus!</span></p> 
      </div> 

      <div class="col-sm-6 col-xs-12"> 
       <img class="img-responsive" src="http://lorempixel.com/400/250/cats/4" alt=""> 
       <h2 class="thing-header">Vivamus elementum semper nisi.</h2> 
       <p class="thing-description">Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu.</p> 
       <p class="thing-link"><span>Consequat vitae!</span></p> 
      </div> 

     </div> 
     <div class="row prefooter"> 
      <p>Eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus.</p> 
     </div> 
    </div> 
</div> 

<div class="bg-bottom"> 
    <div class="container"> 
     <div class="row content-section-2"> 
      <h1><i class="fa fa-github"></i> <a href="https://github.com/alanbuchanan">alanbuchanan</h1></a> 
     </div> 
    </div> 
</div> 

這裏是我的CSS:

@import url(https://fonts.googleapis.com/css?family=Source+Sans+Pro:200,400); 
$font-thin: 'Source Sans Pro', sans-serif; 

$top-section-bg-color: #48240A; 
$btn-bg-color: #a5682a; 
$btn-border-width: 2px; 
$main-color: #EEEEEE; 
$top-section-spacing: 80px; 

$content-section-1-bg-color: #EEEEEE; 
$content-section-1-font-color: black; 

$content-section-2-bg-color: $top-section-bg-color; 

body { 
    font-family: $font-thin; 
    color: $main-color; 
    font-weight: 200; 
    text-rendering: optimizeLegibility; 
    font-size: 14px; 
} 

a, a:active, a:hover, a:focus { 
    text-decoration: none; 
    border-bottom: 1px dashed $main-color; 
    color: $main-color; 
} 

.bg-brown { 
    background-color: $top-section-bg-color; 
} 

.bg-white { 
    background-color: $content-section-1-bg-color; 
} 

.top-section { 

    img { 
     display: block; 
     margin: 0 auto; 
     margin-top: $top-section-spacing; 
    } 

    h1 { 
     text-align: center; 
     color: $main-color; 
     margin-top: $top-section-spacing; 
    } 

    p { 
     text-align: center; 
     margin-top: $top-section-spacing; 
    } 

    button { 
     font-family: $font-thin; 
     display: block; 

     background-color: $btn-bg-color; 

     color: $main-color;  
     font-size: 20px; 
     font-weight: 200; 
     text-decoration: none; 

     cursor: pointer; 

     padding: 9px 20px; 
     margin: $top-section-spacing auto $top-section-spacing auto; 

     border-radius:6px; 
     -moz-border-radius: 5px; 
     -webkit-border-radius: 5px; 
     border-style: none; 
     border: $btn-border-width solid $btn-bg-color; 

     transition: background-color 0.2s ease; 

     &:focus { 
      outline: none; 
     } 
    } 
} 


.content-section-1 { 

    background-color: $content-section-1-bg-color; 
    color: $content-section-1-font-color; 
    margin-top: 100px; 

    img { 
     display: block; 
     margin: 0 auto; 
     width: 100%; 
    } 

    .thing-header { 
     margin: 40px 0; 
     text-align: center; 
     font-weight: 200; 
    } 

    .thing-description { 
     text-align: center; 
     margin: 0; 
     padding: 0; 
     font-size: 14px; 
    } 

    .thing-link { 
     display: block; 
     text-align: center; 
     font-weight: 400; 
     margin-bottom: 40px; 

     span { 
      text-decoration: none; 
      border-bottom: 1px dashed black; 
     } 
    } 
} 

.prefooter { 
    color: $content-section-1-font-color; 
    padding: 50px; 
    text-align: center; 
} 

.bg-bottom { 
    background-color: $content-section-2-bg-color; 
} 

.content-section-2 { 
    h1 { 
     text-align: center; 
     font-weight: 200; 
     padding: 50px; 
    } 
} 

html.no-touch button:hover { 
    background-color: $top-section-bg-color; 
    border: $btn-border-width solid $btn-bg-color; 
} 

@media screen and (min-width: 320px) and (max-width: 480px) { 

    .lorem-image { 
     width: 220px; 
    } 

    .thing-header, .thing-description, .thing-link { 
     text-align: left !important; 
    } 

} 

回答

1

由於您使用的是col-sm-6並且沒有其他網格斷點,您只需清除768px以上每個第二列的浮點數。這解決了列的高度差異,而無需在每12列使用的行上添加行。

注意:我爲每列添加了一類item,您可以使用任何有意義的東西,因爲這是通用的。

@media (min-width: 768px) { 
    .item:nth-child(2n+1) { 
    clear: left; 
    } 
} 

查看工作示例在FullPage中的代碼段並減少視口。

@import url(https://fonts.googleapis.com/css?family=Source+Sans+Pro:200,400); 
 
body { 
 
    font-family: "Source Sans Pro", sans-serif; 
 
    color: #EEEEEE; 
 
    font-weight: 200; 
 
    text-rendering: optimizeLegibility; 
 
} 
 
a, 
 
a:active, 
 
a:hover, 
 
a:focus { 
 
    text-decoration: none; 
 
    border-bottom: 1px dashed #EEEEEE; 
 
    color: #EEEEEE; 
 
} 
 
.bg-brown { 
 
    background-color: #48240A; 
 
} 
 
.bg-white { 
 
    background-color: #EEEEEE; 
 
} 
 
.top-section img { 
 
    display: block; 
 
    margin: 0 auto; 
 
    margin-top: 80px; 
 
} 
 
.top-section h1 { 
 
    text-align: center; 
 
    color: #EEEEEE; 
 
    margin-top: 80px; 
 
} 
 
.top-section p { 
 
    text-align: center; 
 
    margin-top: 80px; 
 
} 
 
.top-section button { 
 
    font-family: "Source Sans Pro", sans-serif; 
 
    display: block; 
 
    background-color: #a5682a; 
 
    color: #EEEEEE; 
 
    font-size: 20px; 
 
    font-weight: 200; 
 
    text-decoration: none; 
 
    cursor: pointer; 
 
    padding: 9px 20px; 
 
    margin: 80px auto 80px auto; 
 
    border-radius: 6px; 
 
    -moz-border-radius: 5px; 
 
    -webkit-border-radius: 5px; 
 
    border-style: none; 
 
    border: 2px solid #a5682a; 
 
    transition: background-color 0.2s ease; 
 
} 
 
.top-section button:focus { 
 
    outline: none; 
 
} 
 
.content-section-1 { 
 
    background-color: #EEEEEE; 
 
    color: black; 
 
    margin-top: 50px; 
 
} 
 
.content-section-1 img { 
 
    margin: 0 auto; 
 
} 
 
.content-section-1 .thing-header { 
 
    margin: 40px 0; 
 
    text-align: center; 
 
    font-weight: 200; 
 
} 
 
.content-section-1 .thing-description { 
 
    text-align: center; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
.content-section-1 .thing-link { 
 
    display: block; 
 
    text-align: center; 
 
    font-weight: 400; 
 
    margin-bottom: 40px; 
 
} 
 
.content-section-1 .thing-link span { 
 
    text-decoration: none; 
 
    border-bottom: 1px dashed black; 
 
} 
 
.prefooter { 
 
    color: black; 
 
    padding: 20px; 
 
    margin-bottom: 20px; 
 
    text-align: center; 
 
} 
 
.bg-bottom { 
 
    background-color: #48240A; 
 
} 
 
.content-section-2 h1 { 
 
    text-align: center; 
 
    font-weight: 200; 
 
    padding: 50px; 
 
} 
 
html.no-touch button:hover { 
 
    background-color: #48240A; 
 
    border: 2px solid #a5682a; 
 
} 
 
.img-row { 
 
    margin: 0 2%; 
 
} 
 
@media screen and (min-width: 320px) and (max-width: 480px) { 
 
    .lorem-image { 
 
    width: 220px; 
 
    } 
 
    .thing-header, 
 
    .thing-description, 
 
    .thing-link { 
 
    text-align: left !important; 
 
    } 
 
} 
 
@media (min-width: 768px) { 
 
    .item:nth-child(2n+1) { 
 
    clear: left; 
 
    } 
 
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" /> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /> 
 
<div class="bg-brown"> 
 
    <div class="container"> 
 
    <div class="row top-section"> 
 
     <img class="lorem-image" src="http://i.imgur.com/7Eiswkk.png" alt=""> 
 
     <h1>Ipsum Dolor sit Amet.</h1> 
 
     <p class="col-lg-offset-2 col-lg-8">Consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p> 
 
     <div class="col-lg-12"> 
 
     <button>Donec quam felis!</button> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<div class="bg-white"> 
 
    <div class="container"> 
 
    <div class="row content-section-1"> 
 

 
     <div class="col-sm-6 item"> 
 
     <img class="img-responsive" src="http://lorempixel.com/400/250/cats/1" alt=""> 
 
     <h2 class="thing-header">Ultricies nec.</h2> 
 
     <p class="thing-description">Pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.</p> 
 
     <p class="thing-link"><span>Donec pede justo!</span> 
 
     </p> 
 
     </div> 
 

 
     <div class="col-sm-6 item"> 
 
     <img class="img-responsive" src="http://lorempixel.com/400/250/cats/2" alt=""> 
 
     <h2 class="thing-header">Fringilla vel.</h2> 
 
     <p class="thing-description">Aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut.</p> 
 
     <p class="thing-link"><span>Imperdiet a, venenatis vitae, justo!</span> 
 
     </p> 
 
     </div> 
 

 
     <div class="col-sm-6 item"> 
 
     <img class="img-responsive" src="http://lorempixel.com/400/250/cats/3" alt=""> 
 
     <h2 class="thing-header">Nullam.</h2> 
 
     <p class="thing-description">Dictum felis eu pede mollis pretium. Integer tincidunt.</p> 
 
     <p class="thing-link"><span>Cras dapibus!</span> 
 
     </p> 
 
     </div> 
 

 
     <div class="col-sm-6 item"> 
 
     <img class="img-responsive" src="http://lorempixel.com/400/250/cats/4" alt=""> 
 
     <h2 class="thing-header">Vivamus elementum semper nisi.</h2> 
 
     <p class="thing-description">Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu.</p> 
 
     <p class="thing-link"><span>Consequat vitae!</span> 
 
     </p> 
 
     </div> 
 

 
     <div class="col-sm-6 item"> 
 
     <img class="img-responsive" src="http://lorempixel.com/400/250/cats/2" alt=""> 
 
     <h2 class="thing-header">Fringilla vel.</h2> 
 
     <p class="thing-description">Aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut.</p> 
 
     <p class="thing-link"><span>Imperdiet a, venenatis vitae, justo! Imperdiet a, venenatis vitae, justo!</span> 
 
     </p> 
 
     </div> 
 

 
     <div class="col-sm-6 item"> 
 
     <img class="img-responsive" src="http://lorempixel.com/400/250/cats/1" alt=""> 
 
     <h2 class="thing-header">Ultricies nec.</h2> 
 
     <p class="thing-description">Pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Nulla consequat massa quis enim. Nulla consequat massa quis enim.</p> 
 
     <p class="thing-link"><span>Donec pede justo! Imperdiet a, venenatis vitae, justo!</span> 
 
     </p> 
 
     </div> 
 

 
    </div> 
 

 
    <div class="row prefooter"> 
 
     <p>Eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus.</p> 
 
    </div> 
 

 
    </div> 
 
</div> 
 

 

 
<div class="bg-bottom"> 
 
    <div class="container"> 
 
    <div class="row content-section-2"> 
 
     <h1><i class="fa fa-github"></i> <a href="https://github.com/alanbuchanan">alanbuchanan</a></h1> 
 
    </div> 
 
    </div> 
 
</div>

+0

當我在媒體查詢中設置'max-width:991px'時,我發現這是最好的方法。 – alanbuchanan

-1

這是因爲長的文字你有內部。我建議你添加這些爲.thing-description

white-space:nowrap; 
    overflow:hidden; 
    text-overflow:clip; 

UPDATED PEN

+0

你怎麼知道那是因爲長的文字? – alanbuchanan

+1

這不是解決問題的最佳方法!有些文本無法看到,如果你刪除'overflow:hidden;'文本將在下一列http://i.imgur.com/s2vik4t.png?1 @alanbuchanan –

+0

這是文本省略號解決方案,在他的情況下無效! –

2

問題是上述要素的高度!

正如你可以在圖片中看到的,上面的元素推動元素吹起它,因爲它很好!

enter image description here

例如儘量把margin: 0px; P元素在底部和畫面會回到它的位置

enter image description here

,但是這不是正確的解決方案!正確的解決方案是將每個2個元素放在一個div中,然後關閉它,並將另外兩個div放在另一個div中並關閉,這樣每個元素都可以放置,而不會傷害下面的元素,所以你的HTML看起來是像這樣

<div class="row"> 
    <div class="col-sm-6 col-xs-12"></div> 
    <div class="col-sm-6 col-xs-12"></div> 
</div> 
<div class="row"> 
    <div class="col-sm-6 col-xs-12"></div> 
    <div class="col-sm-6 col-xs-12"></div> 
</div> 

除此之外,你正在做好人,祝你的網站好運。

+0

如果'.col-sm-6'塊中的內容不作爲循環的一部分輸出,那麼當然需要進一步編程以將每個對包裝在'.row'中時,該解決方案就沒有問題。 –

+1

我不喜歡使用'.row'因爲額外的餘量,但這是一個很好的習慣,謝謝你的編輯@KarlDawson –

+0

@KarlDawson @Khaled Al-Ansari謝謝你,我把兩個div放到一個'row'中2%保證金 – alanbuchanan

1

正如已經指出的那樣,該問題與由於圖像大小和/或文本行長度而引起的內容框的變化高度有關。

訣竅是讓所有框匹配最高內容框的高度,併爲此使用一小段JavaScript。

<div class="row"> 
    <div class="col-xs-12 col-sm-6 jsEqualHeight"></div> 
    <div class="col-xs-12 col-sm-6 jsEqualHeight"></div> 
</div> 

使用.jsEqualHeight

if (jQuery) { 
    (function ($) { 
     "use strict"; 
     $(document).ready(function() { 

      $(window).on('resize', function() { 
       equalHeights(); 
      }); 

      equalHeights(); 

      function equalHeights() { 
       if (window.matchMedia("(min-width: 768px)").matches) { 

        var elementHeights = $('.jsEqualHeight').map(function() { 
         return $(this).height(); 
        }).get(); 

        // Math.max takes a variable number of arguments 
        // 'apply' is equivalent to passing each height as an argument 
        var maxHeight = Math.max.apply(null, elementHeights); 

        // Set each height to the max height 
        $('.jsEqualHeight').height(maxHeight); 
       } else { 
        $('.jsEqualHeight').css({ "height": "auto" }); 
       } 
      } 
     }); 
    }(jQuery)); 
} 

您可能需要在matchMedia功能媒體查詢的價值發揮到適合您的解決方案,並根據您的支持的瀏覽器基質,填充工具JS的舊版瀏覽器不支持matchMedia

+0

我試圖弄清楚如何將其他divs的高度設置爲最高高度。感謝一個有趣的解決方案 – alanbuchanan