第一个项目编号显示在最右侧,但所有其他项目编号都向左浮动,我如何将 1 放在它应该出现的位置?

The first item number displays on the far right but all others float left, how do I get the 1 where it should be?

link是我一直在做的项目。我遇到的问题是,添加到列表中的第一个项目在最右侧显示数字 1,而它应该像添加的其他列表项目一样向左浮动。如果有人能指出我正确的方向,我也许能弄明白。只需要一点点颠簸。

/*  JavaScript 6th Edition
    Chapter 8
    Hands-on Project 8-4

    Filename: styles.css
*/

/* apply a natural box layout model to all elements */
* {
   -moz-box-sizing: border-box;
   -webkit-box-sizing: border-box;
   box-sizing: border-box;
}

/* reset rules */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
   margin: 0;
   padding: 0;
   border: 0;
   font-size: 100%;
   font: inherit;
   vertical-align: baseline;
}

/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
   display: block;
}

body {
   line-height: 1;
   width: 600px;
   background: white;
   margin: 0 auto;
   font-family: Arial, Helvetica, sans-serif; 
}

ol, ul {
   list-style: none;
}

/* page header */
header {
   background: #FFC340;
   width: 100%;
   color: black;
   font-size: 48px;
   text-align: center;
   line-height: 1.5em;
   border-bottom: 1px solid black;
   border-left: 1px solid black;
   border-right: 1px solid black;
}

/* main content */
article {
   text-align: left;
   background: white;
   overflow: auto;
   border-bottom: 1px solid black;
   border-left: 1px solid black;
   border-right: 1px solid black;
}

article h2 {
   font-weight: bold;
   font-size: 24px;
   padding: 1em;
   padding-bottom: 0;
   float: left;
}

div {
   padding: 1em;
   border: none;
   text-align: left;
}

div p {
   font-weight: bold;
   margin-bottom: 0.4em;
}

ol {
   list-style-type: decimal;
   padding-left: 1em;
}

li {
   margin: 0 0 1em 1em;
}

li span.first {
   background: green;
   color: white;
   padding: 2px 5px;
   margin-right: 3px;
   -moz-border-radius: 5px;
   -webkit-border-radius: 5px;
   border-radius: 5px;
}

li span.last {
   background: red;
   color: white;
   padding: 2px 5px;
   margin-right: 3px;
   -moz-border-radius: 5px;
   -webkit-border-radius: 5px;
   border-radius: 5px;
}

/* form */
form {
   background: white;
   padding: 5px;
   float: left;
  }

fieldset {
   padding: 1em;
   border: none;
}

input {
   font-size: 1em;
   float: left;
   margin: 0 auto;
}

#newItem {
   width: 15em;
}

label {
   display: block;
   font-weight: bold;
   text-align: left;
}

input, label, button {
   margin: 5px 10px;
}
<article>
    <h2>To Do List</h2>
    <form>
        <fieldset>
            <input type="text" id="newItem">
            <input type="button" id="button" value="Add Item">
        </fieldset>
    </form>
    <div id="results">
        <ol><li><span class="first">first</span><span class="last">last</span></li><li><span class="first">first</span><span class="last">last</span></li><li><span class="first">first</span><span class="last">last</span></li><li><span class="first">first</span><span class="last">last</span></li></ol>
    </div>
</article>

因为表单是浮动的

如果您删除浮动(这可能是不必要的),第一个列表项编号将出现在正确的位置。

尝试添加这个:

#results { clear: left; }

在您的 css 文件中 styles.css

将您的 css class 定义更改为以下内容。

/* form */
form {
   background: white;
   padding: 5px;
}

演示:

        
        /*  JavaScript 6th Edition
    Chapter 8
    Hands-on Project 8-4

    Filename: styles.css
*/

/* apply a natural box layout model to all elements */
* {
   -moz-box-sizing: border-box;
   -webkit-box-sizing: border-box;
   box-sizing: border-box;
}

/* reset rules */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
   margin: 0;
   padding: 0;
   border: 0;
   font-size: 100%;
   font: inherit;
   vertical-align: baseline;
}

/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
   display: block;
}

body {
   line-height: 1;
   width: 600px;
   background: white;
   margin: 0 auto;
   font-family: Arial, Helvetica, sans-serif; 
}

ol, ul {
   list-style: none;
}

/* page header */
header {
   background: #FFC340;
   width: 100%;
   color: black;
   font-size: 48px;
   text-align: center;
   line-height: 1.5em;
   border-bottom: 1px solid black;
   border-left: 1px solid black;
   border-right: 1px solid black;
}

/* main content */
article {
   text-align: left;
   background: white;
   overflow: auto;
   border-bottom: 1px solid black;
   border-left: 1px solid black;
   border-right: 1px solid black;
}

article h2 {
   font-weight: bold;
   font-size: 24px;
   padding: 1em;
   padding-bottom: 0;
   float: left;
}

div {
   padding: 1em;
   border: none;
   text-align: left;
}

div p {
   font-weight: bold;
   margin-bottom: 0.4em;
}

ol {
   list-style-type: decimal;
   padding-left: 1em;
}

li {
   margin: 0 0 1em 1em;
}

li span.first {
   background: green;
   color: white;
   padding: 2px 5px;
   margin-right: 3px;
   -moz-border-radius: 5px;
   -webkit-border-radius: 5px;
   border-radius: 5px;
}

li span.last {
   background: red;
   color: white;
   padding: 2px 5px;
   margin-right: 3px;
   -moz-border-radius: 5px;
   -webkit-border-radius: 5px;
   border-radius: 5px;
}

/* form */
form {
   background: white;
   padding: 5px;
  }

fieldset {
   padding: 1em;
   border: none;
}

input {
   font-size: 1em;
   float: left;
   margin: 0 auto;
}

#newItem {
   width: 15em;
}

label {
   display: block;
   font-weight: bold;
   text-align: left;
}

input, label, button {
   margin: 5px 10px;
}
<article>
    <h2>To Do List</h2>
    <form>
        <fieldset>
            <input type="text" id="newItem">
            <input type="button" id="button" value="Add Item">
        </fieldset>
    </form>
    <div id="results">
        <ol><li><span class="first">first</span><span class="last">last</span></li><li><span class="first">first</span><span class="last">last</span></li><li><span class="first">first</span><span class="last">last</span></li><li><span class="first">first</span><span class="last">last</span></li></ol>
    </div>
</article>

如前所述,问题是列表上方的浮动元素没有被清除。

要解决此问题,只需在 <form><ol> 之间插入 <br clear="left" />

https://jsfiddle.net/nLpqzua9/1/

如果您想避免不必要的额外标记,请在 div#results 上应用 clear: left;

https://jsfiddle.net/nLpqzua9/2/