简体   繁体   English

显示设置为内联块的元素在MVC for循环中不起作用

[英]Element with display set to inline-block does not work in MVC for loop

I have a for loop in my MVC Razor view: 我的MVC Razor视图中有一个for循环:

@for (int i = 0; i < 10; i++)
{
   <div class="myInlineBlockElement">some value</div>        
}

And the class myInlineBlockElement has a style display: inline-block . 并且myInlineBlockElement类具有样式display: inline-block

The problem is that I cannot make the for output the div all in sequence. 问题是我无法按顺序将for输出全部设为div。 It always get pretty formatted in output. 它总是在输出中得到漂亮的格式。 Because of this, there is some spaces between the divs (as it would be expected from a inline-block). 因此,div之间有一些间隔(如内联块所期望的)。

Is there a way to output the for loop elements in a single line? 有没有一种方法可以在一行中输出for循环元素?

Placing the elements with no spaces between them isn't the only way to get around the gap which appears between inline-block elements. 放置元素之间没有空格并不是解决inline-block元素之间出现的空白的唯一方法。 Instead you can use CSS to give the parent of your div elements a font-size of 0, then re-add your desired font-size on the div elements themselves. 相反,您可以使用CSS为div元素的父级赋予font-size 0,然后在div元素本身上重新添加所需的font-size

 .container { font-size: 0; } .myInlineBlockElement { background: tomato; color: #fff; display: inline-block; font-size: 14px; } 
 <div class="container"> <div class="myInlineBlockElement">some value</div> <div class="myInlineBlockElement">some value</div> <div class="myInlineBlockElement">some value</div> <div class="myInlineBlockElement">some value</div> </div> 

As you can see, there are no gaps between the .myInlineBlockElement elements in the above snippet. 如您所见,以上代码段中的.myInlineBlockElement元素之间没有缝隙。

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

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