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>

结果

相关推荐
程序猿阿伟3 分钟前
《解码SCSS:悬浮与点击效果的高阶塑造法则》
前端·html·scss
睡不着先生5 分钟前
打造丝滑滚动体验:Scroll-driven Animations 正式上线!
css·chrome·html
hj10432 小时前
html打印合同模板
前端·html
有事没事实验室2 小时前
web方向第一次考核内容
前端·css·开源·html5
海的诗篇_2 小时前
前端开发面试题总结-vue2框架篇(二)
前端·javascript·css·vue.js·前端框架·vue
我不吃饼干2 小时前
倒反天罡,CSS 中竟然可以写 JavaScript
前端·javascript·css
蓝婷儿11 小时前
Python 爬虫入门 Day 2 - HTML解析入门(使用 BeautifulSoup)
爬虫·python·html
不太厉害的程序员11 小时前
NC65配置xml找不到Bean
xml·java·后端·eclipse
葡萄糖o_o11 小时前
ResizeObserver的错误
前端·javascript·html
MK-mm12 小时前
CSS盒子 flex弹性布局
前端·css·html