如何将此 CSS 菜单与其包含的 div 右侧对齐?
我在尝试找出将以下菜单(仅使用 css)与其包含的 div 的右侧对齐背后的逻辑时遇到了一些麻烦。
我知道默认情况下它会向左对齐。通过向列表添加 50%,50% 相对定位,它会产生居中位置。但如何让它向右对齐呢?
/* metaltop-lilac */
/* -------------------------------------------------- */
#metaltop-lilac {
clear:left;
float:left;
padding:0;
background:#B096CB url(metaltop-lilac.gif) top repeat-x;
border-bottom:1px solid #6D677E;
width:100%;
border-top:4px solid #000;
overflow:hidden;
font-family:Verdana, sans-serif;
}
#metaltop-lilac ul {
float:left;
margin:0;
padding:0;
list-style:none;
position:relative;
left:50%;
text-align:center;
}
#metaltop-lilac ul li {
display:block;
float:left;
list-style:none;
margin:0;
padding:0;
position:relative;
right:50%;
}
#metaltop-lilac ul li.first {
border-left:1px solid #6D677E;
}
#metaltop-lilac ul li.last {
border-right:1px solid #E2D8EC;
}
#metaltop-lilac ul li a {
display:block;
float:left;
margin:0;
padding:20px 10px 6px 10px;
border-left:1px solid #E2D8EC;
border-right:1px solid #6D677E;
text-decoration:none;
color:#57467D;
font-size:.8em;
font-weight:bold;
text-transform:uppercase;
line-height:1.3em;
}
#metaltop-lilac ul li a:hover {
color:#000;
background-color:#E6D8F5;
border-left:1px solid #fff;
padding-top:18px;
padding-bottom:8px;
}
#metaltop-lilac ul li.active a,
#metaltop-lilac ul li.active a:hover {
color:#fff;
background:#000;
border-left:1px solid #000;
padding-top:18px;
padding-bottom:8px;
}
有人有什么想法吗?
@thirtydot - 菜单教程位于以下页面:
http://matthewjamestaylor.com/blog/beautiful-css-centered-menus-no-hacks-full-cross-browser-support
这里是附加样式表:
@charset "utf-8";
/* CSS Document */
body {
font: 100%/1.4 Verdana, Arial, Helvetica, sans-serif;
background: #4E5869;
margin: 0;
padding: 0;
color: #000;
}
/* ~~ Element/tag selectors ~~ */
ul, ol, dl { /* Due to variations between browsers, it's best practices to zero padding and margin on lists. For consistency, you can either specify the amounts you want here, or on the list items (LI, DT, DD) they contain. Remember that what you do here will cascade to the .nav list unless you write a more specific selector. */
padding: 0;
margin: 0;
}
h1, h2, h3, h4, h5, h6, p {
margin-top: 0; /* removing the top margin gets around an issue where margins can escape from their containing div. The remaining bottom margin will hold it away from any elements that follow. */
padding-right: 15px;
padding-left: 15px; /* adding the padding to the sides of the elements within the divs, instead of the divs themselves, gets rid of any box model math. A nested div with side padding can also be used as an alternate method. */
}
a img { /* this selector removes the default blue border displayed in some browsers around an image when it is surrounded by a link */
border: none;
}
/* ~~ Styling for your site's links must remain in this order - including the group of selectors that create the hover effect. ~~ */
a:link {
color:#414958;
text-decoration: underline; /* unless you style your links to look extremely unique, it's best to provide underlines for quick visual identification */
}
a:visited {
color: #4E5869;
text-decoration: underline;
}
a:hover, a:active, a:focus { /* this group of selectors will give a keyboard navigator the same hover experience as the person using a mouse. */
text-decoration: none;
}
/* ~~ this container surrounds all other divs giving them their percentage-based width ~~ */
.container {
width: 80%;
max-width: 1260px;/* a max-width may be desirable to keep this layout from getting too wide on a large monitor. This keeps line length more readable. IE6 does not respect this declaration. */
min-width: 780px;/* a min-width may be desirable to keep this layout from getting too narrow. This keeps line length more readable in the side columns. IE6 does not respect this declaration. */
background: #FFF;
margin: 0 auto; /* the auto value on the sides, coupled with the width, centers the layout. It is not needed if you set the .container's width to 100%. */
}
/* ~~the header is not given a width. It will extend the full width of your layout. It contains an image placeholder that should be replaced with your own linked logo~~ */
.header {
background: #6F7D94;
}
/* ~~ This is the layout information. ~~
1) Padding is only placed on the top and/or bottom of the div. The elements within this div have padding on their sides. This saves you from any "box model math". Keep in mind, if you add any side padding or border to the div itself, it will be added to the width you define to create the *total* width. You may also choose to remove the padding on the element in the div and place a second div within it with no width and the padding necessary for your design.
*/
.content {
padding: 10px 0;
}
/* ~~ This grouped selector gives the lists in the .content area space ~~ */
.content ul, .content ol {
padding: 0 15px 15px 40px; /* this padding mirrors the right padding in the headings and paragraph rule above. Padding was placed on the bottom for space between other elements on the lists and on the left to create the indention. These may be adjusted as you wish. */
}
/* ~~ The footer ~~ */
.footer {
padding: 10px 0;
background: #6F7D94;
}
/* ~~ miscellaneous float/clear classes ~~ */
.fltrt { /* this class can be used to float an element right in your page. The floated element must precede the element it should be next to on the page. */
float: right;
margin-left: 8px;
}
.fltlft { /* this class can be used to float an element left in your page. The floated element must precede the element it should be next to on the page. */
float: left;
margin-right: 8px;
}
.clearfloat { /* this class can be placed on a <br /> or empty div as the final element following the last floated div (within the #container) if the #footer is removed or taken out of the #container */
clear:both;
height:0;
font-size: 1px;
line-height: 0px;
}
和 html 源:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
@import url("style.css") screen;
@import url("menu.css") screen;
//-->
</style>
</head>
<body>
<div class="container">
<div class="header">
<div id="metaltop-lilac">
<ul>
<li class="first"><a href="#">Home Page</a></li>
<li class="active"><a href="#">Our Products</a></li>
<li><a href="#">About Us</a></li>
<li class="last"><a href="#">Contact Us</a></li>
</ul>
</div>
<!-- end .header --></div>
<div class="content">
<p>test</p>
<!-- end .content --></div>
<div class="footer">
<!-- end .footer --></div>
<!-- end .container --></div>
</body>
</html>
I am having some trouble trying to figure out the logic behind aligning the following menu, that uses css only, to the right hand side of its containing div.
I understand that by default it will get aligned to the left. By adding 50%,50% relative positioning to the list, it produces a centered position. But how do I get it to align to the right?
/* metaltop-lilac */
/* -------------------------------------------------- */
#metaltop-lilac {
clear:left;
float:left;
padding:0;
background:#B096CB url(metaltop-lilac.gif) top repeat-x;
border-bottom:1px solid #6D677E;
width:100%;
border-top:4px solid #000;
overflow:hidden;
font-family:Verdana, sans-serif;
}
#metaltop-lilac ul {
float:left;
margin:0;
padding:0;
list-style:none;
position:relative;
left:50%;
text-align:center;
}
#metaltop-lilac ul li {
display:block;
float:left;
list-style:none;
margin:0;
padding:0;
position:relative;
right:50%;
}
#metaltop-lilac ul li.first {
border-left:1px solid #6D677E;
}
#metaltop-lilac ul li.last {
border-right:1px solid #E2D8EC;
}
#metaltop-lilac ul li a {
display:block;
float:left;
margin:0;
padding:20px 10px 6px 10px;
border-left:1px solid #E2D8EC;
border-right:1px solid #6D677E;
text-decoration:none;
color:#57467D;
font-size:.8em;
font-weight:bold;
text-transform:uppercase;
line-height:1.3em;
}
#metaltop-lilac ul li a:hover {
color:#000;
background-color:#E6D8F5;
border-left:1px solid #fff;
padding-top:18px;
padding-bottom:8px;
}
#metaltop-lilac ul li.active a,
#metaltop-lilac ul li.active a:hover {
color:#fff;
background:#000;
border-left:1px solid #000;
padding-top:18px;
padding-bottom:8px;
}
Any ideas anyone?
@thirtydot - The menu tutorial is located on the following page:
http://matthewjamestaylor.com/blog/beautiful-css-centered-menus-no-hacks-full-cross-browser-support
Here is the additional stylesheet:
@charset "utf-8";
/* CSS Document */
body {
font: 100%/1.4 Verdana, Arial, Helvetica, sans-serif;
background: #4E5869;
margin: 0;
padding: 0;
color: #000;
}
/* ~~ Element/tag selectors ~~ */
ul, ol, dl { /* Due to variations between browsers, it's best practices to zero padding and margin on lists. For consistency, you can either specify the amounts you want here, or on the list items (LI, DT, DD) they contain. Remember that what you do here will cascade to the .nav list unless you write a more specific selector. */
padding: 0;
margin: 0;
}
h1, h2, h3, h4, h5, h6, p {
margin-top: 0; /* removing the top margin gets around an issue where margins can escape from their containing div. The remaining bottom margin will hold it away from any elements that follow. */
padding-right: 15px;
padding-left: 15px; /* adding the padding to the sides of the elements within the divs, instead of the divs themselves, gets rid of any box model math. A nested div with side padding can also be used as an alternate method. */
}
a img { /* this selector removes the default blue border displayed in some browsers around an image when it is surrounded by a link */
border: none;
}
/* ~~ Styling for your site's links must remain in this order - including the group of selectors that create the hover effect. ~~ */
a:link {
color:#414958;
text-decoration: underline; /* unless you style your links to look extremely unique, it's best to provide underlines for quick visual identification */
}
a:visited {
color: #4E5869;
text-decoration: underline;
}
a:hover, a:active, a:focus { /* this group of selectors will give a keyboard navigator the same hover experience as the person using a mouse. */
text-decoration: none;
}
/* ~~ this container surrounds all other divs giving them their percentage-based width ~~ */
.container {
width: 80%;
max-width: 1260px;/* a max-width may be desirable to keep this layout from getting too wide on a large monitor. This keeps line length more readable. IE6 does not respect this declaration. */
min-width: 780px;/* a min-width may be desirable to keep this layout from getting too narrow. This keeps line length more readable in the side columns. IE6 does not respect this declaration. */
background: #FFF;
margin: 0 auto; /* the auto value on the sides, coupled with the width, centers the layout. It is not needed if you set the .container's width to 100%. */
}
/* ~~the header is not given a width. It will extend the full width of your layout. It contains an image placeholder that should be replaced with your own linked logo~~ */
.header {
background: #6F7D94;
}
/* ~~ This is the layout information. ~~
1) Padding is only placed on the top and/or bottom of the div. The elements within this div have padding on their sides. This saves you from any "box model math". Keep in mind, if you add any side padding or border to the div itself, it will be added to the width you define to create the *total* width. You may also choose to remove the padding on the element in the div and place a second div within it with no width and the padding necessary for your design.
*/
.content {
padding: 10px 0;
}
/* ~~ This grouped selector gives the lists in the .content area space ~~ */
.content ul, .content ol {
padding: 0 15px 15px 40px; /* this padding mirrors the right padding in the headings and paragraph rule above. Padding was placed on the bottom for space between other elements on the lists and on the left to create the indention. These may be adjusted as you wish. */
}
/* ~~ The footer ~~ */
.footer {
padding: 10px 0;
background: #6F7D94;
}
/* ~~ miscellaneous float/clear classes ~~ */
.fltrt { /* this class can be used to float an element right in your page. The floated element must precede the element it should be next to on the page. */
float: right;
margin-left: 8px;
}
.fltlft { /* this class can be used to float an element left in your page. The floated element must precede the element it should be next to on the page. */
float: left;
margin-right: 8px;
}
.clearfloat { /* this class can be placed on a <br /> or empty div as the final element following the last floated div (within the #container) if the #footer is removed or taken out of the #container */
clear:both;
height:0;
font-size: 1px;
line-height: 0px;
}
and html source:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
@import url("style.css") screen;
@import url("menu.css") screen;
//-->
</style>
</head>
<body>
<div class="container">
<div class="header">
<div id="metaltop-lilac">
<ul>
<li class="first"><a href="#">Home Page</a></li>
<li class="active"><a href="#">Our Products</a></li>
<li><a href="#">About Us</a></li>
<li class="last"><a href="#">Contact Us</a></li>
</ul>
</div>
<!-- end .header --></div>
<div class="content">
<p>test</p>
<!-- end .content --></div>
<div class="footer">
<!-- end .footer --></div>
<!-- end .container --></div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
#metaltop-lilac
上,将float: left
更改为float: right
。如果不是这样,请尝试对
#metaltop-lilac ul
进行相同的更改。请务必取消居中技术。
On
#metaltop-lilac
, changefloat: left
tofloat: right
.If not that, try the same change on
#metaltop-lilac ul
.Be sure to remove your centering technique.
如果可以接受按像素定位,则可以设置
position:relative;
,然后设置left: 100px;
(或任何菜单宽度)。这将产生偏移您的菜单距其相对位置右侧 100 像素。If positioning by pixels is acceptable, you can set
position: relative;
and then setleft: 100px;
, (or whatever the width of the menu is.) This will offset your menu 100px to the right of it's relative position.