简体   繁体   English

防止div占用100%的宽度

[英]prevent div's from taking 100% width

<div style="position: absolute; top: 0px; left: 0px; right: 0px; bottom: 0px;">

  <div style="
         border: 2px solid black;
         margin: 0 auto;
         text-align: center;
         padding: 3px;">

    Hello<br />Hola

  </div>

  <div style="
         border: 2px solid black;
         margin: 0 auto;
         text-align: center;
         padding: 3px;">

    Another Sentence

  </div>

</div>

I have a problem: the borders of the inner div's reach over the whole width of the page, but i want them to only frame the content inside them. 我有一个问题:内部div的边界覆盖整个页面的宽度,但我希望它们只构建其中的内容。 If i use: display: inline the borders frame each line separately and overlap, so that doesn't work - can somebody help? 如果我使用: display: inline边框将每一行分开并重叠,这样就行不通了 - 有人可以帮忙吗?

PS the style's aren't declared like this in the original document but in a stylesheet PS样式在原始文档中没有这样声明,而是在样式表中声明

Assign a width to the absolutely positioned element? 为绝对定位的元素指定宽度? If you're looking for shrink-wrapping, float:left or display:inline-block are perfect for that. 如果你正在寻找收缩包装, float:leftdisplay:inline-block是完美的。

Try display:inline-block , it always helps me in situations like this. 尝试display:inline-block ,它总是帮助我在这样的情况下。

http://jsfiddle.net/FaYLk/ http://jsfiddle.net/FaYLk/

Its not as simple to just do: 它不是那么简单:

display: inline-block;

You must do more than that to cross browser this. 你必须做更多的事情才能跨浏览器。

display: inline-block;
display: -moz-inline-stack; /* for firefox 2 */
*display: inline; /* for ie 6 and 7 */

Put a container around all the content. 在所有内容周围放置一个容器。 Eg 例如

<div class='container'> <div>I wont be 100%</div> <div>Nor will I :)</div> </div>
.container{ display: inline-block; } 

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

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