Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap: how to position an element horizontally anywhere in a grid design?

I am learning Bootstrap. I am using Bootstrap for a design with fixed width. From the online documentation, I know it is easy to position elements starting from a column. For example:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Bootstrap version</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <style type="text/css">
        body {
            padding-top: 60px;
            padding-bottom: 40px;
        }
    </style>
    <link href="css/bootstrap-responsive.css" rel="stylesheet">

    <link href="learn.css" rel="stylesheet">

    <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
        <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

</head>

<body>


<div class="container">
    <div class="row">
    <div class="span3" style="background-color:grey">span3</div>
    <div class="span5" style="background-color:pink"><div id="text">span5 text</div></div>
    <div class="span4" style="background-color:navy">span4</div>
    </div>
</div> 


    <!-- Le javascript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="js/jquery-1.9.1.min.js"><\/script>')</script>    
    <script src="js/bootstrap.min.js"></script> 
</body>
</html>

In the above,

<div id="text">span5 text</div>

horizontally starts exactly where the fourth column starts. I would like to know how Bootstrap expert users use CSS to position

<div id="text">span5 text</div>

to make it start between the third and fourth columns or ends between the eighth and ninth columns.

I am thinking about using either:

<style>
#text {
margin-left:-10px;
}
</style>

or

<style>
#text {
    position:relative;
    left:-10px;
}
</style>

to position the div, but not sure whether which is preferred and would like to know what experts do and some other ways.

Thanks!

like image 447
curious1 Avatar asked Jun 13 '13 04:06

curious1


2 Answers

By default, there is always a margin-left (or gutter) between the Bootstrap spans (see here: http://bootply.com/64463). If you want to override this, it is possible with CSS, but then you'll lose the responsive benefits of Bootstrap, because the element will be removed from the normal page flow.

A better option may be a custom Bootstrap build with no gutter: http://twitter.github.io/bootstrap/customize.html#variables

Or, you can create a custom CSS class that you apply to rows as needed like this Gutter-less example: http://bootply.com/61712

like image 109
Zim Avatar answered Oct 04 '22 13:10

Zim


I use this solution, that working for me:

<div class="jumbotron" style="height:100%;">
  <div>
    <table style="height:100%;width:100%">
        <thead>
            <tr>
                <td>
                    <div class="row">
                        <div class="col-md-6">
                            head 1
                        </div>
                        <div class="col-md-6">
                            head 2
                        </div>
                    </div>
                </td>
            </tr>
        </thead>

        <tbody>
            <tr>
                <td>
                    <div class="row">
                        <div class="col-md-6">
                            body 1
                        </div>
                        <div class="col-md-6">
                            body 2
                        </div>
                    </div>
                </td>
            </tr>
        </tbody>

        <tfoot>
            <tr>
                <td>
                    <div class="row">
                        <div class="col-md-6">
                            foot 1
                        </div>
                        <div class="col-md-6">
                            foot 2
                        </div>
                    </div>
                </td>
            </tr>
        </tfoot>
    </table>
</div>

enter image description here

like image 24
Hossein Ganjyar Avatar answered Oct 04 '22 13:10

Hossein Ganjyar