如何将图片置于div中心?

时间:2015-09-15 10:18:46

标签: html css

如何在div中水平居中图片?现在图片在左边,虽然它应该居中。我尝试了很多,包括定位,但没有任何作用。

请参阅下面的代码。

谢谢。

.p_pic-1 {
    background-image: url(http://www.templatemo.com/templates/templatemo_406_flex/images/member1.jpg);
	float: left;
    width: 122px;
    height: 122px;
	-webkit-border-radius: 50%;
	-moz-border-radius: 50%;
	border-radius: 50%;
}
.p_pic-2 {
    background-image: url(http://www.templatemo.com/templates/templatemo_406_flex/images/member1.jpg);
	float: left;
    width: 122px;
    height: 122px;
	-webkit-border-radius: 50%;
	-moz-border-radius: 50%;
	border-radius: 50%;
}
.p_pic-3 {
    background-image: url(http://www.templatemo.com/templates/templatemo_406_flex/images/member1.jpg);
	float: left;
    width: 122px;
    height: 122px;
	-webkit-border-radius: 50%;
	-moz-border-radius: 50%;
	border-radius: 50%;
}
.p_pic-4 {
    background-image: url(http://www.templatemo.com/templates/templatemo_406_flex/images/member1.jpg);
	float: left;
    width: 122px;
    height: 122px;
	-webkit-border-radius: 50%;
	-moz-border-radius: 50%;
	border-radius: 50%;
}

.p-1 {
	float:left;
	width:220px;
	height: 100%;
  
	background: #a1a1a1;
}
.p-2 {
	float: left;
	width:220px;
	height: 100%;
	margin-left: 20px;
  
	background: #a1a1a1;
}
.p-3{
	float:left;
	width:220px;
	height: 100%;
	margin-left: 20px;
 
	background: #a1a1a1;
}
.p-4 {
	float: left;
	width:220px;
	height: 100%;
	margin-left: 20px;
   
	background: #a1a1a1;
}

.p-pics {

	width: 940px;
	text-align:center;
	margin: 70px auto 0 auto;
    overflow: auto;
}

.p-name {
	display: block;
	float:left;
	width:100%;
	font-weight: bold;
	color: #333;
	font-size: 16px;
}

.p-description {
	font-weight: normal;
	color: #666;
	font-size: 16px;
	text-align: center;
	float:left;
	margin-top: 0;
}

.p_wrap {

  	width: 100%;  	  
	text-align:center;
  
}
<div class="p-pics">
		<div class="p-container">
		<h2 class="pa_h2">Head Text</h2>
		<div class="p-1">
			<a class="p_pic-1" href="index.html"></a>
			<div class="p_wrap">
			<h4 class="p-name">Some Text</h4>
			<p class="p-description">This is just a description, just a text, some text.</p>
			</div>
        </div>
		<div class="p-2">
			<a class="p_pic-2" href="index.html"></a>
			<div class="p_wrap">
			<h4 class="p-name">Some Text</h4>
			<p class="p-description">This is just a description, just a text, some text, text text text.</p>
			</div>
        </div>
		<div class="p-3">
			<a class="p_pic-3" href="index.html"></a>
			<div class="p_wrap">
			<h4 class="p-name">Some Text</h4>
			<p class="p-description">This is just a description, just a text, some text, text text text. Some very-very long text. Just a text, yeah.
			</div>
	   </div>
		<div class="p-4">
			<a class="p_pic-4" href="index.html"></a>
			<div class="p_wrap">
			<h4 class="p-name">Some Text</h4>
			<p class="p-description">This is just a description, just a text, some text, text text text.</p>
			</div>
		</div>
		</div>
	</div>

4 个答案:

答案 0 :(得分:3)

你可以使用:

background-position: center top;

第一个“中心”是水平对齐,“顶部”是垂直对齐。您也可以使用px。它会将图像的数量从左向右移动(水平),从顶部移动到按钮(垂直)

答案 1 :(得分:0)

.p_pic-1 {
    background-image: url(http://www.templatemo.com/templates/templatemo_406_flex/images/member1.jpg);
	
    width: 122px;
    height: 122px;
	-webkit-border-radius: 50%;
	-moz-border-radius: 50%;
	border-radius: 50%;
    display:block;
    margin: 0 auto;
}
.p_pic-2 {
    background-image: url(http://www.templatemo.com/templates/templatemo_406_flex/images/member1.jpg);
	
    width: 122px;
    height: 122px;
	-webkit-border-radius: 50%;
	-moz-border-radius: 50%;
	border-radius: 50%;
      display:block;
    margin: 0 auto;
}
.p_pic-3 {
    background-image: url(http://www.templatemo.com/templates/templatemo_406_flex/images/member1.jpg);
	
    width: 122px;
    height: 122px;
	-webkit-border-radius: 50%;
	-moz-border-radius: 50%;
	border-radius: 50%;
      display:block;
    margin: 0 auto;
}
.p_pic-4 {
    background-image: url(http://www.templatemo.com/templates/templatemo_406_flex/images/member1.jpg);
	
    width: 122px;
    height: 122px;
	-webkit-border-radius: 50%;
	-moz-border-radius: 50%;
	border-radius: 50%;
      display:block;
    margin: 0 auto;
}

.p-1 {
	float:left;
	width:220px;
	height: 100%;
  
	background: #a1a1a1;
}
.p-2 {
	float: left;
	width:220px;
	height: 100%;
	margin-left: 20px;
  
	background: #a1a1a1;
}
.p-3{
	float:left;
	width:220px;
	height: 100%;
	margin-left: 20px;
 
	background: #a1a1a1;
}
.p-4 {
	float: left;
	width:220px;
	height: 100%;
	margin-left: 20px;
   
	background: #a1a1a1;
}

.p-pics {

	width: 940px;
	text-align:center;
	margin: 70px auto 0 auto;
    overflow: auto;
}

.p-name {
	display: block;
	float:left;
	width:100%;
	font-weight: bold;
	color: #333;
	font-size: 16px;
}

.p-description {
	font-weight: normal;
	color: #666;
	font-size: 16px;
	text-align: center;
	float:left;
	margin-top: 0;
}

.p_wrap {

  	width: 100%;  	  
	text-align:center;
  
}
<div class="p-pics">
		<div class="p-container">
		<h2 class="pa_h2">Head Text</h2>
		<div class="p-1">
			<a class="p_pic-1" href="index.html"></a>
			<div class="p_wrap">
			<h4 class="p-name">Some Text</h4>
			<p class="p-description">This is just a description, just a text, some text.</p>
			</div>
        </div>
		<div class="p-2">
			<a class="p_pic-2" href="index.html"></a>
			<div class="p_wrap">
			<h4 class="p-name">Some Text</h4>
			<p class="p-description">This is just a description, just a text, some text, text text text.</p>
			</div>
        </div>
		<div class="p-3">
			<a class="p_pic-3" href="index.html"></a>
			<div class="p_wrap">
			<h4 class="p-name">Some Text</h4>
			<p class="p-description">This is just a description, just a text, some text, text text text. Some very-very long text. Just a text, yeah.
			</div>
	   </div>
		<div class="p-4">
			<a class="p_pic-4" href="index.html"></a>
			<div class="p_wrap">
			<h4 class="p-name">Some Text</h4>
			<p class="p-description">This is just a description, just a text, some text, text text text.</p>
			</div>
		</div>
		</div>
	</div>

试试这个,它运行正常.... https://jsfiddle.net/maheswaran/eo25p0od/

答案 2 :(得分:0)

你可以给予

background: url(templatemo.com/templates/templatemo_406_flex/images/member1.jpg) center center;

margin: 0 auto;

在背景图片退出的每个班级中。但请记住删除

float:left;
你之前给过的。

答案 3 :(得分:0)

为该图像添加包装类并设置宽度高度和边距:0 auto;对于那个包装器,它将获得居中对齐

.p_pic-1 {
    background-image: url(http://www.templatemo.com/templates/templatemo_406_flex/images/member1.jpg);
	float: left;
    width: 122px;
    height: 122px;
	-webkit-border-radius: 50%;
	-moz-border-radius: 50%;
	border-radius: 50%;
}
.p_pic-2 {
    background-image: url(http://www.templatemo.com/templates/templatemo_406_flex/images/member1.jpg);
	float: left;
    width: 122px;
    height: 122px;
	-webkit-border-radius: 50%;
	-moz-border-radius: 50%;
	border-radius: 50%;
}
.p_pic-3 {
    background-image: url(http://www.templatemo.com/templates/templatemo_406_flex/images/member1.jpg);
	float: left;
    width: 122px;
    height: 122px;
	-webkit-border-radius: 50%;
	-moz-border-radius: 50%;
	border-radius: 50%;
}
.p_pic-4 {
    background-image: url(http://www.templatemo.com/templates/templatemo_406_flex/images/member1.jpg);
	float: left;
    width: 122px;
    height: 122px;
	-webkit-border-radius: 50%;
	-moz-border-radius: 50%;
	border-radius: 50%;
}

.p-1 {
	float:left;
	width:220px;
	height: 100%;
  
	background: #a1a1a1;
}
.p-2 {
	float: left;
	width:220px;
	height: 100%;
	margin-left: 20px;
  
	background: #a1a1a1;
}
.p-3{
	float:left;
	width:220px;
	height: 100%;
	margin-left: 20px;
 
	background: #a1a1a1;
}
.p-4 {
	float: left;
	width:220px;
	height: 100%;
	margin-left: 20px;
   
	background: #a1a1a1;
}

.p-pics {

	width: 940px;
	text-align:center;
	margin: 70px auto 0 auto;
    overflow: auto;
}

.p-name {
	display: block;
	float:left;
	width:100%;
	font-weight: bold;
	color: #333;
	font-size: 16px;
}

.p-description {
	font-weight: normal;
	color: #666;
	font-size: 16px;
	text-align: center;
	float:left;
	margin-top: 0;
}

.p_wrap {

  	width: 100%;  	  
	text-align:center;
  
}
.pic-wrap {
   width: 122px;
   height: 122px;
   margin: 0 auto;
}
<div class="p-pics">
		<div class="p-container">
		<h2 class="pa_h2">Head Text</h2>
		<div class="p-1">
          <div class="pic-wrap">
			<a class="p_pic-1" href="index.html"></a>
          </div>
			<div class="p_wrap">
			<h4 class="p-name">Some Text</h4>
			<p class="p-description">This is just a description, just a text, some text.</p>
			</div>
        </div>
		<div class="p-2">
          <div class="pic-wrap">
			<a class="p_pic-2" href="index.html"></a>
          </div>
			<div class="p_wrap">
			<h4 class="p-name">Some Text</h4>
			<p class="p-description">This is just a description, just a text, some text, text text text.</p>
			</div>
        </div>
		<div class="p-3">
          <div class="pic-wrap">
			<a class="p_pic-3" href="index.html"></a>
          </div>
			<div class="p_wrap">
			<h4 class="p-name">Some Text</h4>
			<p class="p-description">This is just a description, just a text, some text, text text text. Some very-very long text. Just a text, yeah.
			</div>
	   </div>
		<div class="p-4">
          <div class="pic-wrap">
			<a class="p_pic-4" href="index.html"></a>
          </div>
			<div class="p_wrap">
			<h4 class="p-name">Some Text</h4>
			<p class="p-description">This is just a description, just a text, some text, text text text.</p>
			</div>
		</div>
		</div>
	</div>