在 Twitter 引導程序中為行添加背景顏色和填充 (Adding background color and padding to row in Twitter bootstrap)


問題描述

在 Twitter 引導程序中為行添加背景顏色和填充 (Adding background color and padding to row in Twitter bootstrap)

I realise that padding in Twitter Bootstrap has been done somewhat to death and I have previously managed to achieve it to an extent but am currently stuck on trying to get my three span4 columns to sit nicely on a padded white background row.

I have tried various ways of adding in an extra, unsemantic div but to no avail.

I have tried:

<div id="mydiv">

            <div class="wrap">

                <div class="row">

                    <div class="span4">
                        <ul>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                        </ul>
                    </div> <!-- .span4 -->

                    <div class="span4">
                        <ul>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                        </ul>
                    </div> <!-- .span4 -->

                    <div class="span4">
                        <ul>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                        </ul>
                    </div> <!-- .span4 -->

                </div> <!-- .row -->

            </div> <!-- wrap -->

        </div> <!-- mydiv -->

And also:

<div id="mydiv" class="row">

            <div class="wrap">

                    <div class="span4">
                        <ul>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                        </ul>
                    </div> <!-- .span4 -->

                    <div class="span4">
                        <ul>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                        </ul>
                    </div> <!-- .span4 -->

                    <div class="span4">
                        <ul>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                            <li>Random link</li>
                        </ul>
                    </div> <!-- .span4 -->

            </div> <!-- wrap -->

        </div> <!-- mydiv -->

The "wrap" div has the following CSS:

.wrap {
padding: 35px;
background-color: #F7F7F7;
border: 1px solid #DDD;

}

But each time, the row either gets pushed down to the next line or doesn't wrap around the content at all.

I have previously managed to add padding by using an unsemantic div after the row but this time, it doesn't seem to work.

Could anyone point me in the right direction?

Thanks.


參考解法

方法 1:

Rather than adding an additional div for styling, you should just add another style to the row, e.g., row-padded, and pass new styles down to the children of it accordingly.  For instance:

CSS

.row-padded {
  padding: 35px;
  background-color: #F7F7F7;
  border: 1px solid #DDD;
}

.row-padded .span4 {
  background-color: #bbb;
  width: 30%;
}

HTML

<pre class="lang-html prettyprint-override"><div id="mydiv" class="container">

  <div class="row row-padded">

    <div class="span4">
      <ul>
        <li>Random link</li>
        <li>Random link</li>
        <li>Random link</li>
        <li>Random link</li>
        <li>Random link</li>
      </ul>
    </div> <!-- .span4 -->

    <div class="span4">
      <ul>
        <li>Random link</li>
        <li>Random link</li>
        <li>Random link</li>
        <li>Random link</li>
        <li>Random link</li>
      </ul>
    </div> <!-- .span4 -->

    <div class="span4">
      <ul>
        <li>Random link</li>
        <li>Random link</li>
        <li>Random link</li>
        <li>Random link</li>
        <li>Random link</li>
      </ul>
    </div> <!-- .span4 -->

  </div> <!-- .row -->

</div> <!-- container -->​
</code></pre>

JSFiddle

Note: You probably want to define a responsive explicit value (in px) for the .span4 .row-padded width rather than merely the percent width.  I only used the percent for brevity.

(by onjegoldersmerv)

參考文件

  1. Adding background color and padding to row in Twitter bootstrap (CC BY-SA 3.0/4.0)

#padding #row #twitter-bootstrap






相關問題

'android:paddingRight' 與 'android:drawableRight' 一起使用 (’android:paddingRight’ working with ‘android:drawableRight’)

雙行導航欄 - 是什麼原因造成的? (double row navigation bar- what's causing it?)

在 Twitter 引導程序中為行添加背景顏色和填充 (Adding background color and padding to row in Twitter bootstrap)

如何控制 addView 到佈局 (How to control addView to a layout)

Ці можа выкарыстанне працэнтаў для вышыні запаўнення/поля/межы быць лепшым, чым em? (Can using percentages for height of padding/margin/border be better than em?)

填充對絕對定位的下拉菜單有意想不到的影響 (Padding has unexpected effect on drop down menu with absolute postioning)

內部 div 相對於包裝器具有不需要的空間 (Inner div has unwanted space in relation to wrapper)

css @font-face 不會在所有瀏覽器中垂直對齊文本 (css @font-face doesn't align text vertically across all browsers)

填充佔用了額外空間,我希望我的 div 像引導 div 一樣 (padding is taking up extraspaces,i want my div's to act like bootstrap div's)

Android -- 使用帶有自定義背景的 ListView 時如何刪除那一點額外的填充? (Android -- How to remove that little extra padding when using a ListView with custom background?)

使用顯示時沒有底部填充:網格和滾動 (No bottom padding when using display: grid and scroll)

TCL 中的零填充 (zero padding in TCL)







留言討論