简体   繁体   English

如何防止DIV扩展以占用所有可用宽度?

[英]How do I keep a DIV from expanding to take up all available width?

In the following HTML, I'd like the frame around the image to be snug -- not to stretch out and take up all the available width in the parent container. 在下面的HTML中,我希望图像周围的框架是舒适的 - 不要拉伸并占用父容器中的所有可用宽度。 I know there are a couple of ways to do this (including horrible things like manually setting its width to a particular number of pixels), but what is the right way? 我知道有几种方法可以做到这一点(包括手动将其宽度设置为特定像素数等可怕的事情),但是正确的方法是什么?

Edit: One answer suggests I turn off "display:block" -- but this causes the rendering to look malformed in every browser I've tested it in. Is there a way to get a nice-looking rendering with "display:block" off? 编辑:一个答案表明我关闭了“display:block” - 但是这会导致渲染在我测试过的每个浏览器中都显得格格不入。有没有办法通过“display:block”获得漂亮的渲染效果关闭?

Edit: If I add "float: left" to the pictureframe and "clear:both" to the P tag, it looks great. 编辑:如果我将“float:left”添加到相框并将“clear:both”添加到P标签,它看起来很棒。 But I don't always want these frames floated to the left. 但我并不总是希望这些框架浮动到左侧。 Is there a more direct way to accomplish whatever "float" is doing? 是否有更直接的方式来完成“浮动”正在做的事情?

 .pictureframe { display: block; margin: 5px; padding: 5px; border: solid brown 2px; background-color: #ffeecc; } #foo { border: solid blue 2px; float: left; } img { display: block; } 
 <div id="foo"> <span class="pictureframe"> <img alt='' src="http://stackoverflow.com/favicon.ico" /> </span> <p> Why is the beige rectangle so wide? </p> </div> 

The right way is to use: 正确的方法是使用:

.pictureframe {
    display: inline-block;
}

Edit: Floating the element also produces the same effect, this is because floating elements use the same shrink-to-fit algorithm for determining the width. 编辑:浮动元素也会产生相同的效果,这是因为浮动元素使用相同的缩小到适合算法来确定宽度。

The beige rectangle is so wide because you have display: block on the span, turning an inline element into a block element. 米色矩形是如此宽,因为你有显示:跨度上的块,将内联元素转换为块元素。 A block element is supposed to take up all available width, an inline element does not. 块元素应该占用所有可用宽度,而内联元素则不占用。 Try removing the display: block from the css. 尝试从css中删除display:block。

Adding "float:left" to the span.pictureFrame selector fixes the problem as that's what "float:left" does :) Apart from everything else floating an element to the left will make it occupy only the space required by its contents. 将“float:left”添加到span.pictureFrame选择器可以解决问题,就像“float:left”所做的那样:)除了其他一切之外,向左移动一个元素将使其仅占用其内容所需的空间。 Any following block elements (the "p" for example) will float around the "floated" element. 任何后续的块元素(例如“p”)将浮动在“浮动”元素周围。 If you "clear" the float of the "p" it would follow the normal document flow thus going below span.pictureFrame. 如果你“清除”“p”的浮动,它将遵循正常的文档流程,因此在span.pictureFrame下面。 In fact you need "clear:left" as the element has been "float:left"-ed. 实际上你需要“clear:left”,因为元素是“float:left”-ed。 For a more formal explanation you can check the CSS spec although it is beyond most people's comprehension. 有关更正式的解释,您可以检查CSS规范,尽管它超出了大多数人的理解。

Yes display:inline-block is your friend. display:inline-block是你的朋友。 Also have a look at: display:-moz-inline-block and display:-moz-inline-box . 另请看: display:-moz-inline-blockdisplay:-moz-inline-box

The only way I've been able to do picture frames reliably across browsers is to set the width dynamically. 我能够在浏览器中可靠地制作相框的唯一方法是动态设置宽度。 Here is an example using jQuery: 以下是使用jQuery的示例:

$(window).load(function(){
  $('img').wrap('<div class="pictureFrame"></div>');
  $('div.pictureFrame').each(function(i) {
    $(this).width($('*:first', this).width());
  });
});

This will work even if you don't know the image dimensions ahead of time, because it waits for the images to load (note we're using $(window).load rather than the more common $(document).ready) before adding the picture frame. 即使您不提前知道图像尺寸,这也会起作用,因为它等待图像加载(注意我们使用$(window).load而不是更常见的$(document).ready)添加相框。 It's a bit ugly, but it works. 这有点难看,但它确实有效。

Here is the pictureFrame CSS for this example: 以下是此示例的pictureFrame CSS:

.pictureFrame {
  background-color:#FFFFFF;
  border:1px solid #CCCCCC;
  line-height:0;
  padding:5px;
}

I'd love to see a reliable, cross-browser, CSS-only solution to this problem. 我很想看到这个问题的可靠,跨浏览器,仅CSS的解决方案。 This solution is something I came up with for a past project after much frustration trying to get it working with only CSS and HTML. 这个解决方案是我为过去的项目提出的,经过多次挫折后试图让它只使用CSS和HTML。

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

相关问题 我如何同时(1)保持 <div> 占用所有可用宽度,并且(2)使它与邻居的边距崩溃? - How do I simultaneously (1) keep a <div> from taking up all available width and (2) make it collapse margins with its neighbors? 如何获得一个浮动div占用所有可用空间 - How do I get a floating div to take up all available space 如何使绝对定位的div占用所有可用的宽度空间? - How to make an absolutely positioned div take all the available width space? 如何让我的 GridView 占据其列中可用的所有宽度? - How can I get my GridView to take up all the width available to it within its column? 如何设置div来占用基于父div的剩余可用空间而不进行包装? - How do I set a div to take up the remaining space available based on the parent's div without wrapping? css:防止div宽度扩展到可用宽度 - css : prevent div width from expanding to available width 如何设置JLabel占用所有可用的宽度空间? - How do you set a JLabel to take all the available width space? 如何使输入文本字段占用所有可用宽度? - How do I make the input text field take all available width? 如何使DIV仅占用其包含的元素的宽度? - How do I make my DIV only take up the width of the elements it contains? 如何让div占据屏幕的整个宽度? - How can I have a div take up the full width of the screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM