Appearance
示例:使用 flex-shrink: 0
固定元素不被挤压
html
<!DOCTYPE html>
<html lang="en">
<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>Flex Shrink Example</title>
<style>
.container {
display: flex;
gap: 10px;
}
.item {
flex-shrink: 0;
width: 100px;
height: 100px;
background-color: lightblue;
}
.item-flex {
flex-grow: 1;
background-color: lightgreen;
}
.outside-circle {
width: 100px;
position: relative;
background: #e91e63;
border-radius: 10px 0px 10px 0px;
}
.outside-circle::before {
content: "";
position: absolute;
width: 20px;
height: 20px;
left: 0px;
transform: rotate(180deg);
bottom: -20px;
background: #000;
background: radial-gradient(circle at 0 0, transparent 20px, #e91e63 21px);
}
.outside-circle::after {
content: "";
position: absolute;
width: 20px;
transform: rotate(90deg);
height: 20px;
right: -20px;
top: 0;
background: #000;
background: radial-gradient(circle at 100% 0, transparent 20px, #e91e63 21px);
}
.main {
width: 100%;
height: 229px;
display: flex;
justify-content: center;
align-items: center;
}
span {
display: inline-block;
width: 21ch;
font: bold 200% Consolas, Monaco, monospace;
overflow: hidden;
white-space: nowrap;
font-weight: 500;
border-right: 1px solid transparent;
animation: typing 10s steps(21), caret .5s steps(1) infinite;
}
@keyframes typing {
from {
width: 0;
}
}
@keyframes caret {
50% {
border-right-color: currentColor
}
}
</style>
</head>
<body>
<h2>Flex Shrink Example</h2>
<div class="container">
<div class="item">Fixed Item 1</div>
<div class="item item-flex">Flexible Item</div>
<div class="item">Fixed Item 2</div>
</div>
<h2>Custom Rounded Corners Example</h2>
<div class="outside-circle">outside-circle</div>
<h2>Typing Effect Example</h2>
<main class="main">
<span>You-need-to-know-css!</span>
</main>
</body>
</html>
代码说明
Flex Shrink Example:
.container
使用display: flex
来创建一个弹性盒子容器。.item
使用flex-shrink: 0
来防止元素被挤压,从而保持其固定宽度。.item-flex
使用flex-grow: 1
使其可以扩展并填充剩余空间。
Custom Rounded Corners Example:
.outside-circle
是一个带有独特圆角样式的div
元素。- 使用伪元素
::before
和::after
来创建自定义的圆角效果。
Typing Effect Example:
.main
是一个居中对齐的容器。span
通过keyframes
动画实现打字效果。
这个示例展示了如何使用CSS来控制布局、实现自定义样式,以及添加动画效果。