简体   繁体   English

图像周围的白色空间

[英]White space around image

I have some text around an image that has been floated left. 我有一些文字围绕着一个漂浮在左边的图像。 But the text goes right up against the border of the image. 但是文本正对着图像的边界。 How do I make some white space around it? 如何在它周围留出一些空白区域?

At the moment I've got in the CSS: 目前我已经进入了CSS:

.image {
  float:left
}

and the view: 和观点:

<% if article.newspic.exists? %> 
        <div class ="image">
        <%= newspic_thumbnail_tag(article) %>
        </div>
    <% end %>

  <%= simple_format(article.body) %><br>

If I add a margin-right to the image, then the text will simply start from under the image. 如果我向图像添加margin-right ,则文本将从图像下方开始。

Adding a margin-right should work in this case but I've had issues with margins and floats before, particularly when dealing with negative margins but you also have issues with collapsing margins. 在这种情况下添加保证金权利应该有效,但我之前遇到过保证金和浮动问题,特别是在处理负利润时,但是你也有崩溃利润率的问题。 That may or may not be the behaviour you want. 这可能是您想要的行为,也可能不是。 Often I've ended up defensively enclosing floated content in a div and using padding instead as the results tend to be more intuitive. 通常我最终在防守中将浮动内容封装在div中并使用填充代替,因为结果往往更直观。

Also IE7 doesn't handle negative margins larger than the content width so you have to use enclosing elements in that case. 此外,IE7不会处理大于内容宽度的负边距,因此在这种情况下您必须使用封闭元素。 Here is an example of that technique . 这是该技术的一个例子

.image {
  padding-right: 10px
}

If adding a margin-right to the image makes the text simply show up underneath, then you need to increase the size of the parent container. 如果向图像添加边距权限使文本只显示在下面,则需要增加父容器的大小。

If the total width of the 2 components is larger than the size of the parent container, one of them goes to the next line. 如果2个组件的总宽度大于父容器的大小,则其中一个组件将转到下一行。

Code examples on 代码示例

<div class="parentDiv">
    <div class="image">
        **image here (assume it's 100px wide)**
    </div>
    <div class="otherText">
       **text here**
    </div>
</div>

This won't work because image size + image margin + otherText width > parentDiv width. 这不起作用,因为图像大小+图像边距+ otherText宽度> parentDiv宽度。 It will cause text to go to the next line: 它会导致文本转到下一行:

.parentDiv
{
    width: 500px;
}

.image
{
    float: left;
    margin-right: 3px;
}

.otherText
{
    float: left;
    width: 400px;
}

This will work: 这将有效:

.parentDiv
{
    width: 510px;
}

.image
{
    float: left;
    margin-right: 3px;
}

.otherText
{
    float: left;
    width: 400px;
}

Add a margin-right to the image. 为图像添加边距权限。

.imageclass
{
  margin-right: 3px;
}

Add either: 添加:

  • margin-left or padding-left to the text, or margin-left或padding-left to the text,or
  • margin-right or padding-right to the image 边距右或填充右图像

It's difficult to say what would be better without seeing your CSS / XHTML. 如果没有看到你的CSS / XHTML,很难说会更好。

Just ran into this yesterday. 刚刚遇到这个问题。 If you're planning on using a border around the image, be sure to use margin properties instead of padding, or you'll wind up with whitespace between the border of the image and the image itself. 如果您打算在图像周围使用边框,请务必使用边距属性而不是填充,否则您将在图像边框和图像本身之间使用空格。

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

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