简体   繁体   English

试图调整我的 <h1> 和 <p> 我图像左侧的元素

[英]Trying to align my <h1> and <p> elements at left of my image

Im trying to align my title, image and paragraph to the left of my news image. 我试图将我的标题,图像和段落对齐到我的新闻图像的左侧。

And Im using float left for doing this but its not working, 我使用浮动左边做这个,但它没有工作,

Im having this: 我有这个:

在此输入图像描述

But Im trying this: 但我试着这样做:

在此输入图像描述

Do you see where might be the problem? 你知道问题出在哪里吗?

My fiddle with the problem Im having: 我提出的问题我有:

http://jsfiddle.net/upH4U/1/ http://jsfiddle.net/upH4U/1/

My html: 我的HTML:

<div id="news-content">
    <h1>News</h1>
    <article id="loop-news">
        <div class="img_container">
            <img src="../image1.jpg" title=""/> 
        </div>
        <h2><a href="#" >Title of the news</a><br /></h2>
        <span id="date">Tuesday, May, 2014</span>
        <p>
            This is my paragraph and I want to align it to left of my image.
            <a href="#">see more</a>
        </p>  
    </article>

    <article id="loop-news">
        <div class="img_container">
            <img src="../image1.jpg" title=""/> 
        </div>
        <h2><a href="#" >Title of the news</a><br /></h2>
        <span id="date">Tuesday, May, 2014</span>
        <p>
            This is my paragraph and I want to align it to left of my image.
            <a href="#">see more</a>
        </p>  
    </article>
</div>

My css: 我的css:

#news-content
{
    float:left;
    width:480px;
    background:#f3f3f3;
}

#news-content h1
{
    font-size:20px;
    font-weight:100;
    margin-bottom:10px;
    color:gray;
    text-align:left;
}

#loop-news
{
    width:400px;
    margin-bottom:10px; 
    text-align:center;
}

.img_container
{
    width:160px;
    height: 165px;
    float:left;
    cursor: pointer;
    border:3px solid #ccc; 
}

#loop-news h2 a
{
    font-size:20px;  
    color:#3B5998; 
    text-decoration:none;
    margin:0 auto 0 5px;
    font-weight:100;
    float:left;
}

#loop-news a
{
    font-size:14px; 
    text-decoration:none;
    margin-left:2px;
}

#loop-news #date
{
    font-size:13px; 
    font-weight:normal;
    color:#7a7a7a;  
}

#loop-news p
{
    font-size: 13px;  
    text-align:justify; 
    line-height:25px;
    word-spacing:-2px;
    width:300px;
    float:left;
    margin-left:5px;
}

quick answer, use clearfix - there are a few options there 快速回答,使用clearfix - 有一些选项

CSS CSS

#loop-news
{
    width:400px;
    /*margin-bottom:10px; moving margin to seperator*/
    /*text-align:center;*/
}

#loop-news p
{
    font-size: 13px;  
    /*text-align:justify; 
    line-height:25px;
    word-spacing:-2px;
    width:300px;
    float:left;*/
    margin-left:5px;
}

#loop-news {
    overflow:hidden; /*quick clear fix*/
}

.loop-news-content {
    overflow:hidden;
}

#loop-news *:first-child { margin-top:0; }
#loop-news *:last-child { margin-bottom:0; }

#loop-news h2 { margin:0; }
.loop-news-meta { margin-top:0; }

Heres the updated fiddle http://jsfiddle.net/Varinder/HTNk8/2/ 继承人更新的小提琴http://jsfiddle.net/Varinder/HTNk8/2/

这是一个显示如何将文本与图像对齐的页面: http//www.w3schools.com/tags/att_img_align.asp

You need to clear your float by adding clear:left; 你需要通过添加clear:left;来清除你的浮动clear:left; to the same class that uses float: left . 到使用float: left的同一个类float: left In your case, you can use the CSS declaration: 在您的情况下,您可以使用CSS声明:

#news-content article { clear: left; }

Also, having multiple elements with the same ID can have unexpected results. 此外,具有相同 ID的多个元素可能会产生意外结果。 You should consider using classes instead . 您应该考虑使用类

your width is overflow; 你的宽度溢出; dont use it or fix it 不要使用它或修复它

    #loop-news p
{
    font-size: 13px;  
    text-align:justify; 
    line-height:25px;
    word-spacing:-2px;
    float:left;
    margin-left:5px;
}

将图像漂浮到右侧,就像这个小提琴一样

float:right;

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

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