div内自动适配ie中li的宽度

发布于 2024-09-28 13:49:59 字数 425 浏览 9 评论 0原文

<div style='width:500px'>  
<ul>  
  <li> some text in 1 line</li>  
  <li> some text in 1 line</li>  
  <li> some text 2 line</li>  
  <li> some 2</li>  
  <li> 2</li>  
</ul>
</div>

我不知道在 ie 中显示项目的正确 css 代码是什么,例如:

前两个结果自动适合第一行,其余结果自动适合第二行。 所以基本的想法是让 li 根据数据大小获得自己的宽度。

提前致谢

<div style='width:500px'>  
<ul>  
  <li> some text in 1 line</li>  
  <li> some text in 1 line</li>  
  <li> some text 2 line</li>  
  <li> some 2</li>  
  <li> 2</li>  
</ul>
</div>

I don't know what is the correct css code for display of items in ie like:

first two results automatic fit in first line and rest of the results on second line.
so basic idea is to let the li get it's own width depending on the data size.

Thanks in advance

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

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

发布评论

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

评论(6

自控 2024-10-05 13:49:59

我认为有两种方法可以实现您所要求的目标:

选项 1:

  • 标记设置为 display:block;float:left;
  • 选项 2:
    制作

  • 标签 display:inline-block;white-space:nowrap;
  • 我自己会选择选项 2,因为它可以避免浮动带来的怪癖。此外,您可能会发现无论如何您最终都需要 nowrap,即使使用选项 1。

    [编辑]

    您可能还需要设置

      标记的样式。也许是 width:100%; 和/或 display:block;

    我仍然认为 display:inline-block;white-space:nowrap; 应该为

  • 标签执行此操作。但如果它不起作用,如果你说它在什么方面不起作用,将会有所帮助。
  • 另外:您还说过有些答案在 IE 中不起作用,但您没有说哪个版本的 IE - IE6、7、8 和 9 对以下内容的支持程度非常 CSS;这将有助于了解您需要支持哪些。

    I see two ways to achieve what I think you're asking:

    Option 1:
    Make the <li> tags display:block;, and float:left;

    Option 2:
    Make the <li> tags display:inline-block; and white-space:nowrap;

    I'd go for option 2 myself, as it'll avoid the quirks you can get with floats. Also you may find you end up needing nowrap anyway, even with option 1.

    [EDIT]

    You may also need to style the <ul> tag. Maybe width:100%; and/or display:block;.

    I still say display:inline-block; and white-space:nowrap; should do it for the <li> tags. But if it isn't working, it would help if you said in what way it's not working.

    Also: You've also been saying some answers aren't working in IE, but you haven't said which version of IE -- IE6,7,8 and 9 have very different levels of support for CSS; it would help to know which ones you need to support.

    染年凉城似染瑾 2024-10-05 13:49:59

    您有多种方法可以实现您想要的功能:

    • 正如 MatTheCat 所说,使用一个 display: block 即可实现 display: inline
    • 类似地,float: left;< /code> 除了第二个元素之外的所有元素也可以将
    • 它们全部设置为 display: inline 并在第二个元素的末尾有一个
      (有点讨厌,但简单)

    但是,将它们视为两个单独的列表可能会更好。在不知道你用它做什么的情况下,很难说,但是将元素分开将使它更具可读性,并让你更好地控制两条线的定位和样式。

    如果目标只是让列表元素水平流动以填充 div 并且“第一行前两个”只是一个示例,那么只需在每个列表元素上设置 display: inline 即可。

    如果您特别希望前两个结果的宽度为 250px(div 的一半),请将样式设置为 float: left; width: 50%; 在这两个上,其余部分内联显示。

    You've got several ways to do what you want:

    • As MatTheCat says, display: inline with one display: block will work
    • Similarly, float: left; on all but the second will also do the trick
    • Set them all to display: inline and have a <br /> at the end of the second element (a bit nasty, but simple)

    However, you're probably far better off treating them as two separate lists. Without knowing what you're using it for, it's hard to say, but splitting the elements up will make it more readable and let you have better control over the positioning and styling of the two lines.

    If the aim is just to have the list elements flow horizontally to fill the div and "first two on first line" is just an example, then simply set display: inline on each list element.

    If you specifically want the first two results to have a width of 250px (half of your div), set the style as float: left; width: 50%; on those two and have the remainder diaplay inline.

    孤檠 2024-10-05 13:49:59

    我建议将导航设计为“像”表格一样:

    • Container div - display: table;相当于

    • 列表容器 - display: table-row;相当于
    • 列表项 - 显示表格:table-cell;相当于

    元素样式上的 Padding 只是为了均匀间距,并且可以根据需要进行调整,单元格项目计算出剩余宽度并相对于单元尺寸使用它。

    适用于 FF / Chrome / Safair /IE 9 和8

    IE7及以下版本您将需要使用浮动解决方案!

    工作示例:

    <html>
    <head>
        <title>test</title>
        <style type="text/css">
    
            div {
                margin: 0 auto 0 auto;
                width: 954px;
                height: 36px;
                border: 1px solid #000000;
                display: table;
            }
    
            ul {
                width: 100%;
                display: table-row;
                list-style: none;
                height: 100%;
            }
    
    
            li {
                display: table-cell;
                white-space: nowrap;
                border: 1px solid #00ff00;
                color: #ff0000;
            }
    
            a {
                text-align: center;
                display: block;
                padding: 0 15px;
                line-height: 36px;
            }
        </style>
    </head>
    <body>
        <div>
            <ul>
                <li><a>Item1</a></li>
                <li><a>Long Item 2</a></li>
                <li><a>Very Long Item 3</a></li>
                <li><a>Even longer example Item 4</a></li>
                <li><a>Item5</a></li>
                <li><a>Item6</a></li>
                <li><a>Medium Item7</a></li>
                <li><a>Item8</a></li>
            </ul>
        </div>
    </body>
    

    I would suggest styling the navigation "like" a table :

    • Containter div - display: table; equilivent to <table>
    • List containter - display: table-row; equilivent to <tr>
    • List Items - display table: table-cell; equilivent to <td>

    Padding on the <a> element style is just to even the spacing and can be adjusted as needed, the cell items work out the remaining width and use it up relative on the cell sizes.

    Working on FF / Chrome / Safair /IE 9 & 8

    IE7 and below you will need to use a floating solution!

    Working example:

    <html>
    <head>
        <title>test</title>
        <style type="text/css">
    
            div {
                margin: 0 auto 0 auto;
                width: 954px;
                height: 36px;
                border: 1px solid #000000;
                display: table;
            }
    
            ul {
                width: 100%;
                display: table-row;
                list-style: none;
                height: 100%;
            }
    
    
            li {
                display: table-cell;
                white-space: nowrap;
                border: 1px solid #00ff00;
                color: #ff0000;
            }
    
            a {
                text-align: center;
                display: block;
                padding: 0 15px;
                line-height: 36px;
            }
        </style>
    </head>
    <body>
        <div>
            <ul>
                <li><a>Item1</a></li>
                <li><a>Long Item 2</a></li>
                <li><a>Very Long Item 3</a></li>
                <li><a>Even longer example Item 4</a></li>
                <li><a>Item5</a></li>
                <li><a>Item6</a></li>
                <li><a>Medium Item7</a></li>
                <li><a>Item8</a></li>
            </ul>
        </div>
    </body>
    

    简单 2024-10-05 13:49:59

    浮动div,它应该有帮助......

    Float the div, it should help...

    怀中猫帐中妖 2024-10-05 13:49:59

    我认为最好的方法是应用

    li { display:inline; }
    

    并插入一个没有此规则的

  • 来执行换行。
  • 但是您不能将所有数据放在 ONE

  • 的同一行中吗?
  • I think the best way is to apply

    li { display:inline; }
    

    and insert a <li> without this rule to perform a line break.

    But can't you place all data in the same line in ONE <li> ?

    ぇ气 2024-10-05 13:49:59

    可能浮动属性可能有帮助

    • 1 行中的一些文本
    • 1 行中的一些文本
    • 一些文本 2 行
    • 一些2
    • 2

    <代码>
    李{
    浮动:左;
    显示:块;
    }

    May be floating properties may help

    <div style='width:500px; overflow: hidden;'>

    <ul>

    <li> some text in 1 line</li>

    <li> some text in 1 line</li>

    <li> some text 2 line</li>

    <li> some 2</li>

    <li>2</li>

    </ul>

    </div>


    li {
    float: left;
    display: block;
    }

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