文/小魔女css
要求:背景色分别为red、green、blue;左右各定宽200px;中间随屏幕大小变化;内容互不遮挡;html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.container{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
font-size: 0px;
}
.left{
display: inline-block;
width: 200px;
height: 100%;
background-color: red;
}
.main{
display: inline-block;
width: calc(100% - 400px);
height: 100%;
background-color: green;
}
.right{
display: inline-block;
width: 200px;
height: 100%;
background-color: blue;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="main"></div>
<div class="right"></div>
</div>
</body>
</html>
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.container{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
}
.left{
width: 200px;
background-color: red;
}
.main{
flex: 1;
background-color: green;
}
.right{
width: 200px;
background-color: blue;
}
</style>
</head>
<body>
<!--
flex:1的含义
flex-grow : 1; // 这意味着div将以与窗口大小相同的比例增加
flex-shrink : 1; // 这意味着div将以与窗口大小相同的比例缩小
flex-basis : 0; // 这意味着div没有这样的起始值,而且将根据可用的屏幕大小占用屏幕。例如: - 若是包装器中有3个div,则每一个div将占用33%。 -->
<div class="container">
<div class="left"></div>
<div class="main"></div>
<div class="right"></div>
</div>
</body>
</html>
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.container{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.left{
float: left;
width: 200px;
height: 100%;
background-color: red;
}
.right{
float: right;
width: 200px;
height: 100%;
background-color: blue;
}
.main{
width: auto;
height: 100%;
overflow: hidden;
background-color: green;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="right"></div>
<div class="main"></div>
</div>
</body>
</html>
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.container{
width: 100%;
height: 400px;
display: table;
}
.left{
width: 200px;
height: 100%;
background-color: red;
display: table-cell;
}
.right{
width: 200px;
height: 100%;
background-color: blue;
display: table-cell;
}
.main{
width: auto;
height: 100%;
background-color: green;
display: table-cell;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="main"></div>
<div class="right"></div>
</div>
</body>
</html>
复制代码
基础前端
“面试
margin和padding的百分比都是相对父元素的宽度来设置的。 absolute和float两个元素,默认绝对定位覆盖在浮动元素上面。设置z-index:-1后,浮动元素在绝对定位元素的上面。 设置position:relative后,元素相对于自身进行定位。(相对于元素在文档中的初始位置) ↑注意:使用相对定位,不管是否移动,元素仍然占据原来的空间。所以,移动元素会致使它覆盖其余框。 position:absolute,若是没有已定位的元素,就会找到body。 ↑注意:absolute和float不能写在同一个元素(relative和float能够),absolute或者float会隐式改变display的类型(display:none除外)。设置position:absolute,float:left,float:right任意一个值,都会使元素变成display:inline-block。即便设置display:inline,display:block也无效。 float在IE6下的双边距bug就是用display:inline来解决的。position:relative不会隐式改变display。**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.container {
width: 100%;
height: 500px;
padding: 0 300px 0 200px;
box-sizing: border-box;
/* box-sizing差点忘了 border-box的宽度是内容+border+padding 而content-box的宽度只是内容 */
}
.container>div {
float: left;
height: 100%;
}
.main {
width: 100%;
background-color: green;
}
.left {
width: 200px;
margin-left: -100%;
position: relative;
left: -200px;
background-color: red;
}
.right {
width: 300px;
margin-left: -300px;
position: relative;
left: 300px;
background-color: blue;
}
</style>
</head>
<body>
<div class="container">
<div class="main"></div>
<div class="left"></div>
<div class="right"></div>
</div>
</body>
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.container {
width: 100%;
height: 500px;
}
.container>div {
float: left;
height: 100%;
}
.main {
width: 100%;
}
.submain {
height: 100%;
/* 浮动元素的子元素不给高度,至关于高度为0 */
background-color: green;
margin: 0 300px 0 200px;
}
.left {
width: 200px;
margin-left: -100%;
background-color: red;
}
.right {
width: 300px;
margin-left: -300px;
background-color: blue;
}
</style>
</head>
<body>
<div class="container">
<div class="main">
<div class="submain"></div>
</div>
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.container {
width: 100%;
height: 500px;
}
.container>div {
height: 100%;
}
.main {
margin: 0 300px 0 200px;
background-color: green;
}
.left {
width: 200px;
float: left;
background-color: red;
}
.right {
width: 300px;
float: right;
position: relative;
top: -500px;
/* 添加上面两行的缘由是,main是块级元素,独占一行,right元素会在第二行显示。 */
background-color: blue;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="main"></div>
<div class="right"></div>
</div>
</body>
</html>
复制代码
html:markdown
<div class="container">
<div class="left">
<p>aaa</p>
</div>
<div class="main">
<p>bbb</p>
<p>bbb</p>
<p>bbb</p>
</div>
<div class="right">
<p>ccc</p>
<p>ccc</p>
</div>
</div>
复制代码
<style type="text/css">
.container {
display: table;
margin: 0 auto;
border: 1px solid black;
}
.container>div {
width: 200px;
display: table-cell;
}
.left {
background-color: red;
}
.main {
background-color: orange;
}
.right {
background-color: yellow;
}
</style>
复制代码
<style type="text/css">
.container {
display: flex;
width: 600px;
margin: 0 auto;
border: 1px solid black;
}
.container>div {
flex: 1;
/* flex-grow:1; flex-shrink 1;flex-basis:0;
div与窗口大小相同的比例增加、缩小,div没有起始值,而且将根据可用屏幕大小占用屏幕
*/
}
</style>
复制代码
<style type="text/css">
.container {
width: 600px;
margin: 0 auto;
border: 1px solid black;
overflow: hidden;
}
.container>div {
float: left;
width: 200px;
margin-bottom: -999em;
padding-bottom: 999em;
}
</style>
复制代码
一个220x220的元素A,一个20x20的元素B,B中有一个文字“C”,字体大小15px。要求,A在屏幕中央,B在A中央,文字“C”在B中央,中央即为水平垂直都居中。 html:oop
<div class="container">
<div class="a">
<div class="b">C</div>
</div>
</div>
复制代码
<style type="text/css">
.container {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: flex;
align-items: center;
justify-content: center;
}
.a {
width: 200px;
height: 200px;
background-color: red;
display: flex;
align-items: center;
justify-content: center;
}
.b {
width: 20px;
height: 20px;
font-size: 15px;
background-color: yellow;
display: flex;
align-items: center;
justify-content: center;
}
</style>
复制代码
<style>
.container {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.a,
.b {
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
.a {
width: 200px;
height: 200px;
background-color: red;
}
.b {
width: 20px;
height: 20px;
font-size: 15px;
background-color: yellow;
}
</style>
复制代码
.a{ width: calc(100% - 40px); }布局
.container { margin: 0 20px; } .a { width: 100%; }字体
.a {
width: calc(100vw - 40px);
height: calc((100vw - 40px)/2);
}
复制代码
好比width:50%; 以后设置该box为正方形设置height:0; padding-bottom:50%;flex
月是故乡明,你偶尔想念故乡,故乡一直在想你。在评论区为故乡送上最真挚的祝福吧~spa
本文使用 mdnice 排版