Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - Add background to some columns in grid

Ok, I am new with CSS and this is just causing me trouble. How do I add a background color to multiple columns but not all columns.

I want one background color on span2 and a different color on span10. The problem I run into is issues with padding. When I apply the background to certain columns it won't have a nice even padding around the content. Does this make sense? How do I add a background to certain columns with nested columns and still maintain nice even padding?

HTML

<div class="row bg">
  <div class="span10"> 

        <!-- Main hero unit for a primary marketing message or call to action -->
        <div class="sidebar">
            <div class="hero-unit">
                <h1>Join</h1>
                <p>This is a template for a simple marketing or informational website. It includes a large callout called the hero unit and three supporting pieces of content. Use it as a starting point to create something more unique.</p>
                <p><a class="btn btn-primary btn-large">Register Now &raquo;</a></p>
            </div>
        </div>

        <!-- Example row of columns -->
        <div class="row">
            <div class="span5">
                <div class="bot-pad">
                    <h2>We are Fun!</h2>
                    <p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
                    <p><a class="btn" href="#">View details &raquo;</a></p>
                </div>
            </div>
            <div class="span5">
                <div class="right-pad bot-pad">
                    <h2>Learn more about yourself</h2>
                    <p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
                    <p><a class="btn" href="#">View details &raquo;</a></p>
                </div>
            </div>
        </div>
    </div>
</div>

CSS

.bg
{
    background: url('/assets/img/grid.png');
    .border-radius(5px); //LESS mixin for creating border radius
}
.right-pad
{
    padding-right: 20px;
}

.bot-pad
{
    padding-bottom: 20px;
}

Explanation

So the bg class is applying the background. Then my nested columns have weird padding, so I went through and added classes like right-pad to the columns that need right padding and bot-pad to columns that need padding on the bottom. I know how incredibly wrong this is semantically, I just don't know how else to get my needed results. Thanks.

Here is another example of what I am trying to do... however they do not provide a solution either https://github.com/twitter/bootstrap/issues/1446

like image 748
zechdc Avatar asked Sep 26 '12 04:09

zechdc


1 Answers

It's not a good idea setting the background-color on the span# level. Why? Because span# is a positioning helper and if you change paddings or margins the entire grid will break.

So, what's the native Twitter Bootstrap element for rendering a coloured background wrapper? It's well.

How to use it?

First, insert the well element inside the span#:

<div class="span5">
    <div class="well well-red">
        <!-- Your content -->
    </div>
</div>

And second, assign properties through an extension of the .well class (I've used .well-red):

.well-red {
    background-color: #ff0000;
}

This way you have always available the parent class .well but you can apply different properties at will.

like image 93
albertedevigo Avatar answered Oct 06 '22 23:10

albertedevigo