Table of Contents
The operating environment of this article: Windows7 system, HTML5&&CSS3 version, Dell G3 computer.
Background style
Commonly used background styles
background-size
background-clip与background-origin
边框样式
常用的边框样式
border-radius
border-image
Shadow
Home Web Front-end CSS Tutorial How to add borders to css background image

How to add borders to css background image

Mar 01, 2021 am 10:18 AM
css

How to add a border to a css background image: First create an HTML sample file; then introduce a background image through "background-image"; and finally set the border through the border attribute.

How to add borders to css background image

The operating environment of this article: Windows7 system, HTML5&&CSS3 version, Dell G3 computer.

Background style

Commonly used background styles
  • Background color: background-color
background-color: gray;
background-color: #808080;
background-color: rgb(128,128,128);
Copy after login
  • Background image: background -image
body{background-image: url("images/bg.jpg");}
Copy after login
  • Background-image repeat: background-repeat
value description
repeat Default. The background image will repeat both vertically and horizontally
repeat-x The background image will repeat horizontally
repeat -y The background image will repeat vertically
no-repeat The background image will be displayed only once
inherit Specifies that the setting of the background-repeat attribute should be inherited from the parent element
  • Background image positioning: background-position

background-position attribute value:

①Use keyword: background-position:center left

②Use percentage value: background-position: 50% 50%

③Use length value:background-position:300px 100px

/*背景样式综合使用*/
background: #00ff00 url(image/bg.jpg)  no-repeat center;
Copy after login
  • Background image fixed: background- attachment

background-attachment property sets whether the background image is fixed or scrolls with the rest of the page.

  1. scroll: Default value. The background image moves as the page is scrolled.
  2. fixed: The background image does not move when the rest of the page scrolls.
    [Recommended: css video tutorial]
background-size
  • Specifies the size of the background image
  • Syntax: background-size: length| percentage| cover| contain;
##ValueDescriptionlength Set the width and height of the background image as a length value composed of floating point numbers and units. If only the first value is set, then The second value will be set to "auto"percentageSets the width and height of the background image as a percentage of the parent element. If only the first one is set value, the second value will be set to "auto"coverKeep the width and height ratio of the background image itself, and scale the image to exactly cover the defined The area of ​​the backgroundcontainKeep the width and height ratio of the background image itself, and scale the image to a width or height that exactly fits the area of ​​the defined background

How to add borders to css background image

cover and contain


nbsp;html>


    <meta>
    <title>背景</title>
    <style>
        p{width: 700px;height: 400px;border: 2px solid plum;background-repeat: no-repeat;
            background-image: url(img/design.jpg);background-size: cover;}
    </style>


    <p>图片尺寸</p>

Copy after login
background-origin
    Specifies the positioning area of ​​the background image ( Reference position of background-position)
  • Syntax:
  • background-origin: border-box | padding-box | content-box;
The default is

padding-box

How to add borders to css background image

background-origin


nbsp;html>


    <meta> 
    <style> 
        p{border:10px solid black;padding:35px;background-image:url(img/smiley.gif);
        background-repeat:no-repeat;background-position:0px 0px;}
        #p1{background-origin: padding-box;}
        #p2{background-origin:content-box;}
    </style>


    <p>背景图像边界框的相对位置:</p>
    <p>
      CSS 允许应用纯色作为背景,也允许使用背景图像创建相当复杂的效果。CSS 在这方面的能力远远在 HTML 之上。CSS 允许应用纯色作为背景,也允许使用背景图像创建相当复杂的效果。CSS 在这方面的能力远远在 HTML 之上。
    </p>
    <p>背景图像的相对位置的内容框:</p>
    <p>
      CSS 允许应用纯色作为背景,也允许使用背景图像创建相当复杂的效果。CSS 在这方面的能力远远在 HTML 之上。CSS 允许应用纯色作为背景,也允许使用背景图像创建相当复杂的效果。CSS 在这方面的能力远远在 HTML 之上。
    </p>

Copy after login
background-clip
    Specifies the background Drawing area (determines where the background is displayed).
  • Syntax: background-clip: border-box| padding-box| content-box;
ValueDescription border-boxCrop the background outward from the border areapadding-boxCrop the background outward from the padding areacontent-boxCrop the background outward from the content area
background-clip与background-origin
  • background-clip:border| padding| content  指定背景在哪些区域可以显示,但与背景开始绘制的位置无关。背景的绘制的位置可以出现在不显示背景的区域,这时就相当于背景图片被不显示背景的区域裁剪了一部分。
  • background-origin:padding| border| content  指定背景从哪个区域(边框、补白或内容)开始绘制。可以用此属性在边框上绘制背景,但边框上的背景显不显示出来就要由background-clip来决定了。
nbsp;html>


    <meta> 
    <style>
        #example1 {
            width: 800px;
            height: 450px;
            border: 20px dotted black;
            padding: 50px;
            background-image: url(img/girl.jpg);
            background-size: 100% 100%;
            background-repeat: no-repeat;           
            background-clip: padding-box;/*padding-box以外的都要剪辑掉*/
            background-origin: border-box;/*从border-box开始进行剪辑*/
        }
    </style>s


    <p></p>

Copy after login

边框样式

常用的边框样式

How to add borders to css background image

border

  • 设置边框宽度:border-width 宽度值。示例: border-width:1px;

  • 设置边框颜色:border-color 颜色。示例:border-color:#cccccc;

  • 设置边框样式:border-style 样式关键字。示例:border-style: solid;

How to add borders to css background image

边框线类型

  • 边框复合样式:border: width / style / color 示例:border: 3px dotted#ff9900
border-radius

圆角边框——border-radius 属性。

  • 语法 border-radius: 1-4 length | %;
  • border-radius属性可包含两个参数值,第一个值表示圆角的水平半径,第二个值表示圆角的垂直半径,两个参数值通过斜线分隔。如果仅包含一个参数值,表示两个值相同,即1/4圆角。
单位 描述
length 定义圆角的形状。(圆角半径)
% 比百分比定义圆角的形状

How to add borders to css background image

border-radius

如果要绘制的圆角边框4个角的半径各不相同时,需按左上角、右上角、右下角、左下角的顺序设置每个方向圆角半径的值。

  • border-top-left-radius、border-top-right-radius、border-bottom-right-radius、border-bottom-left-radius(绘制圆角边框的四个角的半径各不相同时,按照上述的顺序)
  • 如果省略 bottom-right,则与 top-left 相同。如果省略 top-right,则与 top-left 相同。
p{
    border-top-left-radius: 0px;
    border-top-right-radius: 10px;
    border-bottom-right-radius: 20px;
    border-bottom-left-radius: 30px;
    /*上述的四句代码等同于:border-radius:0px 10px 20px 30px;*/
}
Copy after login
border-image

图像边框——border-image 属性

  • 可以让元素的长度或宽度处于随时变化的边框统一使用一个图像文件进行绘制。
  • 语法:border-image: url(图像文件的路径) A B C D

ABCD四个参数表示浏览器自动把图像分隔时的上边距、右边距、下边距以及左边距。

How to add borders to css background image

border-image 属性是一个简写属性,用于设置以下属性:

描述
border-image-source 定义用在边框的图片的路径
border-image-slice 定义如何裁切背景图像
border-image-width 图片边框的宽度
border-image-outset 边框图像区域超出边框的量
border-image-repeat 图像边框是否应平铺(repeated)、铺满(rounded)或拉伸(stretched)

Shadow

Add one or more shadows to the box - box-shadow property

Syntax: box-shadow: h-shadow v-shadow blur spread color inset;

How to add borders to css background image

How to add borders to css background image

The above is the detailed content of How to add borders to css background image. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

See all articles