CSS and HTML:
If you’re just starting out in Web Development, you’ve no doubt seen these acronyms mentioned before, but might not be too clear on them.
That’s why in this guide, I’m going to break them both down and uncover everything you need to know about these two (spoiler alert) fundamental languages of the web.
We’ll explore just exactly what HTML and CSS are, as well as their features, similarities, and differences. Not only that, but we’ll also cover the advantages and disadvantages of each and when to use which, and whether one can exist without the other.
Furthermore, you'll also learn which to tackle first if you’re a newcomer and how these languages bring websites to life with real-world case studies.
Sounds good?
Alright, let’s dive in…
HTML, or ‘HyperText Markup Language,’ is the standard language for creating web pages. It forms the structure and layout of a webpage by using tags and attributes. Understanding HTML is crucial because it lays the foundation upon which all websites are built.
For example
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<title>Title of the Web Page</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Well, in the example above:
<title>
sets the title of the webpage<body>
tag is where content will be presented to the users when they load the webpage in a browser<h1>
and <p>
define different parts of the content, in this case, the text the users are seeing on the page. <h1>
refers to the heading, and <p>
refers to the text sectionI went ahead and created this so you can see it in action below:
HTML has a whole bunch of features that make it an essential part of web development, so let's get into them.
HTML is a beginner-friendly language with a simple syntax that doesn't require any special software to start coding. All you need is a text editor and a web browser to get started.
Sidenote: You could even use a web browser tool like CodePen instead of a text editor if you wanted to.
Codepen is great because it allows you to dive into writing code within your browser without having to install any additional software!
This has an added bonus, in that CodePen also has numerous examples of what can be done with HTML, CSS, and JS that you can check out.
You simply pick an example, check out the code, and then play around to see how it works.
This was one of the first tools I used in learning web development and it made things a lot more fun and exciting because I immediately saw the incredible world of possibilities.
HTML easily integrates with other languages like CSS (more on this soon) and JavaScript.
This then allows web developers to create dynamic and interactive websites, leveraging the benefits of each different programming language.
In fact, every website you’ve ever visited (unless you were browsing the internet in the early 1990s) will use a combination of these 3 technologies to create their user interface.
That being said, no content can exist on the internet though without HTML - it really is the backbone that everything else is built upon.
HTML also supports various multimedia elements like images, audio, video, etc, helping you to make your website more engaging and interactive.
For example
Here you can see a webpage built solely with basic HTML, with no CSS or JavaScript.
And yet, thanks to the modern designs of audio and video players, it works perfectly.
It just doesn’t blow anyone away aesthetically - for now anyway! We can definitely improve on the style and we will later in this post.
Here’s the code also so you can see how it works:
<!-- index.html -->
<!DOCTYPE html>
<html>
<body>
<h1>Audio and Video Embedding in HTML</h1>
<h2>Audio Example</h2>
<audio controls>
<source src="https://music.com/audio-example.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<h2>Video Example</h2>
<video width="640" height="360" controls autoplay muted loop>
<source src="https://video.com/waterfall.mp4" type="video/mp4">
Your browser does not support the video element.
</video>
</body>
</html>
HTML also plays a crucial role in ensuring that websites are accessible to people with disabilities. One way it achieves this is through the use of semantic HTML tags.
These tags not only define the structure of a web page but also provide important information about the content they surround.
For example
A <nav>
tag indicates a navigation section of a webpage, while an <article>
tag signifies a self-contained piece of content.
Screen readers and other assistive technologies rely on these semantic cues to interpret and present content in an accessible way that is inclusive to all users, regardless of their abilities.
Screen readers and other assistive technologies rely on these semantic cues to interpret and present content in an accessible way that is inclusive to all users, regardless of their abilities.
Every programming language comes with its own strengths and weaknesses, and HTML is no exception.
It's essential for serving content to web pages, but it can’t do everything we expect a modern web page to do without a little help from other languages.
Easy to learn and use: As mentioned earlier, HTML is a beginner-friendly language with straightforward syntax, making it easy to learn and use. It has also become more powerful with HTML 5, including the introduction of multimedia elements. Not only that, but there are a heap of resources out therefor you to learn all of the specific elements and attributes
Wide browser support: HTML is supported by nearly all browsers, regardless of the device or platform. This means that it will work on any computer, tablet, or smartphone, regardless of the browser you are using. (Unless you have a computer from the 1990s or older)
Free to use: HTML is free to use, so there's no need to purchase any software or pay any fees to code with HTML
Independent of any Operating System: HTML can be written and executed on any operating system without requiring any specific ones
Limited functionality: HTML is static, which means it lacks the interactive features that other languages like JavaScript can offer. This means that without JavaScript, it is impossible for a user to interact in any way. You can’t submit any data, or even expand and close a navigation menu
Can get a little messy: When used for large websites with multiple pages, maintaining HTML can become complex and difficult. Each page in a multiple page website will need to have its own file and if you have links between them, they will need to be updated on every page
No support for mathematical operations: There's no support for calculations or logical decision-making processes in HTML. Instead, this would all be handled in something like JavaScript, though CSS can do limited calculations
HTML is an essential and beginner-friendly language for creating web page structures, supported by nearly all browsers and operating systems. It's a cornerstone of web development, and you literally cannot build without it.
However, it has limited functionality without other languages, (which is why you would also combine it with JavaScript for interactivity), and can become complex to maintain for large websites. Additionally, if we want visually stunning web pages, CSS is also necessary to enhance the look and feel of HTML pages.
With all that covered, let’s take a look at CSS and see how it works.
CSS, short for ‘Cascading Style Sheets’, serves as the styling language for web pages.
In simple terms, if HTML is the skeleton providing the structure of a webpage, then CSS is the clothing that adds some visual appeal.
So while HTML determines where elements are placed, such as images, headers, and paragraphs, it’s the CSS that controls the appearance of those elements. This includes choosing colors, fonts, and sizes for headers and paragraphs.
Additionally, CSS offers layout styles such as flexbox and grid, which help organize and arrange elements on a webpage in a structured and visually pleasing way.
For example
If we go back to our original example that was just HTML and now add in CSS, we can better see this in action.
It still contains a header and a paragraph, but now the headline color has changed, as well as the background color. Not only that, but the text has moved away from the edges of the page.
Here’s the code with CSS now added:
/* style.css */
body {
background-color: lightblue;
font-family: Arial, sans-serif;
}
h1 {
color: navy;
margin-left: 10px;
}
p {
margin-left: 15px;
margin-top: -20px;
}
Well, let's take a look and break down each major section of the code.
The body
selector targets the entire webpage (the body
element within the HTML), and applies two styles:
background-color
of lightblue
font-family
of Arial (or if Arial is not available, another sans-serif font)(The font is usually the first thing I change, as the default Times New Roman doesn’t have the most modern look, whereas Arial is a much cleaner font that is installed natively on Windows and MacOS).
By applying the font family to the body
element, it will automatically be inherited by every other element. You can actually see this in effect, as the header and paragraph have this font without having to add this property to either one directly.
The h1
selector will affect all level one headers. (There are 6 headers in total, H1 is the largest header and then H2-H6 are increasingly smaller).
The CSS for this section also applies a color
of navy
and a left margin of 10px
to them. This already makes everything look better as the font isn’t stuck to the left side of the screen.
(If you’re not familiar, ‘px’ stands for pixel and each pixel is a dot on the screen).
It's not just the headline that's moved.
The p
selector also added some margin-left and margin-top in order to position it underneath the header.
While we are using a negative value of -20px on margin-top to bring the element up, this is counteracting the margin that is on both an <h1>
and <p>
element by default.
Simple!
So, now that we've got a basic understanding of what CSS is and how it can alter the look of our very basic example with just a few lines of code, let's dive deeper into its features.
Just like with HTML, CSS also comes with a myriad of features that help developers to create visually appealing and functional web pages, so let's break them down.
As we’ve mentioned already, CSS provides a high degree of control over how HTML elements are displayed, including color, font, width, height, etc.
It also allows you to precisely lay out web pages and position HTML elements down to the pixel.
CSS also helps to keep the content separate from the way it looks. This separation makes maintenance easier and improves site performance.
With CSS, you can create responsive designs that adapt and respond to different device resolutions, making your website look great on any device.
This is something that is super important - especially because far more users are accessing websites on their smartphones than ever before.
Fun fact: Most modern browsers like Google Chrome and Microsoft Edge will even allow you to preview how your website will look in multiple screen sizes with emulation, so you can see your responsive design changes.
Below is an image of my personal website, previewed on an iPhone 14 Pro Max screen!
Rather than styling individual HTML elements one by one, styles can be declared once in a CSS selector and then applied to elements throughout the website, thereby saving a lot of coding effort.
For example
By linking to a separate stylesheet we can work on the HTML and CSS in two separate files.
The benefit of this could be that you might want to have general styles in your style.css file and you can link it to multiple HTML files if you have multiple pages in your project:
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<!-- Text -->
<div class="box color">Colored text</div>
<div class="box font-size">Font size</div>
<div class="box font-family">Font family</div>
<div class="box text-align">Centered text</div>
</div>
</body>
</html>
In this image, we can see our HTML with no CSS added. Everything looks the same, just 4 different pieces of text.
So now let’s add the CSS.
/* style.css */
body {
font-family: 'Arial', sans-serif;
background-color: whitesmoke;
color: black;
margin: 0;
padding: 0;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.box {
background-color: white;
padding: 20px;
margin-bottom: 20px;
border-radius: 8px;
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
}
.color {
color: dodgerblue;
}
.font-size {
font-size: 24px;
}
.font-family {
font-family: 'Courier New', Courier, monospace;
}
.text-align {
text-align: center;
}
As you can see, adding the CSS drastically changes the look of our page.
So let's break it all down, and look at what the code is doing.
The body selector defines styles for the entire document, such as setting the font family to Arial or a sans-serif font, the background color to whitesmoke, and the text color to black.
It also removes any default margin and padding, providing a clean starting point for the page layout.
.container
The .container
class is used to style a container element within the document.
It sets the maximum width of the container to 800 pixels, centers it horizontally on the page using margin: 0 auto;, and adds 20 pixels of padding inside the container.
These styles help create a structured layout for content on the webpage, ensuring it is visually appealing and easy to read.
.box
Additionally, the .box
class defines styles for box-like elements on the page.
It gives these elements a white background, 20 pixels of padding, and 20 pixels of margin at the bottom to create space between boxes. The class also applies a border radius of 8 pixels, giving the boxes a slightly rounded appearance, and adds a subtle box shadow for depth.
These styles add visual interest to the page and help differentiate different sections or elements from one another.
As you can see, padding, which is the space within an element and margin, which is the space outside of an element, can be used to create separation that makes a website look more clean and professional.
Finally, we are changing the color, font-size, font-family and text alignment in each individual box element to showcase some of the basic ways that text can be changed using CSS.
And just like that, it was super easy to change!
So, now that we've gone through the basics of HTML and CSS and explored their features, let's compare and contrast them to better understand their functionalities and how they work together.
We've mentioned a few of these already, but it's worth saying again:
Styling Control: CSS provides precise control over style and layout of web pages, including having a different set of styling for smartphones than for laptop screens. You can adjust the color, font, size, spacing, and many other aesthetic aspects with CSS
Efficiency: With CSS, you can write style rules once and apply them to multiple elements throughout your website, saving time and ensuring consistency. You could even have one stylesheet for a multi-page website to not have to write out common styling
Page Load Speed: By keeping the styling separate from the HTML document, CSS files can be downloaded once and stored on the user’s device, which can lead to faster page load times. The content of the page will usually be updated more often than the styling will be, think of an example like Wikipedia, where you’re usually searching for different things, but the styling remains the same on each visit.
For example, Microsoft Edge uses the same underlying technology as Google Chrome (as of 2020), which makes the browsers identical in how they will render CSS
A slight learning curve: While it's not as complex as some other programming languages, mastering CSS and understanding how certain styles will affect the layout can be difficult for beginners. There are a lot of rules to learn and a lot of combinations of properties you can combine. After some repetition though, you’ll get a handle on what your style is and you’ll likely use similar strategies across different projects
Layout Limitations: There are some design limitations when it comes to complex layouts, especially when considering older browsers that may not fully support the latest CSS features. It's also difficult to ensure that every web page is styled perfectly for every screen size because there are so many variations out there, although this is more of a general design struggle that everyone deals with regardless
CSS provides precise control over the styling and layout of web pages, improves efficiency by allowing reusable styles, and enhances page load speeds by separating styling from HTML.
However, it can face cross-browser compatibility issues, has a slight learning curve for beginners, and may have limitations with complex layouts and older browser support.
<p>
for paragraphs, <img>
for images). CSS uses selectors to target HTML elements and properties to define how those elements should be displayed. For example, a CSS rule might set the color and font size for all <p>
tags on a page.html
extension, while CSS files have a .css
extension. This distinction helps organize the structure and style aspects of web development<style>
tags, but it's a common practice to write CSS in separate files and then link them to HTML files using the <link>
tag. This separation keeps the content and styling organized and manageable. HTML cannot be written inside CSS filesWe’ve looked at the features, pros, and cons, so you probably have a pretty good idea of when to use these by now.
Just in case though, these are my recommendations:
HTML should be used when you are creating the content and structure of a webpage. This includes things such as headers, paragraphs, lists, links, images, and forms.
Essentially, anything that is an element of your webpage should be created using HTML. I would usually start a project, like a portfolio website by adding all of the content in my HTML first before starting on the CSS.
CSS, on the other hand, comes into play when you're ready to style your webpage. After the structure and content of the webpage have been created with HTML, CSS is used to style these elements. This includes colors, fonts, alignment, margins, padding, and much more.
Yes, you can use HTML without CSS. However, your webpage will lack any kind of styling and will look very basic and be static.
In the below example, we can see what Wikipedia looks like without any CSS. It is a lot of text, with no layout, everything is just displayed one element after another.
You can check out any website like this by using Firefox and pressing alt
, then v
, then y
, and finally n
to turn off styling.
And here is what that same page looks like with CSS:
As you can see there are different containers created and highlighted with CSS.
We can even see there are two different columns and a lot more information that can be fit on the screen with a better layout.
No. CSS cannot be used without HTML because CSS is designed to style and layout HTML elements.
As we already know, HTML provides the content and structure, while CSS enhances the visual presentation. This means that without HTML, there would be no elements for CSS to style.
It’s kind of obvious once you’ve read the questions above, but start out by learning HTML first because it forms the structure of the webpage. That and the fact that you can’t use CSS without HTML.
However, I will say that you should stick with HTML for a little while until you get the hang of it. This is because although these languages work hand in hand, a good understanding of HTML is crucial before you can start making your pages look good with CSS.
Once you're comfortable with HTML and managing the content on your webpage, you can move on to CSS to start styling your webpage and making it visually appealing.
This is literally the exact approach that I recommend and even teach in my Advanced CSS course.
The first 3 modules teach HTML only so we can understand the depth of it and what it is capable of before diving into any CSS. This includes multimedia, text formatting, and form inputs.
Make sure to check my course out if you want to learn advanced CSS and HTML for web development.
It will take you step-by-step from complete beginner to real mastery by learning modern and advanced CSS techniques. Plus you'll be building awesome real-world projects along the way to practice your skills and build your portfolio.
Yes, you need to know HTML and CSS to effectively build websites with JavaScript.
While it’s true that JavaScript can dynamically create and manipulate HTML elements and apply CSS styles, it still relies on the foundational principles of HTML and CSS to do so, so understanding them is a huge help.
Together, these three technologies form the core of modern web development, allowing you to create engaging, interactive, and visually appealing websites.
For example:
// Create HTML elements dynamically using JavaScript
const body = document.body;
// Create a heading element
const heading = document.createElement('h1');
heading.textContent = 'Hello, World!';
body.appendChild(heading);
// Apply CSS styles using JavaScript
heading.style.color = 'blue';
heading.style.textAlign = 'center';
heading.style.fontFamily = 'Arial, sans-serif';
Here you can see that JavaScript is being used to create a heading element and apply styles to the HTML and CSS dynamically. This means that understanding and learning HTML and CSS is crucial for effectively using JavaScript in web development.
So as you can see, even in the age of technology like JavaScript and Three.js, it’s still incredibly important to understand and know how to use HTML and CSS.
You can’t create fully functional and visually appealing websites without them. HTML provides the foundation, while CSS adds the design elements that make your site stand out.
Mastering these two languages will give you the skills to create engaging, interactive, and aesthetically pleasing websites that can adapt to various devices and browsers.
So why not learn them both now?
As I mentioned before, I teach a course on beginner to advanced CSS and HTML, that also fits perfectly into our courses on JavaScript, Python and other Web Development languages.
No prior knowledge needed to get started, and I walk you through everything step-by-step.
You’ll build projects and gain confidence, and learn fast, in just an hour per day.
And as an added bonus?
When you take this course and join the Zero To Mastery Academy, you’ll also have access to every Web Development course that we have (as well as every other course), so you can follow a complete path to become a working professional Web Developer.
Not only that, but you also get access to our private Discord server!
Here you can ask questions of me directly (or any teacher) as well as fellow students and working professionals.
So what are you waiting for? Join now, and I’ll see you in Discord!