I’m trying to vertically center some text and am struggling hard.
I’ve tried a few variations of CSS and HTML directly and each seem to just make it worse.
The text I’m trying to get vertically centered is “I’m a first time Arizona gardener. I wanted to showcase what I’m growing, and how it progresses over time.”
It’s in the first well, next to my mug shot. When the screen is maximized it hugs the top and I dislike it…
If you have an example with HTML or CSS that corrects this I’d love a link to it =).
This guy was pretty chuffed when vertical align was “solved” with flexbox.
Bootstrap 4 has a flexbox option… not sure what you get with the cdn link though (as you are using with codepen)
Your other options are padding or absolute positioning etc but they can give rubbish results when the screen size changes (and thus the container size if set to a relative height).
To use the vertical-align property, you must apply it to an inline element (such as a img) or use table and cell layout. otherwise it will do nothing.
Honestly, yeh it can be a pain… until you swallow that flexbox pill and then it’s just not an issue. You can use flexbox in conjunction with bootstrap 3 (in lieu of col-sx-4 and all that business), and as I said, BS4 can include it too if you download the flex version rather than grid (once your not working in codepen anymore). otherwise… it’s tables i’m afraid.
first of all, you put two cols with size 7 in a same row… bootstrap supports only 12 columns per row. so, that will break your layout, you need fix it:
ok, now you can do the trick, in the parent div, the container of text, you can set a fixed height, to hold your text at vertical position, now, at this parent div, set position: relative;
and in your text set:position: absolute; top: 50%; transform: translateY(-50%);
I’ve gotten it working, it required js though. (I’d rather not use any JS if I can avoid it, since it’s not something I really understand or am focused on learning currently)
I’ve tried this solution from @leandroruel and I must be doing it wrong, can’t get it to function.
I think I’d have persevered with either @leandroruel’s solution or the flexbox way until it was working as it seems the stack overflow version (even with all those upvotes) is a bit of a hack (browser support beware) and not necessary these days.
I want to understand how something works than do the way I have it set up now.
I’m trying to not use any JS since it’s not something I’ve really jumped into.
I’ve found this to work pretty well. It’s been tested in Chrome, FF, and IE 10+ (Doesn’t work correctly for versions prior to IE10+, but that is a very small market share). If you want to add space between the image and text, you can add a margin to either of them and adjust it using media queries for different sized screens.
The code I used will still work with the version of bootstrap he’s using in the codepen. In the testing environment that I used, I was using the same version of bootstrap. Flex is a css property and can be used with or without bootstrap. It depends on how you implement it. If you’re trying to center two separate bootstrap columns, then you may have to opt for something different. But if you’re trying to vertically center something that’s contained within the same bootstrap column, which is what I posted in my code above, then you should be able to use the display: flex option regardless of whether you’re using bootstrap or not because it doesn’t directly interact with the bootstrap framework. I did do some testing with the flex display option to work with bootstrap columns and got it to work, but it took some trial and error to get the look and feel that I was after.