CSS--图片链接水平居中展示的方法

原文网址:CSS--图片链接居中展示的方法-CSDN博客

简介

本文介绍CSS图片链接水平居中展示的方法。

图片链接

问题复现

源码

html 复制代码
<html xml:lang="cn" lang="cn">

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <meta name="keywords" content="anchor,fixed navigation hide anchor">
    <title></title>
</head>

<body>

<div class="container2"
     style="width: 400px ; height: 400px ; border: 3px solid black;">
    <a target="_blank" href="https://baidu.com">
        <img src=../img/bat.png alt="乒乓球拍">
    </a>
</div>

</body>

</html>

结果

方案1:img指定margin

给img元素添加margin,左右两侧自动,并指定display为block。

代码

html 复制代码
<html xml:lang="cn" lang="cn">

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <meta name="keywords" content="anchor,fixed navigation hide anchor">
    <title></title>
</head>

<body>

<div class="container2"
     style="width: 400px ; height: 400px ; border: 3px solid black;">
    <a target="_blank" href="https://baidu.com">
        <img src=../img/bat.png style="margin: 0 auto; display: block" alt="乒乓球拍">
    </a>
</div>

</body>

</html>

结果

方案2:父元素指定text-align

将父元素设置为:text-align: center

代码

html 复制代码
<html xml:lang="cn" lang="cn">

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <meta name="keywords" content="anchor,fixed navigation hide anchor">
    <title></title>
</head>

<body>

<div class="container2"
     style="width: 400px ; height: 400px ; text-align: center; border: 3px solid black;">
    <a target="_blank" href="https://baidu.com">
        <img src=../img/bat.png alt="乒乓球拍">
    </a>
</div>

</body>

</html>

结果

方案3:img绝对定位

代码

html 复制代码
<html xml:lang="cn" lang="cn">

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <meta name="keywords" content="anchor,fixed navigation hide anchor">
    <title></title>
</head>

<body>

<div class="container2" style="width: 400px; height: 400px; border: 3px solid black; position: relative;">
    <a target="_blank" href="https://baidu.com">
        <img src="../img/bat.png" alt="乒乓球拍" style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);">
    </a>
</div>

</body>

</html>

结果

元素背景图

问题复现

代码

html 复制代码
<html xml:lang="cn" lang="cn">

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <meta name="keywords" content="anchor,fixed navigation hide anchor">
    <title></title>
</head>

<body>

<div class="container1"
     style="width: 400px ; height: 400px ; border: 3px solid black;
background-image: url(../img/bat.png); background-repeat:no-repeat;">

</div>

</body>

</html>

结果

解决方案

添加CSS:background-position:center center;

代码

html 复制代码
<html xml:lang="cn" lang="cn">

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <meta name="keywords" content="anchor,fixed navigation hide anchor">
    <title></title>
    <style>
        body {
            /*display: flex;*/
        }
    </style>
</head>

<body>

<div class="container1"
     style="width: 400px ; height: 400px ; text-align: center; border: 3px solid black;
background-image: url(../img/bat.png); background-repeat:no-repeat;background-position:center center;">

</div>

</body>

</html>

结果

相关推荐
augenstern4167 小时前
HTML面试题
前端·html
sunbyte12 小时前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | GithubProfies(GitHub 个人资料)
前端·javascript·css·vue.js·github·tailwindcss
前端领航者13 小时前
国际化LTR&RTL布局实战
前端·css
天天扭码13 小时前
很全面的前端面试题——CSS篇(下)
前端·css·面试
a别念m13 小时前
HTML5 离线存储
前端·html·html5
代码小学僧14 小时前
「双端 + 响应式」企业官网开发经验分享
前端·css·响应式设计
杨超越luckly14 小时前
HTML应用指南:利用GET请求获取全国永辉超市门店位置信息
大数据·信息可视化·数据分析·html·argis·门店
用户97044387811614 小时前
taobao商品详情数据获取实战方法
算法·html
ttod_qzstudio14 小时前
彻底移除 HTML 元素:element.remove() 的本质与最佳实践
前端·javascript·typescript·html
爱编程的喵15 小时前
前端路由深度解析:从传统页面到SPA的完美蜕变
前端·react.js·html