CSS background-image property - W3Schools

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

More "Try it Yourself" examples below. Definition and Usage. The background-image property sets one or more background images for an element. Tutorials References Exercises VideosNEW Menu Login PaidCourses WebsiteNEW HTML CSS JAVASCRIPT SQL PYTHON PHP BOOTSTRAP HOWTO W3.CSS JAVA JQUERY C++ C# R React Kotlin    × Tutorials HTMLandCSS LearnHTML LearnCSS LearnRWD LearnBootstrap LearnW3.CSS LearnColors LearnIcons LearnGraphics LearnSVG LearnCanvas LearnHowTo LearnSass DataAnalytics LearnAI LearnMachineLearning LearnDataScience LearnNumPy LearnPandas LearnSciPy LearnMatplotlib LearnStatistics LearnExcel XMLTutorials LearnXML LearnXMLAJAX LearnXMLDOM LearnXMLDTD LearnXMLSchema LearnXSLT LearnXPath LearnXQuery JavaScript LearnJavaScript LearnjQuery LearnReact LearnAngularJS LearnJSON LearnAJAX LearnAppML LearnW3.JS Programming LearnPython LearnJava LearnC++ LearnC# LearnR LearnKotlin LearnGo ServerSide LearnSQL LearnMySQL LearnPHP LearnASP LearnNode.js LearnRaspberryPi LearnGit WebBuilding CreateaWebsiteNEW WebTemplates WebStatistics WebCertificates WebDevelopment CodeEditor TestYourTypingSpeed PlayaCodeGame CyberSecurity Accessibility DataAnalytics LearnAI LearnMachineLearning LearnDataScience LearnNumPy LearnPandas LearnSciPy LearnMatplotlib LearnStatistics LearnExcel LearnGoogleSheets XMLTutorials LearnXML LearnXMLAJAX LearnXMLDOM LearnXMLDTD LearnXMLSchema LearnXSLT LearnXPath LearnXQuery × References HTML HTMLTagReference HTMLBrowserSupport HTMLEventReference HTMLColorReference HTMLAttributeReference HTMLCanvasReference HTMLSVGReference GoogleMapsReference CSS CSSReference CSSBrowserSupport CSSSelectorReference Bootstrap3Reference Bootstrap4Reference W3.CSSReference IconReference SassReference JavaScript JavaScriptReference HTMLDOMReference jQueryReference AngularJSReference AppMLReference W3.JSReference Programming PythonReference JavaReference ServerSide SQLReference MySQLReference PHPReference ASPReference XML XMLDOMReference XMLHttpReference XSLTReference XMLSchemaReference CharacterSets HTMLCharacterSets HTMLASCII HTMLANSI HTMLWindows-1252 HTMLISO-8859-1 HTMLSymbols HTMLUTF-8 × ExercisesandQuizzes Exercises HTMLExercises CSSExercises JavaScriptExercises SQLExercises MySQLExercises PHPExercises PythonExercises NumPyExercises PandasExercises SciPyExercises jQueryExercises JavaExercises C++Exercises C#Exercises RExercises KotlinExercises GoExercises BootstrapExercises Bootstrap4Exercises Bootstrap5Exercises GitExercises Quizzes HTMLQuiz CSSQuiz JavaScriptQuiz SQLQuiz MySQLQuiz PHPQuiz PythonQuiz NumPyQuiz PandasQuiz SciPyQuiz jQueryQuiz JavaQuiz C++Quiz C#Quiz RQuiz XMLQuiz CyberSecurityQuiz BootstrapQuiz Bootstrap4Quiz Bootstrap5Quiz AccessibilityQuiz Courses HTMLCourse CSSCourse JavaScriptCourse FrontEndCourse SQLCourse PHPCourse PythonCourse NumPyCourse PandasCourse DataAnalyticsCourse jQueryCourse JavaCourse C++Course C#Course RCourse XMLCourse CyberSecurityCourse AccessibilityCourse Certificates HTMLCertificate CSSCertificate JavaScriptCertificate FrontEndCertificate SQLCertificate PHPCertificate PythonCertificate DataScienceCertificate Bootstrap3Certificate Bootstrap4Certificate jQueryCertificate JavaCertificate C++Certificate ReactCertificate XMLCertificate × Tutorials References Exercises PaidCourses Spaces Videos Shop CSSReference CSSReference CSSBrowserSupport CSSSelectors CSSFunctions CSSReferenceAural CSSWebSafeFonts CSSFallbackFonts CSSAnimatable CSSUnits CSSPX-EMConverter CSSColors CSSColorValues CSSDefaultValues CSSEntities CSSProperties align-content align-items align-self all animation animation-delay animation-direction animation-duration animation-fill-mode animation-iteration-count animation-name animation-play-state animation-timing-function backface-visibility background background-attachment background-blend-mode background-clip background-color background-image background-origin background-position background-repeat background-size border border-bottom border-bottom-color border-bottom-left-radius border-bottom-right-radius border-bottom-style border-bottom-width border-collapse border-color border-image border-image-outset border-image-repeat border-image-slice border-image-source border-image-width border-left border-left-color border-left-style border-left-width border-radius border-right border-right-color border-right-style border-right-width border-spacing border-style border-top border-top-color border-top-left-radius border-top-right-radius border-top-style border-top-width border-width bottom box-decoration-break box-shadow box-sizing break-after break-before break-inside caption-side caret-color @charset clear clip clip-path color column-count column-fill column-gap column-rule column-rule-color column-rule-style column-rule-width column-span column-width columns content counter-increment counter-reset cursor direction display empty-cells filter flex flex-basis flex-direction flex-flow flex-grow flex-shrink flex-wrap float font @font-face font-family font-feature-settings font-kerning font-size font-size-adjust font-stretch font-style font-variant font-variant-caps font-weight gap grid grid-area grid-auto-columns grid-auto-flow grid-auto-rows grid-column grid-column-end grid-column-gap grid-column-start grid-gap grid-row grid-row-end grid-row-gap grid-row-start grid-template grid-template-areas grid-template-columns grid-template-rows hanging-punctuation height hyphens image-rendering @import isolation justify-content @keyframes left letter-spacing line-height list-style list-style-image list-style-position list-style-type margin margin-bottom margin-left margin-right margin-top max-height max-width @media min-height min-width mix-blend-mode object-fit object-position opacity order orphans outline outline-color outline-offset outline-style outline-width overflow overflow-wrap overflow-x overflow-y padding padding-bottom padding-left padding-right padding-top page-break-after page-break-before page-break-inside perspective perspective-origin pointer-events position quotes resize right row-gap scroll-behavior tab-size table-layout text-align text-align-last text-decoration text-decoration-color text-decoration-line text-decoration-style text-indent text-justify text-overflow text-shadow text-transform top transform transform-origin transform-style transition transition-delay transition-duration transition-property transition-timing-function unicode-bidi user-select vertical-align visibility white-space widows width word-break word-spacing word-wrap writing-mode z-index CSSbackground-imageProperty ❮Previous CompleteCSSReference Next❯ Example Setabackground-imageforthe

element: body{  background-image:url("paper.gif");  background-color:#cccccc; } TryitYourself» Example Settwobackgroundimagesfortheelement: body{ background-image:url("img_tree.gif"),url("paper.gif");  background-color:#cccccc;} TryitYourself» More"TryitYourself"examplesbelow. DefinitionandUsage Thebackground-imagepropertysetsoneormorebackgroundimagesforanelement. Bydefault,abackground-imageisplacedatthetop-leftcornerofan element,andrepeatedbothverticallyandhorizontally. Tip:Thebackgroundofanelementisthetotalsizeoftheelement,including paddingandborder(butnotthemargin). Tip:Alwayssetabackground-colortobeusediftheimageisunavailable. Defaultvalue: none Inherited: no Animatable: no.Readaboutanimatable Version: CSS1+newvaluesinCSS3 JavaScriptsyntax: object.style.backgroundImage="url(img_tree.gif)" Tryit BrowserSupport Thenumbersinthetablespecifythefirstbrowserversionthatfullysupportstheproperty. Property background-image 1.0 4.0 1.0 1.0 3.5 Note:IE8andearlierdonotsupportmultiplebackgroundimages. CSSSyntax background-image:url|none|initial|inherit; PropertyValues Value Description url('URL') TheURLtotheimage.Tospecifymorethanoneimage,separatetheURLswithacomma none Nobackgroundimagewillbedisplayed.Thisisdefault conic-gradient() Setsaconicgradientasthebackgroundimage.Defineatleasttwo colors linear-gradient() Setsalineargradientasthebackgroundimage.Defineatleasttwo colors(toptobottom) radial-gradient() Setsaradialgradientasthebackgroundimage.Defineatleasttwo colors(centertoedges) repeating-conic-gradient() Repeatsaconicgradient repeating-linear-gradient() Repeatsalineargradient repeating-radial-gradient() Repeatsaradialgradient initial Setsthispropertytoitsdefaultvalue.Readaboutinitial inherit Inheritsthispropertyfromitsparentelement.Readaboutinherit MoreExamples Example Setstwobackgroundimagesfortheelement.Letthefirst imageappearonlyonce(withno-repeat),andletthesecondimageberepeated: body{ background-image:url("img_tree.gif"),url("paper.gif");   background-repeat:no-repeat,repeat; background-color:#cccccc;} TryitYourself» Example Usedifferentbackgroundpropertiestocreatea"hero"image: .hero-image{ background-image:url("photographer.jpg");/*The imageused*/ background-color:#cccccc;/*Usediftheimageis unavailable*/ height:500px;/*Youmustsetaspecifiedheight*/  background-position:center;/*Centertheimage*/  background-repeat:no-repeat;/*Donotrepeattheimage*/  background-size:cover;/*Resizethebackgroundimagetocovertheentirecontainer*/} TryitYourself» Example Setsalinear-gradient(twocolors)asabackgroundimagefora
element: #grad1{  height:200px; background-color:#cccccc; background-image: linear-gradient(red,yellow);} TryitYourself» Example Setsalinear-gradient(threecolors)asabackgroundimagefora
element: #grad1{ height:200px; background-color:#cccccc; background-image: linear-gradient(red,yellow,green);} TryitYourself» Example Therepeating-linear-gradient()functionisusedtorepeatlineargradients: #grad1{ height:200px; background-color:#cccccc;  background-image: repeating-linear-gradient(red,yellow10%,green20%);} TryitYourself» Example Setsaradial-gradient(twocolors)asabackgroundimagefora
element: #grad1{  height:200px;  background-color:#cccccc;  background-image: radial-gradient(red,yellow);} TryitYourself» Example Setsaradial-gradient(threecolors)asabackgroundimagefora
element: #grad1{ height:200px;  background-color:#cccccc;  background-image: radial-gradient(red,yellow,green);} TryitYourself» Example Therepeating-radial-gradient()functionisusedtorepeatradialgradients: #grad1{ height:200px; background-color:#cccccc; background-image: repeating-radial-gradient(red,yellow10%,green20%);} TryitYourself» RelatedPages CSStutorial:CSSBackground, CSSBackgrounds(advanced), CSSGradients HTMLDOMreference:backgroundImageproperty ❮Previous CompleteCSSReference Next❯ NEW WejustlaunchedW3Schoolsvideos Explorenow COLORPICKER LIKEUS Getcertifiedbycompletingacoursetoday! w3schoolsCERTIFIED.2021 Getstarted CODEGAME PlayGame


請為這篇文章評分?