Technologieaustausch

CSS-Selektoren, allgemeine Eigenschaften, Boxmodell und Positionierung

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

CSS

Wähler

CSS-Selektor: Wählen Sie das entsprechende Tag auf der Seite (Dokument) aus und legen Sie den Stil fest

Wildcard-Selektor
  1. //*:通配符选择器,选择页面中所有的标签
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style type="text/css">
  7. *{
  8. color: red;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <h1>一级标签</h1>
  14. <h2>二级标签</h2>
  15. <p>用良心做教育</p>
  16. <p>做真实的自己</p>
  17. <span>匠心育人</span>
  18. <span>初心至善</span>
  19. </body>
  20. </html>
Basisselektor

Grundlegende Selektoren sind in Tag-Selektoren, Klassen-Selektoren und ID-Selektoren unterteilt.

Tag-Selektor
  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title></title>
  5. </head>
  6. <style type="text/css">
  7. p{
  8. color: #FF0000;
  9. }
  10. </style>
  11. <body>
  12. <h1>一级标签</h1>
  13. <p>用良心做教育</p>
  14. <span>匠心育人</span>
  15. </body>
  16. </html>
Klassenselektor

Zu ... Anfang

  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title></title>
  5. <style type="text/css">
  6. .myclass{
  7. color: #FF0000;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <h1 class="myclass">一级标签</h1>
  13. <h2>二级标签</h2>
  14. </body>
  15. </html>
ID-Auswahl

Die ID beginnt mit # und ist eindeutig

  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title></title>
  5. </head>
  6. <style type="text/css">
  7. #myid{
  8. color: #FF0000;
  9. }
  10. </style>
  11. <body>
  12. <h1 id="myid">一级标签</h1>
  13. <h2>二级标签</h2>
  14. </body>
  15. </html>
Grundlegende Auswahlpriorität

ID-Auswahl &gt; Klassenauswahl &gt; Tag-Auswahl

Gruppenauswahl
  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title></title>
  5. </head>
  6. <style type="text/css">
  7. h1,h3,h5,p{color: red;}
  8. </style>
  9. <body>
  10. <h1>一级标签</h1>
  11. <h2>二级标签</h2>
  12. <h3>三级标签</h3>
  13. <h4>四级标签</h4>
  14. <h5>五级标签</h5>
  15. <h6>六级标签</h6>
  16. <p>用良心做教育</p>
  17. <p>做真实的自己</p>
  18. </body>
  19. </html>
abgeleiteter Selektor

Auch als Kontextselektoren bekannt

Unterteilt in Nachkommenselektoren, Nachkommenselektoren und benachbarte Geschwisterselektoren

Nachkommenselektor

Beachten Sie die Verwendung von Leerzeichen

  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title></title>
  5. </head>
  6. <style type="text/css">
  7. ul a{
  8. color: red;
  9. }
  10. </style>
  11. <body>
  12. <ul>
  13. <li>
  14. <a href="#">超链接1</a>
  15. </li>
  16. <li>
  17. <a href="#">超链接2</a>
  18. </li>
  19. <li>
  20. <a href="#">超链接3</a>
  21. </li>
  22. </ul>
  23. <ul>
  24. <li>
  25. <a href="#">超链接4</a>
  26. </li>
  27. <li>
  28. <a href="#">超链接5</a>
  29. </li>
  30. <li>
  31. <a href="#">超链接6</a>
  32. </li>
  33. </ul>
  34. <a href="#">超链接7</a>
  35. <a href="#">超链接8</a>
  36. <a href="#">超链接9</a>
  37. </body>
  38. </html>
Nachkommenselektor

HINWEIS: Verwenden Sie das Symbol &gt;

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <style type="text/css">
  8. li>a{
  9. color: red;
  10. }
  11. </style>
  12. <body>
  13. <ul>
  14. <li>
  15. <a href="#">超链接1</a>
  16. </li>
  17. <li>
  18. <a href="#">超链接2</a>
  19. </li>
  20. <li>
  21. <a href="#">超链接3</a>
  22. </li>
  23. </ul>
  24. <ul>
  25. <li>
  26. <a href="#">超链接4</a>
  27. </li>
  28. <li>
  29. <a href="#">超链接5</a>
  30. </li>
  31. <li>
  32. <a href="#">超链接6</a>
  33. </li>
  34. </ul>
  35. <a href="#">超链接7</a>
  36. <a href="#">超链接8</a>
  37. <a href="#">超链接9</a>
  38. </body>
  39. </html>
Nachbar-Geschwister-Selektor

Beachten Sie die Verwendung des +-Zeichens

Stile wirken auf Letzteres

  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title></title>
  5. <style type="text/css">
  6. a+a{
  7. color: red;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <ul>
  13. <li>
  14. <a href="#">超链接1</a>
  15. </li>
  16. <li>
  17. <a href="#">超链接2</a>
  18. </li>
  19. <li>
  20. <a href="#">超链接3</a>
  21. </li>
  22. </ul>
  23. <ul>
  24. <li>
  25. <a href="#">超链接4</a>
  26. </li>
  27. <li>
  28. <a href="#">超链接5</a>
  29. </li>
  30. <li>
  31. <a href="#">超链接6</a>
  32. </li>
  33. </ul>
  34. <a href="#">超链接7</a>
  35. <a href="#">超链接8</a>
  36. <a href="#">超链接9</a>
  37. </body>
  38. </html>
Attributselektor
  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title></title>
  5. <style type="text/css">
  6. /*
  7. * 属性选择器
  8. *
  9. * 单个属性的属性选择器:
  10. * input[placeholder]{color: red;}
  11. *
  12. * 单个属性+值的属性选择器:
  13. * input[type="text"]{color: red;}
  14. *
  15. * 多个属性的属性选择器:
  16. * input[name][placeholder]{color: red;}
  17. *
  18. * 多个属性+值的属性选择器:
  19. * input[type="text"][placeholder]{color: red;}
  20. * input[type="text"][placeholder="请输入账号..."]{color: red;}s
  21. */
  22. </style>
  23. </head>
  24. <body>
  25. <form action="服务器地址" method="post">
  26. 账号:<input type="text" name="username" placeholder="请输入账号..."/>
  27. <br />
  28. 密码:<input type="password" name="password" placeholder="请输入密码..."/>
  29. <br />
  30. 确认密码:<input type="password" name="repassword"/>
  31. <br />
  32. 姓名:<input type="text" name="name"/>
  33. <br />
  34. 昵称:<input type="text" name="nickName" placeholder="请输入昵称..."/>
  35. <br />
  36. 性别:
  37. <input type="radio" name="sex" value="man" checked="checked"/>
  38. <input type="radio" name="sex" value="woman"/>
  39. <br />
  40. 爱好:
  41. <input type="checkbox" name="hobbies" value="football" checked="checked"/>足球
  42. <input type="checkbox" name="hobbies" value="basketball" checked="checked"/>篮球
  43. <input type="checkbox" name="hobbies" value="shop"/>购物
  44. <br />
  45. 国籍:
  46. <select name="nationality">
  47. <option value="001">韩国</option>
  48. <option value="002">美国</option>
  49. <option value="003" selected="selected">中国</option>
  50. <option value="004">日本</option>
  51. </select>
  52. <br />
  53. <input type="submit" value="注册" />
  54. </form>
  55. </body>
  56. </html>
Anker-Pseudoklasse

Legen Sie den Stil für jeden Status des Hyperlinks fest (nicht besuchter Status, besuchter Status, Status des Hyperlinks mit der Maus darüber, Status mit gedrückter Maus).

  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title></title>
  5. <style type="text/css">
  6. a:link {color: #FFCCFF} /* 未访问的链接 */
  7. a:visited {color: #FF0000} /* 已访问的链接 */
  8. a:hover {color: #66FF66} /* 鼠标移动到链接上 */
  9. a:active {color: #33FFFF} /* 选定的链接 */
  10. </style>
  11. </head>
  12. <body>
  13. <a href="http://www.163.com">跳转页面</a>
  14. </body>
  15. </html>

Gemeinsame Eigenschaften

Stil Priorität
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <!--
  7. 样式的优先级别:
  8. 行内样式 > 内/外部样式
  9. 注意:内外部样式要看加载顺序
  10. -->
  11. <!--外部样式表-->
  12. <link rel="stylesheet" type="text/css" href="../../css/new_file.css"/>
  13. <!--内部样式表-->
  14. <style type="text/css">
  15. h1{color: red;}
  16. </style>
  17. </head>
  18. <body>
  19. <!--行内样式表-->
  20. <h1 style="color: blue;">我好像在哪儿见过你</h1>
  21. </body>
  22. </html>
Häufig verwendete CSS-Eigenschaften

1.Schriftattribute

Name des Anwesens

Schriftfamilie(Schriftart)

Schriftgröße (Größe)

Schriftstil (Stil)

---- normaler Standardstil

---- kursiv kursiv

---- schräge Neigung

---- erbt den von der übergeordneten Klasse geerbten Schriftstil

Schriftstärke (Fettschrift)

----normaler Standardstil

----fett fett

----fetter ist dicker

----leichter ist dünner

2.Textattribute

Name des Anwesens

Buchstabenabstand (Buchstabenabstand)

Textdekoration (Unterstreichungsdekoration)

text-align (Textausrichtung)

text-indent (Texteinzug)

Zeilenhöhe (Zeilenhöhe)

3.Hintergrund

Name des Anwesens

Hintergrundfarbe

Hintergrundbild

Hintergrund Wiederholung

4. Grenze

Name des Anwesens

Rahmen unten

durchgezogen (durchgezogene Linie)

gestrichelt (gestrichelte Linie)

doppelt (doppelte durchgezogene Linie)

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. </body>
  9. </html>
  10. <!DOCTYPE html>
  11. <html>
  12. <head>
  13. <meta charset="UTF-8">
  14. <title></title>
  15. <style type="text/css">
  16. p{
  17. font-family: "微软雅黑";/*设置字体*/
  18. font-size: 50px;/*设置字体大小*/
  19. font-style: italic;/*设置字体样式*/
  20. font-weight: bold;/*设置字体粗细*/
  21. letter-spacing: 10px;/*设置文本间隔*/
  22. text-decoration: underline;/*设置文本划线*/
  23. text-align: center;/*设置对齐方式*/
  24. background-color: red;/*设置背景颜色*/
  25. color: white;/*设置字体颜色*/
  26. border: orange 5px solid;/*设置边框:颜色,粗细,实线*/
  27. }
  28. a:link {color: #000000} /* 未访问的链接 */
  29. a:visited {color: #000000} /* 已访问的链接 */
  30. a:hover {color: #3366FF} /* 鼠标移动到链接上 */
  31. a:active {color: #3366FF} /* 选定的链接 */
  32. a{
  33. text-decoration: none;/*去除划线*/
  34. }
  35. button{
  36. width: 500px;
  37. height: 500px;
  38. background-image: url(../../img/01.jpg);/*设置背景图片*/
  39. background-repeat:repeat-y;/*设置平铺方式*/
  40. }
  41. img{
  42. border-radius: 50%;/*倒圆角*/
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <p>我好像在哪儿见过你</p>
  48. <a href="#">地图</a>
  49. <br />
  50. <button>普通按钮</button>
  51. <br />
  52. <img src="../../img/01.jpg" width="100px" height="100px" />
  53. </body>
  54. </html>

Boxmodell

Polsterung
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <style type="text/css">
  8. div{
  9. width: 200px;
  10. height: 200px;
  11. border: orange 1px solid;
  12. padding: 50px;/*设置上下左右内边距*/
  13. padding-top: 50px;/*设置上内边距*/
  14. padding-bottom: 50px;/*设置下内边距*/
  15. padding-left: 50px;/*设置左内边距*/
  16. padding-right: 50px;/*设置右内边距*/
  17. /*
  18. * 注意1:内边距可能会把盒子撑变形
  19. * 注意2:IE老版本不支持内边距
  20. * 经验:能不用内边距就不用
  21. */
  22. }
  23. </style>
  24. <body>
  25. <div>
  26. <span>我好像在哪儿见过你</span>
  27. </div>
  28. </body>
  29. </html>
Ränder
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <style type="text/css">
  8. div{
  9. width: 200px;
  10. height: 200px;
  11. border: orange 1px solid;
  12. margin-top: 50px;/*设置上外边距*/
  13. margin-bottom: 50px;/*设置下外边距*/
  14. margin-left: 50px;/*设置左外边距*/
  15. margin-right: 50px;/*设置右外边距*/
  16. margin: 50px;/*设置上下左右外边距*/
  17. }
  18. </style>
  19. <body>
  20. <div>
  21. <span>我好像在哪儿见过你</span>
  22. </div>
  23. </body>
  24. </html>
Horizontal zentrieren
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <style type="text/css">
  8. div{
  9. width: 200px;
  10. height: 200px;
  11. border: orange 1px solid;
  12. margin: 0 auto;/*水平居中*/
  13. }
  14. </style>
  15. <body>
  16. <div>
  17. <span>我好像在哪儿见过你</span>
  18. </div>
  19. </body>
  20. </html>

Position

relative Positionierung

Relative Positionierung: Behalten Sie die ursprüngliche Position bei und verschieben Sie sie relativ zur ursprünglichen Position

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <style type="text/css">
  8. #manager{
  9. border: orange 1px solid;
  10. margin-top: 50px;
  11. }
  12. #box01{
  13. width: 100px;
  14. height: 100px;
  15. border: red 1px solid;
  16. }
  17. #box02{
  18. width: 100px;
  19. height: 100px;
  20. border: green 1px solid;
  21. position: relative;/*相对定位:保留原有位置,相对于原来的位置进行位移*/
  22. top: 50px;
  23. left: 50px;
  24. }
  25. #box03{
  26. width: 100px;
  27. height: 100px;
  28. border: blue 1px solid;
  29. }
  30. </style>
  31. <body>
  32. <div id="manager">
  33. <div id="box01"></div>
  34. <div id="box02"></div>
  35. <div id="box03"></div>
  36. </div>
  37. </body>
  38. </html>
absolute Positionierung

Absolute Positionierung: Behalten Sie die ursprüngliche Position nicht bei, suchen Sie nach dem übergeordneten Element und bestimmen Sie, ob das übergeordnete Element ein Positionsattribut hat. Wenn ja, positionieren Sie es entsprechend dem übergeordneten Element. Wenn nicht, suchen Sie weiter nach dem übergeordneten Element bis zum Körper

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <style type="text/css">
  8. #manager{
  9. border: orange 1px solid;
  10. margin-top: 50px;
  11. }
  12. #box01{
  13. width: 100px;
  14. height: 100px;
  15. border: red 1px solid;
  16. }
  17. #box02{
  18. width: 100px;
  19. height: 100px;
  20. border: green 1px solid;
  21. position: absolute;/*绝对定位:不保留原有位置,向上找寻父级元素,判断父级元素中是否有position属性,如果有就按照父级元素进行位置,如果没有就继续向上找寻父级元素,直到body为止*/
  22. top: 50px;
  23. left: 50px;
  24. }
  25. #box03{
  26. width: 100px;
  27. height: 100px;
  28. border: blue 1px solid;
  29. }
  30. </style>
  31. <body>
  32. <div id="manager">
  33. <div id="box01"></div>
  34. <div id="box02"></div>
  35. <div id="box03"></div>
  36. </div>
  37. </body>
  38. </html>

Feste Positionierung
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <style type="text/css">
  8. div{
  9. position: fixed;/*固定定位*/
  10. top: 80%;
  11. right: 20%;
  12. }
  13. </style>
  14. <body>
  15. <a name="top"></a><!--下锚点-->
  16. <div>
  17. <a href="#top">置顶</a>
  18. </div>
  19. <!--导航栏-->
  20. <a href="#new01">法治</a>
  21. <a href="#new02">国际</a>
  22. <a href="#new03">娱乐</a>
  23. <a name="new01"></a><!--下锚点-->
  24. <h1>法治新闻</h1>
  25. <h1>法治新闻</h1>
  26. <h1>法治新闻</h1>
  27. <h1>法治新闻</h1>
  28. <a name="new02"></a><!--下锚点-->
  29. <h1>国际新闻</h1>
  30. <h1>国际新闻</h1>
  31. <h1>国际新闻</h1>
  32. <h1>国际新闻</h1>
  33. <h1>国际新闻</h1>
  34. <h1>国际新闻</h1>
  35. <a name="new03"></a><!--下锚点-->
  36. <h1>娱乐新闻</h1>
  37. <h1>娱乐新闻</h1>
  38. <h1>娱乐新闻</h1>
  39. <h1>娱乐新闻</h1>
  40. <h1>娱乐新闻</h1>
  41. <h1>娱乐新闻</h1>
  42. <h1>娱乐新闻</h1>
  43. <h1>娱乐新闻</h1>
  44. </body>
  45. </html>