如何强制列表项与CSS一行显示?

时间:2015-09-18 13:53:22

标签: html css html-lists

我正在使用列表项作为“假菜单项”,如下所示:

 <li>Match
    <ul class="second-level-menu">
      <li class="inlineblock" id="mniMatchWorkersWithJobLocs" name="mniMatchWorkersWithJobLocs">Workers with Jobs/Locations</li>
      <li id="mniMatchWorkersWithShifts" name="mniMatchWorkersWithShifts">Workers with Shifts</li>
    </ul>
  </li>

问题在于较长的子菜单项,例如“有工作/地点的工人”正在突破两行;我希望他们留在一条线上。

我伪聪明地认为仅仅分配li这个类就足够了:

.inlineblock {
  display: inline-block;
}

......但它没有削减芥末。我错过了什么或者在想什么?

更新

根据答案,我现在有了这个CSS:

.inlineblock {
  display: inline-block;
}
li.inlineblock ul li{
  display: inline-block;
}

...而且这个HTML:

  <li class="inlineblock">Match
    <ul class="second-level-menu">
      <li id="mniMatchWorkersWithJobLocs" name="mniMatchWorkersWithJobLocs">Workers with Jobs/Locations</li>
      <li id="mniMatchWorkersWithShifts" name="mniMatchWorkersWithShifts">Workers with Shifts</li>
    </ul>
  </li>

......但我仍然看到了这一点:

enter image description here

以下是虚假菜单的完整HTML:

<template name="mnuScheduler">
  <div class="grid">
    <div class="col-All">
      <nav>
        <ul class="top-level-menu">
          <li>Schedules
            <ul class="second-level-menu">
              <li id="mniOpenExisting" name="mniOpenExisting">Open Existing</li>
              <li>Create New...
                <ul class="third-level-menu">
                  <li id="mniCreateNewScheduleBasedOnExisting" name="mniCreateNewScheduleBasedOnExisting">Based on Existing</li>
                  <li id="mniCreateNewScheduleFromScratch" name="mniCreateNewScheduleFromScratch">From Scratch</li>
                </ul>
              </li>
              <li id="mniSaveCurrentSchedule" name="mniSaveCurrentSchedule">Save Current</li>
              <li id="mniEmailCurrentSchedule" name="mniEmailCurrentSchedule">Email Current</li>
              <li id="mniPrintCurrentSchedule" name="mniPrintCurrentSchedule">Print Current</li>
            </ul>
          </li>
          <li>Jobs/Locations
            <ul class="second-level-menu">
              <li id="mniAddNewJobLoc" name="mniAddNewJobLoc">Add New</li>
              <li id="mniViewOrEditJobLoc" name="mniViewOrEditJobLoc">View or Edit</li>
            </ul>
          </li>
          <li>Shifts
            <ul class="second-level-menu">
              <li id="mniAddNewShift" name="mniAddNewShift">Add New</li>
              <li id="mniViewOrEditShift" name="mniViewOrEditShift">View or Edit</li>
            </ul>
          </li>
          <li>Workers
            <ul class="second-level-menu">
              <li id="mniAddNewWorker" name="mniAddNewWorker">Add New</li>
              <li id="mniViewOrEditWorker" name="mniViewOrEditWorker">View or Edit</li>
            </ul>
          </li>
          <li class="inlineblock">Match
            <ul class="second-level-menu">
              <li id="mniMatchWorkersWithJobLocs" name="mniMatchWorkersWithJobLocs">Workers with Jobs/Locations</li>
              <li id="mniMatchWorkersWithShifts" name="mniMatchWorkersWithShifts">Workers with Shifts</li>
            </ul>
          </li>
          <li>Rules
            <ul class="second-level-menu">
              <li id="mniSetRules" name="mniSetRules">Establish/Maintain</li>
            </ul>
          </li>
          <li>Help
            <ul class="second-level-menu">
              <li id="mniAbout" name="mniAbout">About</li>
              <li id="mniHowTo" name="mniHowTo">How To...</li>
              <li id="mniContact" name="mniContact">Contact Us</li>
              <li id="mniAcquireLicense" name="mniAcquireLicense">Acquire License</li>
            </ul>
          </li>
        </ul>
      </nav>
    </div>
  </div>
</template>

更新2

此:

CSS:
.inlineblock {
  white-space: nowrap;
}
li.inlineblock ul li{
  display: inline-block;
}

HTML:

  <li class="inlineblock">Match
    <ul class="second-level-menu">
      <li id="mniMatchWorkersWithJobLocs" name="mniMatchWorkersWithJobLocs">Workers with Jobs/Locations</li>
      <li id="mniMatchWorkersWithShifts" name="mniMatchWorkersWithShifts">Workers with Shifts</li>
    </ul>
  </li>

...导致两个子菜单项出现在同一行(前进一步,后退两步)

此:

/*.inlineblock {
  white-space: nowrap;
}
li.inlineblock ul li{
  display: inline-block;
}*/
li {
  white-space: nowrap;
}

...确实将两个菜单项放在他们自己的单独行上,但第一个是“切断”(它显示为“有工作/劳动的工人”)

更新3

回应Joseph Marikle的要求,这里是所有的CSS:

html {
  font-family:'Segoe UI Light', 'Bookman Old Style', Calibri, Candara, serif;
}
header {
  background: #d2edf4;
  background-image: linear-gradient(to bottom, #d0edf5, #e1e5f0 100%);
  padding: 20px 15px 15px 15px;
  position: relative;
}
/*body {
  font-family: 'Palatino Linotype', 'Bookman Antigua', Palatino, serif;
  background-color: lightyellow;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;

  font-size: 14px;

  width:80%;
  margin-left:auto;
  margin-right:auto;
  padding: 10px 50px 200px;
}*/
body {
  height: 100%;
  min-height: 100%;
  width: 80%;
  margin: 0 auto;
  padding: 50px 48px 0;
  line-height: 1;
  color: #333;
  background: #FFF;
  font-family: ff-tis-web-pro, 'Palatino Linotype', 'Bookman Antigua', Palatino, Georgia, serif;
  -webkit-text-size-adjust: 100%;
}
nav {
    background-color: forestgreen;
}
form {
    background-color: lightyellow;
}
h1 {
  color: navy;
  font-family: "Segoe UI", serif;
  font-size: 2.5em;
  margin: 0;
  margin-bottom: 10px;
  display: inline-block;
  margin-right: 1em;
}
h2 {
  font-family: 'Bookman Old Style', Verdana, sans-serif;
}
input:focus {
  background-color: lightyellow;
}
input:hover {
  background-color: azure;
}
table {
  width: 1200px;
  margin: 0 auto;
  background-color: azure;
  /*min-height: 100%;*/
  margin-top: 80px;
}
table, th, td {
    border: 1px solid navy;
    border-collapse: collapse;
}
th, td {
    padding: 5px;
    /*text-align: left;*/
}
input[type="text"] {
  margin-bottom: 4px;
}
input[type="submit"] {
  margin-top: 8px;
}
input[type="time"] {
  margin: 3px;
}
p {
  font-size: 14px;
}

[class*='col-'] {
    float: left;
    margin: 4px;
}
.grid {
  background: white;
}
.module {
  background: lightyellow;
}
.col-All {
    width: 90%;
}
.col-2-3 {
    width: 60%;
}
.col-1-3 {
    width: 30%;
}
.col-1-2 {
    width: 45%;
}
.col-1-4 {
    width: 22.5%;
}

/* for aligning input texts that follow labels on forms and such; can create other sizes too (label120,etc.) */
.label88 {
    width: 88px;
    display: inline-block;
}
.label144 {
    width: 144px;
    display: inline-block;
}
.joblocoptionallabel {
  width: 124px;
  display: inline-block;
}
/*.inlineblock {
  white-space: nowrap;
}
li.inlineblock ul li{
  display: inline-block;
}*/
li {
  white-space: nowrap;
}
.hide {
  visibility: hidden;
  display: none;
}
.smallcaps {
    font-variant: small-caps;
}
.container {
  max-width: 1200px;
  margin: 0 auto;
  background: lightyellow;
}
.dateLabel {
  font-size: 16px;
  font-family: Candara, 'Segoe UI', sans-serif;
  color: forestgreen;
}
.shiftLabel {
  font-size: 13px;
  font-family: Calibri, serif;
  color: red;
}
.jobLoc {
  margin-right: 16px;
  width: 115px;
}
.jobLocCount {
  width: 40px;
}
.trabajar {
  width: 159px;
  margin-right: 16px;
  margin-top: 4px;
}
.shortTextInput {
    width: 64px;
}
.floatleft {
    float: left;
    display: block;
}
/* Menu-specific styles/rules, adapted from  */
.third-level-menu {
    position: absolute;
    top: 0;
    right: -150px;
    width: 150px;
    list-style: none;
    padding: 0;
    margin: 0;
    display: none;
}
.third-level-menu > li {
    height: 30px;
    background: gray;
}
.third-level-menu > li:hover {
    background: white;
}
.second-level-menu {
    position: absolute;
    top: 30px;
    left: 0;
    width: 150px;
    list-style: none;
    padding: 0;
    margin: 0;
    display: none;
}
.second-level-menu > li {
    position: relative;
    height: 30px;
    background: orange;
    color: white;
}
.second-level-menu > li:hover {
    background: green;
}
.top-level-menu {
    list-style: none;
    padding: 0;
    margin-bottom: 12px;
    width: 100%;
    display:block;
    /*border: 1px; <= not working*/
}
.top-level-menu > li {
    position: relative;
    float: left;
    height: 30px;
    width: 150px;
    background: moccasin;
}
.top-level-menu > li:hover {
    background: lightgray;
}
.top-level-menu li:hover > ul {
    /* On hover, display the next level's menu */
    display: inline;
}
/* End of Menu-specific Styles */

6 个答案:

答案 0 :(得分:1)

你只需要给他们两个班级,

 <li>Match
    <ul class="second-level-menu">
      <li class="inlineblock" id="mniMatchWorkersWithJobLocs" name="mniMatchWorkersWithJobLocs">Workers with Jobs/Locations</li>
      <li class="inlineblock" id="mniMatchWorkersWithShifts" name="mniMatchWorkersWithShifts">Workers with Shifts</li>
    </ul>
  </li>

答案 1 :(得分:1)

这样就可以阻止您始终添加该类,并将列表中的所有符号转换为内联。

HTML

<li class="inlineblock">Match
    <ul class="second-level-menu">
      <li id="mniMatchWorkersWithJobLocs" name="mniMatchWorkersWithJobLocs">Workers with Jobs/Locations</li>
      <li id="mniMatchWorkersWithShifts" name="mniMatchWorkersWithShifts">Workers with Shifts</li>
    </ul>
  </li>

CSS

li.inlineblock ul li{
  display: inline-block;
}

Code Example

如果你想让Matdch在同一条线上。

CSS

li.inlineblock,li.inlineblock ul,li.inlineblock ul li{
  display: inline-block;
}

Code Example

答案 2 :(得分:1)

我建议将你的文本元素放在一个块元素中,例如锚点,段落,标签等......标签,否则你可以使用css来表示非破坏行:

li
{
   white-space:nowrap
}

答案 3 :(得分:1)

使用&#39; white-space:nowrap;&#39;在你的Css。

.inlineblock {
    white-space: nowrap;
}

我建议不要习惯在将来使用这样的类名,这可能会导致混乱。名字喜欢&#34; foo&#34;和&#34; bar&#34;总是工作。

答案 4 :(得分:1)

您应该将该类应用于列表本身,而不是每个列表项。

<强> HTML:

<li class="inlineblock">Match
    <ul class="second-level-menu">
      <li id="mniMatchWorkersWithJobLocs" name="mniMatchWorkersWithJobLocs">Workers with Jobs/Locations</li>
      <li id="mniMatchWorkersWithShifts" name="mniMatchWorkersWithShifts">Workers with Shifts</li>
    </ul>
  </li>

这将确保所有项目都以块显示。

<强> CSS:

li.inlineblock ul li{
  display: inline-block;
}

这将在匹配列表中设置无序列表的列表项,以便像您希望的那样以内嵌方式显示。

答案 5 :(得分:1)

最初的问题是列表项目在菜单中包裹,理想情况下应该将全名保持在一行上。第一个提出的解决方案是应用function concatenateInjections(id, ar, scrpt){ if( typeof scrpt !== 'undefined' ) ar = ar.concat([scrpt]); var i = ar.length; var idx = 0 ; (function (){ var that = arguments.callee; idx++; if(idx <= i){ var f = ar[idx-1]; chrome.tabs.executeScript(id, { file: f }, function(){ that(idx);} ); } })(); } ,但在这个特定的例子中,原始海报有一个菜单可以切断溢出(使用white-space:nowrap)。为了克服这个问题,只需更改嵌入容器列表的宽度(一个简单的overflow:hidden元素)就简单得多了。最初设置宽度ul可能是为了确保菜单看起来与具有短名称的项目的子菜单项一致。通过在150px设置(或留下)宽度并给它auto,可以实现同样的效果而不会丢失大量的额外宽列表。这是有效的,因为子菜单是相对于父项目的。 100%将与父min-width: 100%;的宽度相同。

TL; DR :总之,删除宽度并添加100%的最小宽度解决了OP的问题。

代码包含以下更改:

li
html {
  font-family:'Segoe UI Light', 'Bookman Old Style', Calibri, Candara, serif;
}
header {
  background: #d2edf4;
  background-image: linear-gradient(to bottom, #d0edf5, #e1e5f0 100%);
  padding: 20px 15px 15px 15px;
  position: relative;
}
/*body {
  font-family: 'Palatino Linotype', 'Bookman Antigua', Palatino, serif;
  background-color: lightyellow;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;

  font-size: 14px;

  width:80%;
  margin-left:auto;
  margin-right:auto;
  padding: 10px 50px 200px;
}*/
body {
  height: 100%;
  min-height: 100%;
  width: 80%;
  margin: 0 auto;
  padding: 50px 48px 0;
  line-height: 1;
  color: #333;
  background: #FFF;
  font-family: ff-tis-web-pro, 'Palatino Linotype', 'Bookman Antigua', Palatino, Georgia, serif;
  -webkit-text-size-adjust: 100%;
}
nav {
    background-color: forestgreen;
}
form {
    background-color: lightyellow;
}
h1 {
  color: navy;
  font-family: "Segoe UI", serif;
  font-size: 2.5em;
  margin: 0;
  margin-bottom: 10px;
  display: inline-block;
  margin-right: 1em;
}
h2 {
  font-family: 'Bookman Old Style', Verdana, sans-serif;
}
input:focus {
  background-color: lightyellow;
}
input:hover {
  background-color: azure;
}
table {
  width: 1200px;
  margin: 0 auto;
  background-color: azure;
  /*min-height: 100%;*/
  margin-top: 80px;
}
table, th, td {
    border: 1px solid navy;
    border-collapse: collapse;
}
th, td {
    padding: 5px;
    /*text-align: left;*/
}
input[type="text"] {
  margin-bottom: 4px;
}
input[type="submit"] {
  margin-top: 8px;
}
input[type="time"] {
  margin: 3px;
}
p {
  font-size: 14px;
}

[class*='col-'] {
    float: left;
    margin: 4px;
}
.grid {
  background: white;
}
.module {
  background: lightyellow;
}
.col-All {
    width: 90%;
}
.col-2-3 {
    width: 60%;
}
.col-1-3 {
    width: 30%;
}
.col-1-2 {
    width: 45%;
}
.col-1-4 {
    width: 22.5%;
}

/* for aligning input texts that follow labels on forms and such; can create other sizes too (label120,etc.) */
.label88 {
    width: 88px;
    display: inline-block;
}
.label144 {
    width: 144px;
    display: inline-block;
}
.joblocoptionallabel {
  width: 124px;
  display: inline-block;
}
/*.inlineblock {
  white-space: nowrap;
}
li.inlineblock ul li{
  display: inline-block;
}*/
li {
  white-space: nowrap;
}
.hide {
  visibility: hidden;
  display: none;
}
.smallcaps {
    font-variant: small-caps;
}
.container {
  max-width: 1200px;
  margin: 0 auto;
  background: lightyellow;
}
.dateLabel {
  font-size: 16px;
  font-family: Candara, 'Segoe UI', sans-serif;
  color: forestgreen;
}
.shiftLabel {
  font-size: 13px;
  font-family: Calibri, serif;
  color: red;
}
.jobLoc {
  margin-right: 16px;
  width: 115px;
}
.jobLocCount {
  width: 40px;
}
.trabajar {
  width: 159px;
  margin-right: 16px;
  margin-top: 4px;
}
.shortTextInput {
    width: 64px;
}
.floatleft {
    float: left;
    display: block;
}
/* Menu-specific styles/rules, adapted from  */
.third-level-menu {
    position: absolute;
    top: 0;
    right: -150px;
    width: 150px;
    list-style: none;
    padding: 0;
    margin: 0;
    display: none;
}
.third-level-menu > li {
    height: 30px;
    background: gray;
}
.third-level-menu > li:hover {
    background: white;
}
.second-level-menu {
    position: absolute;
    top: 30px;
    left: 0;
    /*width: 150px;*/
    min-width: 100%;
    list-style: none;
    padding: 0;
    margin: 0;
    display: none;
}
.second-level-menu > li {
    position: relative;
    height: 30px;
    background: orange;
    color: white;
}
.second-level-menu > li:hover {
    background: green;
}
.top-level-menu {
    list-style: none;
    padding: 0;
    margin-bottom: 12px;
    width: 100%;
    display:block;
    /*border: 1px; <= not working*/
}
.top-level-menu > li {
    position: relative;
    float: left;
    height: 30px;
    width: 150px;
    background: moccasin;
}
.top-level-menu > li:hover {
    background: lightgray;
}
.top-level-menu li:hover > ul {
    /* On hover, display the next level's menu */
    display: inline;
}
/* End of Menu-specific Styles */