1.单行文本过长
<style> body, h3 { margin: 0; padding: 0; } .box { width: 150px; height: 150px; position: relative; margin: 40px auto; } .box h3 { height: 30px; line-height: 30px; font-weight: 100; width: 100%; background-color: rgba(0, 0, 0, 0.5); font-size: 16px; color: #fff; position: absolute; bottom: 0; text-align: center; /*以防万一,标题过长时,用...省略号显示*/ white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } </style> <body> <div class="box"> <img src="images/flex-06.jpg" alt="" /> <h3>"以防万一"标题过长产生的问题</h3> </div> </body>
2.类别标签中文本过长
<style> .box { width: 250px; height: 250px; position: relative; } .box span { position: absolute; background-color: rgba(119, 245, 197, 0.8); line-height: 1.3; font-size: 12px; padding: 5px 10px; border-radius: 0 50px 50px 0px; left: 0px; top: 5px; /*以防万一标签内容过长,控制最大宽度,内容过多折行显示*/ max-width: 70%; } </style> </head> <body> <div class="box"> <img src="images/ms.jpg" alt="" /> <span>植物奶油 巧克力 草莓 榴莲 花生 芝麻 小米 鸡蛋</span> </div> </body>
3.防止图片挤压拉伸
<style> .box { width: 200px; height: 200px; border-radius: 50%; overflow: hidden; } .box img { width: 100%; height: 100%; /*保持图片原有尺寸来裁剪*/ object-fit: cover; } </style> <body> <div class="box"> <img src="images/tx2.png" alt="" /> </div> </body>
4.图片加载失败文字显示问题
<style> .box { width: 250px; height: 156px; position: relative; } .box img { width: 100%; height: 100%; object-fit: cover; /*"以防万一"图片加载失败,加上背景色,保证文字能正常显示*/ background-color: #666; } .box h3 { width: 100%; font-size: 20px; text-align: center; position: absolute; top: 40px; color: #fff; } </style> <body> <div class="box"> <img src="images/rotate3.webp" alt="" /> <h3>美丽的风景图</h3> </div> </body>
5.必要时显示滚动条
<style> .box { width: 160px; padding: 20px; height: 200px; background-color: skyblue; line-height: 2; border-radius: 20px; } .box .content { padding-right: 10px; max-height: 100%; /*以防万一,用户内容不足时,不需要显示滚动条,只有内容溢出时才显示*/ overflow-y: auto; } /* 整个滚动条*/ .content::-webkit-scrollbar { width: 5px; padding: 0px 20px; } /* 滚动条轨道*/ .content::-webkit-scrollbar-track { border-radius: 10px; background-color: #000; margin: 20px 0px; } /*滚动条上的滚动滑块*/ .content::-webkit-scrollbar-thumb { width: 14px; border-radius: 10px; background-color: #ddd; } </style> <body> <div class="box"> <div class="content"> 在内容比较长的情况下,可以通过设置 overflow-y控制滚动条是否展示。但是这里更推荐将 </div> </div> </body>
6.预留滚动条空间 防止重排
<style> .box { width: 160px; padding: 20px; height: 200px; background-color: skyblue; line-height: 2; border-radius: 20px; } .box .content { max-height: 100%; /*以防万一,用户内容不足时,不需要显示滚动条,只有内容溢出时才显示*/ overflow-y: auto; /*预留好滚动条位置,必免引起重排*/ scrollbar-gutter: stable; } .content::-webkit-scrollbar { width: 10px; } .content::-webkit-scrollbar-track { border-radius: 10px; background-color: #000; margin: 20px 0px; } .content::-webkit-scrollbar-thumb { width: 14px; border-radius: 10px; background-color: #ddd; } </style> <body> <div class="box"> <div class="content"> 当内容不足时不会出现滚动条,文字占据的宽度要宽些。当内容溢出出现滚动条时,因为滚动条要占据一部分空间,则会造成文字占据的空间变窄,因而会造成重排。 </div> </div> </body>
7.锁定滚动条
<style> body { height: 2000px; } .box { height: 400px; width: 200px; margin: 0px auto; overflow-y: auto; background-color: skyblue; /*当滚动到滚动条底部和顶部时,会触发父元素的滚动条滚动*/ overscroll-behavior-y: contain; } </style> <body> <div class="box"> <p>1</p> <p>2</p> <p>3</p> <p>4</p> <p>5</p> <p>6</p> <p>7</p> <p>8</p> <p>9</p> <p>10</p> <p>11</p> <p>12</p> <p>13</p> <p>14</p> <p>15</p> <p>16</p> <p>17</p> <p>18</p> <p>19</p> <p>20</p> <p>21</p> <p>22</p> <p>23</p> <p>24</p> <p>25</p> <p>26</p> <p>27</p> <p>28</p> <p>29</p> <p>30</p> <p>31</p> <p>32</p> </div> </body>
8.flex布局中,元素使用space-between最后一行两边分布的问题?
<style> .container { width: 500px; display: flex; /*弹性布局*/ justify-content: space-between; /*两端对齐*/ flex-wrap: wrap; /*超出部分换行*/ } .item { width: 120px; height: 100px; background-color: pink; margin-top: 10px; } .container span { width: 120px; /*span宽和子项宽一样*/ } </style> </head> <body> <div class="container"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="item">5</div> <div class="item">6</div> <div class="item">7</div> <!--以防万一,子项个数不够,最后一排出现两端对齐,达不到预期效果--> <span></span> <span></span> </div> </body>
9.grid布局相应断点
<style> .container { width: 100%; display: grid; /*grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));*/ /*以防万一,子项不足占据一行时*/ grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); grid-template-rows: 250px; grid-auto-flow: row; grid-auto-rows: 250px; gap: 10px; } .container .item:nth-child(even) { background-color: skyblue; } .container .item:nth-child(odd) { background-color: pink; } </style> <body> <div class="container"> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> </div> </body>
相关标签: