2015-02-06 49 views
0

最近,而与数字标签做一些测试时,我发现有些奇怪。虽然我可以轻松地将一个图像标记浮动到段落内,并将段落的背景颜色保留在段落和图像本身之下。我无法用包含我的图像的浮动数字标签得到类似的结果。在img标签上浮动是不是当应用于数字标签

而是说话的,这里两个环节,以确保你明白我的问题:

简单的图像沿段漂浮的正常工作:here

相同,但有一个数字标签包围图像标记但不能按预期工作:here

如何在使用数字标记时获得相同的结果?

这是代码,但检查codepen,问题将是显而易见的。

HTML结构(

<div class="wrapper"> 

    <p class="clearfix"> 

    <img class="fox" src="http://raptorimages.hu/wp-content/gallery/blog/fox_img_9734_w500_kovacsa.jpg" alt="fox" > 
<!-- the figure tag is supposed to work such as the image tag above when uncommented --> 
    <!--<figure class="fox"> 
    <img src="http://raptorimages.hu/wp-content/gallery /blog/fox_img_9734_w500_kovacsa.jpg" alt="fox" > 
    <figcaption>Running fox</figcaption> 
    </figure>--> 

    </p> 

    <p>Praesent non risus enim. Duis ac mi tempus, feugiat nunc non, vestibulum magna. Curabitur in tempor lorem. Mauris fermentum vulputate </p> 

</div> 

CSS

*{ 
    box-sizing:border-box; 
} 
.wrapper{ 
    width:90%; 
    background:#cecece; 
    margin: 0 auto; 
    padding: 1.1111111%; 
} 

.wrapper p{ 

    background-color:grey; 
    padding:1.111111%; 

} 

.fox{ 
    float:right; 
    padding:0 0 0 1.1111%; 
} 

img,video,object,embed{ 
    max-width:100%; 
} 

.clearfix:after { 
    content: " "; /* Older browser do not support empty content */ 
    visibility: hidden; 
    display: block; 
    height: 0; 
    clear: both; 
} 

回答

2

根据W3组织,这是用于网络的标准。P只能有Phrasing elements

img是措辞元件,以便<p class="clearfix"><img class='fox' /></p>是有效的html,您的浏览器按预期处理它。

figure不是一个措辞元素,因此<p class="clearfix"><figure class='fox'></figure></p>是无效的html,您的浏览器通过将其更改为<p class="clearfix"></p><figure class="fox"></figure><p class="clearfix"></p>来相应地进行更正。所以你得到不同的输出。

P.S:这是因为您的浏览器如何呈现html