Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vertically center elements in Bulma?

Tags:

css

bulma

How can I vertically center this <div class="columns is-vcentered"> to that red colored section which is enclosing it?

And should I remove or add some classes here to improve this code? Please suggest me. Thanks!

I am new to CSS framework, never tried Bootstrap and instead opted for Bulma.

enter image description here

<section id="eyes" class="section">
    <div class="container">
        <div class="columns is-vcentered">
            <div class="column">
                <h1>Eyes on what matters</h1>
                <p>Backtick is made to look minimal, so you can focus on your code, always. From UI to linter errors, everything is kept non-obtrusive.</p>
            </div>
            <div class="column">
                <img class="image" src="img/roll.jpg" alt="">
            </div>
        </div>
    </div>
</section>

In CSS apart from coloring elements, I've only done this:

section {
    height: 70vh;
}
like image 510
Ajit Avatar asked Jul 04 '17 05:07

Ajit


People also ask

How do you vertically center in Bulma?

The Bulma Vertical alignment is used to align your columns vertically, add the is-vcentered modifier to the columns container. Bulma Vertical Alignment Class: is-vcentered: This class is used to center the columns vertically.

How do I center text vertically in an element?

To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.

How do you center a card in Bulma?

You can center the card-header-title by appending the is-centered modifier.


2 Answers

I think it's a bit funny that .columns does not have display:flex; by default (should it have perhaps?). Anyway, if you use something that adds flex, for example:

class="columns is-flex is-vcentered" 

then you get display:flex from is-desktop and now suddenly is-vcentered works as intended.

Also I think the semantics is off since is-vcentered suggests that it is columns that gets vertically centered. But what it actually does (from source):

.columns.is-vcentered {   -webkit-box-align: center;      -ms-flex-align: center;         align-items: center; } 

is to make children of columns to be vertically centered inside columns. So you likely also need to set a height of your columns element for this to work.

I think is-vcentered should be named something like has-vcentered-content, but perhaps I'm missing something obvious.

tl;dr; Add height and flex to the columns-element for is-vcentered to do something.

Sorry, I guess this is more of a extrapolation of the problem and not a solution.

I believe the real solution is probably to use the existing hero-class here. (Which by the way centers manually using paddings, just like in Peter Leger's answer!).

like image 195
ippi Avatar answered Sep 21 '22 12:09

ippi


Update: I came here from googling a way to vertically align items inside .content not .column class. Others might stumble on this place with the same reason.

If you're trying to vertically align elements inside .content class, here's what I did:

.content.is-vcentered {
  display: flex;
  flex-wrap: wrap;
  align-content: center; /* used this for multiple child */
  align-items: center; /* if an only child */
}

Note: This is quite useful for div's that have fixed height.

Here's an example html structure that it worked on

<div class="content is-vcentered has-text-centered">
  <h1>Content</h1>
  <p>that might be from</p>
  <p>wysiwyg containing</p>
  <p>multiple lines</p>
</div>

<div class="content is-vcentered">
  <p>Some text line</p>
</div>

Here's a sample jsfiddle

like image 28
vpibano Avatar answered Sep 25 '22 12:09

vpibano