当前位置: 首页 > wzjs >正文

企业建设网站没有服务器坪地网站建设代理商

企业建设网站没有服务器,坪地网站建设代理商,企业网站建设原则是,tk网站注册CSS3 浮动与清除浮动语法知识点及案例代码 一、浮动基础 浮动语法 选择器 {float: left|right|none|initial|inherit; }left:元素向左浮动。right:元素向右浮动。none:默认值,元素不浮动。initial:使用默认值。inhe…

CSS3 浮动与清除浮动语法知识点及案例代码

一、浮动基础

浮动语法

选择器 {float: left|right|none|initial|inherit;
}
  • left:元素向左浮动。
  • right:元素向右浮动。
  • none:默认值,元素不浮动。
  • initial:使用默认值。
  • inherit:继承父元素的浮动属性。

浮动特点

  1. 浮动元素会脱离文档流。
  2. 浮动元素会尽可能地向左或向右移动,直到遇到包含框的边缘或其他浮动元素。
  3. 块级元素浮动后可以设置宽度和高度。
  4. 浮动元素后的文本会环绕在浮动元素周围。

二、清除浮动

清除浮动语法

选择器 {clear: none|left|right|both|initial|inherit;
}
  • none:默认值,允许两侧有浮动元素。
  • left:不允许左侧有浮动元素。
  • right:不允许右侧有浮动元素。
  • both:不允许两侧有浮动元素。
  • initial:使用默认值。
  • inherit:继承父元素的清除属性。

清除浮动方法

  1. 使用 clear 属性

    .clear {clear: both;
    }
    
  2. 使用 overflow 属性

    .parent {overflow: hidden;
    }
    
  3. 使用 clearfix 技巧

    .clearfix::after {content: "";display: table;clear: both;
    }
    

三、案例代码

案例一:简单的浮动布局

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>浮动布局</title><style>.container {width: 600px;margin: 0 auto;border: 1px solid #ccc;}.left {float: left;width: 200px;height: 200px;background-color: #f99;}.right {float: right;width: 200px;height: 200px;background-color: #9f9;}.content {margin: 0 210px;height: 200px;background-color: #99f;}.clear {clear: both;}</style>
</head>
<body><div class="container"><div class="left">左侧浮动</div><div class="right">右侧浮动</div><div class="content">中间内容</div><div class="clear"></div></div>
</body>
</html>

注释

  • .container:设置容器宽度和边框。
  • .left:向左浮动,设置宽度、高度和背景色。
  • .right:向右浮动,设置宽度、高度和背景色。
  • .content:设置中间内容的宽度、高度和背景色。
  • .clear:清除浮动。

案例二:清除浮动的多种方法

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>清除浮动</title><style>.parent {border: 1px solid #ccc;margin-bottom: 20px;}.child {float: left;width: 100px;height: 100px;background-color: #f99;}/* 方法一:使用 clear 属性 */.clear {clear: both;height: 0;font-size: 0;line-height: 0;}/* 方法二:使用 overflow 属性 */.parent.overflow {overflow: hidden;}/* 方法三:使用 clearfix 技巧 */.clearfix::after {content: "";display: table;clear: both;}</style>
</head>
<body><h2>方法一:使用 clear 属性</h2><div class="parent"><div class="child"></div><div class="clear"></div></div><h2>方法二:使用 overflow 属性</h2><div class="parent overflow"><div class="child"></div></div><h2>方法三:使用 clearfix 技巧</h2><div class="parent clearfix"><div class="child"></div></div>
</body>
</html>

注释

  • .parent:设置父容器的边框和底部外边距。
  • .child:向左浮动,设置宽度、高度和背景色。
  • .clear:使用 clear 属性清除浮动。
  • .parent.overflow:使用 overflow 属性清除浮动。
  • .clearfix::after:使用 clearfix 技巧清除浮动。

以下是开发中常用的实际具体案例,帮助你更好地理解和应用 CSS3 浮动与清除浮动:

案例三:导航栏布局

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>导航栏布局</title><style>* {margin: 0;padding: 0;box-sizing: border-box;}.nav {width: 100%;height: 60px;background-color: #333;}.logo {float: left;width: 200px;height: 60px;background-color: #444;line-height: 60px;text-align: center;color: white;}.menu {float: left;}.menu-item {float: left;width: 100px;height: 60px;line-height: 60px;text-align: center;color: white;list-style: none;}.user {float: right;width: 150px;height: 60px;line-height: 60px;text-align: center;color: white;}.clearfix::after {content: "";display: table;clear: both;}</style>
</head>
<body><div class="nav clearfix"><div class="logo">LOGO</div><ul class="menu"><li class="menu-item">首页</li><li class="menu-item">产品</li><li class="menu-item">服务</li><li class="menu-item">关于我们</li></ul><div class="user">登录/注册</div></div>
</body>
</html>

注释

  • *:重置所有元素的外边距和内边距,并设置盒模型为 border-box
  • .nav:设置导航栏的宽度、高度和背景色。
  • .logo:向左浮动,设置宽度、高度、背景色、行高和文本对齐方式。
  • .menu:向左浮动,包含菜单项。
  • .menu-item:向左浮动,设置宽度、高度、行高、文本对齐方式、颜色和列表样式。
  • .user:向右浮动,设置宽度、高度、行高、文本对齐方式和颜色。
  • .clearfix::after:使用 clearfix 技巧清除浮动,确保导航栏的高度正常。

案例四:产品列表展示

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>产品列表展示</title><style>* {margin: 0;padding: 0;box-sizing: border-box;}.container {width: 1000px;margin: 0 auto;}.product-list {overflow: hidden; /* 清除浮动 */}.product-item {float: left;width: 230px;height: 300px;margin: 10px;background-color: #f9f9f9;border: 1px solid #ddd;border-radius: 5px;padding: 10px;}.product-img {width: 100%;height: 200px;background-color: #eee;margin-bottom: 10px;}.product-name {font-size: 16px;margin-bottom: 5px;}.product-price {color: #ff6000;font-weight: bold;}</style>
</head>
<body><div class="container"><div class="product-list"><div class="product-item"><div class="product-img"></div><div class="product-name">产品名称1</div><div class="product-price">¥199</div></div><div class="product-item"><div class="product-img"></div><div class="product-name">产品名称2</div><div class="product-price">¥299</div></div><div class="product-item"><div class="product-img"></div><div class="product-name">产品名称3</div><div class="product-price">¥399</div></div><div class="product-item"><div class="product-img"></div><div class="product-name">产品名称4</div><div class="product-price">¥499</div></div></div></div>
</body>
</html>

注释

  • .container:设置容器的宽度和居中。
  • .product-list:设置产品列表的清除浮动方式为 overflow: hidden
  • .product-item:向左浮动,设置宽度、高度、外边距、背景色、边框、圆角和内边距。
  • .product-img:设置产品图片的宽度、高度、背景色和外边距。
  • .product-name:设置产品名称的字体大小和外边距。
  • .product-price:设置产品价格的颜色和字体粗细。

案例五:文字环绕图片

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>文字环绕图片</title><style>* {margin: 0;padding: 0;box-sizing: border-box;}.container {width: 800px;margin: 0 auto;padding: 20px;}.image-wrapper {float: left;width: 200px;margin-right: 20px;}.image {width: 100%;height: 150px;background-color: #eee;}.text {font-size: 14px;line-height: 1.6;}</style>
</head>
<body><div class="container"><div class="image-wrapper"><div class="image"></div></div><div class="text"><p>这里是环绕图片的文字内容,图片向左浮动,文字环绕在图片右侧显示。通过浮动实现文字环绕图片的效果,使页面布局更加美观和灵活。</p><p>浮动的图片脱离了文档流,但文字会自动环绕在图片周围,形成自然的排版效果。在实际开发中,这种布局常用于文章、新闻等内容的展示,提升用户体验。</p></div></div>
</body>
</html>

注释

  • .container:设置容器的宽度、居中和内边距。
  • .image-wrapper:向左浮动,设置宽度和右侧外边距。
  • .image:设置图片的宽度、高度和背景色。
  • .text:设置文字的字体大小和行高。

案例六:表单布局

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>表单布局</title><style>* {margin: 0;padding: 0;box-sizing: border-box;}.container {width: 600px;margin: 0 auto;padding: 20px;}.form-group {margin-bottom: 20px;}.label {float: left;width: 100px;text-align: right;margin-right: 10px;line-height: 30px;}.input-wrapper {margin-left: 110px;}.input {width: 100%;height: 30px;padding: 0 5px;border: 1px solid #ddd;border-radius: 3px;}.clearfix::after {content: "";display: table;clear: both;}</style>
</head>
<body><div class="container"><div class="form-group clearfix"><label class="label">用户名:</label><div class="input-wrapper"><input type="text" class="input"></div></div><div class="form-group clearfix"><label class="label">密码:</label><div class="input-wrapper"><input type="password" class="input"></div></div><div class="form-group clearfix"><label class="label">邮箱:</label><div class="input-wrapper"><input type="email" class="input"></div></div></div>
</body>
</html>

注释

  • .container:设置容器的宽度、居中和内边距。
  • .form-group:设置表单组的底部外边距。
  • .label:向左浮动,设置宽度、文本对齐方式、右侧外边距和行高。
  • .input-wrapper:设置输入框容器的左侧外边距,使输入框与标签对齐。
  • .input:设置输入框的宽度、高度、内边距、边框和圆角。
  • .clearfix::after:使用 clearfix 技巧清除浮动,确保表单组的高度正常。

通过以上案例,你可以看到 CSS3 浮动与清除浮动在实际开发中的广泛应用,从简单的布局到复杂的效果实现,它们都发挥着重要的作用。

http://www.dtcms.com/wzjs/826701.html

相关文章:

  • 网站没有备案怎么做淘宝客襄阳市住房城乡建设部网站
  • 正规的佛山网站建设有了域名和云主机怎么做网站
  • 做网站视频背景邢台做网站推广找谁
  • 个人免费建站软件朋友圈推广平台
  • 网站响应式与电脑版有什么区别微信推广平台
  • 南昌餐厅网站建设wordpress qq评论
  • 一个空间可以做两个网站吗做兼职什么网站好
  • 公众号做视频网站吗云网站制作的流程
  • 网站建设简单流程如何面试网站开发
  • 贵港做网站建设价格费用邢台市官网
  • 实业有限公司网站怎么做推广营销策划
  • 网站设计与建设报告呼市赛罕区信息网站做一顿饭工作
  • 苏州网站公司查网站是否备案
  • 建设动漫网站的目的ei网站怎么兼做
  • 宁波企业网站设计网站后台用esayui做
  • thinkphp手机网站开发宠物店网站建设方案
  • vr全景网站怎么做wordpress网络
  • 东莞住房和城乡建设厅网站wordpress分页无效
  • 有哪些免费网站可以发布广告网站建设 艺麟盛世
  • 做网站是什么软件wordpress网站地图插件
  • 网站建设中啥意思没有充值入口的传奇游戏
  • 网站建设前期准备方案wordpress 整站采集
  • 的网站建设公司那个好合肥网络推广培训
  • asp.net做网站Dreamver学校网站的建设费用
  • 做网站最便宜多少钱百度北京总部电话
  • 网站每天做多少外链合适专业的网站建设公司排名
  • 广告网站模板下载 迅雷下载安装百度建立自己的网站
  • 网站建设中出现的问问题天猫网站是怎么做seo优化的
  • 网站建设和优普通网站一年要多少钱
  • 网站建设首选原创先锋wordpress标题加密