简体   繁体   English

在内联块范围内将p个元素显示为块

[英]Display p elements as block within an inline-block span

So I have some inline-block elements like so: 所以我有一些内联块元素,例如:

<span style="display: inline-block">
  <img>
  <p>Some text</p>
  <p>Some more text</p>
  <button>A button</button>
</span>

I want them all inline except I want the first p element positioned on top of the other one yet have both together inline with the rest of the span. 我希望它们全部内联,除了我希望第一个p元素位于另一个之上,而又将它们与范围的其余部分一起内联。 From what I've been reading, it's bad practice to put a div inside a span, so what's the best way to do this? 从我一直在阅读的内容来看,将div放在跨度中是不好的做法,那么什么是最好的方法呢?

It's not "bad practice", it's simply impossible. 这不是“坏习惯”,根本不可能。 The browser will "correct" your HTML and it will not behave as expected. 浏览器将“更正”您的HTML,并且不会按预期运行。

Try using <div style="display:inline-block"> as your container instead. 请尝试使用<div style="display:inline-block">作为容器。

Here's a wild guess at what you're after based on my comment above. 根据以上我的评论,这是对您要追求的目标的一个疯狂猜测。

 .table { display: table; width: 100%; } .row { display: table-row; } .cell { display: table-cell; padding: 5px; background: #ddd; border: 1px solid #aaa; vertical-align: top; } 
 <div class="table"> <div class="row"> <div class="cell"> <img src="http://lorempixel.com/300/200" /> </div> <div class="cell"> <div class="table"> <div class="row"> <div class="cell"> <p>Paragraph one. Paragraph one. Paragraph one.</p> </div> </div> <div class="row"> <div class="cell"> <p>Paragraph two. Paragraph two. Paragraph two.</p> </div> </div> </div> </div> <div class="cell"> <button>Button</button> </div> </div> </div> 

As @Neit pointed out, the browser will correct the DOM when you put block-level elements inside inline elements (see first example). 正如@Neit所指出的,将块级元素放入内联元素中时,浏览器将纠正DOM(请参见第一个示例)。 A div , or maybe a section is definitely a better choice both for valid and semantic markup. 对于有效标记和语义标记, divsection绝对是更好的选择。

Using CSS to change display: does work, but it isn't best practice (for example an em in place of your span above will render exactly the same). 使用CSS更改display: 确实可以,但是不是最佳实践(例如,用em代替上方的span将呈现完全相同的效果)。 Certain versions of browsers will also ignore some types of display: changes; 某些版本的浏览器还将忽略某些display:类型display:更改; thus, your code would fail. 因此,您的代码将失败。 So using a better container is going to provide the fewest headaches. 因此,使用更好的容器可以减少最少的麻烦。 在此处输入图片说明

See the code here: https://jsfiddle.net/9mf91b1v/ 在此处查看代码: https : //jsfiddle.net/9mf91b1v/

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

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