代码之家  ›  专栏  ›  技术社区  ›  Matthew Cox

移除img后的换行符

  •  9
  • Matthew Cox  · 技术社区  · 15 年前

    我在一个分区旁边设置了一个img元素。我尝试了两种不同的方法来删除这两个分区之间发生的换行符,但是没有成功。如有任何意见,我们将不胜感激!

    CSS

    #newsMainBody
    {
    margin-right: auto;
    margin-left: auto;
    background:#61bc49;
    width: 750px;
    height: 900px;
    font-family:"sans-serif";
    text-align:left;
    -moz-border-radius: 10px;/*mozilla*/
    z-index: 1;
    white-space: nowrap;
    }
    
    #starOfMonth
    {
    background: #ffff99;
    width: 275px;
    height: 300px;
    text-align: center;
    font-family:"sans-serif";
    white-space: normal;
    }
    

    HTML

    <div id="newsMainBody">
       <img src="img/farm.jpg" alt="Farm photo" />
       <div id="starOfMonth">
          <br />
          <font style="font-weight:bold;font-size:medium; color:Purple;">DooLittle Farms Childcare</font><br />
          <font style="font-size:small;">"We're Growing Great Kids"</font><br />
          <img id="starImg" src="img/gold-star.jpg" alt="Star of the Week" width="200" height="200"/><br />
          Our Star Of The Week!
        </div>
     </div>
    
    3 回复  |  直到 15 年前
        1
  •  6
  •   Retired_User    15 年前

    添加:

    
    #newsMainBody img {
    float: left;
    }
    
    #startOfMonth {
    float: right;
    }
    
    

    并移除第一个 <br /> 之后 <div id="starOfMonth"> ,它是无用的(如果需要一些空间,请在CSS中使用padding top)

        2
  •  1
  •   M2tM    15 年前

    尝试使图像和DIV浮动:左;您 威尔 需要为每个人提供一个宽度以使其工作。

    #starOfMonth
    {
    background: #ffff99;
    float: left;
    width: 275px;
    height: 300px;
    text-align: center;
    font-family:"sans-serif";
    white-space: normal;
    }
    
    #starOfMonthImage {
    height:275px; /*Optional, but it's a good idea to supply height for images so that the browser doesn't shift things around during the loading process*/
    width:475px; /*OR SOMETHING!  Make it narrower if it isn't working, I haven't been super careful about reading your padding in depth so this may be wrong.*/
    float:left;
    }
    

    然后你可以写:

    <div id="newsMainBody">
       <img src="img/farm.jpg" alt="Farm photo" id="starOfMonthImage" />
       <div id="starOfMonth">
          <br />
          <font style="font-weight:bold;font-size:medium; color:Purple;">DooLittle Farms Childcare</font><br />
          <font style="font-size:small;">"We're Growing Great Kids"</font><br />
          <img id="starImg" src="img/gold-star.jpg" alt="Star of the Week" width="200" height="200"/><br />
          Our Star Of The Week!
        </div>
     </div>
    
        3
  •  0
  •   David Thomas    15 年前

    假设你指的是另一个 Farm photo :

    div#newsMainBody > img {
    /*        display: block; commented out as per @M2tM's comment, below */
        float: left;
        width: 200px;
    }
    #starOfMonth {
        margin-left: 200px; /* or 'width of floated image' + 'a margin of 10px' 
    }                       /* or whatever, just so they're not physically touching */
    

    应该能做到这一点,但我不确定你为什么会有漂浮物 <br /> 里面 #starOfMonth 那可能是个问题。

    现场演示, over at jsbin (显然) image S不加载(给定 src 没有指向任何东西,但它应该让您了解我的方法的作用。)