Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating responsive divs in bootstrap

I'm experimenting with bootstrap and this is the code which I wrote:

<!doctype html>
<html lang="en">
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="./css/bootstrap.min.css">
        <link rel="stylesheet" href="./css/bootstrap-theme.min.css">
        <style type="text/css">
            .row-centered {
                text-align:center;
            }
            .col-centered {
                display:inline-block;
                float:none;
                text-align:left;
                margin-right:-4px;
            }
            .pos{
                position: relative;
                top: 240px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="row row-centered pos">
                <div class="col-lg-8 col-centered">
                    <div class="well"></div>
                </div>
                <div class="col-lg-8 col-centered">
                    <div class="well"></div>
                </div>
                <div class="col-lg-8 col-centered">
                    <div class="well"></div>
                </div>
            </div>
        </div>
    </body>
</html>

The output is as shown in screen shot:
img But when I convert to mobile screen, I get like this:
img1
I want the divs to appear in center with one above one in mobile screens as well. How can I do it? But it isn't responsive.

like image 753
Ajay Kulkarni Avatar asked Mar 13 '15 14:03

Ajay Kulkarni


3 Answers

you can use col-xs-12 in your div's , add this to each div and it will work like you expect it

LIVE DEMO

  <body>
        <div class="container">
            <div class="row row-centered pos">
                <div class="col-lg-8 col-xs-12 col-centered">
                    <div class="well"></div>
                </div>
                <div class="col-lg-8 col-xs-12 col-centered">
                    <div class="well"></div>
                </div>
                <div class="col-lg-8 col-xs-12 col-centered">
                    <div class="well"></div>
                </div>
            </div>
        </div>
    </body>

More about bootstrap grid option

like image 179
ZEE Avatar answered Oct 18 '22 20:10

ZEE


You can define multiple widths for screen sizes :

col-lg : large screen

col-md : middle screen

col-xs : small screen

In your example :

<div class="container">
    <div class="row row-centered pos">
        <div class="col-lg-8 col-xs-12 col-centered">
            <div class="well"></div>
        </div>
        <div class="col-lg-8 col-xs-12 col-centered">
            <div class="well"></div>
        </div>
        <div class="col-lg-8 col-xs-12 col-centered">
            <div class="well"></div>
        </div>
    </div>
 </div>
like image 37
Macks Avatar answered Oct 18 '22 19:10

Macks


What you have done with it, for me, is not bad.
you just need to modify the CSS, as follows:

    .row-centered {
        text-align:center;
        margin:0 auto;
    }
    .col-centered {
        display:block;
        float:none;
    }
    .pos {
        position: relative;
        top: 240px;
    }

When you would like to make it smaller, just reduce 8 to 7 or 6 and it's all done responsively. Using col-lg is true for large screen and it would not make it stuck using it for mobile performance.

like image 1
Joe Kdw Avatar answered Oct 18 '22 19:10

Joe Kdw