CSS笔记

CSS固定表头不滚动

  • css:

    1
    2
    3
    css:
    position: sticky;
    top: 0;
  • html:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    <style>
    table {
    border: 1px solid;
    border-collapse: collapse;
    font-family: sans-serif;
    max-width: 100%;
    margin: 0 auto;
    }
    thead {
    background: black;
    color: white;
    position: sticky;
    text-align: left;
    top: 0;
    }
    tbody tr {
    border-bottom: 1px solid;
    }
    th,td {
    padding: 6px 12px;
    }
    td:nth-of-type(6),th:nth-of-type(6) {
    padding-left: 40px;
    }
    td:nth-of-type(4),th:nth-of-type(4),
    td:nth-of-type(5),th:nth-of-type(5){
    text-align: right;
    }
    tr:nth-of-type(even) {
    background: #eee;
    }
    </style>

    <table>
    <thead>
    <tr>
    <th>ID</th>
    <th>Name</th>
    <th>Category</th>
    <th>Price</th>
    <th>Calories</th>
    <th>Description</th>
    </tr>
    </thead>
    <tbody>
    <!-- Row 1 -->
    <tr>
    <td>1</td>
    <td>Pizza</td>
    <td>Italian</td>
    <td>12.99</td>
    <td>800</td>
    <td>Classic Margherita</td>
    </tr>
    <!-- Row 2 -->
    <tr>
    <td>2</td>
    <td>Burger</td>
    <td>American</td>
    <td>8.99</td>
    <td>600</td>
    <td>Cheeseburger with fries</td>
    </tr>
    <!-- Row 3 -->
    <tr>
    <td>3</td>
    <td>Sushi</td>
    <td>Japanese</td>
    <td>22.99</td>
    <td>500</td>
    <td>Assorted sushi rolls</td>
    </tr>
    <!-- Row 4 -->
    <tr>
    <td>4</td>
    <td>Tacos</td>
    <td>Mexican</td>
    <td>10.50</td>
    <td>450</td>
    <td>Beef and chicken tacos</td>
    </tr>
    <!-- Row 5 -->
    <tr>
    <td>5</td>
    <td>Salad</td>
    <td>Healthy</td>
    <td>7.99</td>
    <td>300</td>
    <td>Fresh garden salad</td>
    </tr>
    <!-- Row 6 -->
    <tr>
    <td>6</td>
    <td>Pasta</td>
    <td>Italian</td>
    <td>14.50</td>
    <td>700</td>
    <td>Spaghetti Bolognese</td>
    </tr>
    <!-- Row 7 -->
    <tr>
    <td>7</td>
    <td>Chicken Curry</td>
    <td>Indian</td>
    <td>12.99</td>
    <td>550</td>
    <td>Authentic Indian curry</td>
    </tr>
    <!-- Row 8 -->
    <tr>
    <td>8</td>
    <td>Sandwich</td>
    <td>American</td>
    <td>6.99</td>
    <td>400</td>
    <td>Club sandwich</td>
    </tr>
    <!-- Row 9 -->
    <tr>
    <td>9</td>
    <td>Sushi Bowl</td>
    <td>Japanese</td>
    <td>18.99</td>
    <td>600</td>
    <td>Chirashi bowl</td>
    </tr>
    <!-- Row 10 -->
    <tr>
    <td>10</td>
    <td>Shrimp Scampi</td>
    <td>Italian</td>
    <td>16.99</td>
    <td>550</td>
    <td>Garlic butter shrimp pasta</td>
    </tr>
    <!-- Row 11 -->
    <tr>
    <td>11</td>
    <td>Fish and Chips</td>
    <td>British</td>
    <td>13.50</td>
    <td>700</td>
    <td>Classic British dish</td>
    </tr>
    <!-- Row 12 -->
    <tr>
    <td>12</td>
    <td>Ramen</td>
    <td>Japanese</td>
    <td>11.99</td>
    <td>500</td>
    <td>Tonkotsu ramen</td>
    </tr>
    <!-- Row 13 -->
    <tr>
    <td>13</td>
    <td>Caesar Salad</td>
    <td>Healthy</td>
    <td>8.50</td>
    <td>350</td>
    <td>Classic Caesar salad</td>
    </tr>
    <!-- Row 14 -->
    <tr>
    <td>14</td>
    <td>Steak</td>
    <td>American</td>
    <td>25.99</td>
    <td>800</td>
    <td>Grilled sirloin steak</td>
    </tr>
    <!-- Row 15 -->
    <tr>
    <td>15</td>
    <td>Pho</td>
    <td>Vietnamese</td>
    <td>10.99</td>
    <td>450</td>
    <td>Beef pho</td>
    </tr>
    <!-- Row 16 -->
    <tr>
    <td>16</td>
    <td>Lasagna</td>
    <td>Italian</td>
    <td>16.50</td>
    <td>700</td>
    <td>Homemade meat lasagna</td>
    </tr>
    <!-- Row 17 -->
    <tr>
    <td>17</td>
    <td>Fajitas</td>
    <td>Mexican</td>
    <td>14.99</td>
    <td>600</td>
    <td>Chicken and beef fajitas</td>
    </tr>
    <!-- Row 18 -->
    <tr>
    <td>18</td>
    <td>Caprese Salad</td>
    <td>Italian</td>
    <td>9.99</td>
    <td>350</td>
    <td>Tomato, mozzarella, and basil</td>
    </tr>
    <!-- Row 19 -->
    <tr>
    <td>19</td>
    <td>Pad Thai</td>
    <td>Thai</td>
    <td>12.50</td>
    <td>500</td>
    <td>Traditional Pad Thai</td>
    </tr>
    <!-- Row 20 -->
    <tr>
    <td>20</td>
    <td>Chicken Alfredo</td>
    <td>Italian</td>
    <td>15.99</td>
    <td>600</td>
    <td>Creamy chicken Alfredo pasta</td>
    </tr>
    <!-- Row 21 -->
    <tr>
    <td>21</td>
    <td>Vegetable Stir Fry</td>
    <td>Chinese</td>
    <td>11.50</td>
    <td>400</td>
    <td>Assorted vegetables stir-fried</td>
    </tr>
    <!-- Row 22 -->
    <tr>
    <td>22</td>
    <td>Quinoa Bowl</td>
    <td>Healthy</td>
    <td>9.99</td>
    <td>350</td>
    <td>Quinoa with mixed vegetables</td>
    </tr>
    <!-- Row 23 -->
    <tr>
    <td>23</td>
    <td>Tom Yum Soup</td>
    <td>Thai</td>
    <td>8.50</td>
    <td>250</td>
    <td>Spicy and sour soup with shrimp</td>
    </tr>
    <!-- Row 24 -->
    <tr>
    <td>24</td>
    <td>Bruschetta</td>
    <td>Italian</td>
    <td>7.99</td>
    <td>300</td>
    <td>Tomato and basil bruschetta</td>
    </tr>
    <!-- Row 25 -->
    <tr>
    <td>25</td>
    <td>Teriyaki Salmon</td>
    <td>Japanese</td>
    <td>18.50</td>
    <td>600</td>
    <td>Grilled teriyaki-glazed salmon</td>
    </tr>
    <!-- Row 26 -->
    <tr>
    <td>26</td>
    <td>Chicken Shawarma</td>
    <td>Middle Eastern</td>
    <td>10.99</td>
    <td>500</td>
    <td>Marinated and grilled chicken</td>
    </tr>
    <!-- Row 27 -->
    <tr>
    <td>27</td>
    <td>Cheese Platter</td>
    <td>Appetizer</td>
    <td>14.99</td>
    <td>400</td>
    <td>Assorted cheeses with crackers</td>
    </tr>
    <!-- Row 28 -->
    <tr>
    <td>28</td>
    <td>Mango Sticky Rice</td>
    <td>Thai</td>
    <td>6.50</td>
    <td>300</td>
    <td>Sweet mango with sticky rice</td>
    </tr>
    <!-- Row 29 -->
    <tr>
    <td>29</td>
    <td>Spinach Artichoke Dip</td>
    <td>Appetizer</td>
    <td>8.99</td>
    <td>350</td>
    <td>Creamy spinach and artichoke dip</td>
    </tr>
    <!-- Row 30 -->
    <tr>
    <td>30</td>
    <td>Cherry Cheesecake</td>
    <td>Dessert</td>
    <td>7.50</td>
    <td>450</td>
    <td>Classic cherry-topped cheesecake</td>
    </tr>
    <!-- Row 31 -->
    <tr>
    <td>31</td>
    <td>Chicken Enchiladas</td>
    <td>Mexican</td>
    <td>12.99</td>
    <td>550</td>
    <td>Shredded chicken enchiladas</td>
    </tr>
    <!-- Row 32 -->
    <tr>
    <td>32</td>
    <td>Beef Stroganoff</td>
    <td>Russian</td>
    <td>15.50</td>
    <td>650</td>
    <td>Creamy beef stroganoff with mushrooms</td>
    </tr>
    <!-- Row 33 -->
    <tr>
    <td>33</td>
    <td>Chicken Caesar Wrap</td>
    <td>American</td>
    <td>9.50</td>
    <td>400</td>
    <td>Grilled chicken in a Caesar wrap</td>
    </tr>
    <!-- Row 34 -->
    <tr>
    <td>34</td>
    <td>Avocado Toast</td>
    <td>Healthy</td>
    <td>6.99</td>
    <td>350</td>
    <td>Sliced avocado on toasted bread</td>
    </tr>
    <!-- Row 35 -->
    <tr>
    <td>35</td>
    <td>Lemon Garlic Shrimp</td>
    <td>Seafood</td>
    <td>16.99</td>
    <td>500</td>
    <td>Grilled shrimp with lemon and garlic</td>
    </tr>
    <!-- Row 36 -->
    <tr>
    <td>36</td>
    <td>Tomato Basil Soup</td>
    <td>Soup</td>
    <td>7.50</td>
    <td>300</td>
    <td>Homemade tomato basil soup</td>
    </tr>
    <!-- Row 37 -->
    <tr>
    <td>37</td>
    <td>Cajun Jambalaya</td>
    <td>Cajun</td>
    <td>14.50</td>
    <td>600</td>
    <td>Spicy Cajun rice dish with meat and vegetables</td>
    </tr>
    <!-- Row 38 -->
    <tr>
    <td>38</td>
    <td>Chocolate Fondue</td>
    <td>Dessert</td>
    <td>11.99</td>
    <td>450</td>
    <td>Assorted fruits dipped in chocolate</td>
    </tr>
    <!-- Row 39 -->
    <tr>
    <td>39</td>
    <td>Eggplant Parmesan</td>
    <td>Italian</td>
    <td>13.99</td>
    <td>550</td>
    <td>Baked eggplant with marinara and cheese</td>
    </tr>
    <!-- Row 40 -->
    <tr>
    <td>40</td>
    <td>Chicken Noodle Soup</td>
    <td>Soup</td>
    <td>8.50</td>
    <td>350</td>
    <td>Classic chicken noodle soup</td>
    </tr>
    </tbody>
    </table>

    Please Make Your Table Headings Sticky

    Please Make Your Table Headings Sticky
    Tables with Scrollable Headers