首頁  >  文章  >  web前端  >  css實作圖片在div中居中的方法有哪些

css實作圖片在div中居中的方法有哪些

王林
王林轉載
2020-11-05 16:44:163034瀏覽

css實作圖片在div中居中的方法有哪些

本文為大家介紹了圖片在div中居中的四種方法,希望對大家有幫助。

方法一:

(推薦教學:css影片教學

html:

  <div class="title">
            <div class="flag"></div>
            <div class="content">
                <img src="img_p1_title.png">
            </div>
        </div>

css

.title {
   width: 100%;
    font-size: 0;
    height: 10%;
}
.title .content img {
    width: 35%;
}
/*--主要的--*/
.content{
    display: inline-block;
    vertical-align: middle;
  }
.flag{
    display: inline-block;
    vertical-align: middle;
    height: 100%;
    width: 0;
  }

方法二:

html

  <div class="title">
  <img src="img_p1_title.png">

css

.title {
  display: flex;
  justify-content: center;
  align-items: center;
}

.title img {
  width: 35%;
}//该方法有些旧系统不支持

方法三:

##html

 <div class="title">
<span>第三种方法</span>

css


.title {
   height: 15%;
   font-size: 18px;
   position: relative;
}

.title span {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

方法四:

html

<div class="title">
<span>第四种方法</span>

css


 .title {
    width: 200px;
    height: 200px;
    vertical-align: middle;
    display: table-cell;
    text-align: center;
}

相關推薦:

CSS教學

以上是css實作圖片在div中居中的方法有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除