Divi Tutorial – Center Align Multiple Buttons

Divi Tutorial – Center Align Multiple Buttons

This one is a really useful tip that I think you’re gonna like.

Aligning buttons. Woot!

Note: This can apply to lots and lots of other cases, I might make more tutorials on flexbox, because I love it so much, but that’ll come later. After I finish this one.

Edit
If you want to learn flexbox and how to build using Divi with awesome responsiveness in mind, check out this course I made.

So, here’s where we start.

Let’s add three buttons. All in one column.

It looks pretty great at this moment.

Just like a test site should.

Now, to flex these bad boys.

First, let’s add a class to the section so we can use this in other places later.

Now to code the alignments.

Note: The final code will be at the end of this post, so you don’t have to copy paste every line separately. Just read along for now.

First, the way flexbox works is by changing the display type. But we need to make it filter down through the row and into the column where it’s needed.

.flexing-away .et_pb_row .et_pb_column {
	display: flex;
}

This gives us:

As you can see, the previous stacked layout is now on one line.

What flexbox does is it scrunches up all the elements inside its display area, and puts them all next to each other.
So we’ll need to align them. Let’s align them to the center both vertically and horizontally.

justify-content is used for horizontal alignment, and align-items is for vertical.

.flexing-away .et_pb_row .et_pb_column {
	display: flex;
	justify-content: center;
	align-items: center;
}

This gives us:

Yay! Progress!
So what we need to do now is to give the buttons some space between them.

There are a couple of ways of doing this..
1. Give the middle button margins to the left and right.
2. Give all the buttons half margins to the left and right.

It’s up to you on how to add the margins as well. You can do it through the Divi builder settings, separate css in the button’s custom css settings, add an nth item selector to the column, or just do it in what I think is the easiest way: just target all the buttons and give them some margins. Like this:

.flexing-away .et_pb_row .et_pb_column .et_pb_module {
	margin-left: .2rem; 
	margin-right: .2rem; 
}

The reason why I wrote the css as above is because
a. it’ll target the modules inside the sections you assigned with .flexing-away, and not any other modules
b. it’ll also target other modules if in such section, which you might need, and
c. I use rem units, but you can change it to ems, or px, it’s up to you. I personally like rem.

So here we have it!

Pretty nifty, eh?

But what about MOBILE????

Ah, yes. We can’t leave out mobile.

So we need to do two things:
1. make the buttons stack
2. make them 100% width with the text inside centered.

here’s the css for that

@media all and (max-width: 479px) {
	.flexing-away .et_pb_row .et_pb_column {
		flex-wrap: wrap; 
	}
	.flexing-away .et_pb_row .et_pb_column .et_pb_module {
		width: 100%; 
		text-align: center; 
	}
}

Of course, you can try resizing the browser, and see what you like and change the max-width pixel width to assign when the buttons are gonna stack.

and there you have it.

Note: all the css should be added in Divi > theme options > custom css for this new super power to be applied across the whole website.

Here’s the final code snippet. I added comments for you to easily modify.

.flexing-away .et_pb_row .et_pb_column {
	display: flex; /* add flexbox */
	justify-content: center; /* keep items centered horizontally */
	align-items: center; /* keep items centered vertically */
}
.flexing-away .et_pb_row .et_pb_column .et_pb_module {
	margin-left: .2rem; /* change to whatever pixels you want */
	margin-right: .2rem; /* it's good to have the same margin on either side */
}
@media all and (max-width: 479px) {
	.flexing-away .et_pb_row .et_pb_column {
		flex-wrap: wrap; /* let the items start switching lines */
	}
	.flexing-away .et_pb_row .et_pb_column .et_pb_module {
		width: 100%; /* no more space left after 100%, so the buttons will break lines */
		text-align: center; /* leave this line in if you want the text inside the button to be centered */
	}
}

 
[sc name=”customcss”]

 
[sc name=”learn css”]
 
[sc name=”responsive-ad”]
 
[sc name=”podcast”]
 

 

Edit:

Because it would seem fitting to show a few real life examples of how this works in the real world, I have added some examples.

 
[sc name=”learn css”]
 
[sc name=”responsive-ad”]
 

Stay tuned for more on Divi+flexbox, and some comprehensive flexbox tuts.

 
Photo by Ross Papas on Unsplash

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. 4.5

  2. Excellent tutorial, PK.

    What if you want to stack all the buttons on desktop but want them to look all the same size? Do you use flex-wrap in that case?

  3. 5

  4. Hi PK,

    Thank you for this clear tutorial!

    And what would be the difference if you work with 3 columns with 3 buttons in a row from let to right?
    When would you use your solution and when the other, can you provide examples?

    Thanks in advance 🙂

  5. Thanks, great tutorial.
    Have used flex in some situations but need to learn more.

  6. Hey and thx for this tutorial. took me long time before i found it 🙂

    but i have a problem with it . when i do it with 3 buttons the last one is lower than others. any idea ?

    https://imgur.com/a/Vc3zy

  7. Reply to Susan:

    Hi, thanks for the question.

    I checked your website just in case you were asking about your website, and I found some buttons that had the problem you mentioned.

    It turns out that the third button has a negative margin.

    .et_pb_button_2_wrapper {
    margin-top: -29px!important;
    }

    That would probably be generated from editing in the builder.

    A safe way to circumvent this problem on mobile would be to do something like this:

    @media all and (max-width: 479px) {
    .et_pb_section.flexing-away .et_pb_row .et_pb_column .et_pb_module.et_pb_button_2_wrapper {margin-top: 0px !important;}
    }

    That would probably safely override that problem.

    Feel free to email me if you’re having more problems.

  8. Thank you. Nice tutorial and solution. In my case, It is not usable due to other elements in the row.

    I found the following solution:

    1. Add “button-center” to CSS class in the row settings.
    2. Add this to custom CSS:
    .button-center .et_pb_column {
    text-align: center !important;
    }
    .button-center .et_pb_button_module_wrapper {
    display: inline;
    }

  9. Hello,
    I was looking for solutions to fix the alignment of the 2nd button when viewed on mobile, the 2nd button was added by Divi Booster.
    I have applied the .flexing-away to the Fullwidth Slider CSS but it did not fix the problem.

    Thanks in advance for your time in reviewing my site.

  10. Hi PK,
    Thanks for your prompt reply. I’ve applied the “flexing-away” via the Fullwidth Slider Module CSS Class, and its code under Divi -Theme Option – General – Custom CSS.

    The problem is with the 1st Slide that contains 2-buttons; Sign-Up & i-Chess. These buttons are not aligned when viewed on Mobile
    .

  11. Hi PK,
    The url is http://TheKnightsChessClub.com
    It’s the homepage, 1st slide. I’ll unlock the content when I get home so you can inspect the source elements.

    Thanks

  12. Hello PK,
    Thanks a million. I was able to adjust the size of the buttons using your recommendations. I am satisfied of the new look when viewed on mobile.
    Glad to have you on the net. Am sure this info would also be essential to other developers.

    I’ll be visiting your site often to keep up with good info.

    Regards

New tutorials

Use a featured video in post templates with Breakdance
ACF repeater fields as accordions in Breakdance
Breakdance 2 vs. Divi 4
Breakdance 2 for Agencies

Why no ads?

Hi, I'm PK, and I maintain and publish content on this site for two reasons.

  1. To give back to the community. I learned so much from people's tutorials and StackOverflow that I wanted to contribute and help others.

  2. To provide a more structured learning experience. People struggle to find the course that guides them from start to finish, based off of real life experience, and my courses provide that.

The only "ads" I have here are for my own courses, and maybe an affiliate link, but that's it. They fund the website resources and provide more motivation for me to produce better content.

Any bit of interest helps. Even sharing with your friends, suggesting my courses to your developer or designer, or subscribing to my YT channel, or joining our Discord. Thanks and I'll see you around!

There's a newsletter!

Sign up to the newsletter for the occasional updates on courses, products, tutorials, and sales.