Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 227
onjegolders Avatar asked Jul 31 '12 11:07

onjegolders


1 Answers

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

<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 -->​

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.

like image 165
merv Avatar answered Oct 26 '22 17:10

merv