← CSS background-image opacity 背景图像透明度 CSS 伪元素::before与::after详解 →

CSS background-position 属性

原创 2025-09-07 CSS 已有人查阅

CSS background-position 属性详解:精确定位背景图像

概述

CSS background-position 属性用于控制背景图像在元素内的位置。通过这个属性,开发者可以精确地指定背景图像的起始位置,实现各种精美的视觉效果。本文将全面介绍 background-position 属性的使用方法、语法结构和实际应用场景。

基本语法

selector {
  background-position: value;
}

默认情况下,背景图像位于元素的左上角(left top),并在水平和垂直方向上重复。

属性值详解

1. 关键字值

  • left top:左上角

  • left center:左侧中央

  • left bottom:左下角

  • center top:顶部中央

  • center center:正中央

  • center bottom:底部中央

  • right top:右上角

  • right center:右侧中央

  • right bottom:右下角

2. 百分比值

使用百分比值可以更精确地控制背景图像的位置,例如:

  • 25% 75%:距离左侧25%,距离顶部75%

  • 50% 50%:正中央(等同于center center)

3. 长度值

使用具体的长度单位(px、em、rem等)定位:

  • 30px 80px:距离左侧30px,距离顶部80px

  • 2em 3em:距离左侧2em,距离顶部3em

基础示例

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>代码号 - background-position 基础示例</title>
    <style>
        .position-container {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 20px;
            padding: 30px;
            max-width: 1000px;
            margin: 0 auto;
        }
        
        .position-box {
            height: 180px;
            border: 2px solid #3498db;
            background-image: url('https://www.ebingou.cn/biancheng/images/1.jpg');
            background-repeat: no-repeat;
            background-size: 100px;
            display: flex;
            align-items: flex-end;
            justify-content: center;
            padding: 10px;
            font-family: Arial, sans-serif;
            color: #2c3e50;
            font-size: 14px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        .left-top {
            background-position: left top;
        }
        
        .center-center {
            background-position: center center;
        }
        
        .right-bottom {
            background-position: right bottom;
        }
        
        .left-center {
            background-position: left center;
        }
        
        .center-top {
            background-position: center top;
        }
        
        .right-center {
            background-position: right center;
        }
        
        .title {
            text-align: center;
            margin: 20px 0;
            color: #2c3e50;
            font-family: Arial, sans-serif;
        }
    </style>
</head>
<body>
    <h1 class="title">代码号CSS学习:background-position属性</h1>
    
    <div class="position-container">
        <div class="position-box left-top">left top</div>
        <div class="position-box center-center">center center</div>
        <div class="position-box right-bottom">right bottom</div>
        <div class="position-box left-center">left center</div>
        <div class="position-box center-top">center top</div>
        <div class="position-box right-center">right center</div>
    </div>
</body>
</html>

百分比和像素值示例

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>代码号 - 百分比和像素定位示例</title>
    <style>
        .value-container {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 30px;
            padding: 40px;
            max-width: 1000px;
            margin: 0 auto;
        }
        
        .value-box {
            height: 200px;
            border: 2px solid #e74c3c;
            background-image: url('https://www.ebingou.cn/biancheng/images/2.jpg');
            background-repeat: no-repeat;
            background-size: 120px;
            display: flex;
            align-items: flex-end;
            justify-content: center;
            padding: 10px;
            font-family: Arial, sans-serif;
            color: #2c3e50;
        }
        
        .percentage {
            background-position: 25% 75%;
        }
        
        .pixels {
            background-position: 40px 60px;
        }
        
        .mixed {
            background-position: 80% 30px;
        }
        
        .negative {
            background-position: -20px -20px;
        }
        
        .title {
            text-align: center;
            margin: 20px 0;
            color: #2c3e50;
            font-family: Arial, sans-serif;
        }
        
        .description {
            text-align: center;
            margin-bottom: 30px;
            color: #7f8c8d;
            max-width: 800px;
            margin-left: auto;
            margin-right: auto;
        }
    </style>
</head>
<body>
    <h1 class="title">代码号CSS学习:百分比和像素定位</h1>
    <p class="description">使用百分比和像素值可以更精确地控制背景图像的位置,实现各种复杂的布局效果。</p>
    
    <div class="value-container">
        <div class="value-box percentage">25% 75%</div>
        <div class="value-box pixels">40px 60px</div>
        <div class="value-box mixed">80% 30px</div>
        <div class="value-box negative">-20px -20px</div>
    </div>
</body>
</html>

实际应用场景

1. 网页头部背景设计

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>代码号 - 网页头部背景示例</title>
    <style>
        body {
            margin: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            color: #333;
        }
        
        .hero-section {
            height: 500px;
            background-image: url('https://www.ebingou.cn/biancheng/images/3.jpg');
            background-size: cover;
            background-position: center top;
            display: flex;
            align-items: center;
            justify-content: center;
            text-align: center;
            color: white;
            position: relative;
        }
        
        .hero-section::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
        }
        
        .hero-content {
            position: relative;
            z-index: 1;
            max-width: 800px;
            padding: 0 20px;
        }
        
        .hero-content h1 {
            font-size: 3rem;
            margin-bottom: 20px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
        }
        
        .hero-content p {
            font-size: 1.2rem;
            margin-bottom: 30px;
            text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
        }
        
        .cta-button {
            display: inline-block;
            padding: 12px 30px;
            background-color: #3498db;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            font-weight: bold;
            transition: background-color 0.3s;
        }
        
        .cta-button:hover {
            background-color: #2980b9;
        }
        
        .content-section {
            padding: 60px 20px;
            max-width: 1000px;
            margin: 0 auto;
        }
        
        .content-section h2 {
            color: #2c3e50;
            margin-bottom: 30px;
            text-align: center;
        }
        
        .features {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 30px;
        }
        
        .feature {
            text-align: center;
            padding: 20px;
            border-radius: 8px;
            background-color: #f8f9fa;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        
        .feature-icon {
            font-size: 2.5rem;
            margin-bottom: 15px;
            color: #3498db;
        }
    </style>
</head>
<body>
    <section class="hero-section">
        <div class="hero-content">
            <h1>欢迎访问代码号编程学习平台</h1>
            <p>提供专业的HTML、CSS、JavaScript教程和实战项目,帮助您掌握前端开发技能</p>
            <a href="https://www.ebingou.cn/biancheng/" class="cta-button">开始学习</a>
        </div>
    </section>
    
    <section class="content-section">
        <h2>我们的特色服务</h2>
        <div class="features">
            <div class="feature">
                <div class="feature-icon">⛏️</div>
                <h3>系统化教程</h3>
                <p>从基础到高级,循序渐进的学习路径</p>
            </div>
            <div class="feature">
                <div class="feature-icon">⚙️</div>
                <h3>实战项目</h3>
                <p>通过实际项目巩固所学知识</p>
            </div>
            <div class="feature">
                <div class="feature-icon">❄️</div>
                <h3>专家指导</h3>
                <p>经验丰富的开发者提供指导</p>
            </div>
        </div>
    </section>
</body>
</html>

2. 卡片式布局中的背景定位

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>代码号 - 卡片背景定位示例</title>
    <style>
        .card-container {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 30px;
            padding: 40px;
            max-width: 1200px;
            margin: 0 auto;
            background-color: #f5f7fa;
        }
        
        .card {
            background-color: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s, box-shadow 0.3s;
        }
        
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
        }
        
        .card-header {
            height: 180px;
            background-size: cover;
            position: relative;
        }
        
        .card-content {
            padding: 20px;
        }
        
        .card h3 {
            color: #2c3e50;
            margin-top: 0;
            font-family: Arial, sans-serif;
        }
        
        .card p {
            color: #7f8c8d;
            line-height: 1.6;
        }
        
        .card-button {
            display: inline-block;
            padding: 8px 16px;
            background-color: #3498db;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            font-size: 14px;
            margin-top: 15px;
        }
        
        /* 不同的背景定位方式 */
        .card1 .card-header {
            background-image: url('https://www.ebingou.cn/biancheng/images/4.jpg');
            background-position: center center;
        }
        
        .card2 .card-header {
            background-image: url('https://www.ebingou.cn/biancheng/images/5.jpg');
            background-position: right top;
        }
        
        .card3 .card-header {
            background-image: url('https://www.ebingou.cn/biancheng/images/6.jpg');
            background-position: left bottom;
        }
        
        .title {
            text-align: center;
            margin: 30px 0 10px;
            color: #2c3e50;
            font-family: Arial, sans-serif;
        }
        
        .description {
            text-align: center;
            margin-bottom: 30px;
            color: #7f8c8d;
            max-width: 800px;
            margin-left: auto;
            margin-right: auto;
        }
    </style>
</head>
<body>
    <h1 class="title">代码号CSS实战:卡片布局中的背景定位</h1>
    <p class="description">通过background-position属性,可以创建出各种精美的卡片设计,提升用户界面的视觉吸引力。</p>
    
    <div class="card-container">
        <div class="card card1">
            <div class="card-header"></div>
            <div class="card-content">
                <h3>中央定位</h3>
                <p>背景图像居中显示,适合人物或焦点在中央的图片。</p>
                <a href="https://www.ebingou.cn/jiaocheng/" class="card-button">查看教程</a>
            </div>
        </div>
        
        <div class="card card2">
            <div class="card-header"></div>
            <div class="card-content">
                <h3>右上角定位</h3>
                <p>背景图像定位在右上角,创建不对称的视觉设计。</p>
                <a href="https://www.ebingou.cn/biancheng/" class="card-button">学习编程</a>
            </div>
        </div>
        
        <div class="card card3">
            <div class="card-header"></div>
            <div class="card-content">
                <h3>左下角定位</h3>
                <p>背景图像定位在左下角,适合强调特定元素的图片。</p>
                <a href="https://www.ebingou.cn/yuanma/" class="card-button">获取源码</a>
            </div>
        </div>
    </div>
</body>
</html>

高级应用技巧

1. 多背景图像定位

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>代码号 - 多背景图像定位示例</title>
    <style>
        .multi-bg-container {
            height: 400px;
            margin: 40px auto;
            max-width: 800px;
            border: 2px solid #3498db;
            border-radius: 8px;
            background-image: 
                url('https://www.ebingou.cn/biancheng/images/s1.jpg'),
                url('https://www.ebingou.cn/biancheng/images/s2.jpg'),
                url('https://www.ebingou.cn/biancheng/images/s3.jpg');
            background-position: 
                left top,
                center center,
                right bottom;
            background-repeat: no-repeat;
            background-size: 150px, 200px, 180px;
            position: relative;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .multi-bg-content {
            background-color: rgba(255, 255, 255, 0.9);
            padding: 20px;
            border-radius: 8px;
            text-align: center;
            max-width: 80%;
        }
        
        .multi-bg-content h2 {
            color: #2c3e50;
            margin-top: 0;
        }
        
        .title {
            text-align: center;
            margin: 30px 0 10px;
            color: #2c3e50;
            font-family: Arial, sans-serif;
        }
        
        .description {
            text-align: center;
            margin-bottom: 30px;
            color: #7f8c8d;
            max-width: 800px;
            margin-left: auto;
            margin-right: auto;
        }
    </style>
</head>
<body>
    <h1 class="title">代码号CSS高级技巧:多背景图像定位</h1>
    <p class="description">CSS允许为元素添加多个背景图像,并为每个图像设置不同的位置,创建复杂的视觉效果。</p>
    
    <div class="multi-bg-container">
        <div class="multi-bg-content">
            <h2>多背景图像示例</h2>
            <p>此元素使用了三个背景图像,分别定位在左上角、中央和右下角位置。</p>
            <p>通过background-position属性,可以精确控制每个图像的位置。</p>
        </div>
    </div>
</body>
</html>

2. 响应式背景定位

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>代码号 - 响应式背景定位示例</title>
    <style>
        .responsive-container {
            height: 400px;
            margin: 40px auto;
            max-width: 1000px;
            border: 2px solid #e74c3c;
            border-radius: 8px;
            background-image: url('https://www.ebingou.cn/biancheng/images/2.jpg');
            background-repeat: no-repeat;
            background-size: cover;
            /* 默认定位 */
            background-position: center center;
            display: flex;
            align-items: center;
            justify-content: center;
            text-align: center;
            color: white;
            text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.7);
        }
        
        .responsive-content {
            max-width: 80%;
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .responsive-container {
                background-position: left center;
                height: 300px;
            }
        }
        
        @media (max-width: 480px) {
            .responsive-container {
                background-position: 20% center;
                height: 250px;
            }
        }
        
        .title {
            text-align: center;
            margin: 30px 0 10px;
            color: #2c3e50;
            font-family: Arial, sans-serif;
        }
        
        .description {
            text-align: center;
            margin-bottom: 30px;
            color: #7f8c8d;
            max-width: 800px;
            margin-left: auto;
            margin-right: auto;
        }
        
        .info-box {
            max-width: 800px;
            margin: 30px auto;
            padding: 20px;
            background-color: #f8f9fa;
            border-radius: 8px;
            border-left: 4px solid #3498db;
        }
        
        .info-box h3 {
            color: #2c3e50;
            margin-top: 0;
        }
    </style>
</head>
<body>
    <h1 class="title">代码号CSS实战:响应式背景定位</h1>
    <p class="description">在不同屏幕尺寸下,通过媒体查询调整background-position值,确保背景图像始终呈现效果。</p>
    
    <div class="responsive-container">
        <div class="responsive-content">
            <h2>响应式背景设计</h2>
            <p>调整浏览器窗口大小,观察背景图像位置的变化</p>
        </div>
    </div>
    
    <div class="info-box">
        <h3>响应式设计提示:</h3>
        <p>在移动设备上,通常需要调整背景图像的位置,确保关键内容始终可见。通过媒体查询修改background-position值,可以优化不同屏幕尺寸下的用户体验。</p>
    </div>
</body>
</html>

本节课程知识要点

  1. 理解定位原理:掌握background-position属性如何控制背景图像在元素内的位置

  2. 关键字值应用:熟练使用left、center、right、top、bottom等关键字组合

  3. 精确控制方法:学会使用百分比和像素值进行精确定位

  4. 多背景处理:掌握为多个背景图像设置不同位置的方法

  5. 响应式设计:了解如何在不同屏幕尺寸下调整背景位置

  6. 实际应用场景

    • 网页头部背景设计

    • 卡片和容器背景定位

    • 创建视觉层次和焦点

    • 响应式背景调整

  7. 注意事项

    • background-position需要与background-repeat: no-repeat配合使用

    • 百分比定位是相对于元素和图像尺寸计算的

    • 负值可以创建特殊的视觉效果

    • 考虑与background-size属性的配合使用

浏览器兼容性

CSS background-position属性在所有现代浏览器中都有很好的支持,包括:

  • Chrome 1+

  • Firefox 1+

  • Safari 1+

  • Edge 12+

  • Opera 3.5+

对于需要支持老旧浏览器的情况,可以考虑使用兼容性写法或提供降级方案。

通过掌握background-position属性,开发者可以更精确地控制背景图像的显示位置,创建出更具视觉吸引力和专业感的网页设计。

← CSS background-image opacity 背景图像透明度 CSS 伪元素::before与::after详解 →
分享笔记 (共有 篇笔记)
验证码:
微信公众号