前端开发过程小知识点记录(开发过程实时更新)

1.#main div 距离页面顶部和左边有间距,但是并没有人为设置

出现现象图:

 原因:大多数浏览器会为 body 和 html 元素添加默认的外边距。可以通过重置这些样式来解决此问题。

解决办法:在css中清除浏览器的默认样式

html, body { margin: 0; /* 清除默认外边距 */ padding: 0; /* 清除默认填充 */ }

2.div 的flex 样式编写

如果要让一个父div内部的多个子div能水平排布,则:

 1         <div id="top">
 2                     <div id="top_left">
 3                         <div id="top_left_content">
 4                         </div>
 5                     </div>
 6                     <div id="top_middle">
 7                         <div id="top_middle_content">
 8                         </div>
 9                     </div>
10                     <div id="top_right">
11                         <div id="top_right_content"></div>
12                     </div>
13                 </div>
 1 #top {
 2   /* border: 0.5px solid rgb(0, 81, 255); */
 3   width: 99.8%;
 4   height: 14%;
 5   
 6   background-image: url("../images/01_top_background.png");
 7   background-size: cover;
 8   background-repeat: no-repeat;
 9   /* background-position: center; */
10   background-position: top left;
11 
12   display: flex;
13   /* 设置为 Flexbox 容器 */
14   align-items: center;
15   /* 在交叉轴(垂直方向)上居中对齐子元素 */
16 
17 }
18 #top_left {
19   flex: 0 0 25%;
20   /* 不增长, 不缩小, 基础宽度为 30% */
21   height: 100%;
22   /* 高度与父元素相同 */
23   /* background-color: rgba(255, 0, 0, 0.5); /* 红色背景 */
24   border: 1px solid red;
25 }
26 
27 #top_middle {
28   flex: 0 0 45%;
29   /* 不增长, 不缩小, 基础宽度为 40% */
30   height: 100%;
31   /* 高度与父元素相同 */
32   background-color: rgba(0, 255, 0, 0.5);
33   /* 绿色背景 */
34 }
35 
36 #top_right {
37   flex: 0 0 30%;
38   /* 不增长, 不缩小, 基础宽度为 30% */
39   height: 100%;
40   /* 高度与父元素相同 */
41   background-color: rgba(0, 0, 255, 0.5);
42   /* 蓝色背景 */
43   border: 1px solid red;
44 }

3.一个div内的背景图,正常浏览器访问正常,当浏览器全屏显示时左侧部分图片超出不显示问题解决

background-position: center;修改为:background-position: top left;

 

完整代码:

 1 #top {  
 2     width: 99.8%;  
 3     height: 14%;  
 4     
 5     background-image: url("../images/01_top_background.png");  
 6     background-size: cover; /* 保持覆盖 */  
 7     background-repeat: no-repeat;  
 8     background-position: top left; /* 从左上角开始显示 */  
 9 
10     display: flex;  
11     align-items: center;  
12 }

 

posted @ 2025-02-14 14:27  上清风  阅读(17)  评论(0)    收藏  举报