HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
53
<head>
<meta charset="utf-8">
<body>
<div>
文章地址:<a href="https://juejin.cn/post/6960844183611375630" target="_blank">【CSS】CSS布局解决方案(终结版)</a>
</div>
<div>
<h1>2.1.1 水平垂直居中布局 - 固定宽高 - absolute + 负margin</h1>
<div class="father211">
<div class="son211"></div>
</div>
</div>
<div>
<h1>2.1.2 水平垂直居中布局 - 固定宽高 - absolute + margin auto</h1>
<div class="father212">
<div class="son212"></div>
</div>
</div>
<div>
<h1>2.2.1 水平垂直居中布局 - 不固定宽高 - absolute + transform</h1>
<div class="father221">
<div class="son221"></div>
</div>
</div>
<div>
<h1>2.2.2 水平垂直居中布局 - 不固定宽高 - flex</h1>
<div class="father222">
<div class="son222"></div>
</div>
</div>
<div>
<h1>2.2.3 水平垂直居中布局 - 不固定宽高 - table布局</h1>
<div class="father223">
<div class="son223"></div>
</div>
</div>
<div>
<h1>3.1 单列布局 - 普通布局</h1>
<div class="container31">
<div class="header31"></div>
<div class="content31"></div>
<div class="footer31"></div>
</div>
</div>
<div>
<h1>3.2 单列布局 - 内容居中</h1>
<div class="container32">
<div class="header32"></div>
<div class="content32"></div>
<div class="footer32"></div>
</div>
</div>
<div>
<h1>4.1 两栏布局 - 左侧定宽、右侧自适应 - float</h1>
<div class="container">
<div class="left41"></div>
<div class="right41"></div>
</div>
</div>
<div>
<h1>4.2 两栏布局 - 左侧定宽、右侧自适应 - flex</h1>
<div class="container42">
<div class="left42"></div>
<div class="right42"></div>
</div>
</div>
<div>
<h1>5.1 三栏布局 - 左右固定、中间自适应 - flex布局</h1>
<div class="box51">
<div class="center51">中</div>
<div class="left51">左</div>
<div class="right51">右</div>
</div>
</div>
<div>
<h1>5.2 三栏布局 - 左右固定、中间自适应 - 圣杯布局</h1>
<div class="box52">
<div class="center52">中哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈中</div>
<div class="left52">左</div>
<div class="right52">右</div>
</div>
</div>
<div>
<h1>5.3 三栏布局 - 左右固定、中间自适应 双飞翼局(TODO 真难弄懂)</h1>
<p>让你快速掌握float和position - 知乎 https://zhuanlan.zhihu.com/p/142630896</p>
<div class="box53">
<div class="center53">
<div class="content53">
中哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈中
</div>
</div>
<div class="left53">左</div>
<div class="right53">右</div>
</div>
</div>
<div>
<h1>6.1 等高布局 - 正值内边距+负值外边距</h1>
<div class="container61">
<div class="left61">左左左</div>
<div class="right61">
<p>右右右右</p>
<p>右右右右</p>
<p>右右右右</p>
</div>
</div>
</div>
<div>
<h1>6.3 等高布局 - flex布局</h1>
<div class="container63">
<div class="left63">左左左</div>
<div class="right63">
<p>右右右右</p>
<p>右右右右</p>
<p>右右右右</p>
</div>
</div>
</div>
</body>
/* 2.1.1 水平垂直居中布局 - 固定宽高 - absolute + 负margin */
.father211 {
position: relative;
width: 500px;
height: 300px;
border: 1px solid red;
background-color: black;
}
.son211 {
position: absolute;
/*水平居中*/
left: 50%;
/* 向左偏移本身一半宽度 */
margin-left: -50px;
/*垂直居中*/
top: 50%;
/* 向上偏移本身一半高度 */
margin-top: -50px;
width: 100px;
height: 100px;
background-color: red;
}
/** 2.1.2 水平垂直居中布局 - 固定宽高 - absolute + margin auto */
.father212 {
position: relative;
width: 500px;
height: 300px;
border: 1px solid red;
background-color: black;
}
.son212 {
position: absolute;
margin: auto;
left: 0;
right: 0;
top: 0;
bottom: 0;
width: 100px;
height: 100px;
background-color: red;
}
/* 2.2.1 水平垂直居中布局 - 不固定宽高 - absolute + transform */
.father221 {
position: relative;
width: 500px;
height: 300px;
border: 1px solid red;
background-color: black;
}
.son221 {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 100px;
height: 100px;
background-color: red;
}
/* 2.2.2 水平垂直居中布局 - 不固定宽高 - flex */
.father222 {
display: flex;
justify-content: center; /*水平居中*/
align-items: center; /*垂直居中*/
width: 500px;
height: 300px;
border: 1px solid red;
background-color: black;
}
.son222 {
width: 100px;
height: 100px;
background-color: red;
}
/* 2.2.3 水平垂直居中布局 - 不固定宽高 - flex */
.father223 {
display: table-cell;
text-align: center;
vertical-align: middle;
width: 500px;
height: 300px;
border: 1px solid red;
background-color: black;
}
.son223 {
display: inline-block;
width: 100px;
height: 100px;
background-color: red;
}
/** 3.1 单列布局 - 普通布局 **/
.header31 {
margin: 0 auto;
max-width: 960px;
height: 100px;
background-color: pink;
}
.content31 {
margin: 0 auto;
max-width: 960px;
height: 300px;
background-color: aquamarine;
}
.footer31 {
margin: 0 auto;
max-width: 960px;
height: 100px;
background-color: skyblue;
}
/** 3.2 单列布局 - 内容居中 **/
.header32 {
margin: 0 auto;
height: 100px;
background-color: pink;
}
.content32 {
margin: 0 auto;
width: 960px;
height: 300px;
background-color: aquamarine;
}
.footer32 {
margin: 0 auto;
height: 100px;
background-color: skyblue;
}
/** 4.1 两栏布局 - 左侧定宽、右侧自适应 - float **/
.left41 {
float: left;
width: 300px;
height: 500px;
background-color: pink;
}
.right41 {
width: 100%;
height: 500px;
background-color: aquamarine;
}
/** 4.2 两栏布局 - 左侧定宽、右侧自适应 - flex **/
.container42 {
display: flex;
}
.left42 {
width: 300px;
height: 500px;
background-color: pink;
}
.right42 {
flex: 1;
width: 100%;
height: 500px;
background-color: aquamarine;
}
/** 5.1 三栏布局 - 左右固定、中间自适应 - flex布局 **/
* {
margin: 0;
padding: 0;
}
.box51 {
min-width: 800px;
height: 600px;
background: gray;
display: flex;
}
.left51 {
order: 0;
width: 200px;
height: 600px;
background: pink;
}
.center51 {
flex: 1;
order: 1;
width: 100%;
height: 600px;
background: aquamarine;
}
.right51 {
order: 2;
width: 200px;
height: 600px;
background: skyblue;
}
/** 5.2 三栏布局 - 左右固定、中间自适应 - 圣杯布局 **/
* {
margin: 0;
padding: 0;
}
.box52 {
min-width: 800px;
height: 600px;
/* 4、padding左右盒子的宽度 */
padding-left: 200px;
padding-right: 200px;
background-color: black;
}
.left52 {
float: left;
margin-left: -100%;
position: relative;
left: -200px;
width: 200px;
height: 600px;
background: pink;
}
.center52 {
float: left;
width: 100%;
height: 600px;
background: aquamarine;
}
.right52 {
float: left;
margin-left: -200px;
position: relative;
right: -200px;
width: 200px;
height: 600px;
background: skyblue;
}
/** 5.3 三栏布局 - 左右固定、中间自适应 双飞翼局 **/
* {
margin: 0;
padding: 0;
}
.box53 {
min-width: 800px;
height: 600px;
}
.left53 {
float: left;
width: 200px;
height: 600px;
margin-left: -100%;
background: pink;
}
.content53 {
margin-left: 200px;
margin-right: 200px;
background: yellowgreen;
}
.center53 {
float: left;
width: 100%;
height: 600px;
background: aquamarine;
}
.right53 {
float: left;
width: 200px;
height: 600px;
margin-left: -200px;
background: skyblue;
}
/** 6.1 等高布局 - 正值内边距+负值外边距 **/
* {
margin: 0;
padding: 0;
}
.container61 {
width: 400px;
margin: 0 auto;
background-color: black;
overflow: hidden;
}
.left61,
.right61 {
float: left;
width: 50%;
text-align: center;
background-color: aquamarine;
/* 设置正值内边距会把背景颜色拉伸 */
padding-bottom: 9999px;
/* 设置负值外边距把边框往里推 */
margin-bottom: -9999px;
}
.right61 {
background-color: pink;
}
/** 6.1 6.3 等高布局 - flex布局 **/
* {
margin: 0;
padding: 0;
}
.container63 {
display: flex;
width: 400px;
margin: 0 auto;
}
.left63,
.right63 {
text-align: center;
background-color: aquamarine;
flex: 1;
}
.right63 {
background-color: pink;
}
Also see: Tab Triggers