I know webflow dont have vertical-align, but can someone find me a workaround for this exemple.
I have a box with fixed with and height, if i have a title with 2 lines, the vertical alignment is OK.
The problem is when i have 1 line, the simple solution is to insert more padding, bit inserting more padding will “destroy” the 2 line box.
Hi @Rui_Almeida, there is no way to center blocks of text using CSS. It’s possible with Tables (too old) and with Flexbox (too new) but those are not supported.
@bartekkustra provided a way to do this dynamically with javascript (jquery implementation) which is more advanced/involved. You can try that out if it’s really necessary. I’d stick with using classes, padding and margin to achieve desired vertical alignment.
It’s actually simple math rather than advaced jquery techniques Put this code in Custom Code section in Dashboard and set proper classes on your website Good luck!
First of all, I just read and realized by reading your code, that webflow adds jquery link automatically at the end of the code right before the </body> tag.
Next… Your code goes like this:
“There is a block with height set that contains image and another block in which there is a text.”
In that case I’d do something else here.
The whole block has 233px height. There is a 150px image at the beginning and the rest is a block for text. We have to find the empty place heigh, so we have to measure the titlebox height as follows: tbh = $('.titlebox').height(); Then we have to find out the difference between the content height and the image + titlebox height: difference = ($('.contentitem').height() - (tbh + 150)); The result is very useful! All you have to do now is to set the margin-top and margin-bottom for those:
After inserting your code, it did not work, but after some minutes cracking my head, i found 2 problem’s.
1º - You right, webflow insert the javascript in the end, after i export the code i have do put in the top.
2º - This one is curious, webflow transform all name classes to “small”, sorry my English.
I normally i do my classes like this “titleBox” “bigPicture”, the first letter of the second word is always CAPS, its more easy to read.
Your code was nor working because webflow “cleans” all CAPS. Just realize that.
Hi guys! I’d like to point out that it is not necessary to add an additional jQuery library to your page. Adding a second jQuery script would add an unnecessary 100k to your page. You can access Webflow’s version of jQuery in any custom embed or <head> script.
Here’s an updated snippet of code, which uses Webflow’s version of jQuery:
<script>
var Webflow = Webflow || [];
Webflow.push(function () {
var inner = $('.title-inner');
var offset = ($('.title-parent').height() - inner.height()) / 2;
inner.css({ 'margin-top': offset, 'margin-bottom': offset });
});
</script>
Simply replace ‘title-inner’ and ‘title-parent’ with your class names.
Note: you have to set the parent container to have a pixel height (like 200px for example) and give the element you want centered these CSS properties.
The perspective(1px) being the important part to note so that you don’t get any blurred elements due to the maths trying to shift the content onto a half-pixel.