简体   繁体   English

将图像左对齐,右对齐div

[英]Aligning an image to the left with div's to the right

I'm having issues making the image float to the right whilst the rest of text and the div float to the right. 我在使图像向右浮动而其余文本和div向右浮动时遇到问题。

I can get it to work with a fixed width image but then if I swap the image for a slightly different size it won't work as I'm using fixed sizes. 我可以使它与固定宽度的图像一起使用,但是如果我将图像交换为稍有不同的尺寸,它将无法正常工作,因为我使用的是固定尺寸。 Is there a way for it to work when I don't know the size of the image? 当我不知道图像大小时,有什么方法可以工作吗? Or would I need to use JS for this? 还是我需要为此使用JS?

http://jsfiddle.net/agcH5/ http://jsfiddle.net/agcH5/

<div style="width:900px">
<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">Course Name</h3>
    </div>
    <div class="panel-body">
        <img src="http://i.imgur.com/TyQ45nk.jpg">
        <div>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sed porttitor diam. Quisque fermentum orci sed velit lacinia, nec vehicula eros consequat. Morbi vestibulum convallis auctor.</p>
        <div class="container-fluid row">
            <div class="col-md-9" style="padding: 0">
                <div class="progress">
                    <div class="progress-bar" role="progressbar" aria-valuenow="80" aria-valuemin="0" aria-valuemax="100" style="width: 60%;">
                        80%
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <a href="#" class="btn btn-sm btn-danger">Continue</a>
            </div>
        </div></div>
    </div>
</div>
</div>

Simply make use of the boostrap's grid classes properly. 只需正确使用boostrap的网格类即可。 I edited the code in your div with class panel-body 我使用类panel-body在您的div中编辑了代码

   <div class="panel-body">
      <div class="col-md-6">
        <img src='http://i.imgur.com/TyQ45nk.jpg'>
      </div>
      <div class="col-md-6">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sed porttitor diam. Quisque fermentum orci sed velit lacinia, nec vehicula eros consequat. Morbi vestibulum convallis auctor.</p>
        <div class="progress">
                    <div class="progress-bar" role="progressbar" aria-valuenow="80" aria-valuemin="0" aria-valuemax="100" style="width: 60%;">
                        80%
                    </div>
                </div>
        <a href="#" class="btn btn-sm btn-danger">Continue</a>
      </div>
    </div>

Demo : http://jsbin.com/gamaj/1/ 演示: http : //jsbin.com/gamaj/1/

You can try using either CSS or an inline style attribute: 您可以尝试使用CSS或内联样式属性:

<img src="http://i.imgur.com/TyQ45nk.jpg" style="float:right">

if you don't want words on the same line as the image, you can add the 'clear:right' attribute as well. 如果您不希望单词与图片位于同一行,则也可以添加'clear:right'属性。

You can add float: left to image 您可以添加float: leftimage

.panel-body img{
    float: left;
    margin: 0 0 10px 0;   
}

.rightContent{
    margin-left: 380px;
}

Check this Demo 检查这个演示

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

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