HTML中Div框怎么弄?

HTML中Div框怎么弄?,第1张

div设置外边框相关如下:\x0d\x0aborder-color:#000设置4边边框颜色为黑色;\x0d\x0aborder-color:+颜色值,即可设置对象边框颜色;\x0d\x0aborder-left-color:#000设置左边框颜色为黑色;\x0d\x0aborder-right-color:#000设置右边框颜色为黑色;\x0d\x0aborder-top-color:#000设置上边框颜色为黑色;\x0d\x0aborder-bottom-color:#000设置下边框颜色为黑色;\x0d\x0a条件:必须设置对象边框存在,设置边框宽度必须大于或等于1px,边框颜色才会显示并有存在意义。\x0d\x0a.divcss5{border-style:solidborder-width:1pxborder-color:#000}\x0d\x0a这样就设置对象边框为实线边框,参数就是solid,边框宽度(厚度)为1px,边框颜色为黑色,这样设置边框颜色才会生效。

html:

<!DOCTYPE html>

<html>

<head>

<title>My webpage</title>

</head>

<body>

<div>

<h1>My first div</h1>

<p>This div uses inline CSS to style its content.</p>

<style>

h1 {

color: red

}

p {

font-size: 16px

text-align: center

}

</style>

</div>

<div>

<h1>My second div</h1>

<p>This div uses an external CSS file to style its content.</p>

<link rel="stylesheet" type="text/css" href="styles.css">

</div>

<div>

<h1>My third div</h1>

<p>This div uses an embedded style sheet to style its content.</p>

<style>

/* This is an embedded style sheet */

h1 {

color: blue

}

p {

font-size: 14px

text-align: left

}

</style>

</div>

</body>

</html>

style.css:

/* This is an external style sheet */

h1 {

color: green

}

p {

font-size: 18px

text-align: right

}

这段代码中,第一个div使用内联CSS来样式其内容,第二个div使用外部CSS文件来样式其内容,第三个div使用嵌入式样式表来样式其内容。

田字格布局,要求大小相同的四个正方形。而html里div如果不加控制的话是独占一行的,现在要做的是把四个大小相同的方块,排列成“田”字。

第一步、新建html文档并搭建框架

新建一个TXT文档,重命名为“田子格布局.html”,然后用记事本打开,输入表头信息,已经html整体框架搭建。包括head与body。

第二步、DIV布局

分别复制4个不同的div作为4部分,并且分别命名为不同id;显示内容为块1、块2、块3、块4。

【提示】div在html里是单独占一列的(如果不控制),现在4个div布局完成。

【代码如下】

</head>

<body>

<div id="prat1">块1</div>

<div id="prat2">块2</div>

<div id="prat3">块3</div>

<div id="prat4">块4</div>

</body>

</html>

第三步、CSS控制4个DIV显示

输入style然后开始对4个div进行控制,分别对四个块进行大小和颜色的设定,处理之后在浏览器中打开显示如下图所示。

【提示】由于是田子格,所以四个div大小应该相同,为了可以区分颜色分别采用不同的颜色。

【代码如下】

<style>

#prat1{

width: 200px

height: 200px

background: blue/*边长200像素的蓝色方块*/

}

#prat2{

width: 200px

height: 200px

background: red/*边长200像素的蓝色方块*/

}

#prat3{

width: 200px

height: 200px

background: yellow/*边长200像素的蓝色方块*/

}

#prat4{

width: 200px

height: 200px

background: green/*边长200像素的蓝色方块*/

}

</style>

第四步、使用浮动

在CSS里控制输入float:left;四个div全部输入一样内容,这时候看到的是四个并排的div,而没有达到想要的效果,如下图所示。

【代码如下】

<style>

#prat1{

width: 200px

height: 200px

background: blue

float: left

}

#prat2{

width: 200px

height: 200px

background: red

float: left

}

#prat3{

width: 200px

height: 200px

background: yellow

float: left

}

#prat4{

width: 200px

height: 200px

background: green

float: left

}

</style>

第五步、清除浮动

在第三块上使用清除浮动clear:left;其余的代码保持不变,然后保存代码,刷新打开的页面,就会看到一个田字格了,如下图所示。

【代码如下】

#prat3{

width: 200px

height: 200px

background: yellow

float: left

clear: left

【注意】只清除第三块的就可以了。

【完整的代码】

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=UTF-8">

<title>田字格布局</title>

<style>

#prat1{

width: 200px

height: 200px

background: blue

float: left

}

#prat2{

width: 200px

height: 200px

background: red

float: left

}

#prat3{

width: 200px

height: 200px

background: yellow

float: left

clear: left

}

#prat4{

width: 200px

height: 200px

background: green

float: left

}

</style>

</head>

<body>

<div id="prat1">块1</div>

<div id="prat2">块2</div>

<div id="prat3">块3</div>

<div id="prat4">块4</div>

</body>

</html>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/8306063.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-15
下一篇 2023-04-15

发表评论

登录后才能评论

评论列表(0条)

保存