search
HomeWeb Front-endHTML TutorialCss基本样式――――背景_html/css_WEB-ITnose

一、CSS允许应用纯色作为背景,也允许使用背景图像创建相当复杂的效果

二、属性:

    background-color    设置元素的背景颜色

body{    background-color: darkgray;}<p>测试一下背景是否可以继承</p>

background-color这个属性是不可以被继承的

p{    background-color: aqua;}

这时候可以看到p元素加上了一个背景颜色,但是这个时候背景颜色比较长,它已经超出了文字的范围,要想修改的话只能在css样式来进行修改,修改它整个p标签的宽度

p{    width: 150px;    background-color: aqua;}

现在可以看到p标签宽度被修改后的效果,但是这个时候的背景颜色有点窄,那么可以通过一个padding属性给它增宽

p{    width: 150px;    padding: 10px;    background-color: aqua;}

    background-image    把图片设置为背景

body{    background-image: url("1.jpg");}

也可以单独地给一个标签加上背景颜色

p{    background-image: url("1.jpg");}

    background-position    设置背景图片的起始位置

body{    background-image: url("1.jpg");    background-repeat: no-repeat;    background-position: right;}

这时候背景图片跑到了右边并且图片只剩下一半了

其实这个属性很少单独出现,我们看到的是一个right,其实它是出现了两个属性

right代表的是“right”和“center”

right代表的是图片所处的当前视图的位置

center是代表当前图片要显示的位置(即图片从中间开始显示)

这个时候可以对background-position属性值做一下修改

background-position: right top;

这时候代表背景图片在视图的右侧显示,从图片的顶部开始(可以看到全面显示的效果)

也可以在background-position内添加具体的数值

background-position: 0px 0px;

这时候代表从左上角(0,0)点开始

很多时候用数值来确定它的位置会方便一些

也可以用百分数来确定它的位置

    background-attachment    背景图像是否固定或者随着页面的其余部分滚动

body{    background-image: url("1.jpg");    background-repeat: no-repeat;    background-attachment: fixed;}

默认情况下背景图片随着页面滚动

这里把background-attachment属性设置为不随着页面滚动

    background-repeat    设置背景图片是否及如何重复

这里在前面已经使用到了


三、CSS3背景

    background-size    规定背景图片的尺寸

body{    background-image: url("1.jpg");    background-repeat: no-repeat;    background-size: 600px 600px;}

    background-origin    规定背景图片的定位区域

    background-clip    规定背景的绘制区域



    

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

HTML's Purpose: Enabling Web Browsers to Display ContentHTML's Purpose: Enabling Web Browsers to Display ContentMay 03, 2025 am 12:03 AM

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

Explain the importance of using consistent coding style for HTML tags and attributes.Explain the importance of using consistent coding style for HTML tags and attributes.May 01, 2025 am 12:01 AM

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.