HTML and CSS Project – How to Build A YouTube Clone Step ...

文章推薦指數: 80 %
投票人數:10人

Our code doesn't look too beautiful after running. Well, we'll fix that with CSS. So let's add CSS to it to really create a YouTube layout. CSS ... Search Submityoursearchquery Forum Donate OgundiranAyobami InthistutorialwewillbuildaYouTubeclonestepbystep.You'lllearnhowtocreatelayoutsandaddcontenttothoselayouts. IfyouhavebeenstrugglingtobuildarealwebsitewithHTML&CSS,thisisthearticleforyou.BecauseIamabouttoteachyoutodoitstepbysteptoreduceyourstruggle. We'lldiscusshowtomakethelayoutoftheYouTubeclonewithHTMLandCSSandyou'lllearnhowtomakeatwo-columnlayout. Justwait:Ifyou'reatotalbeginnerandyoudon'tevenunderstandthedifferencebetweenHTMLandCSS,youcancheckoutthevideobelowtolearneverythingyouneedtogetstarted... It'simportanttobreakthingsintosmallerpiecestomakeourprojectseasiertobuild.So,wehavetobreakthisYouTubecloneintosmallerunitsthatwe'llbebuildingstepbystep. YouTubeCloneBreakdown IntheYouTubeclonewe'llbebuilding,thewebsitehasabout6units: TheHeadercontainsthreesections(left,centerandright).Theleftsectioncontainsthelogoandmenu,thecentercontainsthesearchboxandanicon,andtherightcontainsnavigationicons.Theiconsarebasedonsimilarelements,whichmeansthatwedesignaniconelementandthencopy,paste,andeditittocreatetheothers. TheMainbodycontainstwosections(sidebarandcontent).Thenavigationlinksinthesidebararealsosimilar,sotheyarejustonething.Thesamethinghappenstothevideosinthecontentsection. So,ourYouTubeclonehasaheader,main,sidebar,content,video-card,navigationlinkandnavigationiconasthemajorunits.Thatisthebreakdownoftheunitsofthewebpagewewanttocreate. YouTubeCloneLayout ThefirstthingwehavetodoiscreatethelayoutstructureoftheYouTubeclonewithHTML.We'lldothatwiththebelowcode: YoutubeClonewithHTML&CSS .

. .

Inthistutorial,IassumeyouhaveanunderstandingofhowtouseHTMLmetatagsandhowtolinkaCSSfile.Ifyoudon’t,youcanlearnmoreabouttheminthevideoIaddedabove.Butyoudon’tneedtounderstandthemforwhatwearelearninginthislesson,sokeeponreading. WehaveaheadertagtocreatetheheadersectionoftheYouTubeclone.We'lladdtheYouTubelogo,searchbox,andothernavigationiconstotheheaderlater. Thereisalsothemainsectionthatcontainstheside-barandcontent.Theside-barwillcontainsomenavigationlinkswhilethecontentwillcontainvideos.So,thatisitforthestructurewithjustHTML. Wait!Ourcodedoesn'tlooktoobeautifulafterrunning.Well,we'llfixthatwithCSS.Solet’saddCSStoittoreallycreateaYouTubelayout. CSSfortheYouTubeClone Step1:Usethe@importinCSS @importurl('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap'); Let’sstartwith“importurl(‘path’)...Whatdoesitdo?WeuseittolinktotheGooglefontcalledRobotosothatwecanuseitasthefontofourwebsite. Step2:ResetHTMLdefaultstyles *{ margin:0; padding:0; box-sizing:border-box; } Theasterisk*isaCSSselectorthatselectsallHTMLtagsonourpage.Wesettheirmarginandpaddingto0.Wethensettheirbox-sizingtoborder-box.Whydowedothat? Wewantwidthorheight,border,margin,andpaddingtoadduptobethetotallength.ThisiswhatImean:inCSS,ifaboxhaswidthof100pxandpaddingof10px,thewidthoftheboxwillnowbe110px Butwedon'twantthat–wewanteverythingtobe100px.Widthshouldstillbe100pxincludingthemarginof10pxinsteadofmakingit110px.Thatiswhatbox-sizing:border-boxdoes. Note:whenyou'reusingit,youwillstarttobetterunderstandhowitworks–butfornow,Ijustwantedtogivesomeinsightabeginnercanquicklyrelateto. Step3:Setthefont-family body{ font-family:'Roboto',sans-serif; } Weselectthebodytagandsetitsfont-familytoRobotoandusesans-serifasafall-backincaseRobotoisnotavailable. Step4:Styletheheader /*headersection*/ .header{ display:flex; justify-content:space-between; align-items:center; height:60px; padding:15px; } The.headerclassnameisusedtoselect(orconnectto)theheadersectionofourwebsitesothatwecanaddsomestylestoit. Wesetitsdisplaypropertytoflextocreatealayoutoutofit,andthenwecaneasilydivideitintosections.Wewilldivideitintosectionslater. Justify-content:space-betweenmeanswewantthecontentsintheheadertohavespacebetweenthemoncetherearemorethanone. Align-items:centreisusedtomoveallthecontentsoftheheadertothecentre-leftofyourscreen.Thatiscalledverticalalignment.Wefinallysettheheightoftheheaderto60pxanditspaddingto15px. Step5:Settheheightofthemainsection main{ height:calc(100vh-70px); display:flex; background-color:#f9f9f9; } Wesettheheightofthemainsectiontocalc(100vh-70px)...Whatdoesitmean?Vstandsforaviewport,whichisthevisiblepartofawindow’sscreenwithoutscrolling.“height”meansverticallength,andwecanalsouse“w”whichmeanswidth-horizontallength. Inshort,100vhmeansthetotalheightthatisvisibleinabrowserwithoutscrolling.Andweusecalc(100vh-70px)torunacalculationthatsubtract70pxfrom100vh. Wesetitsdisplaypropertytoflextocreatealayoutoutofit.Finally,wesetitsbackgroundcolourto#f9f99fwhichisakindofsilverorash. Step6:Hidethescrollbar /*Sidebar*/ .side-bar{ height:100%; width:17%; background-color:white; overflow-y:hidden; } Theheightofthe.side-barissetto100%ofitsparent.Thatmeansitwillhavethesameheightasitsparent.Itswidthissetto17%ofitsparentandbackgroundcoloursettowhite. Hey!Whatisoverflow-y:hidden?WhenTwitterloads10tweetsatonce,youcan’tseeeverythingatonceandyouhavetoscroll,right?Inthiscase,wehidethescrollbar.Gracias! Step7:Addmediaqueriesforresponsiveness @media(max-width:768px){ .side-bar{ display:none; } } Weusethismediaquerytomakeawebsiteresponsiveonmobile,tablets,anddesktop.WhentheYouTubecloneisonadevicewhosescreenislessthanorequalto768px(likemobile&tablet),thesidebarwilldisappear.Also,max-width:768pxmeansthatadevice’sscreencanbelessorequalto768px. Alright,wehavebuiltthelayoutofourYouTubeclone.Belowistheresult… HowtoAddContenttotheHeaderSection Inthispart,wearegoingtodiscusshowtodivideanelementintosectionsandaddcontenttotheheadersection. Inshort,wearedividingtheheadersectionoftheYouTubecloneintothreesections:left,center,andright.Andeachofthesectionscontainssometags.Let'sgetstarted! Step1:Addchildrenandgrandchildrentotheheader Here,wewillsimplyaddHTMLtagstotheheadersectionoftheYouTubeClone.We'lldojustthatwiththecodebelow:

menu mic videocam apps notifications account_circle
Afterdividingtheheaderintothreesectionsbyaddingthreeseparateblocksofcode,itistimetouseCSStomakeitmorebeautiful.Let’sgetstarted. Step2:Styletheleftsection .left{ display:flex; align-items:center; } .left#menu{ padding:07px; cursor:pointer; } Don’tforget,wesetthejustify-contentpropertyoftheheadertospace-between,whichmeanstherewillbeequalspacebetweeneverytagintheheader. Now,wegaveaclassleftbecauseitshouldbetotheleftside.Wesetitsdisplaypropertytoflextocreatesectionswiththelayoutoutofit.Itschildrenarealignedtotheleft-centeroftheheader.Wealsoaccessthemenuthatisinsidetheleftsectionwithitsid. Wesetitspaddingtopandbottomto0anditspaddingleftandrightto7px.Itscursorpropertyissettothepointersothatwhenthemouseisonit,itwillshowapointingfinger. Step3:Stylethecentersectionanditsform .search{ display:flex; } .searchform{ display:flex; border:1pxsolid#ddd; height:45px; } Hey!Youshouldknowwhatwedointhesearchclassbynow.:) Wesetitsdisplaypropertytoflexsothatwecancreatealayoutwithitschildren.Wedothesametotheformwhichisinsideofthesearch/centersection. Itsborderthicknessissetto1px,typetosolid,andcolorto#ddd(somethingsilverorash). Step4:Styletheinputinthesearchform .searchinput{ width:600px; padding:10px; border:0; height:100%; border-radius:2px002px } input:focus{ outline:none; border:1pxsolid#ddd; } Weselecttheinputwhichisinsidetheseachsectionwith.searchinput.Wesetitsborder-radiusto2pxtop,0right,0bottom,and2pxleft.Then,whatisborder-radius?Itisthecurvededgesofanobjectwithfourangles. Step5:Stylethesearchandmicicons/buttons .searchbutton{ height:100%; width:60px; border:none; } .mic{ margin-top:10px; } Thebuttoninsidethesearchsectionisalsoselectedwiththe.searchbutton.Itsheightissetto100%ofitsparent.Wedon’twantittohaveanybordersowesetitsborderto0. Weaccessthemicrophoneiconwithitsclassname.micandsetitsmargin-topto10pxsothatitwillmovedownabit. Finally,let'sstyleallthematerial-iconswehaveonthewebpage: .material-icons{ color:rgb(100,100,100); padding:07px; cursor:pointer; } Hurry!WehaveaddedchildrenandsectionstotheheaderoftheYouTubeclone.Checkoutthefinalresultbelow: HowtoAddContenttotheSidebar Inthispart,wearegoingtodiscusshowtoaddnavigationlinkstotheYouTubeclone.Inshort,wewilladdabunchoflinkstotheexistingsidebar. Step1:Addchildrenandgrandchildrentothesidebar WewilladdbelowHTMLcodetothesidebar: home Home Then,weneedtofirststylethenavbar,whichisthewrapperforallthelinks: .nav{ width:100%; display:flex; flex-direction:column; margin-bottom:15px; margin-top:15px; } TheonlythingIwillexplainhereisflex-direction.Thisdetermineswhetherwewantthechildrentoappearinacolumn(vertical)orrow(horizontal).Inthiscasewegowithahorizontaldisplay. Then,let'sstylethenav-linkabovewithCSSasshownbelow: .nav-link{ display:flex; align-items:center; padding:12px25px; } .nav-linkspan{ margin-left:15px; } .nav-link:hover{ background:#e5e5e5; cursor:pointer; } .active{ background:#e5e5e5; } Oops–thereisnothingtoexplainherebecauseIhaveexplainedmanysimilarconceptsalready! Okay,let’stalkabout.home:hover.Thestylesinitwillonlybeappliedwheneverwemoveourcursoroverthehomenavigationlink.Thatisit. Hey…wait.Wehavemanylinksinthesidebar,sohowarewegoingtocreatethat?Well,wejustdowhateverydeveloperloves-copyandpasteandthenedititasinbelow: home Home local_fire_department Trending subscriptions Subscriptions
Afterpastingthreelinks,wewanttomakethemintoseparatecategoriesbyusingthetagtocreatealinethatseparatesthemfromthenextcategory.Now,let’sstylethehrtag. hr{ height:1px; background-color:#e5e5e5; border:none; } Then,wewilladdtheremainingcodeafterthehrtagasinbelow: library_add_check Library history History play_arrow YourVideos watch_later WatchLater thumb_up LikedVideos Wow,wearedonewiththesidebaroftheYouTubecloneandbelowistheresultweget: HowtoAddVideostotheContentSection InthispartoftheYouTubeClonetutorial,wewilladdvideostothecontentarea.Youneedtoduplicatevideo(notvideos)intomanyplacestomakeitlooklikeYoutubeandyoucaneditthemwithuniqueYoutubevideoinformationifit'savailable.

Introverts&ContentCreation|SumuduSiriwardana

FrancescoCiulla 2MViews•3MonthsAgo Now,letapplyCSStoit. .content{ background-color:#f9f9f9; width:100%; height:100%; padding:15px15px; border-top:1pxsolid#ddd; overflow-y:scroll; } .videos{ display:flex; flex-direction:row; justify-content:space-around; flex-wrap:wrap; } .video{ width:270px; margin-bottom:30px; } Ifyoucheckthestylefor.videos,youwillseeflex-wrap.ItistheonlypropertyIhavenotexplainedbefore,solet’sexplainit. Whenthescreencanonlytakefouritems,forexample,otheritemswillbemovedtoanotherrow.Thatiswhat“flex-wrap”does. .thumbnail{ width:100%; height:170px; } .thumbnailimg{ object-fit:cover; height:94%; width:100%; } .authorimg{ object-fit:cover; border-radius:50%; height:40px; width:40px; margin-right:10px; } Theonlythingyoumaynotunderstandabovebecausewehaven’texplaineditbeforeisobject-fit:cover.Thenhowdoweuseit? object-fitinthiscaseisusedtocliptheimagetoitscontainersoastoremoveoverflow(areaswheretheimageisbiggerthanitscontainer)inheightandwidth: .details{ display:flex; } .title{ display:flex; flex-direction:column; } .titleh3{ color:rgb(3,3,3); line-height:18px; font-size:14px; margin-bottom:6px; } .titlea, span{ text-decoration:none; color:rgb(96,96,96); font-size:12px; } Inthiscase,wemakealayoutoutof.detailsandbecausewedon’tsetitsflex-directiontoproperty,itwillbesettorow–whichisitsdefaultvalue.Youseethatalayoutisalsomadeoutofthetitleandsetitschildrentoappearinacolumnbysettingflex-directiontocolumn. Weselecttheh3tagthatisinside.titleandwesetitscolortosomewhatblack…Iwillexplainhowtounderstandthecolorcodelater. line-heightisusedtosettheheightofalineoftextandinthiscase,wesetitto18px. Finallyweuse.titlea,spantoselecttheanchortagin.title.Wealsoselectallspantagsonthepageandsettheirtext-decorationtonone. Sowhatistextdecoration?Ithasadesignsuchasanunderlinethatananchortaghas,andwehideitinthiscasebysettingittonone.WehaveaddedvideostotheYouTubeCloneandthefinalresultisbelow: HowtoMaketheYouTubeCloneResponsive Inthispartofthetutorial,wearegoingtomaketheYouTubeClonewehavebuiltabitresponsive.Howdowedoit?Well,wewilluseCSSmediaqueries.Now,letgetstarted! So,wewilladdtheCSScodebelowtotheCSSfileoftheYouTubeClone. @media(max-width:768px){ .side-bar{ display:none; } .search{ display:none; } } @media(max-width:768px){}isusedtosetthedevicescreensizesthatthecodeinthemediaquerywillapplyto. Inthisexample,max-width:768pxmeansthatthestylesinthemediaquerywillbeappliedtoanyscreensizethatisequaltoorlessthan768px. So,wheneverthescreensizeinuseis768pxorless,wewillhidethesidebarandthesearchinputbysettingtheirdisplaypropertytonone. @media(max-width:900px){ .searchinput{ width:25rem; } } Finally,wemakethethesearchinputabitsmallerwheneverthescreensizeofthedeviceinuseislessorequalto900px. Thatisit. Hurraaaay...we'redoneinmakingtheYouTubeclone.Nowgoandbuildyourownanddon'tforgettoplaywithit. Addorremovesomethingsfromthetutorialtobesureyoutrullyunderstandwhatyou'redoing.Goodluck! Onemorething AyobamiOgundiranloveswritinghistorywithsoftwaredevelopmentandiscurrentlyhelpingthosewhoarestrugglingtounderstandandbuildprojectswithHTML,CSSandJavaScriptthroughYouTooCanCode. JoinmeviaTelegramgrouporDiscordgroup. OgundiranAyobami AyobamiloveswritinghistorywithJavaScript(React)andPHP(Laravel).Hehasbeenmakingprogrammingfuntolearnforlearners.CheckhimoutonYouTube:https://bit.ly/3usOu3s Ifyoureadthisfar,tweettotheauthortoshowthemyoucare.Tweetathanks Learntocodeforfree.freeCodeCamp'sopensourcecurriculumhashelpedmorethan40,000peoplegetjobsasdevelopers.Getstarted



請為這篇文章評分?