如何将此菜单栏移动到右侧

时间:2017-01-18 18:04:51

标签: html css menu blogs

我想把对象移到右边,但不知道怎么做!?任何的想法? 。请帮我。这是HTML和CSS中的代码



/*=====================================
= Top Bar
=====================================*/
#top-bar {
    height: 42px;
    line-height: 40px;
    background: #f9f9f9;
    color: #888;
    font-size: .857em;
    border-bottom: 1px solid #f5f5f5;
}


.top-nav ul li {
    display: block;
    float: left;
}

.top-nav ul {
    margin: 0;
    padding: 0;
}


.top-nav {
    font-family: "Montserrat",Open Sans,Sans-serif;
}

.top-nav ul li a {
    color: #111;
    text-decoration: none;
    display: inline-block;
    padding: 0 15px;
    line-height: .917em;
    color: #888;
    text-decoration: none;
    text-transform: uppercase;
    letter-spacing: .5px;
    font-size: .917em;
    border-right: 1px solid #eee;
}


.top-notification {
    float: right;
}

.top-notification p {
    margin: 0;
    float: left;
    font-size: 13px;
}

.top-notification a {
    color: #111;
    text-decoration: none;
    /* display: inline-block; */
    padding: 5px 9px;
    line-height: .917em;
    color: #888;
    text-decoration: none;
    text-transform: uppercase;
    letter-spacing: .5px;
    font-size: .917em;
    border: 1px solid #D1D1D1;
    margin-left: 22px;
	background:#fff;
}

nav ul {
	padding: 0;
  margin: 0;
	list-style: none;
	position: relative;
	}
	
nav ul li {
	display:inline-block;
	}

nav ul li a:link {
    display: inline-block;
    padding: 15px 25px;
    text-transform: uppercase;
    letter-spacing: 1px;
    position: relative;
    text-decoration: none!important;
    color: #fff;
	width:100%;
}

nav ul li a {
    color: #fff!important;
}

nav a:hover { 
	    background-color: #2da399; 
}

/* Hide Dropdowns by Default */
nav ul ul {
	display: none;
	position: absolute; 
    z-index: 1000;
background: #494949;

}
	
/* Display Dropdowns on Hover */
nav ul li:hover > ul {
	display:inherit;
}
	
/* Fisrt Tier Dropdown */
nav ul ul li {
	min-width:170px;
	float:none;
	display:list-item;
	position: relative;
}

nav ul ul a:hover {
    background-color: #656565;

}

	
/* Change this in order to change the Dropdown symbol */
nav li > a:after { content:  ' +'; }
nav li > a:only-child:after { content: ''; }


.menu {
    width: 1200px;
    margin: auto;
	    z-index: 99999;
    background: #2da399;
}

.menu.cloned {
    width: 100%!important;
    left: 0!important;
}

.menu.cloned nav {
    width: 1200px;
    margin: auto;
}

Here is HTML codes for my website. I think every thing in is fine here and the problem is in CSS codes.

<!--Main Navigation-->
<div class='menu-wrapper'>
 <div class='menu'>
	<nav>
        <ul>
            <li><a href='https://thevoiceofafghanistan.blogspot.se/p/kontakta-mig.html'>Hem</a></li>
            <li><a href='#'>Nyheter </a>
            <!-- First Tier Drop Down -->
            <ul>
                <li><a href='https://thevoiceofafghanistan.blogspot.se/p/afghanistan.html'>Afghanistan</a></li>
                <li><a href='https://thevoiceofafghanistan.blogspot.se/p/sverige-och-e.html'>Sverige</a></li>
                <li><a href='#'>Världen</a></li>
            </ul>        
            </li>
            <li><a href='#'>Vetenskap
</a>
            <!-- First Tier Drop Down -->
            <ul>
              <li><a href='https://www.facebook.com/thevoiceofafghanistan/'>Kemi
</a></li>
                <li><a href='#'>Fysik </a></li>
                <li><a href='#'>Matte</a>
            	<!-- Second Tier Drop Down -->
                <ul>
                    <li><a href=''>Matte 1</a></li>
                    <li><a href='#'>Matte 2</a></li>
                    <li><a href='#'>Matte 3</a>
                        <!-- Third Tier Drop Down -->
                        <ul>
                            <li><a href='#'>Matte 4</a></li>
                            <li><a href='#'>Hjälpmedel
</a></li>
                            
                        </ul>
                    </li>
                </ul>
                </li>
            </ul>
            </li>
            <li><a href='#'>فناوری </a></li>    
            <li><a href='#'>اجتمائی </a></li>
			<li><a href='#'> سیاست</a></li>
            <li><a href='#'>تاریخ </a></li>
			<li><a href=''>کلاس ها</a></li>
			<li><a href='https://thevoiceofafghanistan.blogspot.se/'>صفحه اصلی </a></li>        
		</ul>
    </nav>
</div>
  </div>
&#13;
&#13;
&#13;

This is menu bar

3 个答案:

答案 0 :(得分:0)

你应该在你的CSS中添加一些保证金。由于您没有提供完整的HTML,我无法确定哪个属性确实需要样式。 我认为您应该在菜单中添加margin-left以增加它们之间或父div之间的空间,以便将整个导航栏向右移动。希望这有帮助!

答案 1 :(得分:0)

我会假设当你问如何将它们向右移动时,最终目标是将导航元素置于蓝绿色区域<nav>内。

您可能还要求将导航项与蓝绿色字段的右侧对齐,我已在第二个代码段中解决了这个问题。

中心导航项目

将某个内容置于中心位置的常用方法是将其移至left 50%,然后使用transform将其拉回宽度的50%。为了实现这一点,您需要让<ul>不要占用其父元素的100%。您可以通过多种方式执行此操作,为简单起见,我已将其设置为display: inline-block;

此解决方案允许您添加/删除<li>并仍然位于<nav>的中心位置。

nav ul {
    display: inline-block;
    position: relative;
    left: 50%;
    padding: 0;
    margin: 0;
    transform: translateX( -50%);
    list-style: none;
}
nav ul li {
    display: inline-block;
}
nav ul li a:link {
    display: inline-block;
    padding: 15px 25px;
    text-transform: uppercase;
    letter-spacing: 1px;
    position: relative;
    text-decoration: none !important;
    color: #fff;
}
nav ul li a {
    color: #fff !important;
}
nav a:hover {
    background-color: #2da399;
}

/* Hide Dropdowns by Default */

nav ul ul {
    display: none;
    position: absolute;
    z-index: 1000;
    background: #494949;
}

/* Display Dropdowns on Hover */

nav ul li:hover>ul {
    display: inherit;
}

/* Fisrt Tier Dropdown */

nav ul ul li {
    min-width: 170px;
    float: none;
    display: list-item;
    position: relative;
}
nav ul ul a:hover {
    background-color: #656565;
}

/* Change this in order to change the Dropdown symbol */

nav li>a:after {
    content: ' +';
}
nav li>a:only-child:after {
    content: '';
}
.menu {
    width: 1200px;
    margin: auto;
    z-index: 99999;
    background: #2da399;
}
<!--Main Navigation-->
<div class='menu-wrapper'>
 <div class='menu'>
	<nav>
        <ul>
            <li><a href='https://thevoiceofafghanistan.blogspot.se/p/kontakta-mig.html'>Hem</a></li>
            <li><a href='#'>Nyheter </a>
            <!-- First Tier Drop Down -->
            <ul>
                <li><a href='https://thevoiceofafghanistan.blogspot.se/p/afghanistan.html'>Afghanistan</a></li>
                <li><a href='https://thevoiceofafghanistan.blogspot.se/p/sverige-och-e.html'>Sverige</a></li>
                <li><a href='#'>Världen</a></li>
            </ul>        
            </li>
            <li><a href='#'>Vetenskap
</a>
            <!-- First Tier Drop Down -->
            <ul>
              <li><a href='https://www.facebook.com/thevoiceofafghanistan/'>Kemi
</a></li>
                <li><a href='#'>Fysik </a></li>
                <li><a href='#'>Matte</a>
            	<!-- Second Tier Drop Down -->
                <ul>
                    <li><a href=''>Matte 1</a></li>
                    <li><a href='#'>Matte 2</a></li>
                    <li><a href='#'>Matte 3</a>
                        <!-- Third Tier Drop Down -->
                        <ul>
                            <li><a href='#'>Matte 4</a></li>
                            <li><a href='#'>Hjälpmedel
</a></li>
                            
                        </ul>
                    </li>
                </ul>
                </li>
            </ul>
            </li>
            <li><a href='#'>فناوری </a></li>    
            <li><a href='#'>اجتمائی </a></li>
			<li><a href='#'> سیاست</a></li>
            <li><a href='#'>تاریخ </a></li>
			<li><a href=''>کلاس ها</a></li>
			<li><a href='https://thevoiceofafghanistan.blogspot.se/'>صفحه اصلی </a></li>        
		</ul>
    </nav>
</div>
  </div>

我注意到你包含了比标记使用的CSS更多的CSS,所以我删除了它。

将导航项目对齐

如果您希望将导航与右侧而不是左侧对齐,则可以将<ul>向右浮动。这里唯一的问题是你需要清除浮动,这样你才能看到.menu的背景颜色。我使用了穷人的clearfix overflow: hidden;,但你可以使用Micro Clearfix更现代的选项。

nav ul {
    float: right;
    display: inline;
    position: relative;
    padding: 0;
    margin: 0;
    list-style: none;
}
nav ul li {
    display: inline-block;
}
nav ul li a:link {
    display: inline-block;
    padding: 15px 25px;
    text-transform: uppercase;
    letter-spacing: 1px;
    position: relative;
    text-decoration: none !important;
    color: #fff;
}
nav ul li a {
    color: #fff !important;
}
nav a:hover {
    background-color: #2da399;
}

/* Hide Dropdowns by Default */

nav ul ul {
    display: none;
    position: absolute;
    z-index: 1000;
    background: #494949;
}

/* Display Dropdowns on Hover */

nav ul li:hover>ul {
    display: inherit;
}

/* Fisrt Tier Dropdown */

nav ul ul li {
    min-width: 170px;
    float: none;
    display: list-item;
    position: relative;
}
nav ul ul a:hover {
    background-color: #656565;
}

/* Change this in order to change the Dropdown symbol */

nav li>a:after {
    content: ' +';
}
nav li>a:only-child:after {
    content: '';
}
.menu {
    width: 1200px;
    margin: auto;
    z-index: 99999;
    background: #2da399;
  overflow: hidden; /* Clearfix that allows the background of <nav> to be seen with floated child elements. */
}
<!--Main Navigation-->
<div class='menu-wrapper'>
 <div class='menu'>
	<nav>
        <ul>
            <li><a href='https://thevoiceofafghanistan.blogspot.se/p/kontakta-mig.html'>Hem</a></li>
            <li><a href='#'>Nyheter </a>
            <!-- First Tier Drop Down -->
            <ul>
                <li><a href='https://thevoiceofafghanistan.blogspot.se/p/afghanistan.html'>Afghanistan</a></li>
                <li><a href='https://thevoiceofafghanistan.blogspot.se/p/sverige-och-e.html'>Sverige</a></li>
                <li><a href='#'>Världen</a></li>
            </ul>        
            </li>
            <li><a href='#'>Vetenskap
</a>
            <!-- First Tier Drop Down -->
            <ul>
              <li><a href='https://www.facebook.com/thevoiceofafghanistan/'>Kemi
</a></li>
                <li><a href='#'>Fysik </a></li>
                <li><a href='#'>Matte</a>
            	<!-- Second Tier Drop Down -->
                <ul>
                    <li><a href=''>Matte 1</a></li>
                    <li><a href='#'>Matte 2</a></li>
                    <li><a href='#'>Matte 3</a>
                        <!-- Third Tier Drop Down -->
                        <ul>
                            <li><a href='#'>Matte 4</a></li>
                            <li><a href='#'>Hjälpmedel
</a></li>
                            
                        </ul>
                    </li>
                </ul>
                </li>
            </ul>
            </li>
            <li><a href='#'>فناوری </a></li>    
            <li><a href='#'>اجتمائی </a></li>
			<li><a href='#'> سیاست</a></li>
            <li><a href='#'>تاریخ </a></li>
			<li><a href=''>کلاس ها</a></li>
			<li><a href='https://thevoiceofafghanistan.blogspot.se/'>صفحه اصلی </a></li>        
		</ul>
    </nav>
</div>
  </div>

答案 2 :(得分:-1)

只需将导航栏宽度设置为100 /项目数。它会将菜单项均匀地分布在栏上,尽可能地消除右侧的空隙。

您可以通过相应地设置margin-right属性来删除内联块元素之间的空格。

使用以下代码替换旧的nav ul li:

nav ul li {
display:inline-block;
background-color: #2da399;
width:11%;
margin-right: -4px;
}

以这种方式执行菜单将允许您的菜单完全包裹到各种屏幕宽度。