Introduction
Many beginners often encounter some basic problems when learning HTML and CSS, which can be frustrating.
I originally didnât want to write about basic topics, but I feel that some people may need to see this kind of contentâŚso here are some tips.
There is not much to say, just some issues related to syntax and usage.
Common Issues I Have Noticed
HTML Syntax Problems
Tag Order Problems
I often get asked questions like âWhy isnât my content showing up when I put the tags in?â, or âWhy isnât this workingâŚâ and so on.
The first question I was asked was why the content of the title (<h1> tag) wasnât showing up. I took a look at their code:
|
|
I couldnât believe it â they put the <h1> tag inside the <p> tagâŚ
Itâs clear that they arenât familiar with either of these tags. Both <h1> and <p> are block-level elements, and by default the font size of <h1> is larger than that of <p>. Therefore, putting them together may result in display errors. Normally, these two tags exist on the same level, and both will occupy a line to display. If the larger <h1> tag is nested inside the smaller one, of course you wonât be able to see it~
In summary, there cannot be headings within paragraphs, and they cannot be nested within each other.
Therefore, the correct way to write it should be as follows:
|
|
Missing Symbols
Sometimes I get asked about this issue as well, and I feel like these are all basic errors.
Looking at the code, Iâm like, âWhat is this mess?"
Itâs clearly not standard HTML.
|
|
This type of codeâŚ
Either they werenât paying attention when writing it or theyâre not familiar with how to use these tags.
Or perhaps they donât know how to represent tags.
Let me explain again: there are two types of tags, single tags and double tags.
Taking the tag for inserting an image as an example, the single tag looks like this: <img />. The < at the beginning and > at the end canât be left out, and itâs best to add a / before the closing >.
As for double tags, letâs take the paragraph tag as an example: <p>This is a paragraph.</p>. This type of double tag must have an opening and closing tag. Neither the beginning nor the end can be left out.
When writing double tags and thereâs nesting involved, itâs a good habit to indent each level on a new line.
|
|
This way, the code is not only beautiful, but itâs also easier to maintain and troubleshoot in the future.
Not Differentiating Between <head> and <body>
In addition to the aforementioned issues, there are even cases where people write <div> tags inside the <head>, which indicates that they havenât yet distinguished between the HTML header and content display areas.
I can only explain it this way:
-
The
<head>section is the header information area, which is what the server sends to the browser. The code inside it is not rendered on the page in the browser. -
The
<body>section is the content display area, used to write tags that can be displayed. You can also write<script>tags with JavaScript code nested inside, but CSS styles cannot be written here.
CSS Problems
In addition to syntax issues and not differentiating between sections in HTML, there are also some strange questions people ask me when writing CSS.
Referencing Stylesheets
There are three ways to reference CSS stylesheets, according to textbooks, but the most commonly used are two.
My personal favorite is to use <link> to link the stylesheet. This way, you can split it into two files and write them side by side, making it very convenient.
You donât need to flip back and forth like with inline styles.
As for inline styles? Theyâre not used much, I almost never use them in practice.
But there are still people who donât know how to link stylesheets?
The main issue is not understanding the concept of paths.
Itâs actually very simple â just remember the relative path and then fill it into the href attribute value of the <link> tag.
|
|
Of course, there are still people who donât know how to use inline styles, but thereâs not much to say about it. Just remember that the <style> tag must be placed inside the <head> section and then write the styles using the correct CSS format inside the <style> tag.
For exampleďź
|
|
When writing CSS, losing ;, {, and } and misspelling words.
These are all minor mistakes that can be improved with more practice and attention. The correct template should look like this:
|
|
|
|
Remove default padding and margin
Many beginners do not develop the habit of resetting default padding and margin in CSS when they start coding, which leads to difficulty in styling as they progress.
In fact, itâs quite simple:
|
|
* is a regular wildcard character in CSS, and it has the lowest priority among all selectors. Setting its margin and padding properties to 0 before everything else can eliminate the default padding and margin for all elements before they are selected by a specific selector. This makes it much easier to make more accurate adjustments to element spacing later on.
If youâre still not sure whether or not to include it, try it out and see the difference for yourself.
Inappropriate naming when using class selectors and ID selectors
This is also a big problem that greatly affects the readability and maintainability of the code.
I often see something like this:
|
|
Itâs hard to know what element it actually selects, which increases workloadâŚ
Because at first glance, itâs unclear what it refers to.
Also, using Chinese characters for naming, although I used to like to do this when I was in middle school, this habit is not good because if there are some server encoding issues, the style files may not be loaded properly.
|
|
But some people even use numbers!
And then they ask me why the style is not displaying properly!
|
|
Numbers or names that start with numbers cannot be used as class selector names and IDs in CSS. Similarly, in many programming languages, it is not allowed to name variables using numbers or names that begin with numbers.
To avoid this problem, naming conventions such as camelCase and _ concatenation can be used.
- Upper camel case refers to two words combined, with the first letter of each word capitalized, such as
TopHead. - Lower camel case refers to two words combined, with only the first letter of the second word capitalized, such as
contentPlace.
For naming conventions with more than two words, the _ character is needed to combine them, such as the_menu_bar.
|
|
This significantly improves readability so that one can generally tell at a glance which element corresponds to which, without having to spend time searching through the code.
End
The above are just my personal opinions and methods for addressing some of the small issues that beginner learners may encounter when studying HTML and CSS.
There may be other problems that I havenât discovered yetâŚ
Feel free to leave a comment below to share your thoughts and feedback.