简体   繁体   English

我如何让一个div显示在另一个带有文字的下方?

[英]How do I get a div to show underneath another one with text flowing around it?

The problem I'm having is that I have an image, and an image credit, and an image caption. 我遇到的问题是我有图像,图像信用和图像标题。 When I have the image set to no alignment, it displays fine. 当我将图像设置为无对齐时,它显示正常。 However for a vertical image, when I have the alignment set to right or left, the caption displays to the left or right, respectively, of the image, instead of below it. 但是对于垂直图像,当我将对齐设置为向右或向左时,标题分别显示在图像的左侧或右侧,而不是在图像的下方。

Here's an example of the problem: http://ndsmcobserver.com/2014/01/van-de-casteele-drafted/ 以下是该问题的一个示例: http//ndsmcobserver.com/2014/01/van-de-casteele-drafted/

The relevant piece of code is: 相关的代码是:

<div id="attachment_677" style="width: 442px" class="wp-caption alignnone">
    <div class="media-credit-container alignright">
        <a href="http://cdn.ndsmcobserver.com/wp-content/uploads/2014/01/20131124-Van-de-Casteele-vs-Wisconsin-Barbara-Kazanowska.jpg">
        <img class="size-medium wp-image-677" alt="Former Irish center back Grant Van De Casteele passes in Notre Dame’s 4-0 win Nov. 24, 2013, against Wisconsin. Casteele was drafted by the Colorado Rapids in the first round of the MLS draft Thursday." src="http://cdn.ndsmcobserver.com/wp-content/uploads/2014/01/20131124-Van-de-Casteele-vs-Wisconsin-Barbara-Kazanowska-432x525.jpg" width="432" height="525" scale="0">
        </a><span class="media-credit">Barbara Kazanowska</span>
    </div>
    <p class="wp-caption-text">Former Irish center back Grant Van De Casteele passes in Notre Dame’s 4-0 win Nov. 24, 2013, against Wisconsin. Casteele was drafted by the Colorado Rapids in the first round of the MLS draft Thursday.
    </p>
</div>

Thanks everyone! 感谢大家!

I think the problem is the width of your wrapper-container, in this case "attachment_677". 我认为问题是你的包装容器的宽度,在这种情况下是“attachment_677”。 There is a width: auto !important, so the container uses all the space available. 有一个宽度:auto!important,因此容器使用所有可用空间。 If you remove this property it gets the width from the style-attribute and than the text is below the image. 如果删除此属性,它将从style-attribute获取宽度,而文本则在图像下方。

It sounds like you'll always want your image, credit and caption to be displayed one below the other, so it stands to reason that they should always be contained in the same div . 听起来你总是希望你的图像,信用和标题一个接一个地显示出来,所以按理说它们应该总是包含在同一个div I'd therefore make the following two changes: 因此,我将进行以下两项更改:

  • Move your caption into your media-credit-container div 将标题移到media-credit-container div中
  • Take out your top-level div for the reasons mentioned by MichaB . 出于MichaB提到的原因,拿出你的顶级div。

This should result in the article text wrapping as expected, regardless of whether you want to float your image left, right, or not at all. 无论您是想要向左,向右还是根本不浮动图像,这都应该导致文章文本换行。

<div style="width: 442px" class="media-credit-container alignright">
  <a href="http://cdn.ndsmcobserver.com/wp-content/uploads/2014/01/20131124-Van-de-Casteele-vs-Wisconsin-Barbara-Kazanowska.jpg">
    <img class="size-medium wp-image-677" alt="Former Irish center back Grant Van De Casteele passes in Notre Dame’s 4-0 win Nov. 24, 2013, against Wisconsin. Casteele was drafted by the Colorado Rapids in the first round of the MLS draft Thursday." src="http://cdn.ndsmcobserver.com/wp-content/uploads/2014/01/20131124-Van-de-Casteele-vs-Wisconsin-Barbara-Kazanowska-432x525.jpg" width="432" height="525" scale="0"></img>
  </a>
  <span class="media-credit">Barbara Kazanowska</span>
  <p class="wp-caption-text">Former Irish center back Grant Van De Casteele passes in Notre Dame’s 4-0 win Nov. 24, 2013, against Wisconsin. Casteele was drafted by the Colorado Rapids in the first round of the MLS draft Thursday.
  </p>
</div>

Ended up fixing the problem by adding float:left or float:right respectively to .wp-caption.alignright (or .alignleft). 通过将float:left或float:right分别添加到.wp-caption.alignright(或.alignleft)来解决问题。 I also removed auto !important as suggested by MichaB 我还删除了MichaB建议的auto!important

Removing auto !important alone didn't fix the problem because text wouldn't wrap, and changing the HTML itself wasn't an option because it was generated by WordPress and the Media Credit plugin. 单独删除auto!important并不能解决问题,因为文本不会换行,并且更改HTML本身不是一个选项,因为它是由WordPress和Media Credit插件生成的。

Thanks everyone! 感谢大家!

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

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