css小技巧之浮动,居中,周围阴影,relative妙用

2023-05-16

    css在布局和样式微调中,有很多小技巧,今天总结一些小妙招,这些方法基本上很好用,而且不会轻易弄混淆。

    我们为了看出效果,将页面做了一些修饰,比如很多地方加上了边框,设置了背景色,主要是为了更直观的表现我们的效果。首先贴出一段主体的样式:

/**reset*/
html,body,div,p,span,h1,h2,h3,h4,h5,h6,input{margin:0;padding:0;font-weight:normal;}
#root{
	width:1000px;
	margin:0 auto;
}
.tips{
	color:red;
	font-weight:bold;
	font-size:18px;
	margin:20px 0;
}
.box{
	
}
.box-header,.box-footer{
	border:1px solid #ddd;
	border-radius:3px;
	padding:5px;
}
.box-content{
	margin:10px auto;
}
.box-item{
	width:100px;
	height:100px;
	border:1px solid #000;
	border-radius:3px;
}
.bg-red{
	background:red;
}
.bg-blue{
	background:lightblue;
}
.bg-green{
	background:lightgreen;
}

    1.1、首先要说的是浮动,我们知道,浮动的元素,会因为脱离了默认的布局,周围的元素,会因为浮动而发生改变,为了消除浮动带来的影响,我们需要清除浮动,所以浮动最主要的就是清除浮动,默认我们需要在父元素上清除浮动,无论是左浮动,还是右浮动,或者左右浮动都有,我们均需要在父级元素上做清除浮动的操作。

    浮动与清除浮动的主要样式代码如下:

.clearfix{
    zoom:1;
}
.clearfix:before,.clearfix:after{
	display:table;
	height:0;
	content:'';
}
.clearfix:after{
	clear:both;
}
.fl{
	float:left;
}
.fr{
	float:right;
}

    HTML部分:

<h2>1-1、浮动原生样式</h2>
<div class="box-header">this is header</div>
<div class="box-content clearfix">
   <div class="box-item bg-red fl">left</div>
   <div class="box-item bg-blue fr">right</div>
   <!--
   <div class="box-item bg-green fr">center</div> 
   -->				   
</div>
<div class="box-footer">this is footer</div>

    展示效果:

    对于这种浮动的效果,一个居左,一个居右的情况,在实际应用中也会有很多,可能不是这种效果,但是基本逃不出这样的技巧。重点样式在于父级元素上的.clearfix,以及他们的伪类样式.clearfix:before与.clearfix:after。核心代码在于:

    .clearfix:before,.clearfix:after{display:table;height:0;content:'';}

    .clearfix:after{clear:both;}

    默认的浮动,我们需要做一些工作,父元素需要设置清除浮动样式,子元素设置浮动,我们需要注意的是子元素有三个的情况,这时候,如果右浮动是两个的时候,我们需要将浮动在最右边的元素放在中间浮动的元素左边,如果不这么设置,最终靠中间的元素会被显示在最右边,很奇怪,这种设置,好像很不容易理解。这也是一种技巧。 

     1.2、在css3中,有一种flex布局,也可以实现这种左右布局的情况,他的实现很巧妙,主要在父级元素上设置子元素的对齐属性。

    css样式很简单,如下:

.box-content-flex{
	margin:10px auto;
	display:flex;
	justify-content:space-between;
}

    HTML代码如下:

<h2>1-2、浮动flex样式</h2>
<div class="box-header">this is header</div>
<div class="box-content-flex">
   <div class="box-item bg-red">left</div>  
   <div class="box-item bg-blue">right</div>				   
</div>
<div class="box-footer">this is footer</div>

    html部分和默认通过浮动的方式没有区别,唯一的区别就是在于父级元素上的.box-content-flex这个class 。display:flex;是让块级子元素显示在一行上,类似子集元素设置inline-block的效果。justify-content:space-between;是让子集元素排列的时候,中间有空隙,这样就正好一左一右。效果如下所示:

    2.1、接下来是介绍一个子元素在父容器中水平垂直居中的示例,这个用法很常见,比如对话框的样式,我们一般是让他们在页面正中间。最简单的垂直居中,就是借助定位来实现,即让父元素相对定位,然后子元素绝对定位,向上和向左偏移均为50%,这样,还需要设置外边距,分别为高度和宽度的1/2负值,相当于拉回来一部分,正好居中。 

    样式代码如下所示:

.content-wrapper{
	height:200px;
	background:#eee;
	/**relative for child layout*/
	position:relative;
}
.align-center{
	position:absolute;
	left:50%;
	top:50%;
	margin-left:-50px;
	margin-top:-50px;
}

    HTML代码如下:

<h2>2-1、居中原生样式</h2>
<div class="box-header">this is header</div>
<div class="box-content content-wrapper">
	<div class="box-item bg-green align-center"></div>
</div>
<div class="box-footer">this is footer</div>

     这种布局,适合所有常规的水平垂直居中。样式效果:

    2.2、今天要介绍的另一种水平垂直居中的做法,还是借助于flex布局,他的设置也很简单。

    css样式部分:

/**flex layout*/
.content-wrapper-flex{
	height:200px;
	background:#eee;
	/***/
	display:flex;
	justify-content:center;
	align-items:center;
}

     HTML代码部分:

<h2>2-2、居中flex布局样式</h2>
<div class="box-header">this is header</div>
<div class="box-content content-wrapper-flex">
	<div class="box-item bg-green"></div>
</div>
<div class="box-footer">this is footer</div>

    与浮动效果一样,这个样式技巧中,也是只需要在父级元素上设置即可, 很方便。justify-content:center;align-items:center;这两个设置,分别是设置水平,垂直居中对齐效果。最终的效果: 

    与利用top,left,position这种计算位置来实现居中的方式不同,flex让子元素居中,不需要知道子元素的宽度和高度,很方便。

    3、四周阴影效果,为了和普通阴影效果对比,我们做了两个阴影的示例。

    css样式部分:

/**3 shadow */
.border-shadow-around{
	box-shadow:0 0 10px #000;
}
.border-shadow-not-around{
	box-shadow:5px 5px 10px #000;
}

    HTML代码部分: 

<h2>3、四周阴影</h2>
<div class="box-content-flex">
	<div class="box-item bg-green border-shadow-around">shadow</div>
	<div class="box-item bg-green border-shadow-not-around">shadow-not-around</div>
</div>

    tips:四周阴影,在有些地方我们是会使用到的,默认都是两条边的阴影。我们需要四条边都有阴影,就 设置box-shadow的前面两个参数都为0,即:box-shadow: 0 0 5px #000;

    最终展示效果如下: 

    4、relative定位的妙用,一般有这样的场景,我们在一行中,会有2个或者2个以上的行内元素,他们如果高度不一致,或者还存在左右浮动的情况,会不在一条水平线上,而设计要求,会让我们把他们放置在一条线上,这样一来,我们需要对容器中的子元素,设置垂直对齐方式vertical-align:middle,有时候,即使这么设置了,还是会存在不在一条水平线上的情况:

    我们设置的样式如下:

/**relative tips*/
.content-relative{
	border:1px solid #ddd;
}
.form-label{
	display:inline-block;
	width:100px;
	border:1px solid lightgreen;
	height:28px;
	line-height:28px;
}
.input-item{
	height:28px;
	line-height:28px;
	outline:none;
	border:1px solid #ddd;
	border-radius:3px;
	padding: 2px 5px;
}
input[type='text']:focus{
	border-color:rgb(82,168,236,0.8);
	-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,0,0.075),0 0 8px rgba(82,168,236,0.6);
	-moz-box-shadow:inset 0 1px 1px rgba(0,0,0,0,0.075),0 0 8px rgba(82,168,236,0.6);
	box-shadow:inset 0 1px 1px rgba(0,0,0,0,0.075),0 0 8px rgba(82,168,236,0.6);
}
.btn-primary{
	height:30px;
	line-height:30px;
	color:#fff;
	background:#1890ff;
	border-radius:3px;
	border:1px solid #1890ff;
	padding: 0 10px;
}
.img-wrapper{
	height:30px;
	width:30px;
	display:inline-block;
	vertical-align:middle;
	position:relative;
}
.img-wrapper img{
	width:100%;
}

    html代码部分:

<h2>4、relative定位的妙用</h2>
<div class="box-content content-relative">
	<form>
		<label class="form-label">username:</label>
		<input type="text" name="username" class="input-item" />
		<span class="img-wrapper"><img src="images/avatar.png" alt="haha"/></span>
		<input type="button" value="add" class="btn-primary" />
	</form>
</div>

<div class="box-content content-relative bugfix">
	<form>
		<label class="form-label">username:</label>
		<input type="text" name="username" class="input-item" />
		<span class="img-wrapper"><img src="images/avatar.png" alt="haha"/></span>
		<input type="button" value="add" class="btn-primary" />
	</form>
</div>

 效果展示以及解决办法:

     解决技巧:

/**position-relative top */
.bugfix{
	
}
.bugfix .form-label{
	position:relative;
	top:2px;
}
.bugfix .btn-primary{
	position:relative;
	top:1px;
}

    最后,给出完整的html和css代码:

    index.html

<!doctype html>
<html>
	<head>
		<meta charset="utf-8"/>
		<title>css demo</title>
		<link rel="stylesheet" type="text/css" href="styles/base.css?_t=123"/>
	</head>
	<body>
		<div id="root">
			<div class="box">
			    <h2>1-1、浮动原生样式</h2>
				<div class="box-header">this is header</div>
				<div class="box-content clearfix">
				   <div class="box-item bg-red fl">left</div>
				   <div class="box-item bg-blue fr">right</div>
				   <!--
				   <div class="box-item bg-green fr">center</div> 
                   -->				   
				</div>
				<div class="box-footer">this is footer</div>
				<div class="tips">
					默认的浮动,我们需要做一些工作,父元素需要设置清除浮动样式,子元素设置浮动,我们需要注意的是子元素有三个的情况,这时候,如果右浮动是两个的时候,我们需要将浮动在最右边的元素放在中间浮动的元素左边,如果不这么设置,最终靠中间的元素会被显示在最右边,很奇怪,这种设置,好像很不容易理解。这也是一种技巧。
				</div>
			</div>
			<div class="box">
			    <h2>1-2、浮动flex样式</h2>
				<div class="box-header">this is header</div>
				<div class="box-content-flex">
				   <div class="box-item bg-red">left</div>  
				   <div class="box-item bg-blue">right</div>				   
				</div>
				<div class="box-footer">this is footer</div>
				<div class="tips">
					flex用来做左右浮动也挺合适的,主要适合于两个子元素的情况。它不需要关心子元素的高度和宽度。
				</div>
			</div>
			<div class="box">
			    <h2>2-1、居中原生样式</h2>
				<div class="box-header">this is header</div>
				<div class="box-content content-wrapper">
					<div class="box-item bg-green align-center"></div>
				</div>
				<div class="box-footer">this is footer</div>
				<div class="tips">	最简单的垂直居中,就是借助定位来实现,即让父元素相对定位,然后子元素绝对定位,向上和向左偏移均为50%,这样,还需要设置外边距,分别为高度和宽度的1/2负值,相当于拉回来一部分,正好居中。
				</div>
			</div>
			<div class="box">
			    <h2>2-2、居中flex布局样式</h2>
				<div class="box-header">this is header</div>
				<div class="box-content content-wrapper-flex">
					<div class="box-item bg-green"></div>
				</div>
				<div class="box-footer">this is footer</div>
				<div class="tips">flex布局,水平垂直居中,其实很好实现,主要在需要居中的元素父级上设置display:flex;justify-content:center;align-items:center;这种方式百试不爽,理解起来也非常容易。</div>
			</div>
			<div class="box">
			    <h2>3、四周阴影</h2>
				<div class="box-content-flex">
					<div class="box-item bg-green border-shadow-around">shadow</div>
					<div class="box-item bg-green border-shadow-not-around">shadow-not-around</div>
				</div>
				<div class="tips">
					tips:四周阴影,在有些地方我们是会使用到的,默认都是两条边的阴影。我们需要四条边都有阴影,就
					设置box-shadow的前面两个参数都为0,即:box-shadow: 0 0 5px #000;
				</div>
			</div>
			<div class="box">
			    <h2>4、relative定位的妙用</h2>
				<div class="box-content content-relative">
					<form>
						<label class="form-label">username:</label>
						<input type="text" name="username" class="input-item" />
						<span class="img-wrapper"><img src="images/avatar.png" alt="haha"/></span>
						<input type="button" value="add" class="btn-primary" />
					</form>
				</div>
				<div class="tips">
					tips:默认我们看到,当这些子元素高度不一致的时候,他们其实很难垂直居中。
				</div>
				<div class="box-content content-relative bugfix">
					<form>
						<label class="form-label">username:</label>
						<input type="text" name="username" class="input-item" />
						<span class="img-wrapper"><img src="images/avatar.png" alt="haha"/></span>
						<input type="button" value="add" class="btn-primary" />
					</form>
				</div>
				<div class="tips">经过relative定位和top偏移之后,元素很好的垂直居中了,几乎在一条水平线上。虽然这个top:1px;或者top:2px;表面看起来,移动并不是很明显,但是对于这种微调来说,已经很好解决了位置偏移的问题。而且这种偏移不会影响周围其他元素,这个小技巧,我个人也觉着百试不爽,尤其是当我们使用右浮动,导致元素靠近父级元素顶部的时候,我们这个微调非常方便。</div>
			</div>
		</div>
	</body>
</html>

    base.css

/**reset*/
html,body,div,p,span,h1,h2,h3,h4,h5,h6,input{margin:0;padding:0;font-weight:normal;}
#root{
	width:1000px;
	margin:0 auto;
}
.tips{
	color:red;
	font-weight:bold;
	font-size:18px;
	margin:20px 0;
}
.box{
	
}
.box-header,.box-footer{
	border:1px solid #ddd;
	border-radius:3px;
	padding:5px;
}
.box-content{
	margin:10px auto;
}
.box-item{
	width:100px;
	height:100px;
	border:1px solid #000;
	border-radius:3px;
}
.bg-red{
	background:red;
}
.bg-blue{
	background:lightblue;
}
.bg-green{
	background:lightgreen;
}


/**1 float */
.clearfix{
	*zoom:1;
}
.clearfix:before,.clearfix:after{
	display:table;
	height:0;
	content:'';
}
.clearfix:after{
	clear:both;
}
.fl{
	float:left;
}
.fr{
	float:right;
}

.box-content-flex{
	margin:10px auto;
	display:flex;
	justify-content:space-between;
}

/**2 vertical horizontal center */
.content-wrapper{
	height:200px;
	background:#eee;
	/**relative for child layout*/
	position:relative;
}
.align-center{
	position:absolute;
	left:50%;
	top:50%;
	margin-left:-50px;
	margin-top:-50px;
}
/**flex layout*/
.content-wrapper-flex{
	height:200px;
	background:#eee;
	/***/
	display:flex;
	justify-content:center;
	align-items:center;
}

/**3 shadow */
.border-shadow-around{
	box-shadow:0 0 10px #000;
}
.border-shadow-not-around{
	box-shadow:5px 5px 10px #000;
}
/**relative tips*/
.content-relative{
	border:1px solid #ddd;
}
.form-label{
	display:inline-block;
	width:100px;
	border:1px solid lightgreen;
	height:28px;
	line-height:28px;
}
.input-item{
	height:28px;
	line-height:28px;
	outline:none;
	border:1px solid #ddd;
	border-radius:3px;
	padding: 2px 5px;
}
input[type='text']:focus{
	border-color:rgb(82,168,236,0.8);
	-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,0,0.075),0 0 8px rgba(82,168,236,0.6);
	-moz-box-shadow:inset 0 1px 1px rgba(0,0,0,0,0.075),0 0 8px rgba(82,168,236,0.6);
	box-shadow:inset 0 1px 1px rgba(0,0,0,0,0.075),0 0 8px rgba(82,168,236,0.6);
}
.btn-primary{
	height:30px;
	line-height:30px;
	color:#fff;
	background:#1890ff;
	border-radius:3px;
	border:1px solid #1890ff;
	padding: 0 10px;
}
.img-wrapper{
	height:30px;
	width:30px;
	display:inline-block;
	vertical-align:middle;
	position:relative;
}
.img-wrapper img{
	width:100%;
}
/**position-relative top */
.bugfix{
	
}
.bugfix .form-label{
	position:relative;
	top:2px;
}
.bugfix .btn-primary{
	position:relative;
	top:1px;
}

    还有一个icon,这里就不贴出来了,如果你有兴趣,其实可以自己随便弄一个替换就OK了。 

    样式的学习,都在平时的积累,有一些样式并不是只能有一种办法实现,我们不要搞混淆了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

css小技巧之浮动,居中,周围阴影,relative妙用 的相关文章

  • Support for the experimental syntax 'decorators-legacy' isn't currently enabled

    如题 xff0c 出现这个错误 xff0c 是因为react项目中使用了 64 语法的装饰器 xff0c 而我们项目的一些配置没有设置正确导致的 为了使用装饰器 xff0c 需要修改如下三处配置 xff1a 1 运行依赖 npm insta
  • react hooks useEffect副作用钩子使用

    react框架开发作为入门 xff0c 我们首先学到的是自定义组件 xff0c 然后编写相关处理逻辑 xff0c 绑定事件等等 一般自定义组件 xff0c 我们是通过编写一个class继承React Component xff0c 然后编写
  • 使用mocha与should库做nodejs单元测试

    nodejs是服务端开发 xff0c 他也可以做单元测试 xff0c 只不过 xff0c 这个单元测试一般是针对某一个文件或者一个模块而言 xff0c 我们经常看到github上的很多项目各自的文件夹中都有很多xxx test js这样的文
  • javascript reduce()方法示例

    reduce在大数据框架mapreduce中是一个合并的过程 xff0c 将所有的数据 xff0c 按照一定的规则合并 xff0c 一步步缩小范围 xff0c 最后合并为一个最大的集合或者对象 在javascript中 xff0c redu
  • 无人驾驶小车调试笔记(六)-- 车轮校准

    简介 xff1a 小车的动力完全来自于两个电机带动的车轮 xff0c 在理想状态下 xff0c 给两个电机同样的驱动参数 xff0c 两个车轮会以同样的转速带动小车直线行驶 xff0c 而实际情况是每个电机可能都会有个体差异 xff0c 也
  • 电力电子技术仿真-单项桥式不可控整流电路(无滤波电容)

    电路模型如下图所示 电路元器件与参数设置如下所示 xff1a Powergui xff1b Linear Transformer xff1b Current Measurement xff1b Voltage Measurement xff
  • git命令行切换到某一个提交版本的分支

    通常 xff0c 我们需要在命令行下切换 分支 xff0c 一般而言 xff0c 我们将远程代码克隆 git clone http xxx com project 到本地之后 xff0c 切换分支使用git checkout b branc
  • linuxmint下gcc编译报错:zlib version 1.2.1 or higher is required

    如题所示 xff0c 在linuxmint系统上通过gcc编译一个工具 xff0c 报错 xff1a 通过dpkg l命令查看系统的zlib库 xff0c 是有zlib1g xff0c 这个就是最新的 很多地方说要安装zlib1g dev
  • MFC与第三方类库CWebPage开发javascript函数调用示例

    今天思索一个问题 xff0c 想着怎么用c 43 43 调用百度地图 xff0c 结果网上有一篇文章介绍了如何使用MFC项目结合CWebPage来调用百度地图 看了整篇博客 xff0c 思路很清晰 xff0c 但是看下来 xff0c 其实就
  • mysql中日期时间戳timestamp使用小结

    timestamp时间戳类型在mysql数据库中比较常见 xff0c 但是我们很容易忽视它的一些特征 xff0c 这个我在面试中吃过亏 xff0c 这里对他的一些用法和特点补充一下 也让自己加深印象 一般来说 xff0c 我们使用日期 xf
  • C++打印整数的八进制十进制十六进制以及打印逻辑布尔类型

    C 43 43 中打印一个整数的八进制 xff0c 十进制 xff0c 十六进制很方便 xff0c 无需定义别的函数或者方法 xff0c 直接通过关键字oct dec hex就可以 xff0c 另外打印布尔类型可以通过关键字boolalph
  • docker-compose构建mongodb容器实例

    docker compose可以一次性开启多个docker实例 xff0c 这一点比Dockerfile来构建docker容器要方便的多 docker compose的重点是对yml文件的配置 yml文件的配置需要注意的是严格控制缩进 需要
  • Mycat数据库中间件初体验

    Mycat是阿里开源的数据库中间件 xff0c 用java语言编写 xff0c 目前是1 x版本 xff0c 2 0版本正在研发中 Mycat支持的数据库很多 xff0c 目前常用的基本都包含了 xff0c mysql postgresql
  • docker使用遇到问题Got permission denied while trying to connect to the Docker daemon socket

    docker安装完成 xff0c 一般用户没有权限启动docker服务 xff0c 只能通过sudo来通过root用户权限来启动docker xff0c 此时对于一般用户而言 xff0c 需要执行docker ps或者docker imag
  • linuxmint下安装nvm来管理node版本

    nvm是一个node版本控制的工具 xff0c 他可以查看可以安装的node版本 xff0c 安装node xff0c 以及切换node版本 xff0c 传统的node安装 xff0c 我们是下载压缩包 xff0c 然后指定环境变量 xff
  • electron结合serialport插件开发硬件指令操作项目

    electron可以开发桌面系统 xff0c serialport包是node环境下连接串口设备的依赖 xff0c 如果是用electron做硬件检测项目 xff0c 需要考虑加入serialport包 xff0c 但是我们直接npm in
  • 无人驾驶小车调试笔记(七)-- 相机校准

    简介 xff1a 在第五节的内容中 xff0c 我们学习了使用rqt工具集观看摄像头视频流的方法 xff0c 细心的同学应该会发现camera node发布的视频数据中的图像有变形现象 xff0c 图像变形会导致直线不直 xff0c 部分区
  • npm install安装依赖报错 Error: spawn powershell.exe ENOENT解决办法

    在windows电脑上通过npm install安装 windows build tools的时候报如题所示的错误 xff0c 一般这种错误 xff0c 基本都是环境变量的问题 xff0c 我这里是因为在Path环境变量下找不到powers
  • nc工具测试tcp/udp协议

    nc是netcat的缩写 xff0c 在windows系统上 xff0c 需要单独下载安装 netcat在windows系统上安装很简单 xff0c 需要到如下页面https eternallybored org misc netcat x
  • Promise结合reduce构建顺序执行队列

    Promise是一个解决函数异步执行的对象 xff0c 有了这个对象 xff0c 我们可以构造异步执行的操作 Promise对象可以通过链式调用的方式进行异步操作 xff0c 语法如下 xff1a 如下代码 xff0c 是一个简单的异步方法

随机推荐