简体   繁体   English

导航栏:左右对齐内联文本

[英]nav-bar: align inline text, left and right

Basically, I want "Name" to go to the left of the nav-bar, while "About" "Contact" and "Account" remain on the right of the nav-bar. 基本上,我希望“名称”位于导航栏的左侧,而“关于”,“联系人”和“帐户”则保留在导航栏的右侧。

https://jsfiddle.net/etw2q6sL/1/ https://jsfiddle.net/etw2q6sL/1/

.name {
  font-family: "ChunkFive"; /* works */
  font-size: 22px;          /* works */
  color: #32B432;           /* doesn't */
  text-align: left;         /* doesn't */
}

I used li class="name" , within a ul , within a div id , within a nav class . 我在ul中 ,在div id中 ,在nav class中使用了li class =“ name”

I don't know which parent or child styles to change. 我不知道要更改哪些父母或孩子的样式。 (I wanted to change the color of "Name" to green too but it wasn't working either). (我也想将“名称”的颜色也更改为绿色,但是它也不起作用)。

Here's an update to your Fiddle that should do what you want: https://jsfiddle.net/etw2q6sL/4/ 这是您的小提琴的更新,可以执行您想要的操作: https : //jsfiddle.net/etw2q6sL/4/

The colour rule not working is because your other rule #mainNav a specifies a colour for all the links in the nav, and this is more specific than your .name rule, so the browser chooses the colour from the former. 颜色规则不起作用是因为您的另一个规则#mainNav a为导航中的所有链接指定了一种颜色,并且比.name规则更具体,因此浏览器从前一个中选择颜色。 To make this work as you want, I would make the .name rule more specific by adding extra conditions to its selector - eg change it to #mainNav .name a . 为了使这项工作如您所愿,我将通过在其选择器中添加其他条件来使.name规则更加具体-例如,将其更改为#mainNav .name a To learn more about CSS specificity (the order in which rules are applied) check this out: https://css-tricks.com/specifics-on-css-specificity/ 要了解有关CSS特殊性(应用规则的顺序)的更多信息,请查看以下内容: https : //css-tricks.com/specifics-on-css-specificity/

On the alignment issue, you're confusing text alignment with placement of your list items. 在对齐问题上,您将文本对齐与列表项的位置混淆了。 text-align: left will simply take align any inline content (such as text, spans, etc.) and align it to the left in the current list item - it won't align the list item to the left in the nav. text-align: left将简单地使所有内联内容(例如文本,跨度等)对齐,并将其在当前列表项中向左对齐-不会将列表项在导航中向左对齐。

Instead what you want for this is a float . 相反,您想要的是一个float What I've done is separate out your list into two separate uls, one which has a float: left applied and the other a float: right . 我所做的是将您的列表分为两个单独的ul,一个包含float: left ,另一个带有float: right These floats pull the two lists to the left and right side of the nav. 这些浮标将两个列表拉到导航的左侧和右侧。 This article may help you learn about using floats with a navbar like you're making: http://www.cssnewbie.com/super-simple-horizontal-navigation-bar/ 本文可能会帮助您了解如何在导航栏上使用浮标,就像您正在制作的一样: http : //www.cssnewbie.com/super-simple-horizo​​ntal-navigation-bar/

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

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