html - 如何在 #nav 中移动 li 中的文本而不是更改 li block 的大小

标签 html css

在#nav ul li 中,我似乎无法在不更改 block 大小的情况下固定文本的位置。每当我尝试使用填充时, block 的大小都会发生变化。如何在不改变 block 大小的情况下移动文本?这是代码。

<!DOCTYPE html>
<html>
<head>
<style>

body
{
    padding: 0;
    background-color: #FFC0CB;
}
#container
{
    margin: 0 auto;
    width:100%;
    height: 1500px;
    padding: 0px;
}

#header
{
    overflow: auto;
    background-color: red;  
    margin: 0px;

}


#headTable
{
    float:right;
}

#logo
{
    background-color: green;
    margin: 5px 0px 5px 70px;
    float: left;
    width:150px;
    height:100px;
}

#headTable ul
{
    list-style-type: none;
    margin: 0;
}

#headTable ul li
{
    font-size: 35px;
    padding-top: 20px;
    color: black;
    float: left;
    margin: 20px 50px;
}

#headTable ul li:hover
{
    background-color: blue;
    color:red;
}

#nav
{
    clear: both;
    width:100%;
    height: 95px;
    background-color: purple;

}

#nav ul
{
    overflow: auto;/*
    background-color: yellow;*/
    margin: 0px;
    padding: 0px;
}

#nav ul li
{

    background-color: black;
    color:white;
    font-size: 30px;
    list-style-type: none;
    text-decoration: underline;
    margin: 20px;
    padding: 10px 10px 10px 20px;
    float: left;
    /*text-indent: 0px;
*/}

#page
{
    float: left;
    margin: 10px;
    height:700px;
    width:700px;
    background-color: #FFC0CB;

}

#page p
{
    padding:100px 0px 0px 50px;
    font-size: 20px;
    color: navy; 

}


#content
{
    height: 1000px;
    width: 1334px;
    position: absolute;
    margin:0px; 
    background-color: #00B2EE;

}
#top
{
    float: right;
    position: relative;
    margin: 10px;
    background-color: #7A67EE;
    width:500px;
    height:300px;
}

#low
{ 
    position: relative;
    clear: both;
    float: right;
    margin:-300px 10px 10px 10px;
    background-color: #7A67EE;
    width:500px;
    height:300px;

}

</style>
</head>

<body>

<div id="container">
    <div id="header">
        <div id="logo"> 
            <img src="plane.jpg" width="150" height="100">
        </div>
        <div id="headTable">
            <ul>
                <li>Google</li>
                <li>Google</li>
                <li>Google</li>
            </ul>   
         </div>
    </div>

        <div id="nav">
            <ul>
                <li>Menu</li>
                <li>Blog</li>
                <li>Ins</li>
                <li>BBC</li>
                <li>Contact</li>
                <li>Wow</li>
                <li>Doge</li>
                <li>Such fun</li>
            </ul>       
        </div>

    <div id="content">   

        <div id="page">
            <p>This is a paragraph that should
            stay intact inside the id of Page.
            </p>    
        </div>

        <div id="top">
            <p>THis is a paragraph that should
            stay intact inside the id of top.
            </p>
        </div>

        <div id="low">
            <p>This is a paragraph that should
            stay intact inside the id of bottom.
            </p>    
        </div>

    </div>
</div>              

</body>
</html>

最佳答案

您需要为 li 元素设置宽度,并将导航文本包裹在 span 标签中。然后你可以向左或向右移动它们,导航栏的宽度不会改变。

 <li><span>Menu</span></li>
 <li><span>Blog</span></li>
 <li><span>Ins</span></li>
 <li><span>BBC</span></li>
 <li><span>Contact</span></li>
 <li><span>Wow</span></li>
 <li><span>Doge</span></li>
 <li><span>Such fun</span></li>

#nav ul li{width:70px;background-color: black;color:white;font-size: 30px;list-style-type: none;text-decoration: underline;margin: 20px;padding: 10px 10px 10px 20px;float: left;}
#nav ul li span{margin-left:70px;}

在此处查看可行的解决方案:http://jsfiddle.net/mtruty/73uav/

关于html - 如何在 #nav 中移动 li 中的文本而不是更改 li block 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25001839/

相关文章:

html - 为字体创建基线

javascript - 用于检查 css 属性的 If 语句不起作用

javascript - 如何获取数组对象中的选中元素

javascript - CSS 动画不会在原始 JS 状态下运行

html - 带有框和多列列表的奇怪 CSS 行为

html - 带有隐藏 div 的单个 HTML 文件默认为空白

html - 如何垂直对齐多个图像 css?

php - 如何使用按钮从mysql删除记录?

css - 如何水平和垂直居中对齐div中的文本?

css - 两个箱子并排