CSS中的浮动是一种布局技术,常用于实现元素的排列和定位。通过使用float属性,可以让元素在页面中左浮动或右浮动,使得其他内容围绕该元素布局。
在 CSS 中,可以使用 float 属性来使元素浮动。浮动的元素会脱离正常文档流,可以向左或向右移动,并尽可能靠近其相邻的浮动元素。
float 属性的常见取值包括:
left:元素向左浮动。right:元素向右浮动。none:默认值浮动前:如以下代码,2号浮动前
DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.outer{
width: 400px;
height: 400px;
background-color:aqua;
padding: 10px;
}
.box{
font-size: 20px;
padding: 10px;
}
.box1{
background-color:yellow;
}
.box2{
background-color:antiquewhite;
/* float: left;*/
}
.box3{
background-color:blue;
}
span{
background-color: brown;
font-size: 40px;
vertical-align: middle;
}
img{
height: 200px;
float: left;
margin-right: 0.5em;
}
.text::first-letter{
font-size: 80px;
float: left;
}
style>
head>
<body>
<div class="outer">
<div class="box box1">1div>
<div class="box box2">2div>
<div class="box box3">3div>
div>
body>
html>

浮动2后:

浮动3后

CSS 元素浮动后具有以下特点:
影响:
解决方案:
方案一:在所有浮动元素的最后面,添加一个块级元素,并给该块级元素设置clear:both;
方案二:给浮动的父元素,设置伪元素,通过伪元素清除浮动,如下:(推荐使用)
.clearfix::after{
content: '';
display: block;
clear: both;
}
clear 属性用于指定元素哪些方向的浮动元素是不允许靠近的。它只会对前面有浮动元素的元素生效。
以下是 clear 属性的属性值及功能:
clear: left;: 表示元素的左侧不允许存在浮动元素。如果左侧有浮动元素,这个元素将被清除浮动,并显示在浮动元素下方。
clear: right;: 表示元素的右侧不允许存在浮动元素。如果右侧有浮动元素,这个元素将被清除浮动,并显示在浮动元素下方。
clear: both;: 表示元素两侧都不允许存在浮动元素。即使左右两侧都有浮动元素,这个元素也会被清除浮动,并显示在所有浮动元素下方。
clear: none;: 默认值,表示元素两侧都可以存在浮动元素,不会清除任何浮动
布局中的一个原则:设置浮动的时候,兄弟元素要么全都浮动,要么全都不浮动。
.clearfix::after{
content: '';
display: block;
clear: both;
}
写布局的代码,写代码前先分好块


块里面套块
块块并排(多块占用一行)用浮动,一个块单独占一行不用浮动。记得父元素的浮动消除
实现代码:
DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
*{
margin: 0px;
padding: 0px;
}
.leftfix{
float: left;
}
.rightfix{
float: right;
}
.clearfix::after{
content: '';
display: block;
clear: both;
}
.container{
width:960px;
margin:0 auto;
text-align: center;
}
.loge{
width:200px;
}
.banner1{
width:540px;
margin:0px 10px;
}
.banner2{
width:200px;
}
.loge,.banner1,.banner2{
height: 80px;
background-color: #ccc;
line-height: 80px;
}
.menu{
height: 30px;
background-color: #ccc;
margin-top: 10px;
line-height: 30px;
}
.item1,.item2{
width: 368px;
height: 198px;
border: 1px solid #ccc;
line-height:198px;
margin-right: 10px;
}
.content{
margin-top:10px;
}
.item3,.item4,.item5,.item6{
width: 178px;
height: 198px;
border: 1px solid #ccc;
margin-right: 10px;
line-height: 198px;
}
.bottom{
margin-top: 10px;
}
.item7,.item8,.item9{
width: 188px;
height: 128px;
border: 1px solid #ccc;
}
.item7,.item8{
margin-bottom: 10px;
}
.foot{
width: 960px;
height: 60px;
background-color: #ccc;
margin-top: 10px;
line-height: 58px;
}
style>
head>
<body>
<div class="container ">
<div class="page-header clearfix">
<div class="loge leftfix">logediv>
<div class="banner1 leftfix">banner1div>
<div class="banner2 leftfix">banner2div>
div>
<div class="menu">菜单div>
<div class="content clearfix">
<div class="left leftfix ">
<div class="top clearfix">
<div class="item1 leftfix">栏目一div>
<div class="item2 leftfix">栏目二div>
div>
<div class="bottom clearfix">
<div class="item3 leftfix">栏目三div>
<div class="item4 leftfix">栏目四div>
<div class="item5 leftfix">栏目五div>
<div class="item6 leftfix">栏目六div>
div>
div>
<div class="right rightfix">
<div class="item7">栏目七div>
<div class="item8">栏目八div>
<div class="item9">栏目九div>
div>
div>
<div class="foot">页脚div>
div>
body>
html>
之前不太了解的点
*{
margin: 0px;
padding: 0px;
}
在CSS中,*{ margin: 0px; padding: 0px; }的作用是移除所有HTML元素的外边距(margin)和内边距(padding)。
外边距(margin)是指元素周围的空白区域,用于控制元素与其他元素之间的间距。而内边距(padding)是指元素内容与边框之间的空白区域,用于控制元素内部内容的间距。
通过将margin和padding都设置为0,可以消除所有元素的外边距和内边距,使页面布局更加紧凑。这在某些情况下可能是有用的,例如当你想要创建一个自定义的布局或者去除默认的浏览器样式时。