SS中的浮动(Floats)、定位(Positioning)和显示(Display)属性是前端工程师掌握页面布局的关键。本文将深入探讨这些属性的工作原理和使用场景,帮助开发者更好地理解和运用它们来构建响应式和精确的网页布局。
浮动是CSS中用于实现元素排列的一种方式,它可以让元素脱离正常的文档流,并可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动元素的边缘。
.element {
float: left; /* 或者 'right' */
}
.clear-element {
clear: both; /* 可以是 'left', 'right', 或 'both' */
}
定位属性允许你控制元素的位置,它可以是相对于它的正常位置、相对于最近的已定位祖先元素、相对于视口或绝对位置。
.element {
position: static | relative | absolute | fixed | sticky;
}
.relative-element {
position: relative;
top: 10px;
left: 20px;
}
.absolute-element {
position: absolute;
top: 0;
right: 0;
}
.fixed-element {
position: fixed;
bottom: 0;
left: 0;
}
.sticky-element {
position: sticky;
top: 10px;
}
display属性是CSS中最重要的用于控制布局的属性之一,它定义了元素如何显示在页面上。
.element {
display: block | inline | inline-block | flex | grid | none;
}
.block-element {
display: block;
}
.inline-element {
display: inline;
}
.inline-block-element {
display: inline-block;
}
.flex-container {
display: flex;
}
.grid-container {
display: grid;
}
.hidden-element {
display: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Float, Position, and Display Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="header">
<div class="logo">Logo</div>
<div class="navigation">Navigation</div>
</div>
<div class="main-content">
<div class="sidebar">Sidebar</div>
<div class="content">Content</div>
</div>
<div class="footer">Footer</div>
<div class="fixed-element">Fixed Element</div>
</body>
</html>
/* Reset some default styles */
body, h1, p {
margin: 0;
padding: 0;
}
/* Header styles */
.header {
background-color: #f8f8f8;
border-bottom: 1px solid #e7e7e7;
padding: 10px;
overflow: hidden; /* Clearfix for floated elements */
}
.logo {
float: left;
font-size: 24px;
}
.navigation {
float: right;
font-size: 18px;
}
/* Main content styles */
.main-content {
padding: 20px;
}
.sidebar {
float: left;
width: 200px;
background-color: #ddd;
padding: 10px;
}
.content {
margin-left: 220px; /* Make space for the sidebar */
background-color: #eee;
padding: 10px;
}
/* Footer styles */
.footer {
background-color: #f8f8f8;
border-top: 1px solid #e7e7e7;
text-align: center;
padding: 10px;
position: relative; /* For demonstration purposes */
top: 20px; /* Move the footer down a bit */
}
/* Fixed element styles */
.fixed-element {
position: fixed;
bottom: 10px;
right: 10px;
padding: 5px 10px;
background-color: #333;
color: #fff;
z-index: 1000; /* Ensure it stays on top */
}
/* Clearfix hack */
.clearfix::after {
content: "";
clear: both;
display: table;
}
在这个例子中,我们创建了一个包含头部、侧边栏、主要内容和页脚的基本布局。我们使用浮动来对齐头部的Logo和导航,以及创建一个侧边栏。我们还使用了相对定位来稍微下移页脚,并使用固定定位为页面添加了一个始终可见的固定元素。最后,我们使用了overflow: hidden;来清除头部中浮动元素的影响。
浮动、定位和显示属性是CSS中构建复杂布局的强大工具。通过深入理解和正确应用这些属性,前端工程师可以创建出既美观又功能强大的网页。随着Web标准的不断发展,我们也需要不断学习和适应新的CSS特性,以保持我们技能的前沿性。
这里是云端源想IT,帮你轻松学IT”
嗨~ 今天的你过得还好吗?
有些时候
为了看到光明
你必须冒险闯入黑暗之中
- 2024.03.29 -
CSS定位属性是用于控制网页中元素位置的一种方式,它能够让元素在页面上精准地落在我们想要的位置。
在CSS中,定位(Positioning)是控制元素在页面上如何定位和显示的一种机制。它主要包括四种属性:静态定位(static)、相对定位(relative)、绝对定位(absolute)、固定定位(fixed)。
每种定位方式都有其独特的特点和使用场景,下面将分别介绍这几种定位属性。
静态定位是元素的默认定位方式,元素按照正常的文档流进行排列。在静态定位状态下,不能配合top、bottom、left、right来改变元素的位置。
代码示例:
<!DOCTYPE html>
<html>
<head>
<style>
.static {
background-color: lightblue;
padding: 100px;
}
</style>
</head>
<body>
<div>这是一个静态定位的元素。</div>
</body>
</html>
固定定位使元素相对于浏览器窗口进行定位,即使页面滚动,元素也会保持在固定的位置。
示例代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
body{
/* 给整个页面设置高度,出滚动条以便观察 */
height: 5000px;
}
div{
width: 100px;
height: 100px;
background-color: blue;
/* 固定定位 */
position: fixed;
right: 100px;
bottom: 100px;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
运行结果:
移动前
移动后
比如我们经常看到的网页右下角显示的“返回到顶部”,就可以用固定定位来实现。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
position: relative;
}
.content {
/* 页面内容样式 */
}
#backToTop {
position: fixed;
bottom: 20px;
right: 20px;
background-color: #333;
color: #fff;
border: none;
padding: 10px;
cursor: pointer;
}
</style>
</head>
<body style="height: 5000px;">
<div>
</div>
<button id="backToTop" onclick="scrollToTop()">返回顶部</button>
<script>
function scrollToTop() {
window.scrollTo({top: 0, behavior: 'smooth'});
}
</script>
</body>
</html>
运行结果:
相对定位是将元素对于它在标准流中的位置进行定位,通过设置边移属性top、bottom、left、right,使指定元素相对于其正常位置进行偏移。如果没有定位偏移量,对元素本身没有任何影响。
代码示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1{
width:200px;
height:100px;
background:skyblue;
margin:10px;
}
.box2{
width:200px;
height:100px;
background:pink;
margin:10px;
position:relative;/*相对定位*/
left:100px;/*向右偏移100px*/
top:-50px;/*向上偏移50px*/
}
.box3{
width:200px;
height:100px;
background:yellowgreen;
margin:10px;
}
</style>
</head>
<body>
<div>1</div>
<div>2</div>
<div>3</div>
</body>
</html>
运行结果:
没使用相对定位之前是这样的:
使用相对定位后:相对于原来的位置向右偏移了100px,向上偏移50px。
虽然它的位置发生了变化,但它在标准文档流中的原位置依然保留。
绝对定位使元素相对于最近的非 static 定位祖先元素进行定位。如果没有这样的元素,则相对于初始包含块(initial containing block)。绝对定位的元素会脱离正常的文档流。
<style>
.wrap{
width:500px;
height:400px;
border: 2px solid red;
}
.box1{
width:200px;
height:100px;
background:skyblue;
margin:10px;
}
.box2{
width:200px;
height:100px;
background:pink;
margin:10px;
position:absolute;/*绝对定位*/
left:100px;/*向右偏移100px*/
top:30px;/*向下偏移30px*/
}
.box3{
width:200px;
height:100px;
background:yellowgreen;
margin:10px;
}
</style>
<div>
<div>1</div>
<div>2</div>
<div>3</div>
</div>
将第二个设置为绝对定位后,它脱离了文档流可以定位到页面的任何地方,在标准文档流中的原有位置会空出来,所以第三个会排到第一个下面。
第二个相对于它的父元素向右偏移100,向下偏移30。
想要快速入门前端开发吗?推荐一个前端开发基础课程,这个老师讲的特别好,零基础学习无压力,知识点结合代码,边学边练,可以免费试看试学,还有各种辅助工具和资料,非常适合新手!点这里前往学习哦!云端源想
层叠顺序决定了元素之间的堆叠顺序。z-index 属性用于设置元素的层叠顺序。具有较高 z-index 值的元素会覆盖具有较低 z-index 值的元素。
注意:
代码示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div:nth-of-type(1){
width: 300px;
height: 300px;
background-color: skyblue;
position: absolute;
}
div:nth-of-type(2){
width: 200px;
height: 200px;
background-color: pink;
position: absolute;
}
div:nth-of-type(3){
width: 100px;
height: 100px;
background-color: yellowgreen;
position: absolute;
z-index: -1;
}
</style>
</head>
<body>
<div></div>
<div></div>
<div></div>
</body>
</html>
运行结果:
可以看到,最后一个div依然存在,但是看不见了,原因就是我们改变了z-index属性值。
以上就是CSS定位属性的介绍了,通过这些定位属性,可以灵活地控制网页中元素的位置和堆叠顺序。
在实际应用中,CSS定位属性的使用需要考虑到整体布局和用户体验。合理运用这些定位技巧,可以让你的网页不仅美观,而且易于使用和维护。记住,好的设计总是细节和功能的完美结合。
我们下期再见!
END
文案编辑|云端学长
文案配图|云端学长
内容由:云端源想分享
用CSS有普通流、绝对定位和浮动三种基本的定位机制,如果不是专门指定区块的位置都是在普通流中定位,即从上到下一个接一个地排列,位置
由元素在HTML中的位置决定。
文档中默认都是静态定位
使用相对定位的盒子的位置常以标准流的排版方式为基础, 然后使盒子相对于它在原本的标准位置偏移指定的距离。
相对定位的盒子仍在标准流中, 它后面的盒子仍以标准流方式对待它。
使用relative, 即相对定位, 除了将position属性设置为relative之外, 还需要指定一定的偏移量,
水平方向通过left或者right属性来指定, 垂直方向通过top和bottom来指定。
使用相对定位的盒子,会相对于它原本的位置,通过偏移指定的距离,到达新的位置。而该相对定位的盒子则仍然位于标准流中,它对父块没有任何影响。
使用相对定位的盒子不仅对父块没有任何影响,对兄弟盒子也没有任何影响。
简单理解:
它是相对于"原来的自己"进行定位, 相对定位元素它还占用空间, 它的层次会比普通元素要高
相对定位属性它一般都是用于配合绝对定位来使用
相对定位有坑, 所以一般不用于做"压盖"效果。页面中, 效果极小。就两个作用:
1) 微调元素
2) 做绝对定位的参考,子绝父相(配合绝对定位一起使用)
相关属性
可以用left、right来描述盒子右、左的移动;
可以用top、bottom来描述盒子的下、上的移动。
用绝对定位的盒子以它的"最近"的一个"已经定位"的"祖先元素"为基准进行偏移。如果没有已经定位的祖先元素,那么会以浏览器窗口为基准进行定位。
再有,绝对定位的框从标准流中脱离,这意味着它们对其后的兄弟盒子的定位没有任何影响,其他的盒子就好像这个盒子不存在一样。
简单理解:
绝对定位元素它是相对于"祖先"定位元素(relative、fixed、absolute),如果绝对定位元素它的祖先没有定位元素(static)那么它会以当前的浏览器窗口来进行定位,
绝对定位元素不再占用空间
当position的属性值为fixed,即固定定位。它与绝对定位有些类似,区别主要在于定位的基准不是祖先,而是浏览器窗口或其它显示设备窗口。
也就是当访问者拖动浏览器的窗口滚动条时,固定定位的元素相对于浏览器窗口的位置保持不变。(IE6不支持固定定位)
简单理解:它是以浏览器窗口作为参照标准来进行定位, 固定定位元素它不再占用空间
定位问题:
绝对定位问题:
当我们给一个子元素设置绝对定位,让它在父元素的右下角显示,这个时候
如果父元素的宽和高都为奇数,那么在IE6下我们没有方法让这个子元素压住边框的
这个时候我们建议大家不要使用奇数作为长度
相对定位问题;
IE6下,它认为我们的父元素一定包住它里面的子元素,所以当子元素的宽度和高度大于父元素时,在IE下父元素就会被撑开,
这个时候我们想解决这个问题,我们可以给父元素设置overflow:hidden,但是此时IE6还是不起作用,
所以我们解决方法就是给子元素和父元素都设置相对定位
固定定位的问题:
当我们给一个固定定位的子元素(子元素position:fixed)的父元素添加相对定位或者绝对定位的时候,
它是不会跟着父元素移动的,仍然是相对屏幕的。
实例:导航条
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<title>导航条</title>
<style type="text/css">
*{
margin:0px;
padding:0px;
}
ul,li{
list-style:none;
}
.box{
width:800px;
height:30px;
margin:100px auto;
}
li{
float:left;
width:180px;
text-align: center;
line-height:28px;
border:1px solid red;
margin-left:-1px;
}
li:hover{
border:1px solid green;
position:relative; /* 解决边框被压住问题 解释:后面的li压住前面一个li,使用相对定位后,提高了li显示高度 */
}
</style>
</head>
<body>
<div class="box">
<ul>
<li>HTML+CSS</li>
<li>javascript</li>
<li>jquery</li>
<li>php</li>
</ul>
</div>
</body>
</html>
让层水平居中的方法:
让左右边界自动判断之下,即可达成左右位置呈现居中的目的。
#content {
width: 980px;
margin-right: auto;
margin-left: auto;
position: relative;
}
<div id="content">层内容</div>
若内容区<div>之内还有其他内容<div>,其CSS属性设置就需要加上"定位"position:absolute(或relative),以防firefox等其他浏览器出现错误
<div id="container">
<div id="content">
</div>
</div>
#container {
background-image: url(images/3-2-1-bg.jpg);
height: 400px;
width: 800px;
position: relative;
background-repeat: no-repeat;
margin-right: auto;
margin-left: auto;
}
#content {
margin: 75px;
height: 210px;
width: 610px;
padding: 20px;
border: 1px solid #FFF;
position: absolute;
}
让层垂直居中的方法:
*请认真填写需求信息,我们会在24小时内与您取得联系。