2015-10-15 71 views
1

我基本上有以下HTML/CSS:如何将图像放置在按钮元素中的文本下方?

.actionButton .parent { 
 
    display: table; 
 
} 
 

 
/*try to put image at the bottom*/ 
 
.actionButton img.bottom { 
 
    display: table-footer-group; 
 
} 
 

 
/*try to put div tag containing text on top*/ 
 
.actionButton .top { 
 
    display: table-header-group; 
 
}
<div class="actionButton section"> 
 
    <button class="parent"> 
 
    <img class="bottom" src="//dummyimage.com/100"> 
 
    <div class="top">Click me!</div> 
 
    </button> 
 
</div>

如果按钮被替换div标签,然后在给定的CSS将正确地使文本显示上述格式的组图片。无法重现该按钮。任何人都可以解释为什么,或者如何解决?

+0

我不明白这是嘛? http://jsfiddle.net/h67j73rt/2/ – imGaurav

+0

大声笑。它看起来是正确的,但是元素的顺序在你的代码中得到了体现。我希望他们按照我定义的顺序排列,即img在div之前。 –

回答

2

显然,CSS表在<button>元素中不能正常工作。但是,flexbox + order属性效果不错。 <div>也更改为<span>,因为只有phrasing content被允许在<button>标记中。

.actionButton .parent { 
 
    display: flex; 
 
    flex-direction: column; 
 
} 
 
.actionButton .top { 
 
    order: -1; 
 
}
<div class="actionButton section"> 
 
    <button class="parent"> 
 
     <img class="bottom" src="//dummyimage.com/100"> 
 
     <span class="top"> Click me! </span> 
 
    </button> 
 
</div>

你也可以使用CSS伪元素的图像,而不是内联。

.actionButton .parent:after { 
 
    content: url('//dummyimage.com/100'); 
 
    display: block; 
 
}
<div class="actionButton section"> 
 
    <button class="parent"> 
 
     <span class="top"> Click me! </span> 
 
    </button> 
 
</div>

相关问题