Home > Web Front-end > CSS Tutorial > What are the ways to center images in divs using css?

What are the ways to center images in divs using css?

王林
Release: 2020-11-05 16:44:16
forward
3166 people have browsed it

What are the ways to center images in divs using css?

This article introduces four methods for centering pictures in divs. I hope it will be helpful to you.

Method 1:

(Recommended tutorial: css video tutorial)

html:

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

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;
  }
Copy after login

Method two:

html

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

css

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

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

Method three:

html

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

css

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

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

Method 4:

html

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

css

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

Related recommendations: CSStutorial

The above is the detailed content of What are the ways to center images in divs using css?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template