[英]How do I know what correct HTML code looks like
So I've recently started using Free Code Camp to learn web development, and I've reached my first project in the Front-End Development Certification portion.所以我最近开始使用 Free Code Camp 来学习 Web 开发,并且我已经完成了前端开发认证部分的第一个项目。
I was just wondering, what does "correct" HTML/CSS/jQuery look like?我只是想知道,“正确”的 HTML/CSS/jQuery 是什么样的?
So far the way the site has been teaching me, it's shown me multiple ways to implement things like colors, fonts, buttons, properties, elements, all that good stuff through HTML, CSS, and jQuery.到目前为止,该网站教给我的方式,它向我展示了多种方法来实现诸如颜色、字体、按钮、属性、元素以及通过 HTML、CSS 和 jQuery 实现的所有好东西。
But is there a preferred place to put things?但是有没有更喜欢的地方放东西?
For example, lets say I want to make a paragraph font-color blue.例如,假设我想将段落字体颜色设为蓝色。 Do I:
我:
1) In HTML 1) 在 HTML 中
<p class="text-primary">Hello World</p>
2) In CSS 2) 在 CSS 中
<style>
.font-blue {
font-color: 00F;
}
</style>
3) In jQuery 3) 在 jQuery 中
<script>
$(document).ready(function() {
$(#h2numb3).css("color", "red");
});
</script>
Can anyone give me a definite answer, or what the community has decided is "preferable", for what should be in HTML, CSS, and jQuery?任何人都可以给我一个明确的答案,或者社区已经决定什么是“首选”,HTML、CSS 和 jQuery 应该是什么? Or is it all just based on unique use case scenarios?
或者这一切只是基于独特的用例场景? Like for example, if you want to change all h2 to font monospace, do it in CSS, but if you specifically want to target a header, do it in the start element of the h2 you want to change?
例如,如果您想将所有 h2 更改为等宽字体,请在 CSS 中进行,但如果您特别想定位标题,请在您要更改的 h2 的开始元素中进行?
Or is there something more concrete like "words that appear on the screen go in HTML, things that affect appearance go in CSS, and jQuery is for actions and stuff."或者有没有更具体的东西,比如“出现在屏幕上的文字在 HTML 中,影响外观的东西在 CSS 中,而 jQuery 用于动作和东西。”
Btw, the examples I've put in the paragraph before this are what I based off of what I've learned so far of HTML, CSS, and jQuery.顺便说一句,我在这之前的段落中的示例是基于我迄今为止所学的 HTML、CSS 和 jQuery 的内容。
One of the key mantras in software development is to separate your concerns.软件开发中的关键准则之一是将您的关注点分开。
When writing a web page, your concerns are:在编写网页时,您关心的是:
The structure is defined by your HTML.该结构由您的 HTML 定义。 The HTML should only define the elements on the page, and do it in a semantic way.
HTML 应该只定义页面上的元素,并以语义方式进行定义。 The body contains sections, the sections contain headings and paragraphs and forms.
正文包含节,节包含标题、段落和表格。 The forms contain inputs, etc.
表单包含输入等。
The style is defined by your CSS.样式由您的 CSS 定义。 Here you describe what things look like.
在这里,您描述事物的样子。 A heading is large, blue and centered.
标题是大的、蓝色的并且居中。 A paragraph is small print, serif font, dark grey.
一个段落是小号字体,衬线字体,深灰色。 The image that relates to the article is floated right.
与文章相关的图像向右浮动。
The functional aspects of the page are defined with JavaScript (and perhaps some other technologies).页面的功能方面是用 JavaScript(也许还有一些其他技术)定义的。 Here you describe what happens when you click a button, or drag an element from one place to another.
在这里,您将描述单击按钮或将元素从一个位置拖到另一个位置时发生的情况。
Keeping these concerns separate means you can change one without having to change the others (to some extent).将这些关注点分开意味着你可以改变一个而不必改变其他的(在某种程度上)。 If you want to change the style of all of the paragraphs, you alter the CSS for the
p
tag and voila, all the paragraph styles are changed.如果要更改所有段落的样式,请更改
p
标记的 CSS,瞧,所有段落样式都将更改。 Imagine if you defined the style of each paragraph in the HTML, separately for each element!想象一下,如果您为每个元素分别定义 HTML 中每个段落的样式! What a nightmare if you want to change them all.
如果你想改变它们,那真是一场噩梦。
Similarly, if you want to make it so that whenever someone hovers over a link, you show a summary of the linked page in a tooltip, you would do this in JavaScript.类似地,如果您想让它在有人将鼠标悬停在链接上时,在工具提示中显示链接页面的摘要,您可以在 JavaScript 中执行此操作。 You write a single event handler, and assign to every link.
您编写一个事件处理程序,并分配给每个链接。 Then, when you want to modify it later, it's a single event handler, and you change it for every link.
然后,当您以后想修改它时,它是一个单独的事件处理程序,您可以为每个链接更改它。
This is a simplified explanation, but I hope that gives you some insight into why 'separation of concerns' is an important guideline to follow.这是一个简化的解释,但我希望能让您深入了解为什么“关注点分离”是一个重要的指导方针。
You're getting close with your comment:你越来越接近你的评论:
Or is there something more concrete like "words that appear on the screen go in HTML, things that affect appearance go in CSS, and jQuery is for actions and stuff."
或者有没有更具体的东西,比如“出现在屏幕上的文字在 HTML 中,影响外观的东西在 CSS 中,而 jQuery 用于动作和东西。”
Generally speaking, avoid #1
.一般来说,避免
#1
。
#2
is ok, but you want to look into external stylesheets. #2
,但您想查看外部样式表。 That's when you have a separate file with .css
extension that contains all of your styles.那就是当你有一个包含所有样式的带有
.css
扩展名的单独文件时。
#3
is used when you are working with javascript or in your example jQuery. #3
在您使用 javascript 或示例 jQuery 时使用。 You don't have to apply css like in your example but sometimes it's the only choice.您不必像示例中那样应用 css,但有时它是唯一的选择。
Now, to address your language, there is no "correct" way to do these things.现在,为了解决您的语言问题,没有“正确”的方法来做这些事情。 There are best practices but there are also cases where each one is appropriate.
有最佳做法,但也有每种做法都适用的情况。
If you're just starting to learn, stick to external stylesshets and use the other methods when appropriate or you seem to have no other choice.如果您刚刚开始学习,请坚持使用外部样式表并在适当的时候使用其他方法,否则您似乎别无选择。
The basics of using an external stylesheet is having a file, named whatever you want but commonly styles.css
or main.css
with your styles inside:使用外部样式表的基础是有一个文件,命名为你想要的任何名称,但通常在
styles.css
或main.css
里面包含你的样式:
.font-blue {
font-color: 00F;
}
And in head
portion of your html you link to it via:在你的 html 的
head
,你通过以下方式链接到它:
<link rel="stylesheet" href="/css/main.css">
That way you can apply the style as follows:这样,您可以按如下方式应用样式:
<p class="font-blue"></p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.