Skip to main content

CSS学习

原项目地址

选择器

/*元素选择器*/
html {
}

/*类选择器*/
.gray {
}

/*id选择器*/
#unique {
}

/*伪类选择器*/
a:hover {
}

/*伪元素选择器*/
ul ::marker {
}

/*运算符选择器 选择了<article>元素的初代子元素*/
article > p {
}

字体设置

常用几个如下:其它的font开头的省略

px

body {
font-size: 22px;
font-family: Arial, Helvetica, sans-serif;
font-weight: 300;
}

em rem

h1 {
border: 2px dashed red;
width: 50%;
/* rem base the root element
and em base the itself fontsize */
font-size: 3rem;
padding: 0.5em;
}

如何使用外部字体

@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');

body {
padding: 25%;
font-size: 2rem;
}

/* input, button {
font: inherit;
} */

p {
/* text-transform: capitalize;
text-align: right; */
/* text-indent: 2em; */

/* line-height: 1.5; */
/* letter-spacing: 0.1em; */
/* word-spacing: 0.1em; */
/* font-weight: 900; */
/* font-style: italic; */
/* font-family: 'Courier New', Courier, monospace; */
font-family: 'Roboto', sans-serif;
}

盒子设置

box-sizing: content-box;: 这是默认值。在这种模型下,

元素的宽度和高度仅包括内容区域,不包括边框(border)、

内边距(padding)和外边距(margin)。这是传统的盒模型。

box-sizing: border-box;: 在这种模型下,

元素的宽度和高度包括了内容区域、边框和内边距,

但不包括外边距。

h1 {
box-sizing: border-box;
border: 2px dashed red;
}
.container {
border: 10px double red;
font-size: 1.5rem;
/* margin-top: 1.5em;
margin-right: 2em;
margin-bottom: 2em;
margin-left: 2em; */

/* margin: 1.5em 2em 2em; */
/* define the top right bottom left margin */
/* margin: 1em 2em 3em 4em; */
margin: 1.5em;
padding: 1.5em;
/*
border-top: 5px solid red;
border-right: 10px dotted green; */
outline: 5px solid purple;
outline-offset: -20px;
}

画圆

<div class="circle"></div>
.circle {
margin: 3rem auto;
background-color: gold;
width: 100px;
height: 100px;
border: 2px solid black;
outline: 2px solid red;
outline-offset: 0.25rem;
border-radius: 50%;
}

设置颜色

/*  you can change the hover or focus color by changing the hue or transparent, or opacity */
a:hover, a:focus {
/* color: hsl(189, 44%, 49%) */
color: hsl(207, 44%, 49%, 0.8)
/* opacity: 0.8; */
}

列表操作

ol {
list-style-type: lower-alpha;
padding: 0;
}

ul {
list-style-type: square;
list-style-position: inside;
/* list-style-image: url('../images/checkmark.png'); */
/* list-style: square url('../images/checkmark.png') inside; */

text-align: center;
color: #00f;
line-height: 1.6;
}

ul li:nth-child(even) {
color: red;
}

ul ::marker {
color: red;
font-family: fantasy;
font-size: 1em;
content: "ONLY $5 >> "
}

对链接样式

li a {
display: block;
}

li a,
li a:visited {
text-decoration: none;
color: #333;
}

li a:hover,
li a:focus {
background-color: #333;
color: whitesmoke;
cursor: pointer;
}

li:last-child a {
border-radius: 0 0 2rem 2rem;
}

文字

排版样式

.left {
float: left;
margin-right: 1rem;
}

.right {
float: right;
margin-left: 1rem;
}

.clear {
clear: both;
}

section {
background-color: bisque;
border: 1px solid #333;
padding: 1rem;
/* fix element overflow section block */
/* fix element overflow section block */
/* overflow: auto; */
display: flow-root;
}
.columns {
/* column-count: 4;
column-width: 250px; */
columns: 4 250px;
column-rule: 3px solid #000;
column-gap: 3rem;
}

.columns p {
margin-top: 0;
}

.columns h2 {
margin-top: 0;
background-color: #333;
color: white;
padding: 1rem;
break-inside: avoid;
/* break-before: column; */
}

.columns .quote {
margin-top: 2rem;
font-size: 3rem;
text-align: center;
color: #333;
column-span: all;
}

.nowrap {
white-space: nowrap;
}

position 使用

.absolute {
background-color: #00f;
position: absolute;
top: 0;
left: 0;
/* adjust the element layer 1 to set the top of others */
z-index: 1;
}

.relative {
background-color: #f00;
position: relative;
top: 300px;
left: 100px;
}

.fixed {
background-color: green;
position: fixed;
top: 100px;
}

footer {
background-color: #000;
position: sticky;
bottom: 0;
font-size: 3rem;
}

flex使用


.container {
max-width: 800px;
min-height: 400px;
margin-inline: auto;
border: 1px solid #000;
display: flex;
gap: 1rem;
justify-content: center;
align-items: center;
flex-flow: row wrap;
align-content: space-evenly;
}

.box {
/* min-width: 100px; */
height: 100px;
background-color: #000;
color: #fff;
font-size: 2rem;
padding: 0.5rem;

display: flex;
justify-content: center;
align-items: center;

flex: 1 1 150px;
}

.box:nth-child(2) {
flex: 2 2 150px;
order: 1;
}

grid使用

<body>
<header class="header el"><h1>Header</h1></header>
<main class="container">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</main>
<aside class="sidebar el"><h2>Sidebar</h2></aside>
<footer class="footer el"><h2>Footer</h2></footer>
</body>
body {
font-family: "Roboto", sans-serif;
min-height: 100vh;

display: grid;
grid-template-columns: repeat(9, 1fr);
grid-auto-rows: 50px auto 50px;
grid-template-areas:
"hd hd hd hd hd hd hd hd hd"
"mn mn mn mn mn mn mn sb sb"
"ft ft ft ft ft ft ft ft ft";
column-gap: 0.5rem;
}

.header {
grid-area: hd;
}

.sidebar {
grid-area: sb;
background-color: #00f;
}

.footer {
grid-area: ft;
}

.container {
grid-area: mn;
min-height: 400px;
display: grid;
grid-template-columns: repeat(2, 1fr 2fr);
grid-auto-rows: minmax(150px, auto);
gap: 1rem;
}

.el {
background-color: rebeccapurple;
color: #fff;
display: grid;
place-content: center;
}

.box {
background-color: #000;
color: #fff;
font-size: 2rem;
padding: 0.5rem;
}

.box:first-child {
background-color: #00f;
grid-column: 1 / 4;
grid-row: 1 / 3;

display: grid;
place-content: center;
}

.box:nth-child(2) {
background-color: purple;
/* grid-column: 1 / 5;
grid-row: 3 / 4; */
grid-area: 3 / 1 / 4 / 5;
}

URL使用

<body>
<!-- <div class="container">
<section class="hero">
<figure class="profile-pic-figure">
<img src="img/profile-800x800.png" alt="Profile Picture" title="My Profile Picture" width="800" height="800">
<figcaption class="offscreen">Jane Doe</figcaption>
</figure>
<h1 class="h1">
<span class="nowrap">Hello 👋</span>
<span class="nowrap">I'm Jane</span>
</h1>
</section>
</div> -->
<!-- <section class="example">

<img src="img/pat1.png" alt="Pattern 1" width="200" height="200">

</section> -->
<section>
<p class="clip">Jane</p>
</section>
</body>
body {
font-family: "Nunito", sans-serif;
min-height: 100vh;
background-color: aliceblue;
/* background-image: url("../img/bubbles.png"), linear-gradient(to left, steelblue, #fff);
background-repeat: repeat-y, no-repeat;
background-position: right center; */

background: repeat-y right center url("../img/bubbles.png"), no-repeat linear-gradient(to left, steelblue, #fff);
background-size: 20%, auto;
}

.container {
background-color: rgb(251, 210, 156);
background-image: url('../img/map-2176x1451.png');
background-repeat: no-repeat;
background-position: top;
background-size: cover;
}

.hero {
border-bottom: 2px solid #000;
padding: 20px;
display: flex;
justify-content: flex-start;
align-items: center;
gap: 30px;

background-color: hsla(0, 0%, 100%, 0.35);

}

.h1 {
font-size: 500%;
color: aliceblue;
text-shadow: 2px 2px 5px #000;
/* background-color: hsla(0, 0%, 0%, 0.403);
padding: 0.25rem;
border-radius: 10px; */
}

.profile-pic-figure {
width: 35%;
}

.profile-pic-figure img {
width: 100%;
height: auto;
min-width: 100px;
border: 5px double gray;
border-radius: 50%;
}

.example {
margin-top: 1rem;
padding-left: 20px;
border: 1px solid red;
}

.example img {
width: 25%;
height: auto;
}

.clip {
font-weight: 800;
font-size: 18rem;
text-align: center;
background-image: url("../img/scenic-2200x1331.png");
background-size: 100%;
text-transform: uppercase;
-webkit-background-clip: text;
background-clip: text;
color: transparent;
}

Media使用

<body>
<header>
<h1>Our Staff</h1>
<nav>
<a href="#profile1">Joe</a>
<a href="#profile2">Matt</a>
<a href="#profile3">Jane</a>
</nav>
</header>

<main>
<article id="profile1" class="card">
<figure>
<img
src="img/profile1-500x500.png"
alt="Joe Coder"
width="500"
height="500"
/>
<figcaption>Joe Coder</figcaption>
</figure>
<p><q>I code stuff.</q></p>
</article>
<article id="profile2" class="card">
<figure>
<img
src="img/profile2-500x500.png"
alt="Matt Designer"
width="500"
height="500"
/>
<figcaption>Matt Designer</figcaption>
</figure>
<p><q>I design stuff.</q></p>
</article>
<article id="profile3" class="card">
<figure>
<img
src="img/profile3-500x500.png"
alt="Jane Dev Rel"
width="500"
height="500"
/>
<figcaption>Jane <span class="nowrap">Dev Rel</span></figcaption>
</figure>
<p><q>I teach stuff.</q></p>
</article>
</main>

<footer>
<p>&copy; Acme Co.</p>
</footer>
</body>
@import url("https://fonts.googleapis.com/css2?family=Nunito&display=swap");

/* || RESET */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

img {
display: block;
max-width: 100%;
height: auto;
}

/* || UTILITY CLASSES */
.nowrap {
white-space: nowrap;
}

/* || GENERAL STYLES */
html {
scroll-behavior: smooth;
}

body {
font: 1.5rem "Nunito", sans-serif;
min-height: 100vh;
background-color: #475569;
background-image: radial-gradient(whitesmoke, #475569);
display: flex;
flex-direction: column;
}

header,
footer {
position: sticky;
background-color: #1e293b;
color: whitesmoke;
text-align: center;
}

header {
top: 0;
}

nav {
background-color: #fff;
padding: 0.5rem;
border-bottom: 2px solid #000;
font-weight: bolder;
display: flex;
justify-content: space-evenly;
}

nav a:link,
nav a:visited {
color: #000;
}

nav a:hover,
nav a:focus {
color: hsla(0, 0%, 20%, 0.6);
}

main {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: center;
gap: 1.5rem;
padding: 1rem;
}

footer {
bottom: 0;
}

/* || PROFILE CARD */
.card {
scroll-margin-top: 8rem;
width: min(100%, 350px);
background-color: #cbd5e1;
border: 2px solid #000;
border-radius: 15px;
padding: 1rem;
display: flex;
flex-direction: column;
align-items: center;
}

.card figure {
display: flex;
flex-flow: column nowrap;
}

.card img {
border: 5px double #333;
border-radius: 50%;
}

.card figcaption {
font-weight: bolder;
font-size: 2rem;
margin: 1rem;
text-align: center;
}

/* || SMALL */
@media screen and (min-width: 576px) {
main {
justify-content: center;
flex-flow: row wrap;
padding: 1rem;
}
.card {
width: min(100%, 400px);
}
.card:last-child {
order: -1;
}
}

/* || MEDIUM */
@media screen and (min-width: 768px) {
nav {
display: none;
}
.card {
width: min(100%, 325px);
}
.card figure {
flex-flow: column-reverse;
}
.card p {
margin-top: 1rem;
}
}

/* || LARGE */
@media screen and (min-width: 992px) {
.card {
width: min(100%, 400px);
}
.card:nth-child(2) {
order: -1;
}
}

/* || XL */
@media screen and (min-width: 1200px) {
.card {
width: min(calc(33% - 1rem), 500px);
}
}

/* || MOBILE DEVICE LANDSCAPE */
@media screen and (max-height: 425px) and (min-aspect-ratio: 7/4) {
h1 {
font-size: 1.5rem;
}

nav {
display: none;
}

main {
flex-flow: row nowrap;
justify-content: space-evenly;
align-items: stretch;
}

.card {
width: min(calc(33% - 0.25rem), 200px);
}

.card figcaption,
.card p {
font-size: 1.25rem;
}
}

function 用法

:root {
/* FONT */
--FF: "Nunito", sans-serif;
--FS: clamp(1.75rem, 3vh, 2.25rem);
--FS-SM: clamp(1.25rem, 2vh, 1.5rem);
--FS-XL: 3rem;

/* COLOR */
--BGCOLOR: #475569;
--ALT-BGCOLOR: #1E293B;
--RADIAL-COLOR: whitesmoke;
--LIGHT-COLOR: whitesmoke;
--DARK-COLOR: #000;

/* SQUARE */
--SQUARE-BGCOLOR: papayawhip;
--SQUARE-SIZE: max(150px, 20vw);

/* GENERAL */
--PADDING: 0.5em;
--SHADOWS: 0 6px 5px -5px var(--DARK-COLOR);
--BORDERS: 2px solid var(--DARK-COLOR);
}
h1 {
gap: min(4vw, 15px);
font-size: var(--FS-XL);
background-image: radial-gradient(var(--RADIAL-COLOR), var(--BGCOLOR));
}