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>

结果

相关推荐
大飞pkz32 分钟前
【Unity】使用XML进行数据读存的简单例子
xml·unity·c#·游戏引擎·游戏开发·数据读写
奕维哥2 小时前
数电发票整理:免费实用工具如何高效解析 XML 发票数据
xml
小宁爱Python3 小时前
深入理解CSS显示模式与盒子模型
前端·css
宁酱醇11 小时前
CSS基础_@拉钩教育【笔记】
前端·css
知识分享小能手11 小时前
JavaScript学习教程,从入门到精通,Ajax数据交换格式与跨域处理(26)
xml·开发语言·前端·javascript·学习·ajax·css3
黄同学real12 小时前
HTML5 新增的主要标签整理
前端·html·html5
程序猿阿伟14 小时前
《解锁CSS Flex布局:重塑现代网页布局的底层逻辑》
前端·css
生产队队长14 小时前
CSS:选择器-复合选择器
前端·css
牧天白衣.21 小时前
html中margin的用法
前端·html