div块级元素
<html> <head> <meta charset="utf-8"> <title>
title>
head> <body> <div style="width: 600px;height: 600px;background-color: black;"> <div style="width: 500px;height: 500px;background-color: red;"> <div style="width: 400px;height: 400px;background-color: green;">
div>
div>
div>
body>
html>
可以看到我们定义的样式是三个正方形的块style(css)后面会细讲
style=”width: 600px;height: 600px;background-color: black;
分别对应宽高和背景色
<div style="width: 100px;height: 100px;background-color: black;">
div> <div style="width: 100px;height: 100px;background-color: red;">
div> <div style="width: 100px;height: 100px;background-color: green;">
div>
但是如果我们需要第一行有两个块怎么办呢
浮动和定位
<html> <head> <meta charset="utf-8"> <title>
title>
head> <body> <div style="width: 100px;height: 100px;background-color: black;float:right;">
div> <div style="width: 100px;height: 100px;background-color: red;">
div> <div style="width: 100px;height: 100px;background-color: green;float: left;">
div> <div style="width: 100px;height: 100px;background-color: yellow;float: right;">
div>
body>
html>

style="width: 100px;height: 100px;background-color: black;position: absolute; width:宽 height:高 background-color:背景色
额外说明:盒模型
浏览器查看元素时会显示这样一个图

定义一个元素的宽高时
可以通过
padding:填充
border:边框
margin:边距
总元素的宽度=宽度+左填充+右填充+左边框+右边框+左边距+右边距
总元素的高度=高度+顶部填充+底部填充+上边框+下边框+上边距+下边距
例如
<html> <head> <meta charset="utf-8"> <title>
title>
head> <body> <div style="background-color: black;padding: 0px;width: 100px;height: 100px;">
div><br /> <div style="background-color: black;padding: 20px;width: 100px;height: 100px;">
div><br /> <div style="background-color: black;border: #FF0000 solid 10px;padding: 20px;width: 100px;height: 100px;">
div> <div style="background-color: black;margin: 20px;border: #FF0000 solid 10px;padding: 20px;width: 100px;height: 100px;">
div>
body>
html>

relative:相对于其正常位置进行定位
<html> <head> <meta charset="utf-8"> <title>
title>
head> <body> <div style="width: 100px;height: 100px;background-color: black;position: absolute;left: 300px;top: 0px;">
div> <div style="width: 100px;height: 100px;background-color: red;position: relative;left: 100px;top: 0px;">
div> <div style="width: 100px;height: 100px;background-color: green;position: relative;left: 100px;top: 100px;">
div> <div style="width: 100px;height: 100px;background-color: yellow;position: relative;">
div> <div style="width: 100px;height: 100px;background-color: palevioletred;position: relative;left: 100px;">
div>
body>
html>

可以对比下
对比(一)
<html> <head> <meta charset="utf-8"> <title>
title>
head> <body> <div style="background-color: black;width: 500px;height: 500px;position: absolute;top: 0px;left: 0;">
div> <div style="background-color: red;width: 300px;height: 300px;position: absolute;top: 0px;">
div> <div style="background-color:green;width: 300px;height: 300px;position: relative;top: 0px;left: 50px;">
div>
body>
html>
<html> <head> <meta charset="utf-8"> <title>
title>
head> <body> <div style="background-color: black;width: 800px;height: 800px;position: absolute;top: 0px;left: 0;">
div> <div style="background-color: red;width: 300px;height: 300px;position: absolute;top: 0px;">
div> <div style="background-color:green;width: 300px;height: 300px;position: relative;top: 0px;left: 50px;">
div> <div style="background-color:blue;width: 300px;height: 300px;position:absolute;top: 0px;left: 50px;">
div> <div style="background-color:blue;width: 300px;height: 300px;position:relative;top: 0px;left: 50px;">
div>
body>
html>

实现一个简单布局
<html> <head> <meta charset="utf-8"> <title>
title>
head> <body> <div style="width: 1000px;height: 30px;background-color: black;color: white;text-align: center;">TOP
div> <div style="width: 1000px;height: 300px;position: relative;top: 5px;"> <div style="width: 280px;height: 300px;background-color: red;position: absolute;">LEFT
div> <div style="width: 700px;height: 300px;background-color: green;float: right;">RIGHT
div>
div> <div style="width: 1000px;height: 30px;background-color: yellow;position: relative;top: 10px;text-align: center;">BOTTOM
div>
body>
html>

如果我们讲这些背景填充色去掉
找一些图片填充
<html> <head> <meta charset="utf-8"> <title>
title>
head> <body> <div style="height: 50px;"> <img src="1.png"/>
div> <div style="width: 1200px;height: 300px;position: relative;top: 5px;"> <div style="position: absolute;"> <img src="2.png" />
div> <div style="width: 700px;height: 300px;float: right;"> <img src="3.png" />
div>
div> <div style="width: 1200px;height: 30px;position: relative;top: 70px;"> <img src="4.png" />
div>
body>
html> 这里的1/2/3/4.png是我截的csdn主页的图
<img src="3.png" />此处解释:img图片链接为 同文件夹下的3.png图片
特别提醒
要勤加练习,切忌眼高手低,加油
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/218899.html原文链接:https://javaforall.net
