简体   繁体   English

我如何将图像居中放置在div中而不是文本中

[英]How do I just center the image in my div and not the text

I'm trying to center an image in my div on two classes with the stylesheet. 我正在尝试使用样式表在两个类中将div中的图像居中。 What am I missing in my code? 我的代码中缺少什么?

The apple is centered with the text not being center, but the pear is center AND the text is centered too. 苹果居中,文字未居中,但梨居中,文字也居中。 I want the pear to be centered, but the text undernearth to NOT be centered. 我希望梨居中,但下面的文字不居中。

Thought this would do it, but it doesn't work: 以为这样可以做到,但不起作用:

    .data_cell1_text, .data_cell3_text img {
    display:block; 
    margin:0 auto;
    }

http://jsfiddle.net/wAqrr/23/ http://jsfiddle.net/wAqrr/23/

<div class="wrapper-data">
<div class="data_row">
    <div class="data_cell1_text">


(source: iconarchive.com ) (来源: iconarchive.com

Obama to meet with leaders from Apple, Twitter, and Yahoo today. 奥巴马今天将与苹果,推特和雅虎的领导人会晤。

  • Bullet Point 1 项目符号点1
  • Bullet Point 2 子弹点2
  • Bullet Point 3 子弹点3

Obama to meet with leaders from Apple, Twitter, and Yahoo today. 奥巴马今天将与苹果,推特和雅虎的领导人会晤。

  • Bullet Point 1 项目符号点1
  • Bullet Point 2 子弹点2
  • Bullet Point 3 子弹点3

.wrapper-data {
position:relative;
width:100%;
border: none;
margin: 40px 0 0 0;
overflow: hidden;

} }

.data_row {
    height:100%;
    min-height:100%;
    white-space:nowrap;
    display:table;
    width: 100%;
}

.data_cell1_text {
    width:45%;
    white-space:normal;
    display:table-cell;
}

.data_cell2_text {
    width:10%;
    display:table-cell;
    white-space:normal;
}

.data_cell3_text {
    width:45%;
    display:table-cell;
    white-space:normal;
}

.data_cell1_text, .data_cell3_text img {
    display:block; 
    margin:0 auto;
    }

Wrap your image in a div: 将图片包装在div中:

<div class="theimg">
<img src="https://pbs.twimg.com/profile_images/3588564988/ca2ec46372bf01eff59044dd0046132c.jpeg" width="50" height="50">
</div>

Add this to your CSS: 将此添加到您的CSS:

.theimg {
   text-align:center;
}

Here is a JsFiddle to better represent the idea I have given you. 这是一个JsFiddle,可以更好地代表我给您的想法。

DEMO 演示

Correct your css to this 将您的css更正为此

.data_cell1_text img, .data_cell3_text img {
        display:block; 
        margin:0 auto;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM