Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning multiple div boxes horizontally and vertically

I'm using https://almsaeedstudio.com/preview theme which gives some brilliant boxes layout and social widget boxes layout which I want to use in my project.

Refer to simple box screenshot

and social widget box

screenshot.

I'm trying to arrange multiple simple boxes horizontally where each of the simple box can contain multiple social widget boxes.

Refer to this screenshot for more clarity:

screenshot.

I tried playing with the exiting simple boxes and social widget boxes code and come up with this snippet.

I have created this plunker, somehow css is not getting loaded properly.

<!DOCTYPE html>  <html>      <head>      <link rel="stylesheet" href="style.css">      <script src="script.js"></script>    </head>      <body>      <div class="row">    <div class="col-md-12">      <div style="overflow:auto;">        <div class="" style="width:2050px;">          <div class="box" style="display:inline-block;width:1000px;">            <div class="box-header with-border">              <h3 class="box-title">Monthly Recap Report</h3>              <div class="box-tools pull-right">                <button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>                </button>                <div class="btn-group">                  <button class="btn btn-box-tool dropdown-toggle" data-toggle="dropdown"><i class="fa fa-wrench"></i>                  </button>                  <ul class="dropdown-menu" role="menu">                    <li><a href="#">Action</a>                    </li>                    <li><a href="#">Another action</a>                    </li>                    <li><a href="#">Something else here</a>                    </li>                    <li class="divider"></li>                    <li><a href="#">Separated link</a>                    </li>                  </ul>                </div>                <button class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i>                </button>              </div>            </div>            <!-- /.box-header -->            <div class="box-body" style="display: block;">              <div class="">                      <div class="box box-widget collapsed-box">                  <hr>                  <div class="box-header with-border">                    <div class="user-block">                      <img src="../dist/img/photo2.png" alt="Photo" class="img-responsive pad"><span class="username"><a href="#">Jonathan Burke Jr.</a></span><span class="description">7:30 PM Today</span>                    </div>                    <!-- /.user-block-->                    <div class="box-tools">                      <button data-widget="collapse" class="btn btn-box-tool"><i class="fa fa-plus"></i>                      </button>                    </div>                    <!-- /.box-tools-->                  </div>                  <!-- /.box-header-->                  <div class="box-body" style="display: block;">                    <p>I took this photo this morning. What do you guys think?</p>                    <button class="btn btn-default btn-xs"><i class="fa fa-thumbs-o-up"></i> Like</button><span class="pull-right text-muted">127 likes - 3 comments</span>                  </div>                  <!-- /.box-body-->                  <div class="box-footer box-comments" style="display: block;">                    <div class="box-comment">                      <!-- User image-->                      <img src="../dist/img/photo2.png" alt="Photo" class="img-responsive pad">                      <div class="comment-text"><span class="username">Maria Gonzales<span class="text-muted pull-right">8:03 PM Today</span></span>                        <!-- /.username-->It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.                      </div>                      <!-- /.comment-text-->                    </div>                    <!-- /.box-comment-->                    <div class="box-comment">                      <!-- User image-->                      <img class="img-responsive img-circle img-sm" src="../dist/img/user4-128x128.jpg" alt="alt text">                      <div class="comment-text"><span class="username">Luna Stark<span class="text-muted pull-right">8:03 PM Today</span></span>                        <!-- /.username-->It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.                      </div>                      <!-- /.comment-text-->                    </div>                    <!-- /.box-comment-->                  </div>                  <!-- /.box-footer-->                  <div class="box-footer" style="display: block;">                    <form>                      <img class="img-responsive img-circle img-sm" src="../dist/img/user4-128x128.jpg" alt="alt text">                      <div class="img-push">                        <input type="text" placeholder="Press enter to post comment" class="form-control input-sm">                      </div>                    </form>                  </div>                  <!-- /.box-footer-->                </div>                <div class="box box-widget collapsed-box">                  <hr>                  <div class="box-header with-border">                    <div class="user-block">                      <img src="../dist/img/photo2.png" alt="Photo" class="img-responsive pad"><span class="username"><a href="#">Jonathan Burke Jr.</a></span><span class="description">7:30 PM Today</span>                    </div>                    <!-- /.user-block-->                    <div class="box-tools">                      <button data-widget="collapse" class="btn btn-box-tool"><i class="fa fa-plus"></i>                      </button>                    </div>                    <!-- /.box-tools-->                  </div>                  <!-- /.box-header-->                  <div class="box-body" style="display: block;">                    <p>I took this photo this morning. What do you guys think?</p>                    <button class="btn btn-default btn-xs"><i class="fa fa-thumbs-o-up"></i> Like</button><span class="pull-right text-muted">127 likes - 3 comments</span>                  </div>                  <!-- /.box-body-->                  <div class="box-footer box-comments" style="display: block;">                    <div class="box-comment">                      <!-- User image-->                      <img src="../dist/img/photo2.png" alt="Photo" class="img-responsive pad">                      <div class="comment-text"><span class="username">Maria Gonzales<span class="text-muted pull-right">8:03 PM Today</span></span>                        <!-- /.username-->It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.                      </div>                      <!-- /.comment-text-->                    </div>                    <!-- /.box-comment-->                    <div class="box-comment">                      <!-- User image-->                      <img class="img-responsive img-circle img-sm" src="../dist/img/user4-128x128.jpg" alt="alt text">                      <div class="comment-text"><span class="username">Luna Stark<span class="text-muted pull-right">8:03 PM Today</span></span>                        <!-- /.username-->It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.                      </div>                      <!-- /.comment-text-->                    </div>                    <!-- /.box-comment-->                  </div>                  <!-- /.box-footer-->                  <div class="box-footer" style="display: block;">                    <form>                      <img src="../dist/img/photo2.png" alt="Photo" class="img-responsive pad">                      <div class="img-push">                        <input type="text" placeholder="Press enter to post comment" class="form-control input-sm">                      </div>                    </form>                  </div>                  <!-- /.box-footer-->                </div>                  </div>              <!-- /.row -->            </div>            <!-- ./box-body -->            <div class="box-footer" style="display: block;">              <!-- /.row -->            </div>            <!-- /.box-footer -->          </div>          <!-- /.box -->            <div class="box" style="display:inline-block;width:1000px;">            <div class="box-header with-border">              <h3 class="box-title">Monthly Recap Report</h3>              <div class="box-tools pull-right">                <button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>                </button>                <div class="btn-group">                  <button class="btn btn-box-tool dropdown-toggle" data-toggle="dropdown"><i class="fa fa-wrench"></i>                  </button>                  <ul class="dropdown-menu" role="menu">                    <li><a href="#">Action</a>                    </li>                    <li><a href="#">Another action</a>                    </li>                    <li><a href="#">Something else here</a>                    </li>                    <li class="divider"></li>                    <li><a href="#">Separated link</a>                    </li>                  </ul>                </div>                <button class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i>                </button>              </div>            </div>            <!-- /.box-header -->            <div class="box-body" style="display: block;">              <div class="">                      <div class="box box-widget collapsed-box">                  <hr>                  <div class="box-header with-border">                    <div class="user-block">                      <img src="../dist/img/photo2.png" alt="Photo" class="img-responsive pad"><span class="username"><a href="#">Jonathan Burke Jr.</a></span><span class="description">7:30 PM Today</span>                    </div>                    <!-- /.user-block-->                    <div class="box-tools">                      <button data-widget="collapse" class="btn btn-box-tool"><i class="fa fa-plus"></i>                      </button>                    </div>                    <!-- /.box-tools-->                  </div>                  <!-- /.box-header-->                  <div class="box-body" style="display: block;">                    <p>I took this photo this morning. What do you guys think?</p>                    <button class="btn btn-default btn-xs"><i class="fa fa-thumbs-o-up"></i> Like</button><span class="pull-right text-muted">127 likes - 3 comments</span>                  </div>                  <!-- /.box-body-->                  <div class="box-footer box-comments" style="display: block;">                    <div class="box-comment">                      <!-- User image-->                      <img class="img-responsive img-circle img-sm" src="../dist/img/user4-128x128.jpg" alt="alt text">                      <div class="comment-text"><span class="username">Maria Gonzales<span class="text-muted pull-right">8:03 PM Today</span></span>                        <!-- /.username-->It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.                      </div>                      <!-- /.comment-text-->                    </div>                    <!-- /.box-comment-->                    <div class="box-comment">                      <!-- User image-->                      <img src="../dist/img/user4-128x128.jpg" alt="user image" class="img-circle img-sm">                      <div class="comment-text"><span class="username">Luna Stark<span class="text-muted pull-right">8:03 PM Today</span></span>                        <!-- /.username-->It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.                      </div>                      <!-- /.comment-text-->                    </div>                    <!-- /.box-comment-->                  </div>                  <!-- /.box-footer-->                  <div class="box-footer" style="display: block;">                    <form>                      <img class="img-responsive img-circle img-sm" src="../dist/img/user4-128x128.jpg" alt="alt text">                      <div class="img-push">                        <input type="text" placeholder="Press enter to post comment" class="form-control input-sm">                      </div>                    </form>                  </div>                  <!-- /.box-footer-->                </div>                <div class="box box-widget collapsed-box">                  <hr>                  <div class="box-header with-border">                    <div class="user-block">                      <img src="../dist/img/photo2.png" alt="Photo" class="img-responsive pad"><span class="username"><a href="#">Jonathan Burke Jr.</a></span><span class="description">7:30 PM Today</span>                    </div>                    <!-- /.user-block-->                    <div class="box-tools">                      <button data-widget="collapse" class="btn btn-box-tool"><i class="fa fa-plus"></i>                      </button>                    </div>                    <!-- /.box-tools-->                  </div>                  <!-- /.box-header-->                  <div class="box-body" style="display: block;">                    <p>I took this photo this morning. What do you guys think?</p>                    <button class="btn btn-default btn-xs"><i class="fa fa-thumbs-o-up"></i> Like</button><span class="pull-right text-muted">127 likes - 3 comments</span>                  </div>                  <!-- /.box-body-->                  <div class="box-footer box-comments" style="display: block;">                    <div class="box-comment">                      <!-- User image-->                      <img src="../dist/img/photo2.png" alt="Photo" class="img-responsive pad">                      <div class="comment-text"><span class="username">Maria Gonzales<span class="text-muted pull-right">8:03 PM Today</span></span>                        <!-- /.username-->It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.                      </div>                      <!-- /.comment-text-->                    </div>                    <!-- /.box-comment-->                    <div class="box-comment">                      <!-- User image-->                      <img src="../dist/img/user4-128x128.jpg" alt="user image" class="img-circle img-sm">                      <div class="comment-text"><span class="username">Luna Stark<span class="text-muted pull-right">8:03 PM Today</span></span>                        <!-- /.username-->It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.                      </div>                      <!-- /.comment-text-->                    </div>                    <!-- /.box-comment-->                  </div>                  <!-- /.box-footer-->                  <div class="box-footer" style="display: block;">                    <form>                      <img src="../dist/img/photo2.png" alt="Photo" class="img-responsive pad">                      <div class="img-push">                        <input type="text" placeholder="Press enter to post comment" class="form-control input-sm">                      </div>                    </form>                  </div>                  <!-- /.box-footer-->                </div>                  </div>              <!-- /.row -->            </div>            <!-- ./box-body -->            <div class="box-footer" style="display: block;">              <!-- /.row -->            </div>            <!-- /.box-footer -->          </div>            </div>        <!-- /.col -->      </div>    </div>  </div>    </body>    </html>

http://plnkr.co/edit/slpJLIRVGfMSC8JWG1bT?p=preview

But its not working. Can anyone please help me how to accomplish this ?

P.S.: I have searched on internet and found similar threads but none is working for me. Horizontally align div without float

I'm still a beginner in CSS and would really appreciate if I can get some help here. I'm breaking my head on this for a long time.

Update

I think it makes sense to clearly write out the actual issues and try to solve them one by one.

  1. Horizontal boxes are not aligned on the same row if the inner social widget box is collapsed/expanded. How can I ensure the height of the horizontal box is fixed irrespective of the inner social widget box height ? Refer to screenshot for same. Mis-alignment of horizontal boxes
  2. There are some answers which mention the use of display: float:left; but my issue is the variable width which actually ensures all horizontal boxes on the same row.
  <div class="" style="width:2050px;"> 

How do I ensure the width:2050px; to increase dynamically as I will be adding inner boxes on fly. P.S.: I'm using angularjs for ui. Is there any CSS trick which is independent of the width:2050px; That way there will be no dependency on the total width calculation.

  1. How to fix the height of inner social widget box ? The inner social widget box overflows the actual horizontol container. how can I fix this ?

  2. Sharing an image of what actually I'm trying to accomplish. Multiple Timelines.

In short I want to accomplish point 4 with this theme's existing boxes and social widget boxes. If there is any other better way of doing this, please share the same.

In case anything is not clear, please feel free to mention it in comment. I'll update the question accordingly.

Thanks

Update 2:

I think same height columns is what making this problem more complicated. What I can do is having a scroll bar inside horizontol box which can have multiple social widgets boxes. That way we can have a fixed height for each of the horizontol column.

Update 3:

While zer00ne@ has provided one solution which is based on Flex. I have read on some forums that it doesn;t work on all browsers. Since my web-page is going to be mobile friendly, I;m more inclined towards achieving my desired results using general CSS techniques.

In path of achieving my result, I created following version http://plnkr.co/edit/awVmJWJo0AdrQvdbXG2y?p=preview using this SO thread. Following is screenshot of same:

enter image description here

Now I'm facing one issue of text getting out of inner social widget box. I need some help on this thing.

In addition to that, can people take a review of these if this solution is any better or not ?

Thanks

like image 422
dark_shadow Avatar asked Sep 05 '15 14:09

dark_shadow


People also ask

How do you vertically and horizontally align a div?

You can do this by setting the display property to “flex.” Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do I align a div with horizontally?

To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.


2 Answers

>>>>>>>>>>>>>>>>>>>>FLEXBOX SOLUTION<<<<<<<<<<<<<<<<<<<<

Here is the REAL SOLUTION to the ORIGINAL QUESTION if anyone is actually interested.

dark_shadow:

While zer00ne@ has provided one solution which is based on Flex.

Problem resolved see my demos below, it speaks for itself. I have no idea why starikovs is getting upvotes at all when there is clearly no solution provided.

I had to recreate the page because the extra classless <div>s you placed inside the markup was confusing. The significant change was adding flexbox to the layout. I used two flexbox containers, one that controlled the two columns .flexRow and another inside of each column to control the widgetboxes, .flexCol. Those classless <div>s are combined into a <section class="colWrap" I added intrinsic measurements so that your layout isn't stuck at a fixed width of 2050px, you'll still need to adjust both .box to an intrinsic measurement, 1000px fixed is going to give grief in the future. The changes will be annotated when I get back. Unless of course this isn't what you wanted?

LAST to the LAST UPDATE

>>>>>>>>>>PLUNKER<<<<<<<<<<

EDIT

Just add a fixed height to .colWrap, suggest 100vh to 150vh

I checked out the height of both columns and they are in fact identical down to the decimal. See the screenshots:

Column 1

Column 2


OLD

You just need everything aligned, correct? Ok, look here please: http://embed.plnkr.co/MRI69qLoTkiL9F68g54M/preview

I added this to the <head>

<!DOCTYPE html> <html>    <head>     <base href="https://almsaeedstudio.com/themes/AdminLTE/">     <link href="https://almsaeedstudio.com/themes/AdminLTE/bootstrap/css/bootstrap.min.css" rel="stylesheet"/>     <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet"/>     <link href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet"/> 

UPDATE

Added the script as well. It's located before the closing </body> tag.

<script src="plugins/jQuery/jQuery-2.1.4.min.js"></script> <!-- Bootstrap 3.3.5 --> <script src="bootstrap/js/bootstrap.min.js"></script> <!-- FastClick --> <script src="plugins/fastclick/fastclick.min.js"></script> <!-- AdminLTE App --> <script src="dist/js/app.min.js"></script> <!-- Sparkline --> <script src="plugins/sparkline/jquery.sparkline.min.js"></script> <!-- jvectormap --> <script src="plugins/jvectormap/jquery-jvectormap-1.2.2.min.js"></script> <script src="plugins/jvectormap/jquery-jvectormap-world-mill-en.js"></script> <!-- SlimScroll 1.3.0 --> <script src="plugins/slimScroll/jquery.slimscroll.min.js"></script> <!-- ChartJS 1.0.1 --> <script src="plugins/chartjs/Chart.min.js"></script> <!-- AdminLTE dashboard demo (This is only for demo purposes) --> <script src="dist/js/pages/dashboard2.js"></script> <!-- AdminLTE for demo purposes --> <script src="dist/js/demo.js"></script> 

You probably don't need all of them, but the essential ones are:

  • bootstrap.min.css
  • font-awesome.min.css
  • jQuery-2.1.4.min.js
  • bootstrap.min.js
  • app.min.js
  • jquery.slimscroll.min.js

There's a lot of relative URLs (ex. ../dist/img/photo2.png), so I added the following to the top of the <head>:

<base href="https://almsaeedstudio.com/themes/AdminLTE/">

The majority of these external files are located at that base url. If the download package didn't properly provide adequate assets, I always go to the source of the site's demo. Frequently the developer(s) neglect the differences between the dist and the demo.

UPDATE

As I understand the problem is that the layout needs to be properly aligned with widgetboxes or in the absence of widgetboxes. I don't think using display:none on widgetboxes is the way this template was designed. Consider the following annotated excerpts from the file, app.min.js

Excerpts from the AdminLTE script, app.min.js

Notes at the bottom.

/*! AdminLTE app.js  * ================  * Main JS application file for AdminLTE v2. This file  * should be included in all pages. It controls some layout  * options and implements exclusive AdminLTE plugins.ᵃ  * /*...*/†   $.AdminLTE.boxWidget = {     selectors: $.AdminLTE.options.boxWidgetOptions.boxWidgetSelectors,     icons: $.AdminLTE.options.boxWidgetOptions.boxWidgetIcons,     animationSpeed: $.AdminLTE.options.animationSpeed,     activate: function (a) {       var b = this;       a || (a = document), $(a).on("click", b.selectors.collapse,         function (a) {           a.preventDefault(), b.collapse($(this))         }), $(a).on("click", b.selectors.remove, function (a) {         a.preventDefault(), b.remove($(this))       })     },    ᵇcollapse: function (a) {       var b = this,         c = a.parents(".box").first(),         d = c.find(           "> .box-body, > .box-footer, > form  >.box-body, > form > .box-footer"         );       c.hasClass("collapsed-box") ? (a.children(":first").removeClass(         b.icons.open).addClass(b.icons.collapse), d.slideDown(         b.animationSpeed,         function () {           c.removeClass("collapsed-box")         })) : (a.children(":first").removeClass(b.icons.collapse)         .addClass(b.icons.open), d.slideUp(b.animationSpeed,           function () {             c.addClass("collapsed-box")           }))     },    ᶜ remove: function (a) {       var b = a.parents(".box").first();       b.slideUp(this.animationSpeed)     }   } } if("undefined" == typeof jQuery) throw new Error(   "AdminLTE requires jQuery");  /*...*/†   ᵈ function (a) {     "use strict";     a.fn.boxRefresh = function (b) {       function c(a) {         a.append(f), e.onLoadStart.call(a)       }        function d(a) {         a.find(f).remove(), e.onLoadDone.call(a)       }       var e = a.extend({           trigger: ".refresh-btn",           source: "",           onLoadStart: function (a) {             return a           },           onLoadDone: function (a) {             return a           }         }, b),         f = a(           '<div class="overlay"><div class="fa fa-refresh fa-spin"></div></div>'         );       return this.each(function () {         if("" === e.source) return void(window.console &&           window.console.log(             "Please specify a source first - boxRefresh()")         );         var b = a(this),           f = b.find(e.trigger).first();         f.on("click", function (a) {           a.preventDefault(), c(b), b.find(".box-body").load(             e.source,             function () {               d(b)             })         })       })     }   }(jQuery),   function (a) {     "use strict";     a.fn.activateBox = function () {       a.AdminLTE.boxWidget.activate(this)     }   }(jQuery)  function (a) {     "use strict";     a.fn.boxRefresh = function (b) {       function c(a) {         a.append(f), e.onLoadStart.call(a)       }        function d(a) {         a.find(f).remove(), e.onLoadDone.call(a)       }       var e = a.extend({           trigger: ".refresh-btn",           source: "",           onLoadStart: function (a) {             return a           },           onLoadDone: function (a) {             return a           }         }, b),         f = a(           '<div class="overlay"><div class="fa fa-refresh fa-spin"></div></div>'         );       return this.each(function () {         if("" === e.source) return void(window.console &&           window.console.log(             "Please specify a source first - boxRefresh()")         );         var b = a(this),           f = b.find(e.trigger).first();         f.on("click", function (a) {           a.preventDefault(), c(b), b.find(".box-body").load(             e.source,             function () {               d(b)             })         })       })     }   }(jQuery),   function (a) {     "use strict";     a.fn.activateBox = function () {       a.AdminLTE.boxWidget.activate(this)     }   }(jQuery) 

† This code is skipped over

ᵃ The developer implies that this app is not a complete solution but a complete solution is available to buy.

ᵇ The boxwidgets collapse and height should adjust accordingly.

ᶜ The boxwidgets can be removed and height should be adjusted accordingly.

ᵈ The function boxRefresh() is a public method I believe. It could be used after an addition or subtraction of a widget I suppose.

I'm not the best at interpreting third party plugins, so any extra observations and/or corrections are welcome.

LAST UPDATE

I got it so when any section is collapsed, they will slide up rather than down. As for the 2 main columns, they behave as they should and if the first column is actually removed, then the second column will take the first column's place.

like image 149
zer00ne Avatar answered Oct 05 '22 06:10

zer00ne


  1. The new way of aligning is to use flexbox. Here's a simple example to show the power:

HTML:

<div class="wrapper">     <div></div>     <div></div>     <div></div> </div> 

CSS:

.wrapper {     display: flex; } 

Now your divs inside .wrapper are aligned in a row.

  1. BTW, you can use Autoprefixer to get the right browser prefixes.

  2. Flexbox is supported by all the major browsers: http://caniuse.com/#search=flexbox (with prefixes)

With flexbox you can align items as you want simply, aligning them vertically in the center, horizontally in the center, etc.

like image 34
starikovs Avatar answered Oct 05 '22 05:10

starikovs