How to create a YouTube video background with CSS
文章推薦指數: 80 %
In this tutorial we will learn how to set a fullscreen Youtube video as a background using CSS and HTML.
fullPage.js
GetfullPage
JavaScript
WordPressTheme
PluginElementor
PluginGutenberg
Extensions
Contact
HowtocreateaYouTubevideobackgroundwithCSS
Jul12,2021
UsinganembeddedYouTubevideoasabackgroundisacommontechniqueinnowadayswebsites.YoucanusetheHTML5videotagtocreateabackgroundvideo.However,thatwouldmeanhostingavideoonlinesomewhere.AndIdon’tknowyou,butinmycase,Ialwaystrytoavoidthatoption.Youtubevideoembedsareagoodalternativetoself-hostedvideos.Theysavebandwidthandtheyloadsuperfast.But,sincetheyareinaniframe,theycanbeabitmoretrickytodealwith.Inthistutorial,we'llbegoingoverhowtocreateaYouTubevideobackgroundforourwebsite.
GettingtheHTMLfortheembeddedYoutubevideo
ThefirstthingwehavetodoisgrabbingtheembedcodeoftheYouTubevideothatwewanttodisplayinthebackground.Justgotoyourchosenvideoandclickonthe“share”button.
Severaloptionswillpopup.Clickonthe“embed”one.
Thiswillopenupanewboxonyourscreenwiththecodeforthevideoyouwanttoembed.Disablethe“Showplayercontrols”intheembedoptionsandthenclickonthe“copy”button.
Nowcreateadivcontainerandpastethereyourembedcode.
UseayoutubevideoasafullscreenbackgroundwithCSS
TheCSSisappliedonthetextdivtomoveitalongwithanyofitscontentsontopofthevideo:
#text{position:absolute;color:#FFFFFF;left:50%;top:50%;transform:translate(-50%,-50%);}
WehaveappliedthesameCSSpropertiesaswedidontheiframetocenterthetext-divinthecenterofourwebpage.You'llwanttomakesurethatthetextwhichisplacedontopofourYouTubevideobackgroundhashighcontrastsothatitcaneasilyberead.
YoucanalsoplaceotherelementssuchashoverbuttonsandusethesameCSSpropertiestoplacethemontopofourYouTubevideobackground.
AddingtheYoutubeembeddedloopoption
Ifyouuseashortvideoyou'llnoticehowtherelatedvideosshowoncethevideoisfinished.Toavoidthiswehavetousealittletrick.Addingtheparameterloop=1tothesourceofthevideoisnolongerenough,wehavetoaddmoreparameterandthisistheplaylistonetowhichwewillassigntheIDoftheyoutubevideo.
So,ifwehavethisvideoweshouldlookforitsid,whichisthecodeafterthe/embed/partorafter?v=whenaccessingthevideothroughtyoutube.Then,wesimplypasteitonourplaylistparam:
&playlist=Yj2iELI6OeI&loop=1
Ourfinalembeddedvideoshoullooklikethis:
延伸文章資訊
- 1HTML and CSS Project – How to Build A YouTube Clone Step ...
Our code doesn't look too beautiful after running. Well, we'll fix that with CSS. So let's add CS...
- 2How to create a YouTube video background with CSS
In this tutorial we will learn how to set a fullscreen Youtube video as a background using CSS an...
- 3免外掛!4種語法嵌入響應式/循環/自動播放Youtube影片 - 温唯
比如說,調整好在電腦顯示的影片尺寸之後,到了手機一定會超過螢幕範圍,這是因為YouTube預設的嵌入語法,缺少〔響應式〕的CSS樣式,才會造成非常糟糕的 ...
- 4HTML YouTube Videos - W3Schools
Well organized and easy to understand Web building tutorials with lots of examples of how to use ...
- 5Build A YouTube Clone Using HTML, CSS and JS 100 ...
Best Practice: Master your Front-End Web Developer Skills By Building A YouTube Clone Using HTML,...