console
window.addEventListener('load', function () {
var goBack = document.querySelector('.goBack');
window.addEventListener('scroll', function () {
if (window.pageYOffset >= 100) {
goBack.style.display = 'block';
} else {
goBack.style.display = 'none';
}
});
goBack.addEventListener('click', function () {
window.scroll(0, 0);
});
})
<body>
<div class="page">
<div class="inner">
<div class="cont">
<div class="goBack"></div>
<h1 class="title"><span>CSS3 图像填充</span></h1>
</div>
<div class="cont">
<div class="w">
<h3>
<span></span>
<span>CSS3 图像填充</span>
</h3>
<p><code>object-fit</code> 属性用于规定应如何调整<code>img</code>或<code>video</code>的大小来适应其容器</p>
<p><code>object-fit</code> 属性的所有值:</p>
<div class="table-box">
<table>
<thead>
<tr>
<th>属性值</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>fill</code></td>
<td>默认值,调整替换后的内容大小,以填充元素的内容框,如有必要,将拉伸或挤压物体以适应该对象</td>
</tr>
<tr>
<td><code>contain</code></td>
<td>缩放替换后的内容以保持其纵横比,同时将其放入元素的内容框</td>
</tr>
<tr>
<td><code>cover</code></td>
<td>调整替换内容的大小,以在填充元素的整个内容框时保持其长宽比。该对象将被裁剪以适应</td>
</tr>
<tr>
<td><code>none</code></td>
<td>不对替换的内容调整大小</td>
</tr>
<tr>
<td><code>scale-down</code></td>
<td>调整内容大小就像没有指定内容或包含内容一样(将导致较小的具体对象尺寸)</td>
</tr>
</tbody>
</table>
</div>
<div class="link">
<a href="https://www.w3school.com.cn/tiy/t.asp?f=css3_object-fit_2" target="_blank">亲自试一试</a>
</div>
<figure>
<img src="https://www.w3school.com.cn/i/photo/tulip.jpg" alt="">
</figure>
<div class="demo clear">
<div class="fill">
<img src="https://www.w3school.com.cn/i/photo/tulip.jpg" style="object-fit:fill" alt="">
<h4>fill</h4>
</div>
<div class="contain">
<img src="https://www.w3school.com.cn/i/photo/tulip.jpg" style="object-fit:contain" alt="">
<h4>contain</h4>
</div>
<div class="cover">
<img src="https://www.w3school.com.cn/i/photo/tulip.jpg" style="object-fit:cover" alt="">
<h4>cover</h4>
</div>
<div class="none">
<img src="https://www.w3school.com.cn/i/photo/tulip.jpg" style="object-fit:none" alt="">
<h4>none</h4>
</div>
<div class="scale">
<img src="https://www.w3school.com.cn/i/photo/tulip.jpg" style="object-fit:scale-down" alt="">
<h4>scale-down</h4>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
* {
margin: 0;
padding: 0;
outline: 0;
box-sizing: border-box;
}
.fl {
float: left;
}
.fr {
float: right;
}
.clear:after {
visibility: hidden;
clear: both;
display: block;
content: '';
height: 0
}
.clear {
*zoom: 1;
}
a {
color: #333;
text-decoration: none;
}
i,
em {
font-style: normal;
}
i {
color: #c81623;
}
b {
color: #135ce0;
}
strong {
color: #373737;
}
img {
border: 0;
vertical-align: middle;
}
input {
outline: none;
}
button {
cursor: pointer;
}
div.goBack {
display: none;
position: fixed;
bottom: 22px;
left: 50%;
transform: translateX(-50%);
margin-left: 358px;
width: 26px;
height: 26px;
background: url(https://g.csdnimg.cn/side-toolbar/3.4/images/fanhuidingbucopy.png) no-repeat;
background-size: cover;
z-index: 999;
}
html {
-webkit-text-size-adjust: 100%;
}
body {
font-family: system-ui,-apple-system,BlinkMacSystemFont,Helvetica Neue,PingFang SC,Hiragino Sans GB,Microsoft YaHei UI,Microsoft YaHei,Arial,"\5B8B\4F53",sans-serif;
-webkit-font-smoothing: antialiased;
-webkit-appearance: none;
-webkit-tap-highlight-color: rgba(0,0,0,0);
text-underline-position: under;
text-decoration-skip-ink: none;
background-color: #fff;
}
.page {
position: relative;
word-wrap: break-word;
hyphens: auto;
background-color: #fff;
padding: calc(10px + env(safe-area-inset-top)) calc(20px + env(safe-area-inset-right)) 0 calc(20px + env(safe-area-inset-left));
}
.inner {
position: relative;
max-width: 677px;
margin-left: auto;
margin-right: auto;
zoom: 1;
}
.inner .title {
font-size: 22px;
line-height: 1.4;
margin-bottom: 15px;
text-align: center;
}
.inner .title span {
padding: 0 0 10px;
border-bottom: 3px solid rgb(32, 87, 146);
}
h1, h2, h3, h4, h5, h6 {
font-weight: 700;
font-size: 16px;
}
.cont {
visibility: visible;
position: relative;
overflow: auto;
color: #222;
font-size: 17px;
word-wrap: break-word;
hyphens: auto;
text-align: justify;
z-index: 0;
}
.cont * {
max-width: 100%!important;
box-sizing: border-box!important;
-webkit-box-sizing: border-box!important;
word-wrap: break-word!important;
}
.cont .w {
position: relative;
font-size: 16px;
color: black;
margin-bottom: 15px;
padding-right: 10px;
padding-left: 10px;
word-break: break-word;
overflow-wrap: break-word;
text-align: left;
line-height: 1.25;
letter-spacing: 2px;
font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
visibility: visible;
border: 1px solid transparent;
}
.cont .w:last-child {
margin-bottom: 100px;
}
.cont h3 {
margin-top: 20px;
margin-bottom: 15px;
font-weight: bold;
color: #373737;
font-size: 18px;
visibility: visible;
}
.cont h3 span:nth-child(1) {
position: absolute;
left: -1px;
margin-top: 2px;
width: 4px;
height: 20px;
background-color: rgb(199, 29, 35);
border-radius: 2px;
display: block;
float: left;
visibility: visible
}
.cont h3 span:nth-child(2) {
margin-left: 0px;
line-height: 26px;
visibility: visible;
}
.cont p {
margin-left: 1px;
margin-right: 8px;
padding-top: 8px;
padding-bottom: 8px;
line-height: 26px;
font-size: 14px;
word-spacing: 2px;
color: rgb(55, 55, 55);
text-align: justify;
visibility: visible;
min-height: 1em;
}
.cont ul {
padding-left: 42px;
font-size: 14px;
}
.cont ul li p {
line-height: 35px;
word-spacing: 2px;
margin-left: 5px;
margin-right: 8px;
padding: 0;
}
.cont ol {
padding-left: 48px;
font-size: 14px;
}
.contol li p {
line-height: 35px;
word-spacing: 2px;
margin-left: -2px;
margin-right: 8px;
padding: 0;
}
.cont .table-box {
overflow-x: auto;
}
.cont table {
display: table;
width: 100%;
max-width: 600px !important;
margin: 0 auto 15px;
border-collapse: collapse;
word-break: break-word;
overflow-wrap: break-word;
font-size: 14px;
}
.cont table tr {
text-align: left;
border-top: 1px solid #dfe2e5;
}
.cont table tr td:nth-child(1) {
white-space: nowrap;
}
.cont table th {
color: #135ce0;
font-size: 17px;
white-space: nowrap;
}
.cont table th,
.cont table td {
border: 1px solid #dfe2e5;
padding: 9px 20px;
}
.cont table td p {
font-size: 14px;
font-weight: 400;
line-height: 1.5;
text-indent: 0;
margin: 0;
padding: 0;
}
.cont table td p:nth-child(n+2) {
margin: 10px 0 0 0;
}
.cont figure {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin: 15px auto 15px;
visibility: visible;
}
.cont figure img {
box-shadow: rgb(55 55 55 / 12%) 2px 4px 6px 0px;
border-radius: 8px;
visibility: visible !important;
height: auto !important;
width: 300px !important;
}
.cont .link {
margin: 20px 0;
}
.cont .link a {
padding: 10px 16px;
font-size: 13px;
color: white;
background-color: #e9686b;
border-radius: 8px;
}
.cont blockquote {
color: rgb(106, 115, 125);
margin: 15px 8px 20px;
padding: 16px;
border-width: 1px;
border-top-style: solid;
border-right-style: solid;
border-bottom-style: solid;
border-left: 3px solid #DBDBDB;
border-color: rgb(199, 29, 35);
border-radius: 8px;
box-shadow: rgb(199 29 35) 8px 8px 0px;
background: rgb(255, 255, 255);
}
.cont blockquote p {
font-size: 14px;
word-spacing: 2px;
line-height: 26px;
color: rgb(55, 55, 55);
text-align: left;
}
.cont pre {
margin: 10px auto 10px;
border-radius: 5px;
box-shadow: none !important;
position: relative;
overflow-x: auto;
padding: 8px 16px;
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
font-size: 13px;
color: #abb2bf;
text-align: left;
line-height: 24px;
letter-spacing: 0px;
-webkit-overflow-scrolling: touch;
background: #272822;
white-space: pre;
}
.cont code {
font-size: 14px;
overflow-wrap: break-word;
font-family: Consolas, Monaco, Andale Mono, Ubuntu Mono, monospace;
word-break: break-all;
color: rgb(199, 29, 35);
}
.t0 {
color: #3f9cd6 !important;
}
.t1 {
color: #dcce80 !important;
}
.t2 {
color: #7cdcfe !important;
}
.t3 {
color: #b5ce9b !important;
}
.t4 {
color: #ce9178 !important;
}
.t5 {
color: #c678dd !important;
}
.t6 {
color: #ff8c00 !important;
}
.t8 {
color: #de645b !important;
}
.t9 {
color: #4a892d !important;
}
.demo {
width: 600px;
margin: 15px auto;
}
.demo>div {
width: 31.33%;
float: left;
border: 1px solid #ccc;
margin: 0 2px 5px;
}
.demo h4 {
width: 100%;
text-align: center;
}
.demo>div>img {
display: block;
margin-left: auto;
margin-right: auto;
width: 200px !important;
height: 300px !important;
}
@media screen and (max-width: 500px) {
.cont .w p {
font-size: 16px;
}
.cont .w ul li p,
.cont .w ol li p {
font-size: 14px;
}
.cont ol p code,
.cont ul p code {
font-size: 14px;
}
.cont ol p a,
.cont ul p a, {
font-size: 14px;
}
.cont .w table td p {
font-size: 14px;
}
.cont .w table code {
font-size: 14px;
}
.cont .w blockquote p {
font-size: 14px;
}
.cont p code {
font-size: 16px;
}
div.goBack {
margin-left: 238px !important;
background-size: 70% 70%;
}
}
@media screen and (max-width: 300px) {
.demo>div {
width: 100%;
}
}