


Background-orgin in CSS3 implements photo display effect with photo frame - case analysis (code example)
Goal of this article:
1. Master the use of background-origin background origin
Question:
1. To achieve the following effects, use pure DIV CSS, The background origin background-orgin must be used
1. Prepare the material: root directory Create an images folder and store all relevant material pictures here. The materials are
2. Create it index.html, write the architecture, how to analyze the architecture
Idea analysis:1. The target is divided into upper and lower parts. Each part is actually displayed with a photo frame (div containing border) photo, so as long as the first implementation is completed, the second part can be copied 2. However, if you look carefully, the display of the above photo and the photo below is still a little different. The background image of the first div is Within the border, and the background image of the div below is within the border 3. Each part is actually a DIV, including the border, and the background image is a photo, which contains a string, the upper and lower parts of the string Contains spacingAccording to the analysis, we get the following code<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>背景原点制作怀旧照片</title> </head> <body> <div class="container"> <div class="photo xtf"> <div class="content">谢霆锋年轻的样子,一个字帅</div> </div> <div class="photo zbz"> <div class="content">张柏芝年轻的样子,一个字美</div> </div> </div> </body> </html>
3. Write the style, create a css folder, create a new index.css inside, how to write the style inside, as follows Is the analysis idea
Idea analysis:1. .container *Idea analysis1.In order to set all elements in the container Public styles, we can write these public codes into .container * styleSo add the following code to index.css:.container *{ padding:0; margin:0; }2, .photo photo1 . According to the requirements, the border is 20px and the dotted line is displayed. The converted code is border-width: 20px; border-style: dashed; the text inside has spacing, so padding: 20px, the font is bold, font-weight: bold, and the color is White color:#fff;
.photo { width:420px; height: 420px; border-width:20px; border-style: dashed; padding:20px; font-weight:bold; color:#fff; }3. First photo settings
Because the border of this photo is red, so border-color: red;Then the background is a photo of a handsome guy, and from the effect you can see that the color between the dotted lines is gray, which is #ccc, so background:#ccc url(../images/Background-orgin in CSS3 implements photo display effect with photo frame - case analysis (code example)) no-repeat; The most important thing is that its background image starts from padding, so
background-origin: padding-box; you must pay attention here, In order to make background-origin takes effect, background must set the attribute no-repeat
So add the following code to index.css:.xtf{ background:#ccc url(../images/Background-orgin in CSS3 implements photo display effect with photo frame - case analysis (code example)) no-repeat; border-color: red; background-origin: padding-box; }4. Settings for the second photo
Because the border of this photo is orange, border-color: orange; and then the background is a photo of a beautiful woman, and you can see from the effect The color between the dotted lines is gray, which is #ccc, so background:#ccc url(../images/Background-orgin in CSS3 implements photo display effect with photo frame - case analysis (code example)) no-repeat; The most important thing is that its background image starts from the border, so
background-origin: border-box;In order for background-origin to take effect, background must set the attribute no-repeat
So add the following code to index.css:.zbz{ background:#ccc url(../images/Background-orgin in CSS3 implements photo display effect with photo frame - case analysis (code example)) no-repeat; background-origin: border-box; border-color: orange; }So far, the entire content of index.css is as follows:
.container *{ padding:0; margin:0; } .photo { width:420px; height: 420px; border-width:20px; border-style: dashed; padding:20px; font-weight:bold; color:#fff; } .xtf{ background:#ccc url(../images/Background-orgin in CSS3 implements photo display effect with photo frame - case analysis (code example)) no-repeat; border-color: red; background-origin: padding-box; } .zbz{ background:#ccc url(../images/Background-orgin in CSS3 implements photo display effect with photo frame - case analysis (code example)) no-repeat; background-origin: border-box; border-color: orange; }Then introduce index.css into index.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>背景原点制作怀旧照片</title> <link href="css/index.css" rel="stylesheet" /> </head> <body> <div class="container"> <div class="photo xtf"> <div class="content">谢霆锋年轻的样子,一个字帅</div> </div> <div class="photo zbz"> <div class="content">张柏芝年轻的样子,一个字美</div> </div> </div> </body> </html>The running effect is as follows:
.xtf{ background:#ccc url(../images/Background-orgin in CSS3 implements photo display effect with photo frame - case analysis (code example)) no-repeat; border-color: red; background-origin: content-box;/**修改成content-box**/ }
运行结果为:
我们就发现背景图片按照内容为原点显示了
总结:
1、学习了背景原点background-origin的用法,它有3个类型,
一个是border-box:背景图片以边框为起始点
一个是padding-box:背景图片以padding为起始点
一个是content-box:背景图片以内容为起始点
The above is the detailed content of Background-orgin in CSS3 implements photo display effect with photo frame - case analysis (code example). For more information, please follow other related articles on the PHP Chinese website!

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more.

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

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),

Dreamweaver CS6
Visual web development tools
