How to align contents in different containers using only css


Tags: css,twitter-bootstrap,css3,twitter-bootstrap-3

Problem :

I am trying to build a Pricing Tablesimilar to one shown below.

on-full-width

Following are my requirements

  • The Get Started button should be on same horizontal level
  • The containers have to be of same height irrespective of the content (The no. of points & length of description in the two can vary as shown)
  • These two containers should stack one above other on low screen width
here is a minimum snippet for my current solution using JS

findTallest = function () {
  var tallestByGroupNum = {}
  $(".item-contents").each(
    function () {
      var grpNum = $(this).parent().attr('data-group-num');
      // !< instead of > to support undefined values
      if (!($(this).height() < tallestByGroupNum[grpNum] )) {
        tallestByGroupNum[grpNum] = $(this).height();
      }
    }
  );

  $(".item-box").height(
    function () {
      return tallestByGroupNum[$(this).attr('data-group-num')];
    }
  );
};

$(window).resize(findTallest);
findTallest();
/* Put your css in here */

.item-box {
  border: 1px solid red;
}
.pricingCard {
  border: 2px solid red;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>



<div class="container">
  <div class="col-xs-6 pricingCard">
    <h1>Free</h1>

    <div class="item-box" data-group-num="1">
      <div class="item-contents">
        Long long long long long long long long long long long long long long long long long description
      </div>
    </div>

    <button class="btn btn-primary">Get Started</button>

    <div class="item-box" data-group-num="2">
      <div class="item-contents">
        <div>Point</div>
        <div>Point</div>
        <div>Point</div>
      </div>
    </div>

    <button class="btn btn-primary">Get Started</button>
  </div>

  <div class="col-xs-6 pricingCard">
    <h1>Premium</h1>

    <div class="item-box" data-group-num="1">
      <div class="item-contents">
        Short description
      </div>
    </div>

    <button class="btn btn-primary">Get Started</button>

    <div class="item-box" data-group-num="2">
      <div class="item-contents">
        <div>Point</div>
        <div>Point</div>
        <div>Point</div>
        <div>Point</div>
        <div>Point</div>
        <div>Point</div>
      </div>
    </div>

    <button class="btn btn-primary">Get Started</button>
  </div>
</div>

To explain the problem, here's the snippet exluding the JS code. Note that the buttons don't align properly as before.

Also the solution suggested in the comments are similar to this only.

/* Put your css in here */

.item-box {
  border: 1px solid red;
}
.pricingCard {
  border: 2px solid red;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>



<div class="container">
  <div class="col-xs-6 pricingCard">
    <h1>Free</h1>

    <div class="item-box" data-group-num="1">
      <div class="item-contents">
        Long long long long long long long long long long long long long long long long long description
      </div>
    </div>

    <button class="btn btn-primary">Get Started</button>

    <div class="item-box" data-group-num="2">
      <div class="item-contents">
        <div>Point</div>
        <div>Point</div>
        <div>Point</div>
      </div>
    </div>

    <button class="btn btn-primary">Get Started</button>
  </div>

  <div class="col-xs-6 pricingCard">
    <h1>Premium</h1>

    <div class="item-box" data-group-num="1">
      <div class="item-contents">
        Short description
      </div>
    </div>

    <button class="btn btn-primary">Get Started</button>

    <div class="item-box" data-group-num="2">
      <div class="item-contents">
        <div>Point</div>
        <div>Point</div>
        <div>Point</div>
        <div>Point</div>
        <div>Point</div>
        <div>Point</div>
      </div>
    </div>

    <button class="btn btn-primary">Get Started</button>
  </div>
</div>

Again to emphasis on the problem here's the comparison enter image description here

Currently I am using javascript to achieve this. I detect the tallest element and then set the height of the parent container to that.

Here the link to the plnkr on my current implmentation is JS.

Can this be done in css alone (using some bootstrap classes).



Solution :

With flexbox, I'd do something like this:

* {
  box-sizing: border-box;
}

.pricing {
  background: #eee;
  display: flex;
  flex-direction: column;
  padding: 2em 0;
}
@media (min-width: 48em) {
  .pricing {
    flex-direction: row;
    flex-wrap: wrap;
  }
}
.pricing .block {
  display: flex;
  justify-content: center;
  flex: 1 1 50%;
}
@media (max-width: 48em) {
  .pricing .block {
    flex: 1;
  }
  .pricing .block:nth-child(odd) {
    order: -1;
  }
  .pricing .block:nth-child(2) {
    margin-top: 2em;
  }
}
.pricing header,
.pricing ul {
  background-color: white;
  box-shadow: 0 0 0 1px silver;
  padding: 1em;
  width: 80%;
}
.pricing header {
  display: flex;
  flex-direction: column;
}
.pricing button {
  margin-top: auto;
}

ul {
  margin: 0;
}
ul li {
  margin-left: 1em;
}

h1,
h2 {
  margin: 0.125em 0;
}

button {
  font: inherit;
}
<div class="pricing">
  <div class="block">
    <header>
      <h1>Free</h1>
      <h2>0$</h2>
      <p>short description</p>
      <button>Get Started</button>
    </header>
  </div>
  <div class="block">
    <header>
      <h1>Premium</h1>
      <h2>5$</h2>
      <p>long description long description long description long description long description long description</p>
      <button>Get Started</button>
    </header>
  </div>
  <div class="block">
    <ul>
      <li>Point 1</li>
      <li>Point 2</li>
      <li>Point 3</li>
    </ul>
  </div>
  <div class="block">
    <ul>
      <li>Point 1</li>
      <li>Point 2</li>
      <li>Point 3</li>
      <li>Point 4</li>
      <li>Point 5</li>
      <li>Point 6</li>
      <li>Point 7</li>
    </ul>
  </div>
</div>


    CSS Howto..

    Adding bootstrap tooltip modifies the style of the element itself - how do I stop this happening?

    How to stop CSS element style from getting into my class style?

    ng-bind-html adds owns css class 'ng-binding' which brokes all nested css, how get rid of it?

    How do I keep sibling divs from pushing each other down when I use “margin-top: 4px” on one of them?

    Implementing a select element in my form, but unsure how to grab user selection for a list

    Hide all, show a class with css

    Font Styling Issue ~ how to make font smooth/Strong in html css

    How to center two columns using CSS?

    How to invert the pentagon created using CSS?

    How to make a shrink-to-fit container with max-height and scrollbars in the content area?

    css: how to remove pseudo elements (after, before, …)

    How to properly preload images, js and css files?

    Android stock browser: How to prevent double tap closing browser

    How can I have a responsive height with absolutley position child elements

    how to achieve following using css ( digits input )

    How do i create a responsive page with fixed positioned header and side nav using only html css?

    How to rotate pseudo element css

    How to add an informative popup over a given text

    How to add a CSS class to an element using a jQuery Selector?

    How to replace a text-align: -webkit-center?

    How to get the “absolute” position of an html element

    How to put multiple Bootstrap inputs on same line?

    How to keep css id to the 'li' in css

    How to get inline-blocks to sit side-by-side?

    How to hide class=“” CSS

    How do I use jQuery to change the value of a css selector value?

    How to deal with `rowspan` and background colors within a table?

    How to show indicator of loading?

    CSS animations - How to toggle direction with one keyframes declaration

    How to use anchor links to scroll within a div without scrolling the main page