Archived topic
Line up featured image with sidebar
9 replies · Started by gleb on April 7, 2021
Is there any way to make the sidebar widgets in line with the post's featured image instead of the title? I tried adding a margin at the top of the sidebar, but the problem with that is it only works for posts where the title spans one line. For posts with a longer title (two lines or more), the sidebar widgets don't line up since the margin is a static number.
Thank you
Hi there,
Any chance you can link us to the site in question?
You can use the private information field.
Let me know :)
Here's the site, thanks Leo. I have an ad in the sidebar so I want it to line up with the featured image of the post (rather than the title). The margin solution works fine if the post title is one line but not if it's longer. For example, you can see in the second link I added the ad doesn't line up.
I disabled AdBlock but not seeing an ad in the sidebar:
https://www.screencast.com/t/YfmP7Efa
It's actually above the post title.
Can you confirm?
There's supposed to be another ad in the sidebar on the top. It's showing for me.
Hmm it doesn't seem to be displaying for me.
This is using the Chrome incognito mode:
https://www.screencast.com/t/3d878QygeZEj
And this is using Firefox private window:
https://www.screencast.com/t/07EyoFmQn
I cleared the website cache, maybe it will show up now?
Hi there,
This is quite tricky to do. You'll need a script that calculates the offsetTop value of the featured image and apply it as a margin for your inside-sidebar.
This kind of customization is quite outside of our scope of support. But to help you out:
Here's a script to do just that:
<script>
function sidebar_auto_margin(){
var header = document.querySelector('header#masthead');
var postImg = document.querySelector('.featured-image');
var rightsidebar = document.querySelector('#right-sidebar');
rightsidebar.style.marginTop = postImg.offsetTop - header.offsetHeight +'px' ;
console.log(postImg.offsetTop + ' sidebar_auto_margin fires!');
console.log(header.offsetHeight +' sidebar_auto_margin fires!');
console.log(postImg.offsetTop - header.style.offsetHeight +' sidebar_auto_margin fires!');
}
window.addEventListener('resize', sidebar_auto_margin, false);
window.onload = sidebar_auto_margin();
</script>
Note: it's not perfect. You may have to remove some padding and margins on your right sidebar so it perfectly aligns.
Thanks Elvin, I'll give this a try. Nice signature btw, lol
No problem. Let us know how it goes. :)