Skip to content Skip to sidebar Skip to footer

Centering Buttons From Different Divs

I have a question about styling: I want to align buttons from different divs which are all contained in a wrapper div:
Image Text... (button1) &

Solution 1:

UPDATE

You edited your question, so I updated my snippet. The principle is still the same as my original answer; you just need to apply the styles to the correct divs.

/* Begin changes */
.box, .contentWrapper {
  display: flex;
  flex-direction: column;
}

.content {
  flex-grow: 1;
  display: flex;
}

.contentWrapper {
  flex-grow: 1;
  justify-content: space-between;
}
/* End changes */

.container {
  display: flex;
}

.container > div {
  width: 200px;
}

.content {
  padding: 6% 8%;
}

.boxButton {
  text-align: center;
}

.image {
  margin-left: auto;
  margin-right: auto;
}
<div class="container">
  <div class="box">
    <div class="image">
      <img src=http://a1.mzstatic.com/us/r30/Purple/v4/47/1b/54/471b54ad-a6cf-f61e-2f33-11ffc6bd8215/icon100x100.jpeg>
    </div>
    <div class="content">
      <div class="contentWrapper">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sapien est, tempus eget aliquet nec, suscipit vitae lectus. Sed ut convallis lorem, non tincidunt sapien.</p>
        <p class="boxButton">
          <button>Button</button>
        </p>
      </div>
    </div>
  </div>
  <div class="box">
    <div class="image">
      <img src=http://a1.mzstatic.com/us/r30/Purple/v4/47/1b/54/471b54ad-a6cf-f61e-2f33-11ffc6bd8215/icon100x100.jpeg>
    </div>
    <div class="content">
      <div class="contentWrapper">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sapien est, tempus eget aliquet nec, suscipit vitae lectus.</p>
        <p class="boxButton">
          <button>Button</button>
        </p>
      </div>
    </div>
  </div>
  <div class="box">
    <div class="image">
      <img src="http://a1.mzstatic.com/us/r30/Purple/v4/47/1b/54/471b54ad-a6cf-f61e-2f33-11ffc6bd8215/icon100x100.jpeg">
    </div>
    <div class="content">
      <div class="contentWrapper">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
        <p class="boxButton">
          <button>Button</button>
        </p>
      </div>
    </div>
  </div>
</div>

Original Answer

If you only need to support modern browsers, there is a flexbox solution.

.box {
  /* The important stuff */
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

/* Not necessary for you; I did this to make the snippet look like your example. */
.container {
  display: flex;
}

.container > div {
  width: 200px;
}
<div class="container">
  <div class="box">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sapien est, tempus eget aliquet nec, suscipit vitae lectus. Sed ut convallis lorem, non tincidunt sapien.</p>
    <div>
      <button>Button</button>
    </div>
  </div>
  <div class="box">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sapien est, tempus eget aliquet nec, suscipit vitae lectus.</p>
    <div>
      <button>Button</button>
    </div>
  </div>
  <div class="box">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    <div>
      <button>Button</button>
    </div>
  </div>
</div>

The important part is to have each .box contain two elements. In this case, I put the text in a <p> and the button in a <div>. Then, use display: flex. justify-content: space-between pulls the <p> towards the top and the <div> towards the bottom.


Solution 2:

Absolutely positioning might be a good way to go, since all those elements are the same height (buttons, and the line of text after the buttons). Just add a container div around those elements.

wrapper {
  postition: relative; 
  padding-bottom: 150px; 
}
container {
  position:absolute; 
  bottom: 40px; 
}

The other option would be just setting a min-height on the paragraph tag before the button, so the elements above the button are all the same height.


Solution 3:

<html>
<head>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<div class="col-md-3 text-center"><a href="#" class="btn btn-info" role="button">Link Button</a></div>
<div class="col-md-3 text-center"><button type="button" class="btn btn-info">Button</button></div>
<div class="col-md-3 text-center"><input type="button" class="btn btn-info" value="Input Button"></div>
<div class="col-md-3 text-center"><input type="submit" class="btn btn-info" value="Submit Button"></div>
</body>
</html>

Solution 4:

just add this css to your button

button{
 bottom: -40px
    position: relative;}

Post a Comment for "Centering Buttons From Different Divs"