欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > CSS >内容正文

CSS

css background 一半_CSS小技巧

发布时间:2025/3/11 CSS 41 豆豆
生活随笔 收集整理的这篇文章主要介绍了 css background 一半_CSS小技巧 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
点击上方蓝字  关注我们

左右布局

将内部的子元素加浮动,父元素清除浮动即可。

代码:

DOCTYPE html><html><head><title>testtitle><style type="text/css">
.big{
width: 300px;
height: 300px;
background-color: #999;}
.smallOne{
width: 100px;
height: 100px;
background-color: red;
float: left;}
.smallTwo{
width: 100px;
height: 100px;
background-color: red;
float: right;}
.clearfix{
content: "";
display: block;
clear: both;}style>head><body><div class="big clearfix"><div class="smallOne">div><div class="smallTwo">div>div>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

效果:

左中右布局

 浮动 清除浮动

代码:

DOCTYPE html><html><head><title>testtitle><style type="text/css">
.big{
width: 300px;
height: 300px;
background-color: #999;}
.smallOne{
width: 100px;
height: 100px;
background-color: red;
float: left;}
.smallTwo{
width: 100px;
height: 100px;
background-color: green;
float: left;}
.smallThree{
width: 100px;
height: 100px;
background-color: red;
float: left;}
.clearfix{
content: "";
display: block;
clear: both;}style>head><body><div class="big clearfix"><div class="smallOne">div><div class="smallTwo">div><div class="smallThree">div>div>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

效果:

 水平居中

 内联元素居中:

text-align: center;

代码:

DOCTYPE html><html><head><title>testtitle><style type="text/css">
.big{
width: 200px;
height: 100px;
background-color: #999;
text-align: center;}style>head><body><div class="big"><span>我在这里span>div>body>html>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

效果图:

 块级元素居中:

margin:0 auto

代码:

DOCTYPE html><html><head><title>testtitle><style type="text/css">
.big{
width: 200px;
height: 100px;
background-color: #999;
text-align: center;}
.small{
width: 100px;
height: 20px;
background-color: red;
margin: 0 auto;}style>head><body><div class="big"><div class="small">我在这里div>div>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

效果图:

垂直居中

父元素高度确定的单行文本

设置 height = line-height

 代码:

DOCTYPE html><html><head><title>testtitle><style type="text/css">
.big{
width: 200px;
height: 100px;
background-color: #aaa;
line-height: 100px;}style>head><body><div class="big">
这里是文本!div>body>html>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

效果图:

块级元素垂直居中

利用父元素相对定位,子元素绝对定位,并且处置、水平方向个偏移50%,子元素利用负值偏移自身宽度、长度的一半,这种方式同样适用于水平居中。

代码:

DOCTYPE html><html><head><title>testtitle><style type="text/css">
.big{
height: 200px;
width: 200px;
background-color: red;
position: relative;}
.small{
height: 50px;
width: 50px;
background-color: black;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);}style>head><body><div class="big"><div class="small">div>div>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

效果图:

更多精彩请继续关注小编了解哦!

计算机毕业设计(源程序+论文+开题报告+文献综述+翻译+答辩稿)

联系QQ:2932963541进行咨询

网站地址:http://www.webtmall.com/扫码关注最新动态点击此处“阅读全文”查看更多内容

总结

以上是生活随笔为你收集整理的css background 一半_CSS小技巧的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。