背景图像位置css

This tutorial will show you a simple way to code a full page background image using CSS. And you'll also learn how to make that image responsive to your users' screen size.

本教程将向您展示一种使用CSS编写整页背景图像的简单方法。 您还将学习如何使图像响应用户的屏幕尺寸。

Making a background image fully stretch out to cover the entire browser viewport is a common task in web design. Fortunately, this task can be taken care of with a few lines of CSS.

使背景图像完全伸展以覆盖整个浏览器视口是Web设计中的常见任务。 幸运的是,可以使用几行CSS来完成此任务。

用图像覆盖视口 (Cover Viewport with Image)

First, we will want to make sure our page actually covers the entire viewport:

首先,我们要确保我们的页面实际上覆盖了整个视口:

html {min-height: 100%;
}

Inside html, we will use the background-image property to set the image:

html内部,我们将使用background-image属性设置图像:

background-image: url(image.jpg); /*replace image.jpg with path to your image*/

“背景大小”属性的魔力 (Magic of 'Background-Size' Property)

The magic happens with the background-size property:

神奇之处在于background-size属性:

background-size: cover;

cover tells the browser to make sure the image always covers the entire container, in this case html. The browser will cover the container even if it has to stretch the image or cut a little bit off the edges.

cover告诉浏览器确保图像始终覆盖整个容器,在本例中为html 。 即使浏览器必须拉伸图像或从边缘切掉一点,浏览器也将覆盖它。

Because the browser may stretch the image, you should use a background image that has high enough resolution. Otherwise the image may appear pixelated.

因为浏览器可能会拉伸图像,所以您应该使用分辨率足够高的背景图像。 否则,图像可能会显得像素化。

If you care about having all of the image appear in the background, then you will want to make sure the image is relatively close in aspect ratio compared to the screen size.

如果您想让所有图像都出现在背景中,那么您将要确保图像的长宽比与屏幕尺寸相比相对接近。

如何微调图像位置并避免平铺 (How to Fine Tune an Image Position and Avoid Tiling)

The browser can also choose to display your background image as tiles depending on its size. To prevent this from happening, use no-repeat:

浏览器还可以选择根据背景图像的大小将其显示为图块。 为防止这种情况发生,请使用no-repeat

background-repeat: no-repeat;

To keep things pretty, we will keep our image always centered:

为了保持美观,我们将图像始终居中:

background-position: center center;

This will center the image both horizontally and vertically at all times.

这将始终使图像水平和垂直居中。

We have now produced a fully responsive image that will always cover the entire background:

现在,我们生成了一个完全响应的图像,它将始终覆盖整个背景:

html {min-height: 100%;background-image: url(image.jpg);background-size: cover;background-repeat: no-repeat;background-position: center center;
}

滚动时如何固定图像 (How to Fix an Image in Place When Scrolling)

Now, just in case you want to add text on top of the background image and that text overflows the current viewport, you may wish to make sure your image stay in the background when the user scrolls down to see the rest of the text:

现在,以防万一您想在背景图像上方添加文本并且该文本溢出当前视口,您可能希望确保当用户向下滚动以查看文本的其余部分时图像保留在背景中:

background-attachment: fixed;

You can include all of the background properties described above using short notation:

您可以使用短记号包括上述所有背景属性:

background: url(image.jpg) center center cover no-repeat fixed;

可选:如何为移动设备添加媒体查询 (Optional: How to Add a Media Query for Mobile)

To add some icing on the cake, you may wish to add a media query for smaller screens:

要为蛋糕锦上添花,您可能希望为较小的屏幕添加媒体查询:

@media only screen and (max-width: 767px) {html {background-image: url(smaller-image.jpg);}
}

You can use Photoshop or some other image editing software to downsize your original image to improve page load speed on mobile internet connections.

您可以使用Photoshop或其他一些图像编辑软件来缩小原始图像的尺寸,以提高移动Internet连接上的页面加载速度。

So now that you know the magic of creating a responsive, full page image background, it is time to go create some beautiful websites!

因此,既然您知道创建自适应的全页图像背景的魔力,那么现在该创建一些漂亮的网站了!

是否想查看更多Web开发技巧和知识? (Want to See More Web Development Tips and Knowledge?)

  • Subscribe to my weekly newsletter

    订阅我的每周新闻

  • Visit my blog at  1000 Mile World

    访问我在1000 Mile World上的博客

  • Follow me on Twitter

    在Twitter上关注我

翻译自: https://www.freecodecamp.org/news/css-full-page-background-image-tutorial/

背景图像位置css

背景图像位置css_CSS背景图像大小教程–如何对整页背景图像进行编码相关推荐

  1. 黑马程序员前端-CSS背景:颜色、图片、平铺、背景图片位置、背景图像

    前端学习笔记教程不定期更新中,传送门: 前端HTML第一天:什么是网页?什么是HTML?网页怎么形成? 黑马程序员前端-CSS入门总结 黑马程序员前端-CSS之emmet语法 黑马程序员前端-CSS的 ...

  2. html div背景图片路径,css如何设置背景图片位置?

    在使用背景图片时,经常会遇到背景图片所在的位置不是我们想要的,那如何设置背景图片的位置呢?下面我们来看一下css设置背景图片位置的方法. 在背景图片的背景图片位置属性中,background-posi ...

  3. php背景图片 存放位置,CSS中背景图片位置 background-position 的使用方法

    在使用背景图片时,经常会遇到背景图片所在的位置不是我们想要的,那如何设置背景图片的位置呢?以下就来介绍CSS中背景图片位置 background-position 的用法. 在背景图片的背景图片位置属 ...

  4. div css sprites精灵-CSS图像拼合 CSS贴图定位网页背景素材图片拼合定位布局技术教程

    div css sprites精灵-CSS图像拼合 CSS贴图定位网页背景素材图片拼合定位布局技术教程篇与css sprites实例篇 css sprites拼合背景图片素材实现布局效果截图 一.什么 ...

  5. css设置背景颜色/背景图像/背景图像平铺/背景图像位置/背景图像固定显示/综合设置元素背景的方法(学习笔记)

    一.设置背景颜色 在CSS当中,网页元素的背景颜色使用background-color属性来设置,和文本颜色的用法类似. 用法 <style>h2{color:red;backround- ...

  6. Css的背景(背景颜色、背景图片、背景平铺、背景图片位置、背景图像固定)

    通过css背景属性,可以给页面元素添加背景样式. 背景属性可以设置背景颜色.背景图片.背景平铺.背景图片位置.背景图像固定等. 背景颜色: Background-color:#******: < ...

  7. 设置背景图像位置、块元素和行内元素、display

    84.设置背景图像位置 (background-position:right bottom) 85.元素的类型: (1)块元素:<h><p><div><ul& ...

  8. python 图像无缝拼接,OpenCV Python 系列教程3 - Core 组件

    基本知识 灰度图像的存储方式: image 多通道图像存储方式 image OpenCV 中的通道存储为 BGR 像素值的存储方式 RGB 模式,显示设备采用这种模式 HSV.HLS 将颜色分解成色调 ...

  9. CSS3 背景起始位置 background-origin属性

    默认情况下,背景图像从元素的 padding 区域的左上角开始显示.然而,有时候却希望背景图像从边框的左上角.或内容区的左上角开始显示. 这时,就可以通过 background-origin属性来设置 ...

最新文章

  1. 听妈妈的话-07年感动的延续
  2. Redis进阶-Jedis以及Spring Boot操作 Redis 5.x Cluster
  3. 博客园 CSS 代码定制
  4. 王思聪欠款1.5亿成被执行人 孙宇晨:我帮你还钱!
  5. BiLSTM+CRF医学病例命名实体识别项目
  6. NFS客户端挂载目录后无写入权限的解决方案
  7. 正宗PC Unix实验环境
  8. springweb 初步理解
  9. 论文模型构建的步骤_论文实证经验分享|VAR模型实操步骤(下)
  10. 洛谷 P1957 口算练习题
  11. 什么是三网合一短信接口呢
  12. 给定平面上任意三个点的坐标,检验它们能否构成三角形。
  13. 大牛教你这样阅读android源码
  14. JAVA 利用多方法查找质数(素数)
  15. 十二届蓝桥杯Scratch国赛试题
  16. 利用LSTM对脑电波信号进行分类
  17. MySQL 8.0安装教程
  18. 计算机课说话检查200字,上课说话检讨书200字
  19. 赶紧来修炼内功~字符串函数详解大全(三)
  20. 安装PS AI过程--最新(photoshop AI)photoshop beta 安装

热门文章

  1. 苹果电脑安装python3密码_mac系统安装Python3初体验
  2. 根据xml 文件生成 xsd文件 以及相关测试方法
  3. springboot 读取配置文件内容的几种方式
  4. python简易版实例_Python3之简单搭建自带服务器的实例讲解
  5. 1小时学会:最简单的iOS直播推流(四)如何使用GPUImage,如何美颜
  6. 宝塔的服务忽然挂掉解决方法
  7. [SCOI2009]生日礼物
  8. android保持服务不休眠(持续运行)以及唤醒屏幕的方法
  9. 如何让phpmyadmin输入密码再进入
  10. 【SQL Server】系统学习之一:表表达式