How to vertically center elements in Bulma? - css

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.
<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;
}

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!).

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

The columns are not vertically centered because you have used a height for the section. Use padding to increase the height.
Remove the class .section (in Bulma)
.section {
background-color: white;
padding: 3rem 1.5rem;
}
and use your own padding.
Example
.red-bg {
background: red;
}
.orange-bg {
background: orange;
}
section {
padding: 100px 15px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.4.3/css/bulma.css" rel="stylesheet" />
<section class="red-bg">
<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="http://placehold.it/400x300" alt="">
</div>
</div>
</div>
</section>
<section class="orange-bg">
<div class="container">
<div class="columns is-vcentered">
<div class="column">
<img class="image" src="http://placehold.it/400x300" alt="">
</div>
<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>
</div>
</section>
<section class="red-bg">
<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="http://placehold.it/400x300" alt="">
</div>
</div>
</div>
</section>
POSTSCRIPT
You can use .columns is-vcentered two times. In that case you can set an height for the section.
section {
height: 70vh;
padding: 15px;
}
.red-bg {
background: red;
}
.orange-bg {
background: orange;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.4.3/css/bulma.css" rel="stylesheet" />
<section class="columns is-vcentered is-centered red-bg">
<div class="container">
<div class="columns is-vcentered is-centered">
<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="http://placehold.it/400x300" alt="">
</div>
</div>
</div>
</section>
<section class="columns is-vcentered orange-bg">
<div class="container">
<div class="columns is-vcentered">
<div class="column">
<img class="image" src="http://placehold.it/400x300" alt="">
</div>
<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>
</div>
</section>
<section class="columns is-vcentered red-bg">
<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="http://placehold.it/400x300" alt="">
</div>
</div>
</div>
</section>

Just add .is-flex class on the div.columns and both .is-centered and .is-vcentered will be available (Bulma v.0.7.1):
https://bulma.io/documentation/modifiers/responsive-helpers/
Here's my fully working example with a component centered in the screen:
<template>
<div class="columns is-flex is-vcentered is-centered">
<div class="column is-two-thirds-mobile is-half-tablet">
<Login />
</div>
</div>
</template>
You can also nest columns to the same effect as demonstrated here: Nesting columns.

A little late to the party but I think the best solution in this case is to use margin: auto;.

Related

Why does my division styling style 2 child elements even though i placed a first-child tag? [duplicate]

This question already has an answer here:
Why doesn't nth-of-type/nth-child work on nested elements?
(1 answer)
Closed 6 months ago.
So i have a div with 2 columns and images in both of them,and i want only the first image to have a margin.
HTML code:
<div class="column">
<div class="center">
<img src="assets/images/images/crop-1.png">
</div>
</div>
<div class="column">
<div class="center">
<img src="assets/images/images/sliced-2.png">
</div>
</div>
CSS portion:
div.center img:first-child{
margin-left:100px;
}
The only problem is,the first child selector isn't working as intended and styles both of my images and give them a margin...How do i Fix this?
You css isn't working because both of your img elements are the first child inside of their parent.
What you maybe want is to add a margin to the image inside of the first column.
Something like
.column:first-child img {
margin-left: 100px;
}
Without seeing the rest of your html i cannot know if this will work correctly. It will only work if the first column is indeed the first child inside its parent.
Another solution would be for you to add classes to your images, something like
<div class="column">
<div class="center">
<img class="image image--with-margin" src="assets/images/images/crop-1.png">
</div>
</div>
<div class="column">
<div class="center">
<img class="image" src="assets/images/images/sliced-2.png">
</div>
</div>
and then
.image--with-margin {
margin-left: 100px;
}
you can wrap the columns in a container div and target only the first column
html:
<div class="container">
<div class="column">
<div class="center">
<img src="assets/images/images/crop-1.png">
</div>
</div>
<div class="column">
<div class="center">
<img src="assets/images/images/sliced-2.png">
</div>
</div>
</div>
and css:
.container .column:first-of-type img {
margin-left: 100px;
}

Image out of the container

<div class="container p0">
<div class="col-md-12 p0">
<div class="col-md-6 p0">
<div class="imagen"></div>
</div>
<div class="col-md-6 p0">
<div class="text-content"></div>
</div>
</div>
</div>
I do not have a definite css yet, I still think how I can do this
How can I make the image full width inside a container, just like in the example using bootstrap, The black border is the bootstrap container
What I try to do is the content in a container and the image outside of that container,
Just set these properties--
.imagen img{
width: 100%;
max-width: 100%;
}
Here you go with jsfiddle https://jsfiddle.net/9drawa3h/1/.
<div class="container p0">
<div class="col-md-12 p0">
<div class="col-md-6 p0">
<div class="imagen"></div>
</div>
<div class="col-md-6 p0">
<div class="text-content">
<img src="http://a57.foxnews.com/images.foxnews.com/content/fox-news/tech/2013/04/13/how-to-do-free-online-background-check/_jcr_content/par/featured-media/media-1.img.jpg/876/493/1422493303787.jpg?ve=1&tl=1" />
</div>
</div>
</div>
</div>
I know you are looking for text-content in the second column, but I specified something else.
As I not sure whether you are going to use img tag ro background-image in the 1st column, so I have provided both.
1st Column with background-image and 2nd column with img tag.

Setting div to row height with foundation (equalizer)

I have a complex nested row & column layout using Zurb Foundation. I want to make the divs with text '.grid-text' the same height as the row. I've tried various strategies to do this, but it ends up breaking the flexbox vertical text centering. I've also searched through StackOverflow questions and none address this issue.
I ended up using Foundation's Equalizer but for some reason it isn't working. Any suggestions? I'm open to different strategies.
http://codepen.io/anon/pen/bVNjyv
I tried jquery and equalizer:
$(document).ready( function() {
var rowHeight=$('.v-align').height();
$('.grid-text').height(rowHeight);
});
<div class="row v-align" data-equalizer data-equalizer-mq="large-up">
<div class="large-4 column b full-width">
<div class="grid-text" data-equalizer-watch>
<h3>A</h3>
<p>text</p>
</div>
</div>
<div class="large-8 column full-width">
<img src="http://placehold.it/2604x1302/00FFCC" data-equalizer-watch>
</div>
</div><!--/row9-->
For this kind of layout I tend to use Masonry which plays nice with Foundation.
I'm not sure what environment you're using or if you're just using codepen but you were missing a few JS files that Foundation requires which I added to the pen:
jquery.js
foundation.js
foundation.equalizer.js
Then make sure Foundation is initialised:
$(document).foundation();
I updated your HTML as follows:
<div class="row">
<div class="show-for-large-up large-3 column full-width">
<img src="http://placehold.it/1302x2604/FF3333">
</div>
<div class="large-9 column">
<div class="row" data-equalizer data-equalizer-mq="large-up">
<div class="large-4 column b full-width">
<div class="grid-text" data-equalizer-watch>
<div class="inner">
<h3>A</h3>
<p>text</p>
</div>
</div>
</div>
<div class="large-8 column full-width" data-equalizer-watch>
<img src="http://placehold.it/2604x1302/00FFCC">
</div>
</div>
</div>
<div class="large-6 columns full-width">
<img src="http://placehold.it/2604x1302">
</div>
<div class="show-for-large-up large-3 columns full-width last">
<img src="http://placehold.it/1302x2604/FFFF99">
</div>
<div class="large-9 columns">
<div class="row" data-equalizer data-equalizer-mq="large-up">
<div class="large-8 columns full-width" data-equalizer-watch>
<img src="http://placehold.it/2604x1302/FF9966">
</div>
<div class="large-4 columns b full-width">
<div class="grid-text" data-equalizer-watch>
<div class="inner">
<h3>A</h3>
<p>text</p>
</div>
</div>
</div>
</div>
</div>
</div>
As for the css centring there are many ways you can do this, a great article to read on the subject is Centering in the unknown which I often reference. I would recommend not using flexbox as it's only supported in the latest browsers. In this instance I used the table cell method which I find works best with horizontal centering in Foundation.
I added the div .inner inside your .grid-text div. I then added the following css:
.grid-text {
display: table;
text-align: center;
width: 100%;
// no need to set the height here
// as it's set by data-equalizer
}
.grid-text .inner {
display: table-cell;
vertical-align: middle;
}
Hope that helps.
http://codepen.io/thmsmtylr/pen/EVjXye
well if you mean font size just try:
.v-align {
align-items: center;
font-size: 10px;
}
but test for the size that works properly, of course.

The column positioning breaks in bootstrap

A picture is worth a thousand words so...
Structure:
<div class="row">
<div class="col-lg-2 col-md-3 col-sm-4 text-center column-fix">
<div class="row-fluid">
<div class="row-fluid">
<div class="image">
<img src.... />
</div>
</div>
<div class="row-fluid">
<div class="category-link">
<a href...></a>
</div>
</div>
</div>
</div>
...
</div>
Output:
Basically, when a column is higher than the others, it causes the order to break. I understand this is how floats work, but I need a nice and clean solution for this. At the moment, this is my workaround:
.column-fix {
float: none !important;
display: inline-block !important;
margin: 0 -0.125em !important;
vertical-align:top;
}
And this is the output I get with the fix:
Is there a nice solution that will override all columns in all widths without setting a custom class to every element? I want the bootstrap col-* classes to behave like inline-blocks instead of floats.

How do I align images to the bottom of a div (in bootstrap)?

I would like to be able to align different sized images to the bottom of a div.
I have the following markup:
<div class="container">
<div class="container">
<div class="footer-images">
<img src="img1">
<img src="img2">
<img src="img3">
</div>
</div>
<div class="container">
<div class="copyright">
<p>© Some Company YYYY</p>
</div>
</div>
</div>
I can't figure out how to have all the images aligned to the bottom of the footer-images div. Any help would be greatly appreciated.
try this
.footer-images img{
vertical-align:bottom;
border:0;
}
In Bootstrap v4 you can use the class align-items-end to achieve bottom alignment in a div. You can use this class on the row or on the column.
Example with all column content aligned to the bottom.
<div class="container">
<div class="row align-items-end">
<div class="col">
One of three columns
</div>
<div class="col">
One of three columns
</div>
<div class="col">
One of three columns
</div>
</div>
</div>
Example with first column top, second colum middle and third colunm bottom alignment.
<div class="container">
<div class="row">
<div class="col align-self-start">
One of three columns
</div>
<div class="col align-self-center">
One of three columns
</div>
<div class="col align-self-end">
One of three columns
</div>
</div>
</div>
Source: Bootstrap documentation.
Does this help?
<style type="text/css">
.footer-images {
margin: auto;
display: block;
position: absolute;
bottom: 0;
}
.footer-images .copyright {
text-align: center;
}
</style>
Using this HTML
<div class="container">
<div class="container">
<div class="footer-images">
<img src="http://placehold.it/350x150">
<img src="http://placehold.it/640x480">
<img src="http://placehold.it/120x120">
<div class="container">
<div class="copyright">
<p>© Some Company YYYY</p>
</div>
</div>
</div>
</div>
</div>
used to this css and apply
.footer-images img{
width:xxpx;
height:xxpx; // add here your style as like with height border etc.
vertical-align:top;
border:0;
}
More about bootstrap
It would be enough:
margin-top: auto;
See https://jsfiddle.net/d3jau1gx/

Resources