2017-05-05 73 views
0

我正在动态创建C#外的HTML代码。如何用较小的IMG替换IMG,但不会搞乱样式

我想要的是:用一个更小的IMG文件(这是一个加载gif)替换每个IMG标签。但是:整体风格/布局应保持不变(因为我加载HTML文件是完全动态的,可能包含任何

所以一个样本:

<img alt="" src="http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/mahesh/rectangle-in-wpf/Images/RectI.gif" height="100" width="100"> 
 

 
<span>Geographie</span> 
 

 
<img alt="" src="http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/mahesh/rectangle-in-wpf/Images/RectI.gif" height="100" width="100"> 
 

 
<span>Geschichte</span> 
 

 
<img alt="" src="http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/mahesh/rectangle-in-wpf/Images/RectI.gif" height="100" width="100"> 
 

 
<span>Gesellschaft</span> 
 

 
<img alt="" src="http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/mahesh/rectangle-in-wpf/Images/RectI.gif" height="100" width="100">

几个现在我想用一个较小的图像(这里:一个笑脸)替换其中的一个(这里是:数字3),但是:我想要有相同的布局,所以我试着这样做:创建一个容器,它有相同尺寸的原始图像,然后我在其中创建了小图像。

我的问题是:当我使用DIV,它换行...

<img alt="" src="http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/mahesh/rectangle-in-wpf/Images/RectI.gif" height="100" width="100"> 
 

 
<span>Geographie</span> 
 

 
<img alt="" src="http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/mahesh/rectangle-in-wpf/Images/RectI.gif" height="100" width="100"> 
 

 
<span>Geschichte</span> 
 

 
<div style="height: 100px; width: 100px; background-color:red;display: flex; "> 
 
    <img alt="" src="https://affinity.serif.com/forum/public/style_emoticons/default/smile.png" height="20" width="20" style="margin: auto;"> 
 
</div> 
 

 
<span>Gesellschaft</span> 
 

 
<img alt="" src="http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/mahesh/rectangle-in-wpf/Images/RectI.gif" height="100" width="100">

我该如何解决这个问题?

我真的要寻找的东西,具有相同的尺寸/性能,并且可以包含其他图像只更换-tag ...

+0

难道你们就不能只是使加载GIF图像尺寸相同的其他图片,但实际装载器要小。 – Sank6

+1

'display:inline-flex;垂直对齐:底部的div应该这样做。 – CBroe

+0

我可以用相同的尺寸动态创建新的GIF。但是这太慢了。 – James

回答

1

的原因,您的容器造成线路中断是因为默认的div有一种“显示:块”的风格

您可以通过将显示样式更改为内联块来解决此问题。然后,您需要确保它与“vertical-align:top;”正确对齐。

见固定片段:

<img alt="" src="http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/mahesh/rectangle-in-wpf/Images/RectI.gif" height="100" width="100"> 
 

 
<span>Geographie</span> 
 

 
<img alt="" src="http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/mahesh/rectangle-in-wpf/Images/RectI.gif" height="100" width="100"> 
 

 
<span>Geschichte</span> 
 

 
<div style="height: 100px; width: 100px; background-color:red; display: inline-block; vertical-align:top; "> 
 
    <img alt="" src="https://affinity.serif.com/forum/public/style_emoticons/default/smile.png" height="20" width="20" style="margin: auto;"> 
 
</div> 
 

 
<span>Gesellschaft</span> 
 

 
<img alt="" src="http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/mahesh/rectangle-in-wpf/Images/RectI.gif" height="100" width="100">

+1

_“容器导致换行符的原因是因为div默认具有显示样式:block;”_ - 以及在给定示例中的“display:flex”。但是,这当然会导致相同的“突破性”行为。但我想'flex'是用来让小图像自动居中。因此,简单地使用'inline-flex'可能比尝试使用不同方式将图像置于中心更简单一些。 – CBroe

+1

@CBroe我同意你的解决方案更好。你评论我写我的。我甚至没有意识到内联flex是一个有效的选择。谢谢你教我一件事! – slander