Skip to main content
daveharr1s0n
Inspiring
February 2, 2022
Answered

How do I prevent overlapping of Bootstrap 5 Cards with an embedded tweet?

  • February 2, 2022
  • 4 replies
  • 5941 views

Hello Dreamweaver Pros,

 

I'm experimenting with Bootstrap 5 -- have never really used it but thought I'd give it a try.

 

I'm looking at putting together a page with masonry layout (like Pinterest) using Bootstrap 5 cards and it seems really easy using some Javascript to make it work. However, when I put an embedded tweet into the card there seems to be a problem. It's as if the script pulling up the tweet doesn't load before the other cards and the cards then overlap. I size my browser down to phone view and looks great. Unless I resize it SLOWLY to full screen, the other card still overlaps (just not as much).

 

Here's the generic sample template: http://www.daveharrison.info/test.html

And here is the same but with one tweet as content: http://www.daveharrison.info/test2.html

 

I suspect part of the problem might have something to do with the tweet not loading before the rest of the page loads. If so, is there a simple fix to not load the page until all script has been executed?

 

Is it bad practice to embed a tweet in a Bootstrap card?

 

Any advice appreciated. 

 

Thanks,

 

Dave

 

 

    This topic has been closed for replies.
    Correct answer osgood_

    Thanks Osgood,

     

    As you know I'm no expert, but this is what I suspected. It seems once I resize the browser (slowly) the cards do contain the tweet and the other cards fall in place properly. I was trying to figure out how to delay page load until the twitter scripts have had a chance to do their thing, but that probably makes no sense. How do I set a time out to the masonry script? I would love to give your code a try.

     

    As always, thank you!

     

    Dave

     

     

     

     


    quote

    Thanks Osgood,

     

    As you know I'm no expert, but this is what I suspected. It seems once I resize the browser (slowly) the cards do contain the tweet and the other cards fall in place properly. I was trying to figure out how to delay page load until the twitter scripts have had a chance to do their thing, but that probably makes no sense. How do I set a time out to the masonry script? I would love to give your code a try.

     

    As always, thank you!

     

    Dave


    By @daveharr1s0n

     

     

    Instead of hard coding the script:

     

    <script async src="https://cdn.jsdelivr.net/npm/masonry-layout@4.2.2/dist/masonry.pkgd.min.js" integrity="sha384-GNFwBvfVxBkLMJpYMOABq3c+d3KnQxudP/mGPkzpZSTYykLBNsZEnG2D9G/X/+7D" crossorigin="anonymous"></script>

     

    Try dynamically generating it, so remove the hard coded link to the masonry script and replace it with the below which has a 1 second time delay: 

     

     

    <script>
    setTimeout(function(){
    let masonryScript = document.createElement( 'script' );
    masonryScript.setAttribute('src', 'https://cdn.jsdelivr.net/npm/masonry-layout@4.2.2/dist/masonry.pkgd.min.js');
    document.body.appendChild(masonryScript);	
    }, 1000);
    </script>

     

     

    So far, for me at least, its worked every time BUT its not a guarantee because if the twitter script is delayed for more that 1 second then the masonry script will fire off and you will end up with the same result, overlapping <divs>. Twitter servers should be super efficient at delivering content, so it may help. 

     

    There is another method which checks the twitter script has been loaded every 1 second before the masonry script fires off but that is a bit more involved. Try the above first and see what results you get and if it doesnt work for you we can try the more complex method.

     

    This issue needs to be flagged up to the script produces as using dynamic data pulled in from APIs is common practice. If the script only works with static data that needs to be pointed out somewhere in the scripts information page. I don't know anything about the script, never used it, so maybe it is addressed somewhere in the information they provide.

     

    4 replies

    Liam Dilley
    Inspiring
    February 8, 2022

    A little tip for anyone who has 3rd party code loaded from scripts in this way. Recaptcha is another example.

     

    You can actuall when targeting the right element use:

     

    transform:scale(0.77);

     

    For example to scale that down where normal CSS does not work. That value can also be run through VW for scaling as well.

    Nancy OShea
    Community Expert
    Community Expert
    February 2, 2022

    1. Nobody resizes viewports except web designers.

    Normal users typically keep the same viewport throughout.

     

    2. If you think it will help, you can defer loading external Twitter scripts until after page load.

    https://developer.twitter.com/en/docs/twitter-for-websites/embedded-tweets/guides/cms-best-practices#js-loading

     

     

    Nancy O'Shea— Product User & Community Expert
    Legend
    February 2, 2022
    quote

    1. Nobody resizes viewports except web designers.

    Normal users typically keep the same viewport throughout.

     

    2. If you think it will help, you can defer loading external Twitter scripts until after page load.

    https://developer.twitter.com/en/docs/twitter-for-websites/embedded-tweets/guides/cms-best-practices#js-loading

     

     


    By @Nancy OShea

     

    I think what's happening here is the Twitter script NEEDS to load FIRST so the masonry script can calculate what vertical space the twitter content needs to occupy.

     

    Using a defer on the masonry script tag didnt seem to make any difference when I tested it but I didnt test thoroughly. What did make a difference is setting a time out on the masonry script of 2 seconds and actually writing that script to the page.

     

    Another method that seemed to work was checking if the twitter-tweet-rendered class had been appended to the page by the twitter script, then calling the masonry script.

     

    I was wondering if you could use an async/await function to deploy 2 scripts so the masonry script doesnt execute until after the twitter script. The 2 methods I mention are probably a bit of guess work because the Twitter script might not load for 3 seconds and I dont know if a script will run unless the page is reloaded.

     

    I think there must be a simple way of doing this as someone else must have come across this problem before, maybe best to ask the script providers.

     

    If the OP wants the 2 solutions I came up with I can provide some code.

    osgood_Correct answer
    Legend
    February 3, 2022

    Thanks Osgood,

     

    As you know I'm no expert, but this is what I suspected. It seems once I resize the browser (slowly) the cards do contain the tweet and the other cards fall in place properly. I was trying to figure out how to delay page load until the twitter scripts have had a chance to do their thing, but that probably makes no sense. How do I set a time out to the masonry script? I would love to give your code a try.

     

    As always, thank you!

     

    Dave

     

     

     

     


    quote

    Thanks Osgood,

     

    As you know I'm no expert, but this is what I suspected. It seems once I resize the browser (slowly) the cards do contain the tweet and the other cards fall in place properly. I was trying to figure out how to delay page load until the twitter scripts have had a chance to do their thing, but that probably makes no sense. How do I set a time out to the masonry script? I would love to give your code a try.

     

    As always, thank you!

     

    Dave


    By @daveharr1s0n

     

     

    Instead of hard coding the script:

     

    <script async src="https://cdn.jsdelivr.net/npm/masonry-layout@4.2.2/dist/masonry.pkgd.min.js" integrity="sha384-GNFwBvfVxBkLMJpYMOABq3c+d3KnQxudP/mGPkzpZSTYykLBNsZEnG2D9G/X/+7D" crossorigin="anonymous"></script>

     

    Try dynamically generating it, so remove the hard coded link to the masonry script and replace it with the below which has a 1 second time delay: 

     

     

    <script>
    setTimeout(function(){
    let masonryScript = document.createElement( 'script' );
    masonryScript.setAttribute('src', 'https://cdn.jsdelivr.net/npm/masonry-layout@4.2.2/dist/masonry.pkgd.min.js');
    document.body.appendChild(masonryScript);	
    }, 1000);
    </script>

     

     

    So far, for me at least, its worked every time BUT its not a guarantee because if the twitter script is delayed for more that 1 second then the masonry script will fire off and you will end up with the same result, overlapping <divs>. Twitter servers should be super efficient at delivering content, so it may help. 

     

    There is another method which checks the twitter script has been loaded every 1 second before the masonry script fires off but that is a bit more involved. Try the above first and see what results you get and if it doesnt work for you we can try the more complex method.

     

    This issue needs to be flagged up to the script produces as using dynamic data pulled in from APIs is common practice. If the script only works with static data that needs to be pointed out somewhere in the scripts information page. I don't know anything about the script, never used it, so maybe it is addressed somewhere in the information they provide.

     

    Legend
    February 2, 2022

    It all looks a bit dodgy to me, a lot of absolutely positioned divs. I don't know why dynamic data (the tweet) acts the way it does when static data works as expected. It has something to do with the masonry js file I suspect because if you comment that out then the tweet gets inserted correctly. A sticking plaster fix is to give the card-body that the tweet is in a height of 630px. The tweet itself is set to 615px height I believe, that has the desired effect, but its a really poor, poor hack. I don't know the masonary script or how youre supposed to set the css up but I'm sure there is probably a more elegant solution. If not I wouldn't use it personally, does it actually add much value.

    daveharr1s0n
    Inspiring
    February 3, 2022

    Does it add much value? Maybe not. Just thought it might be fun to try a masonry layout with some embedded social media.  🙂

    BenPleysier
    Community Expert
    Community Expert
    February 2, 2022

    The anatomy of a card is simple.

    Card

       Card Header

       Card Body

       Card Footer

     

    Any content needs to be placed in one of those children.

     

    See: https://getbootstrap.com/docs/5.1/components/card/

     

    This is a video that I published on the subject

    https://youtu.be/C8ZIInQbvP0

     

    Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!