Copy link to clipboard
Copied
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
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
...Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Does it add much value? Maybe not. Just thought it might be fun to try a masonry layout with some embedded social media. 🙂
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thanks, Ben.
That does the trick for the single tweet test page I had. I tried the same template embedding a tweet in each div just to see what would happen. It didn't work, so I changed the script from 1000 to 3000 (didn't even try 2000) and that worked! Sure it takes a second longer, and I suppose if one had too much embedded it wouldn't be the way to go, but not bad!
I wouldn't want to trouble you for the more involved method, unless you had the script handy. This at least gives me something to think about and work with for a simple page.
You're always a great help. I appreciate it!
Dave
Copy link to clipboard
Copied
Throwing more tweets into the mix is obviously taking longer to process so the timeout needs to be increased to allow all of the tweets to fill their respective boxes. I don't think the alternative method would help too much because the masonry script would fire after the alternative method spotted the first tweet on the page, so it would not get time to calculate the space necessary for the other tweets.
Using timeout is not a great solution because if the tweets, for some reason took longer than the time out set then you would still be faced with the problem of overlapping divs
I dont know how you would detect that ALL the tweets had downloaded first before the masonry script fired itself. Maybe by checking how many 'twitter-tweet-rendered' classes there are on the page every 2 seconds using the setInterval javascript function - I think the tweeter script applies that class to each tweet via the script. Once the number matches the total tweets on the page then fire the masonry script.
Copy link to clipboard
Copied
Thanks, Nancy.
I agree, one shouldn't have to resize the browser to get the page to display properly. I only mention because it seemed the cards wouldn't expand to the correct height of its content as the content (tweet) hadn't finished loading.
I'll take a look at the link you posted, but seems the Twitter script lagging behind might be the problem.
I always appreciate your assistance!
Dave
Copy link to clipboard
Copied
Not 100% accurate Nancy, rotation of devices, especially modern laptop/tablet combo's with touch screens and folds are increasingly popular so that rotation dynamically effects a viewport width. But you are correct that a downscale on the fly is normally a developer and its just mainly loading at a certain size.
I will add that the only thing people forget is its NOT "desktop, Ipad, Phone". All the different notebooks and laptops have both a wide range of width and height in the displays as well as the fact that a desktop is not 1200px and more and more people have widescreen etc so you have to set max-width's and limit your projects size as well as work on mobility otherwise it looks broken there too 🙂
Copy link to clipboard
Copied
The OP's problem wasn't rotation width but Twitter feed delay.
I suspect the majority of tablet & mobile users don't fiddle around too much with device width when reading ebooks and viewing content on the web. However they may be compelled to flip it when using a drawing/painting tool like Fresco.
Copy link to clipboard
Copied
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.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now