All the tips in this series come from CSS Revealed extract code: 9x8n

Font typesetting

1. Insert a newline

Display: inline + content: ‘\A’ + white-space: pre

The CSS part

.box48 dt..box48 dd {
  color: rgb(230.59.82);
  display: inline;/* Do not use inline-block */ here
}

.box48 dd {
  margin: 0;
  font-weight: bold;
}

/* Precede all non-first dt with a newline character */
.box48 dt:not(:first-child)::before {
  content: "\A";
  white-space: pre;
  /* Leave whitespace, otherwise content: "\A" will not work */
}

/* Prefixes each non-first DD with */
.box48 dd+dd::before {
  content: ', ';
}
Copy the code

HTML part

<div class="box48">
  <dl>
    <dt>Name:</dt>
    <dd>Lea Verou</dd>
    <dd>[email protected]</dd>
    <dt>Email:</dt>
    <dd>[email protected]</dd>
    <dt>Location:</dt>
    <dd>Earth</dd>
  </dl>
</div>
Copy the code

The effect

2. Zebra stripes on text lines

linear-gradient

Each stripe is exactly the line height of the text

.wrapper {
  width: 150px;
  padding:.5em;
  line-height: 1.5;
  background-origin: content-box;
  /* Set the background to start with content */
  background-clip: content-box;
  /* Crop the background */
  color: rgb(131.24.39);
  background-image: linear-gradient(rgba(230.59.82.0.5) 50%, transparent 0);
  background-size: auto 3em;
}
Copy the code

The effect

3. Fancy ampersands

Introduce native special fonts with @font-face and specify which strings need these fonts with Unicode-range

 @font-face {
  font-family: Ampersand;
  src: local('Baskerville-Italic'), /* Since only Italic will work, I will add -italic */local('GoudyOldStyle-Italic'), local('Garamond-Italic'), local('Palatino-Italic'), local('BookAntiqua-Italic');
  unicode-range: U+26; /* & corresponding to Unicode */
}

h1 {
  color: rgb(230.59.82);
  font-family: Ampersand, Helvetica, sans-serif; /* Put Ampersand first */
}
Copy the code

4. Customize underscores

linear-gradient + text-shadow

.wrapper {
  color: rgb(230.59.82);
  width: 400px;
  line-height: 1.5 em;
  font-size: 30px;
  display: inline-block;
  background: linear-gradient(to top, rgb(122.12.27) 1px, transparent 0);
  /* forms an underscore */
  background-size: 100% 1.5 em;
  background-position: 0 1.3 em;
  text-shadow:.05em 0 white, -.05em 0 white;
  /* The effect of breaking the underline over part */
}
Copy the code

The effect

5. Concave and convex effect

Light background dark word + dark background light word

/* Dark text on a light background */
.p1 {
  padding: 20px 20px;
  background: pink;
  text-shadow: 0 1px 1px hsla(0.0%.100%.8);
  color: rgb(122.12.27);
}
/* Dark background light text */
.p2 {
  padding: 20px 20px;
  background: rgb(122.12.27);
  color: pink;
  text-shadow: 0 -1px 1px black;
}
Copy the code

The effect

6. Glowing fonts

Multi-layer overlay through text-shadow

.wrapper {
  width: 200px;
  font-size: 40px;
  text-align: center;
  color: rgb(230.59.82);
  text-shadow: 0 0 .1em #ffc.0 0 .3em #ffc;
  background: rgb(61.4.12);
}
Copy the code

The effect

The user experience

1. Select an appropriate mouse cursor

1-1 Disable the cursor

cursor: not-allowed;
Copy the code

1-2 Hide the mouse

cursor: url('transparent.gif'); /* Semi-transparent image, rollback mechanism */
cursor: none
Copy the code

2. Expand the clickable area

When the target is too small, you can use padding to resize the target

3. Background blurring

Pseudo-element (Blur background compensation) + Background + filter:blur()

The CSS part

.box {
  overflow: hidden;
  /* With this set, the blurred edges will start from the edge being cropped */
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 200px;
  height: 200px;
}

.box::after..img {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background: url('./images/test.jpg') 0/cover no-repeat;
}

/* to compensate for the blurring */
.box::after {
  z-index: -1;
  content: ' ';
}

.img {
  filter: blur(5px); /* Blur the background */
}

.inner {
  position: relative;
  padding: 10px;
  background-color: #fff;
}
Copy the code

HTML part

<div class="box">
  <div class="img"></div>
  <div class="inner">I am content</div>
</div>
Copy the code

The effect

4. Interactive pictures

Cursor: EW-resize Horizontal (set drag) + cursor: Ew-resize

The CSS part

.wrapper {
  position: relative;
  overflow: hidden;
  width: 400px;
  height: 200px;
}

.mask {
  max-width: 100%;
  position: absolute;
  z-index: 1;
  left: 0;
  top: 0;
  overflow: hidden;
  right: 50%;
  bottom: 0;
  resize: horizontal;
  background-color: rgba(0.0.0.3);
}

/* Custom draggable styles */
.mask::after {
  content: ' ';
  position: absolute;
  width: 1em;
  height: 1em;
  right: 0;
  bottom: 0;
  cursor: ew-resize;
  background: linear-gradient(-45deg, white 50%, transparent 0);
}

img {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}
Copy the code

HTML part

<div class="wrapper">
  <div class="mask"></div>
  <img src="./images/test.jpg" alt="">
</div>
Copy the code

The effect

Structure and layout

1. Adapt internal elements

max-width: min-content;

The parent element wraps around the child element, matching the largest internal image or fixed-width element

The CSS part

figure {
  padding: 10px;
  border: solid 1px #eee;
  max-width: 200px;
  /* Rollback mechanism */
  max-width: min-content;
  margin: auto;
}

img {
  width: 200px;
}
Copy the code

HTML part

<figure>
  <img src="./images/test.jpg" />
  <figcaption>
    The great Sir Adam Catlace was named after
    Countess Ada Lovelace, the first programmer.
  </figcaption>
</figure>
Copy the code

The effect

2. Style according to the number of sibling elements

li:only-child{} = = >/* even if the first is the last */
li:first-child:nth-last-child(1) {}
Copy the code

3. Close to the bottom footer

HTML part

 <header>
  </header>

  <main>
  </main>

  <footer>
  </footer>
Copy the code

3-1 Top and bottom fixed height

header {
  height: 50px;
}

main {
  min-height: calc(100vh - 100px); /* Calculates the minimum height of the content */
}

footer {
  height: 50px;
}
Copy the code

3-2 The top and bottom are not fixed

flex

body {
  display: flex;
  flex-flow: column;
  min-height: 100vh;
}
main {
  flex: 1;
}
Copy the code