简体   繁体   English

持续 <li> 不内联,显示它的大小

[英]Last <li> Not Inline, Messes up the Size of it

So for my webpage I am making a dropdown menu from scratch. 因此,对于我的网页,我正在从头开始制作下拉菜单。 So I have a header which contains 5 list items and a logo. 所以我有一个标题,其中包含5个列表项和一个徽标。 Let's call the list items tab1, tab2, tab3, tab4 and tab5. 让我们将列表项称为tab1,tab2,tab3,tab4和tab5。 My goal is to have tab1, tab2, tab3, tab4 and the logo display inline, but have tab5 display right below tab4. 我的目标是使tab1,tab2,tab3,tab4和徽标内联显示,但在tab4下方显示tab5。 To do this, I set the first 4 tabs and the logo to have have the property display: inline-block and the 5th tab to be just display: block. 为此,我将前四个选项卡和徽标设置为具有属性display:inline-block,第5个选项卡仅显示:block。 This solves the issue of having the fifth tab display under the other tabs and the logo, though the fifth tab does not retain the same width as the other tabs, even though they are classified under the same class .tab. 这解决了使第五个选项卡显示在其他选项卡和徽标下方的问题,尽管第五个选项卡的宽度与其他选项卡相同,即使第五个选项卡被归为同一类。 What ends up happening is the fifth tab becomes the width of the body. 最终发生的是第五个标签变成了身体的宽度。 I tried to fix this by setting the left and right padding of the tab manually, though this does not work. 我试图通过手动设置选项卡的左和右填充来解决此问题,尽管这不起作用。 Is there a way I can have the fifth tab act/display exactly like the other tabs (as in same width, height, etc.), just not displayed inline? 有没有一种方法可以使第五个选项卡的行为/显示与其他选项卡完全相同(以相同的宽度,高度等),而不是内联显示?

Here's my html: 这是我的html:

<div class="header">
    <a class="tab" id="tab1"><li>Home</li></a>
    <a class="tab" id="tab2"><li>About</li></a>
    <img src="images/logo.png" id="logo" width="260px" height="95px;"/>
    <a class="tab" id="tab3"><li>Menu</li></a>
    <a class="tab" id="tab4"><li>Order</li></a>
    <a class="tab" id="tab5"><li>Cater</li></a>
</div>

And here's my css: 这是我的CSS:

li {
    list-style-type: none;
}
.header{
    text-align: center;
    background-color: red;
}
.tab, #logo{
    padding-right: 2.4%;
    padding-left: 2.4%;
    display: inline-block;
    vertical-align: middle;
    border: 2px solid black;
}
#tab5{
    margin-top: 10px;
    display: block;
}

Here's a jsfiddle demonstration: https://jsfiddle.net/gobtt6vk/ 这是一个jsfiddle演示: https ://jsfiddle.net/gobtt6vk/

Thanks! 谢谢!

The #tab5 is occupying 100% of the body 's width because that's what display:block; #tab5占据了body width 100%,因为那是display:block; does. 确实。

You can solve this problem by giving it a width , for example: 您可以通过为其设置width来解决此问题,例如:

#tab5 {
    display: block;
    width: 30px;
    margin: 10px auto 0 auto; /* this aligns #tab5 to center and also gives margin-top:10px; */
}

Or you can solve it by giving it a display:table; 或者您可以通过给它一个display:table;来解决它display:table; instead of display:block; 而不是display:block; so you wont need to give it a fixed width , it will adjust itself to the text. 因此,您将不需要给它固定的width ,它会自动调整为文本。
Also your HTML structure should be improved, so I wasted a little bit of time here and reorganized it for you. 另外,您的HTML结构也应加以改进,因此我在这里浪费了一些时间,为您重新组织了它。

Here's an online example: https://jsfiddle.net/wujncnsd/ 这是一个在线示例: https : //jsfiddle.net/wujncnsd/

<div class="header">
    <ul>
        <li id="tab1" class="tab"><a>Home</a></li>
        <li id="tab2" class="tab"><a>About</a></li>
        <li id="logo">
            <img src="images/logo.png" width="260" height="95"/>
        </li>
        <li id="tab3" class="tab"><a>Menu</a></li>
        <li id="tab4" class="tab"><a>Order</a></li>
        <li id="tab5" class="tab"><a>Cater</a></li>
    </ul>
</div>
ul {margin: 0; padding: 0;}
li {list-style-type: none;}

.header{
    text-align: center;
    background-color: red;
}
.tab, #logo{
    padding-right: 2.4%;
    padding-left: 2.4%;
    display: inline-block;
    vertical-align: middle;
    border: 2px solid black;
}
#tab5{
    margin: 10px auto 0 auto;
    display: table;
}

try this updated code ... 试试这个更新的代码...

CSS: CSS:

li {
    list-style-type: none;
}
.header{
    text-align: center;
    background-color: red;
}
.tab, #logo{
    padding-right: 2.4%;
    padding-left: 2.4%;
    display: inline-block;
    vertical-align: middle;
    border: 2px solid black;
}
#tab5{
    margin-top: 5p;
}

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

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