图片右侧清除外边距 文字压盖 代码优化

图片右侧清除外边距 文字压盖 代码优化

相关代码:

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
<!DOCTYPE html>
<html lang="en">
 
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>练习</title>
<link rel="stylesheet" href="css/css.css">
</head>
 
<body>
<!-- 网页头部 -->
<header>
<div class="center">
<!-- logo -->
<div class="logo">
<img src="images/logo.png">
</div>
<!-- 导航条-->
<div class="nav">
<ul>
<li>HOME</li>
<li>ABOUT</li>
<li>GALLERY</li>
<li>FACULTY</li>
<li>EVENTS</li>
<li>CONTACT</li>
</ul>
</div>
</div>
</header>
 
<!-- 网页横幅 -->
<section class="banner">
<!-- 放大的图片 -->
<img src="images/banner3.jpg">
<!-- 遮罩层 -->
<div class="banner-mask"></div>
<!-- 表单 -->
<div class="banner-form">
<form action="" method="post">
<input type="text" placeholder="your Name">
<input type="text" placeholder="your Phone">
<input type="email" placeholder="your Email">
<textarea placeholder="Write Your Comment Here" rows="4" cols="50"></textarea>
<p>
<input type="submit" value="SEND MESSAGE">
</p>
</form>
</div>
</section>
 
<!-- 网页的主要内容 -->
<section class="content">
<!-- 网页about区域 -->
<div class="about">
<!-- about上半部分 -->
<div class="up">
<h1>ABOUT</h1>
<div class="line"></div>
<span>
Ipsum is simply dummy text of the printing and typesetting<br>
industry. Lorem Ipsum has been the industry's standard dummy<br>
text ever since the 1500s.
</span>
</div>
<!-- about下半部分 -->
<div class="down">
<!-- 下半部分左侧 -->
<div class="left">
<h1>WORD<br>ABOUT US</h1>
<div class="jdms">
<p>
Praesent dignissim viverra est, sed bibendum ligula congue non. Sed ac nisl et felis gravida
commodo? Suspendisse eget ullamcorper ipsum. Suspendisse diam amet.
</p>
<button>EXPLORE</button>
</div>
</div>
<!-- 下半部分中间 -->
<div class="center">
<img src="IMAGES/bb3.jpg">
</div>
<!-- 下半部分右侧 -->
<div class="right">
<div class="top">
<p>70000</p>
<div class="line-top"></div>
Students
</div>
<div class="down">
<p>600</p>
<div class="line-down"></div>
Faculty
</div>
</div>
</div>
</div>
<!-- 网页图文混排区域 -->
<div class="image-text">
<div class="image1">
<img src="images/b1.jpg">
</div>
<div class="text2">
<h1>Library</h1>
<p class="up">Lorem Ipsum is simply dummy text of the printing and typesetting industry</p>
<p class="down">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an
unknown printer
took a galley of type and scrambled it to make a type specimen book.</p>
<button>EXPLORE</button>
</div>
<div class="image3">
<img src="images/b2.jpg">
</div>
<div class="text3">
<h1>Library</h1>
<p class="up">Lorem Ipsum is simply dummy text of the printing and typesetting industry</p>
<p class="down">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an
unknown printer
took a galley of type and scrambled it to make a type specimen book.</p>
<button>EXPLORE</button>
</div>
<div class="text4">
<h1>Library</h1>
<p class="up">Lorem Ipsum is simply dummy text of the printing and typesetting industry</p>
<p class="down">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an
unknown printer
took a galley of type and scrambled it to make a type specimen book.</p>
<button>EXPLORE</button>
</div>
<div class="image6">
<img src="images/b3.jpg">
</div>
<div class="text5">
<h1>Library</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry</p>
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer
took a galley of type and scrambled it to make a type specimen book.</p>
<button>EXPLORE</button>
</div>
<div class="image8">
<img src="images/b4.jpg">
</div>
</div>
 
<!-- 网页Gallery区域 -->
<div class="gallery">
<!-- gallery上部分 -->
<div class="up">
<h1>GALLERY</h1>
<div class="line"></div>
<span>
Lorem Ipsum is simply dummy text of the printing and typesetting<br>
industry. Lorem Ipsum has been the industry's standard dummy<br>
text ever since the 1500s.
</span>
</div>
<!-- gallery下部分 -->
<div class="down">
<dl>
<dt>
<img src="images/03-01.jpg">
</dt>
<dd>Science Lab</dd>
</dl>
<dl>
<dt>
<img src="images/03-02.jpg">
</dt>
<dd>Indoor Stadium</dd>
</dl>
<dl>
<dt>
<img src="images/03-03.jpg">
</dt>
<dd>Transportation</dd>
</dl>
<dl>
<dt>
<img src="images/03-04.jpg">
</dt>
<dd>Kids Room</dd>
</dl>
<dl>
<dt>
<img src="images/03-05.jpg">
</dt>
<dd>Meditation Classes</dd>
</dl>
<dl>
<dt>
<img src="images/03-06.jpg">
</dt>
<dd>Kid Play Ground</dd>
</dl>
</div>
</div>
</section>
<!-- 网页页脚 -->
<footer>
<p>&copy;2016imooc.com京ICP备13046642</p>
</footer>
</body>
 
</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
* {
margin0;
padding0;
}
 
 
/* 去掉列表小圆点 */
ul {
list-stylenone;
}
 
 
/* 头部 */
header {
width100%;
height80px;
background-color#07cbc9;
}
 
header .center {
width1200px;
height100%;
/* 盒子水平居中 */
margin0 auto;
}
 
header .center .logo {
floatleft;
height48px;
margin-top16px;
}
 
header .center .nav {
floatright;
/* 行高等于盒子高,实现单行文本垂直居中 */
height80px;
line-height80px;
}
 
header .center .nav ul li {
floatleft;
font-size16px;
colorwhite;
margin-left20px;
}
 
/* 网页横幅 */
.banner {
height600px;
positionrelative;
}
 
.banner img {
width100%;
height600px;
}
 
.banner .banner-mask {
positionabsolute;
top0;
height600px;
width100%;
background-color: rgba(0000.5);
}
 
.banner .banner-form {
positionabsolute;
top100px;
width504px;
left50%;
margin-left-252px;
}
 
.banner .banner-form form input {
width504px;
height40px;
backgroundnone;
color#808080;
border1px solid #808080;
margin-bottom20px;
}
 
.banner .banner-form form textarea{
width504px;
height110px;
backgroundnone;
color#808080;
border1px solid #808080;
margin-bottom20px;
}
.banner .banner-form form p input{
width200px;
height40px;
}
.banner .banner-form form p {
text-aligncenter;
}
 
/* about区域 */
.content .about{
width1201px;
margin0 auto;
margin-top30px;
}
.content .about .up{
text-aligncenter;
}
 
.content .about .up h1{
font-size32px;
}
.content .about .up .line{
width60px;
height3px;
background-color#07cbc9;
margin:10px auto;
}
.content .about .up span{
text-aligncenter;
colorgray;
font-size14px;
}
.content .about .down{
positionrelative;
width1201px;
height464px;
overflowhidden;
padding-top30px;
}
.content .about .down .left {
floatleft;
}
.content .about .down .left .jdms{
positionabsolute;
width340px;
padding20px;
border1px solid gray;
background-color: rgba(255,255,255,0.5);
margin-top30px;
}
.content .about .down .left .jdms p{
font-size18px;
}
.content .about .down .left .jdms button{
background-colorblack;
colorwhite;
bordernone;
width140px;
height40px;
margin-top20px;
}
.content .about .down .center{
floatleft;
margin-left147px;
}
.content .about .down .center img{
width650px;
height435px;
}
.content .about .down .right{
floatright;
overflowhidden;
}
.content .about .down .right .top{
border1px solid #07cbc9;
width260px;
margin-bottom30px;
text-aligncenter;
padding40px 0;
}
.content .about .down .right .top p{
font-size28px;
font-weightbold;
}
.content .about .down .right .top .line-top{
width60px;
height3px;
background-color#07cbc9;
margin:10px auto;
}
.content .about .down .right .down{
border1px solid #07cbc9;
width260px;
height100%;
text-aligncenter;
padding-bottom40px;
}
.content .about .down .right .down p{
font-size28px;
font-weightbold;
}
.content .about .down .right .down .line-down{
width60px;
height3px;
background-color#07cbc9;
margin:10px auto;
}
 
/* 图文混排区域 */
.content .image-text{
width100%;
overflowhidden;
background-color#07cbc9;
}
 
.content .image-text img{
/* 使图片撑满整个盒子 */
width100%;
height100%;
/* img是行内元素,默认存在间隙,转为块级元素,消除间隙 */
displayblock;
}
.content .image-text>div{
width25%;
height380px;
floatleft;
}
 
.content .image-text h1{
font-size24px;
margin-top20px;
margin-left20px;
margin-bottom30px;
colorwhite;
}
.content .image-text .up{
margin-left20px;
margin-bottom20px;
font-size16px;
colorwhite;
}
.content .image-text .down{
margin-left20px;
margin-bottom30px;
font-size14px;
colorgray;
}
 
.content .image-text button{
displayblock;
margin:0 auto;
background-colorblack;
colorwhite;
bordernone;
width138px;
height40px;
}
 
/* Gallery区域 */
.content .gallery{
width1201px;
margin0 auto;
margin-top30px;
}
.content .gallery .up{
text-aligncenter;
}
 
.content .gallery .up h1{
font-size32px;
}
.content .gallery .up .line{
width60px;
height3px;
background-color#07cbc9;
margin:10px auto;
}
.content .gallery .up span{
text-aligncenter;
colorgray;
font-size14px;
}
.content .gallery .down{
width1200px;
height560px;
padding-left20px;
padding-right20px;
background-colorred;
}
.content .gallery .down dl{
positionrelative;
}
.content .gallery .down dl dt{
floatleft;
padding-top30px;
margin-right40px;
width360px;
height240px;
}
.content .gallery .down dl dt img{
width100%;
height100%;
}
/* 清除第三个图片和最后一个图片的右侧外边距 */
.content .gallery .down dl dt:nth-child(3){
marginright 0;
}
.content .gallery .down dl dt:last-child{
marginright 0;
}
.content .gallery .down dl dd{
positionabsolute;
displayblock;
bottom0;
}
footer{
background-color#07cbc9;
width100%;
height80px;
}
footer p{
text-aligncenter;
line-height80px;
}

https://img1.sycdn.imooc.com//climg/630f540d09598c4109780452.jpg

图片上的文字要怎么浮动在图片上呢?而且位于最下方

https://img1.sycdn.imooc.com//climg/630f53f309caf08a08541284.jpg

最右边这两个图片设置了取消外边距但是没有效果

另外再麻烦老师帮我看看其他的代码有没有可以优化的地方。谢谢老师啦~

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
imooc_慕慕 2022-09-01 11:20:28

同学你好,解答如下:

1、dl dt dd属于块元素独占一行,不用设置定位,将dt设置的属性,放在dl上,效果就对了。

2、这样设置,先给最外面的盒子设置margin:20px auto;这样就设置了上下的20px;将属性设置在dl上,给dl设置margin:15px 20px;这个整体的盒子就会有20px的左右边距,

https://img1.sycdn.imooc.com//climg/63105d7c09e3abff03460237.jpg

https://img1.sycdn.imooc.com//climg/6310208509abe7aa03930157.jpg

https://img1.sycdn.imooc.com//climg/63101f9a0983576403690306.jpg

https://img1.sycdn.imooc.com//climg/63101fb609a632e604950482.jpg

3、其他部分问题修改参考:

网页图文混排区

第五个图片文字没有效果,添加相应的class类名

https://img1.sycdn.imooc.com//climg/631021aa0919a69e07930245.jpg

小三角没有设置

https://img1.sycdn.imooc.com//climg/631028f209e97dd303980591.jpg

https://img1.sycdn.imooc.com//climg/631028d90944dad006500822.jpg

祝学习愉快~

  • 提问者 踩一个脚印 #1

    老师我有点乱,第一个是啥意思😭我不是想设置大盒子的内边距,我是设置了大盒子里面的六个小图片的右边距为40,但是题目里要求第三个图片和最后一个图片的边距为零,我是想清除这两个图片的外边距
    还有那个不是图文混排区的文字,是gallery区的就像图片最下方的文字😭

    2022-09-01 15:15:35
  • imooc_慕慕 回复 提问者 踩一个脚印 #2

    同学你好,解答如下:

    通过代码可以看到,同学给你每一个dt设置了margin-right,这个思路是不优化的,我们直接把设置在dt上的属性,放在dl上,题目要去是中间的是30px距离,也就是每个盒子上下间距15px,这样可以从整体上设置margin属性,我们看一下效果

    https://img1.sycdn.imooc.com//climg/63106842096c53a003690399.jpg

    https://img1.sycdn.imooc.com//climg/631064b70935745a13130581.jpg

    但是此时上下的边距是不等的,我们需要继续调整,

    首先这个down是由内容撑开的,我们不可以设置固定的高度,由于里面使用了浮动,脱离了文档流,因此我们需要清除浮动,使用overflow:hidden;整体的上下也有外边距,我们可以设置这个margin:20px auto;

    https://img1.sycdn.imooc.com//climg/631065cc091036ac03980245.jpg

    https://img1.sycdn.imooc.com//climg/6310688309d1959705370228.jpg

    https://img1.sycdn.imooc.com//climg/631066ad091d66d512310577.jpg

    这样上下也相等了

    题目要求没有要求第三个和最后一个的边距为0,同学是不是理解错了,

    那个图文混排区的文字,是其它部分的错误检查,

    同学的gallery区的问题,上面也有修改的,同学这个不需要使用定位,因为dl dt dd属于块元素,独占一行,并且文字不是定位在图片上的,是在图片的下面,直接给dd设置高度上下居中就可以,然后再缩进20px

    https://img1.sycdn.imooc.com//climg/6310689b09d92a3403650236.jpg

    此时图片与文字直接会有一条间隙,此时我们将图片设置为块元素,

    https://img1.sycdn.imooc.com//climg/63106c1d093b08fd03930318.jpg

    https://img1.sycdn.imooc.com//climg/631069bb09283af404010133.jpg

    https://img1.sycdn.imooc.com//climg/63106c0209abd2b804020321.jpg

    同学可以再重新梳理一下思路,如果还有疑问可以再提问,老师再为你解答

    祝学习愉快~


    2022-09-01 16:18:16
  • 提问者 踩一个脚印 #3

    谢谢老师!我弄懂啦~

    2022-09-01 22:19:07
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码