如何创建网站第二部分:样式和css

在我们关于如何创建网站的夜校系列的第一部分中,我们学习了一些关于网页的基本知识:HTML。在今天的课程中,我们将开始使用样式和CSS将衣服放到我们的网站上。...

在我们关于如何创建网站的夜校系列的第一部分中,我们学习了一些关于网页的基本知识:HTML。在今天的课程中,我们将开始使用样式和CSS将衣服放到我们的网站上。

上面的视频将引导您了解将样式应用于HTML文档中元素的三种方法。它假设你已经看了第一课,所以如果你没有,你可能想先看。在本课结束时,您将了解如何向网页中的元素添加样式属性。额外的参考资料,请查看下面的文本。

你需要什么

  • 用于编写HTML的纯文本编辑器
  • 您可以选择web浏览器来测试您的HTML
  • 希望加强你对网络工作方式的了解

正如你所见,不需要太多就可以开始了。

什么是样式和css(styles and css)?

Image for article titled How to Make a Web Site Part II: Styling and CSS

CSS代表层叠样式表,它是浏览器用来解释网页上元素的外观的语言。CSS语言非常容易理解:您可以定义特定的CSS属性,这些属性涵盖元素尺寸(宽度和高度)、文本(重量、样式、字体类型、颜色等)、位置和间距(边距和填充)等所有内容。

本质上,如果它与元素的外观有关,那么它就与CSS有关。为了扩展上面的隐喻,HTML和文档中的元素是站点的骨架。它们提供了结构。CSS装饰你的网站,使它看起来漂亮或不。那部分取决于你的设计风格。

例如,写出来后,CSS看起来有点像这样:如果我想给一段文本添加一种颜色,那么相应的CSS属性就是,不出所料,color。要定义color属性,只需键入color、冒号、声明样式,然后以分号结束。例如,颜色:红色;

如何将css应用于元素

当然,您需要知道在哪里应用CSS样式,以便页面上的元素反映它们(你不能只输入颜色:红色;任劳任怨,期待结果。您可以通过以下三种方式之一使用CSS将样式应用于网页上的任何元素:

  • Inline styles: This method is best for quick, one-off styles that you want to apply to one element without a lot of overhead. You can add inline styles to any element by defining the style attribute of an element. For example:

    <p style="color:red;">The text inside this paragraph tag will be red.</p>

    You can define more than one property using inline styles as long as you remember the semicolon between them:

    <p style="color:red; font-family:Helvetica; font-size:20px;">The text inside this p tag will be red, 20-pixels wide, and use Helvetica.<p>

    Internal stylesheets: Inside the document (often inside the head), you can define styles for elements in the page using selectors. Internal stylesheets look like this:

    <code><style type="text/css"> h3 { color:red; font-family:Helvetica; font-size:20px; } </style>

    In the example above, the h3 is the selector. It's saying, "All H3 elements on this page should use the following styles." You can also define selectors using classes or element IDs.

    External stylesheets: These stylesheets move the CSS to an external file ending with .css (for example, style.css. The syntax for external stylesheets is exactly like what you saw with internal stylesheets, but you don't need the style declaration. Instead, you link to your external stylesheet like so:

    <link href="stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />

    In this instance, I've got the style.css file inside a stylesheets folder, and I'm telling the web browser: "When you load the page, looks for the CSS here." Everything inside looks the same as what goes inside the style tags of internal stylesheets.

何时使用类、ID和元素选择器?

大多数情况下,为了便于组织,您会希望使用外部样式表。但是如何决定使用哪些选择器呢?让我们快速浏览一下您的选项:

  • Element selectors: You should use an element selector when you want most elements of that type to look the same way on your page. So if you wanted all h2 elements to be 30 pixels, you could add the following to your CSS file:

    <code>h2 { font-size:30px; }

    From then on, every h2 will respect that style unless you override it.

    Class selectors: Classes are useful for broad styles that you want to apply to more than one element, but it may be a style that you want to use a little more selectively than you would when using the element selector. In the video, I defined the very boring red class like so:

    <code>.red { color:red; }

    Using the dot (.) before the text "red", I've told my browser that this selector is a class I'm creating. Now, in any element in my page, I can add an attribute of class="red" and its text will be red.

    ID selectors: Defined in CSS by a preceding hash (#), the ID selector is for one specific element and one element only. Essentially this provides a way to adjust styles for one specific element without using inline styles—keeping all your styling in one place. (IDs are also very useful for JavaScript, but that's well beyond our scope here.) If I wanted an 800x600px div with a red background called my_red_div, it would look like this:

    <code>#my_red_div { width:800px; height:600px; background:red; }

观看视频,看看这一切在实践中。阅读是一回事,但当你能看到它时,一切就变得更有意义了。

就这样?

简而言之,是的!当然CSS还有很多,还有一些更高级的方法来处理CSS选择器,这样你就可以缩小元素继承样式的范围,但这才是最基本的。唯一缺少的东西:属性!

不过,我不打算浏览所有可用的CSS属性(坦率地说,我们在本课中没有时间)。与大多数编程的本质一样,您将从搜索选项中学到比我在一个快速指南中提供的更多的东西。以下是您可能需要浏览的一些CSS引用:

  • w3css引用
  • 添加了Bytes的CSS备忘单
  • 分开的名单

我很想在评论中听到更多喜欢的参考工具!我对这类东西基本上是个谷歌搜索者。我不是CSS专家,但我已经花了大量的时间为我自己的宠物项目黑客CSS,相信我:一点点有很长的路要走,但有很多你可以学习,如果你想深入。


你可以联系亚当帕什,这篇文章的作者,在小费[email protected]. 你也可以在Twitter和Facebook上关注他。

  • 发表于 2021-07-24 23:08
  • 阅读 ( 279 )
  • 分类:通用

你可能感兴趣的文章

创建web界面:从何处开始

...具”,您可能会遇到一堆乱七八糟的文本,并想知道这是如何使网页工作的。 ...

  • 发布于 2021-03-16 05:47
  • 阅读 ( 265 )

使用css格式化文档以便打印

... 相关:如何在CSS中设置背景图像 ...

  • 发布于 2021-03-28 16:05
  • 阅读 ( 227 )

如何在css中设置背景图像

...****工具,但自己写是一个有趣的方式来了解更多的网站如何幕后工作。一个好的初学者项目是创建一个网站,并添加一个与CSS背景图像。这个项目将使您启动和运行与HTML和CSS。 ...

  • 发布于 2021-03-29 12:38
  • 阅读 ( 352 )

如何拥有自己的网站(即使你不能建立一个)第3部分

...dPress软件。今天我们将解释WordPress不太明显的优点,以及如何从新的web软件中获得更多的好处。 这是我们系列文章的第三篇也是最后一篇,介绍如何拥有自己的网站,并通过基本的WordPress安装创建内容。在任何初学者都能理解...

  • 发布于 2021-04-12 12:27
  • 阅读 ( 213 )

如何创建wordpress博客的自定义主题,并使用最少的编码

...得)。你为你的WordPress网站做的设计(你可以在这里学习如何在Photoshop中做)。可选:如果你不想使用内置在WordPress中的主题编辑器,你应该得到一个编程友好的文本编辑器(下面是我们为Windows和OS X挑选的。)可选:由于您的Wor...

  • 发布于 2021-05-28 09:38
  • 阅读 ( 225 )

如何根据自己的喜好定制自己喜欢的网站

...为您提供了定制任何东西的选项。下面我们来看看它们是如何工作的,如何使用它们,甚至如何开始自己**。用户脚本(和用户样式)到底是什么?org将其称为“浏览器的电源”,但更具体地说,它们是用于更改、添加和更改给...

  • 发布于 2021-07-24 17:26
  • 阅读 ( 140 )

如何建立一个网站:完整的初学者指南

上周我们教你如何从头到尾创建一个网站,包括找到一个可靠的网站宿主来托管你的网站。这是完整的指南,你可以在一个方便的地方学习所有的课程。如何创建网站第一部分:理解和编写html在创建网站时,首先需要学习的是...

  • 发布于 2021-07-24 22:04
  • 阅读 ( 227 )

如何创建网站第一部分:理解和编写html

...下理解HTML都是非常宝贵的。考虑到这一点,在我们关于如何创建网站的第一课中,我们将介绍HTML(web的主要标记语言)的顶级基础知识。现在,使用社交媒体和个人登录页来组合一个web呈现是很容易的,但是如果你真的想创建...

  • 发布于 2021-07-24 23:30
  • 阅读 ( 210 )

如何检测网页上移动设备的点击(detect hits from mobile devices on web pages)

...适合手机和其他移动设备的体验。 一旦你花时间学习如何为手机设计网页,并实施你的策略,你还需要确保网站的访问者能够看到这些设计。有很多方法可以做到这一点,有些方法比其他方法更好。下面是一个你可以用来在...

  • 发布于 2021-09-04 21:57
  • 阅读 ( 169 )

如何用css缩进段落(indent paragraphs with css)

...使用文本缩进样式属性。 此属性的语法很简单。下面是如何向文档中的所有段落添加文本缩进。 p { text-indent: 2em;} 定制缩进 有一种方法可以精确地指定要缩进的段落,您可以向要缩进的段落添加一个类,但这需要您编...

  • 发布于 2021-09-04 23:15
  • 阅读 ( 138 )
oh2881227
oh2881227

0 篇文章

相关推荐