如何根据内容水平增长div?

发布于 2024-07-12 14:05:40 字数 547 浏览 9 评论 0原文

请问,有人可以向我解释一下,为什么红色 div 没有向右扩展? 它停在屏幕结束的地方。 我必须做什么才能使其扩展?

有效的一件事是“显示:表格单元格”红色 div,但我想知道是否还有另一种方法以及为什么会发生这种情况......?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head />
<body>
  <div style="background-color: #f00;">
    <div style="width: 2000px; height: 100px; " />
  </div>
</body>
</html>

please, can someone explain to me, why the red div isn't expanding to the right? It stops where the screen ends. What do I have to do, to make it expand?

One thing that works is to "display: table-cell" the red div but I was wondering if there's another way and why this happens...?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head />
<body>
  <div style="background-color: #f00;">
    <div style="width: 2000px; height: 100px; " />
  </div>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

哥,最终变帅啦 2024-07-19 14:05:40

这确实很奇怪。 尝试浮动外部:

<div style="background-color: #f00; float:left;">
    <div style="width: 2000px; height: 100px;" />
</div>

That is indeed bizarre. Try floating the outer:

<div style="background-color: #f00; float:left;">
    <div style="width: 2000px; height: 100px;" />
</div>
一个人的旅程 2024-07-19 14:05:40

它包含的 div 小于 2000px。 你需要做这样的事情:

  <div style="background-color: #f00; width: 2000px;">
    <div style="width: 2000px; height: 100px; " />
  </div>

当然,现在不需要指定内部 div 的宽度,除非它与外部 div 的宽度不同。

The div that it's contained in is smaller than 2000px. You need to do something like this:

  <div style="background-color: #f00; width: 2000px;">
    <div style="width: 2000px; height: 100px; " />
  </div>

Of course, now the inner div's width doesn't need to be specified unless it is different than the outer div's.

如梦亦如幻 2024-07-19 14:05:40

overflow: auto; 放入父

的样式中。

Put overflow: auto; in the parent <div>'s style.

心凉 2024-07-19 14:05:40
  <div style="background-color: #f00;">
    <div style="width: 2000px; height: 100px; " />
  </div>

像这样关闭 DIV 标签(即自关闭标签)会引起麻烦。

它可能是合法的(请参阅此主题),但根据我的经验,它经常会导致奇怪的行为。

然而,这不是问题所在。 以下代码适用于 Chrome 和 IE9。

  <div style="background-color: #f00; display: inline-block; height: 100px;">
    <div style="width: 3000px; background-color: blue; height: 50px;">
        here is some content
      </div>
  </div>

Display: inline-block; 告诉元素根据需要增长(或尽可能少)。

小提琴: http://jsfiddle.net/sArvr/

这看起来确实是违反直觉的行为。

  <div style="background-color: #f00;">
    <div style="width: 2000px; height: 100px; " />
  </div>

Closing a DIV tag like that (i.e. a self-closing tag) will cause trouble.

It may be legal (See this SO Thread) but in my experience it often causes strange behavior.

However, that is not the problem. The following code works in Chrome and IE9.

  <div style="background-color: #f00; display: inline-block; height: 100px;">
    <div style="width: 3000px; background-color: blue; height: 50px;">
        here is some content
      </div>
  </div>

Display: inline-block; tells the element to grow as much (or as little) as necessary.

Fiddle: http://jsfiddle.net/sArvr/

This does seem like counter-intuitive behavior.

謌踐踏愛綪 2024-07-19 14:05:40

看看这个。 随着文本的继续,它会扩展容器......

    <div style="background-color: #f00; height: 100px; display: inline-block;">
      <div style="background-color: blue; height: 50px; white-space:nowrap; display: inline-block;">
        here is some content
      </div>
    </div>

Check with this one. it will expand the container as the text goes on...

    <div style="background-color: #f00; height: 100px; display: inline-block;">
      <div style="background-color: blue; height: 50px; white-space:nowrap; display: inline-block;">
        here is some content
      </div>
    </div>
俯瞰星空 2024-07-19 14:05:40

根据您的用途,我假设 div 元素将成为页面的主要焦点; 如果是这种情况,您可以采用 width: 2000px; 行并将其应用于正文,而不是直接应用于要拉伸的块级元素:

<style>
body
 {
width: 2000px;
 }
</style>

如果此方法不起作用无论出于何种原因,创建透明像素图像并为其提供所需的宽度应该同样有效。

Depending on what you're using this for, I'm assuming the div element is going to be the main focus of the page; if this is the case, you can take that width: 2000px; line and just apply it to the body instead of directly to the block-level element you want to stretch:

<style>
body
 {
width: 2000px;
 }
</style>

If this method doesn't work for you for whatever reason, creating a transparent pixel image and giving it the width you require should work just as well.

花期渐远 2024-07-19 14:05:40

内部 div 的宽度可以更改,我需要增大外部 div,这样我就无法为其分配宽度。

The width of the inner div can change, I need to grow the outer one so I can't assign a width to it.

誰認得朕 2024-07-19 14:05:40

看起来父 div 的最大宽度受到浏览器屏幕宽度的限制,即使子 div 的宽度超出了浏览器屏幕宽度。 这是一个奇怪的发现...

我建议在父 div 上使用 float 。 (参考:crescentfresh)我知道确保表格单元格不适用于 IE6。 在我的机器上对 IE7 进行快速测试也不起作用,尽管它应该......

这应该起作用。 (编辑:这不起作用......)

<div style="background-color: #f00; width: auto;">
  <div style="width: 2000px; height: 100px; "></div>
</div>

It seems like max width of parent div is limited by browser screen width even though child div's width grows beyond browser screen width. This is an odd discovery...

I suggest using float on parent div. (ref: crescentfresh) I know for sure that table-cell does not work for IE6. And a quick test on IE7 on my machine doesn't work either although it should...

This should work. (EDIT: This doesn't work...)

<div style="background-color: #f00; width: auto;">
  <div style="width: 2000px; height: 100px; "></div>
</div>
倒带 2024-07-19 14:05:40


当您增加内容时,我们的 div 应该自动增加其大小。

put <div style="min-height: 100px; " />

when you grow the content more, our div should grow its size automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文