CSS基础理解

2017-07-31  本文已影响26人  抱着熊喵啃什么

CSS基础理解

一、CSS 加载方式有几种

CSS可以通过使用外部样式表、内部样式表、内联样式来使用。

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <title>CSS</title>
  <link rel="stylesheet" href="index.css">
</head>
<body>
  <h1>Hello CSS!</h1>
</body>
</html>

2)通过 @import 引入样式,放入 css 中

<style>
@import url("index.css");
@import url('index.css');
@import url(index.css);
@import 'custom.css';
@import "common.css";
@import url('landscape.css') screen and (orientation:landscape);
</style>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <title>CSS</title>
  <style>
    h1 { background: orange; }
  </style>
</head>
<body>
  <h1>Hello CSS!</h1>
</body>
</html>

二、@charset有什么作用

在外部样式表文件内使用。指定该样式表使用的字符编码。

三、CSS具有哪些选择器

  1. 元素选择器 Element Selectors
p {
  color: red;
}

div {
  color: blue;
}
  1. ID 选择器 ID Selectors
<p id="notification">通知:明天放假</p>
#notification {
  font-size: 24px;
}
  1. 类选择器 Class Selectors
<ul>
  <li class="first done">起床</li>
  <li class="second done">刷牙</li>
  <li class="third">洗脸</li>
</ul>
.first {
  font-weight: bold;
}

.done {
  text-decoration: line-through;
}

4.通用选择器 Universal Selector

* {
  box-sizing: border-box;
}

.flex-container * {
  flex-basis: 100%;
}
  1. [attr]
    [attr] 选择包含 attr 属性的所有元素,不论 attr 的值为何。
[disabled] {
  cursor: not-allowed;
}
  1. [attr=val]
    [attr=val]仅选择 attr 属性被赋值为 val 的所有元素。
[data-color="gray"] {
  color: #ccc;
}
a:link { ... }
a:visited { ... }
a:hover { ... }
a:active { ... }
li:first-child { ... }
li:last-child { ... }
body :not(p) { ... }
p:not(.warning) { ... }
.clearfix::after {
  content: '';
  clear: both;
  display: block;
}
/* 设置 input 元素 placeholder 的字体颜色 */
input::-webkit-input-placeholder {
  color: #aaa;
}
  1. A, B
    A, B 选中匹配 A 或/和 B 的元素
.author, .famous {
  font-weight: bold;
}
<h1>登鹳雀楼</h1>
<p class="author">王之涣<p>
<p class="normal">百日依山尽,黄河入海流。</p>
<p class="famous">欲穷千里目,更上一层楼。</p>
  1. A B
    A B 选中匹配 B 且为匹配 A 的元素的后代元素。
.article a {
  color: #384ebf;
}
  1. A > B
    A > B 选中匹配 B 且为匹配 A 的元素的直接子元素。
.warriors > li {
  background-image: url(../images/warrior.svg);
}
<ul class="warriors">
  <li><!-- ✅ -->
    斯蒂芬·库里
    <ul>
      <li>微博:<a href="http://weibo.com/u/3432945104">@StephenCurry</a></li>
      <li>Twitter: <a href="https://twitter.com/stephencurry30">@StephenCurry30</a></li>
    </ul>
  </li>
  <li>凯文·杜兰特</li><!-- ✅ -->
  <li>克莱·汤普森</li><!-- ✅ -->
  <li>德雷蒙德·格林</li><!-- ✅ -->
</ul>
  1. A + B
    A + B 选中匹配 B 且为匹配 A 的元素的下一相邻元素。
.cavs .lbj + li {
  text-shadow: 1px 1px 5px #ccc;
}
<ul class="cavs">
  <li class="lbj">勒布朗·詹姆斯</li>
  <li>凯里·欧文</li><!-- ✅ -->
  <li>凯文·乐福</li>
</ul>
  1. A ~ B
    A ~ B 选中匹配 B 且为匹配 A 的元素的下 N 个相邻元素。
.cavs .lbj ~ li {
  text-shadow: 1px 1px 5px #ccc;
}
<ul class="cavs">
  <li class="lbj">勒布朗·詹姆斯</li>
  <li>凯里·欧文</li><!-- ✅ -->
  <li>凯文·乐福</li><!-- ✅ -->
</ul>
.players .player.curry, .player.mvp, #lebron-james {
  background-image: url(../images/mvp.png);
}
上一篇 下一篇

猜你喜欢

热点阅读