{"id":4648,"date":"2023-12-20T16:39:37","date_gmt":"2023-12-20T16:39:37","guid":{"rendered":"https:\/\/nikola-breznjak.com\/blog\/?p=4648"},"modified":"2024-04-24T15:20:27","modified_gmt":"2024-04-24T15:20:27","slug":"this-is-how-your-parents-used-css-and-javascript","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/javascript\/this-is-how-your-parents-used-css-and-javascript\/","title":{"rendered":"This is How Your Parents Used CSS and JavaScript"},"content":{"rendered":"<h3>TL;DR<\/h3>\n<p>This is the second part of the &quot;This is How Your Parents Used to Build Websites&quot;. The first blog post is <a href=\"https:\/\/nikola-breznjak.com\/blog\/javascript\/this-is-how-your-parents-used-to-build-websites\/\">here<\/a>.<br \/>\nNow, if you thought our HTML journey was exciting, buckle up! We\u2019re about to dive into the world of CSS and JavaScript. These are the tools that will transform your static HTML pages into vibrant, interactive experiences. <\/p>\n<p>Ready? Let\u2019s go!<\/p>\n<h3>Part 1: CSS &#8211; Making Your Website Stylish<\/h3>\n<h4>Basic Concepts of CSS<\/h4>\n<p>This is how a simple CSS statement would look like: <code>p { color: blue};<\/code>.<\/p>\n<p>There are three basic concepts of CSS:<\/p>\n<ul>\n<li><strong>Selectors<\/strong>:\n<ul>\n<li>Think of them as the &#8216;who&#8217; in styling.<\/li>\n<li>For example, <code>p { }<\/code> targets all <code>&lt;p&gt;<\/code> elements.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Properties<\/strong> and <strong>Values<\/strong>:\n<ul>\n<li>The &#8216;what&#8217; and &#8216;how&#8217;. They go withing the <code>{}<\/code> brackets.<\/li>\n<li>Like <code>color: blue;<\/code> \u2013 this tells the paragraph text to be blue.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>Incorporating CSS into HTML<\/h4>\n<p>There are three ways you can add CSS into HTML:<\/p>\n<ul>\n<li><strong>Inline<\/strong> Styles: Directly in an HTML tag, like <code>&lt;p style=&quot;color: blue;&quot;&gt;<\/code>.<\/li>\n<li><strong>Internal<\/strong> Styles: Inside the <code>&lt;style&gt;<\/code> tag in the HTML head.<\/li>\n<li><strong>External Stylesheets<\/strong>: A separate <code>.css<\/code> file, linked in the HTML head with <code>&lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;<\/code>.<\/li>\n<\/ul>\n<h4>Exploring Common CSS Properties<\/h4>\n<p><strong>Color and Fonts<\/strong><\/p>\n<pre><code>body { color: #333; font-family: &#039;Arial&#039;, sans-serif; }<\/code><\/pre>\n<p><strong>Font Size, Weight, and Style<\/strong><\/p>\n<pre><code>p {\n  font-size: 16px;\n  font-weight: bold;\n  font-style: italic;\n}<\/code><\/pre>\n<p><strong>Text Alignment and Decoration<\/strong><\/p>\n<pre><code>h1 {\n  text-align: center;\n  text-decoration: underline;\n}<\/code><\/pre>\n<p><strong>Margin and Padding<\/strong><\/p>\n<pre><code>.content {\n  margin: 20px;\n  padding: 15px;\n}<\/code><\/pre>\n<p><strong>Borders<\/strong><\/p>\n<pre><code>.box {\n  border: 2px solid #000000; \/* Solid black border *\/\n  border-radius: 10px; \/* Rounded corners *\/\n}<\/code><\/pre>\n<p><strong>Styling Links<\/strong><\/p>\n<ul>\n<li>\n<p>Hover Effect<\/p>\n<pre><code>a:hover {\ncolor: #ff0000; \/* Red color on hover *\/\ntext-decoration: none;\n}<\/code><\/pre>\n<\/li>\n<li>\n<p>Link Visited State<\/p>\n<pre><code>a:visited {\ncolor: #800080; \/* Purple for visited links *\/\n}<\/code><\/pre>\n<\/li>\n<\/ul>\n<p><strong>Layout with Flexbox<\/strong><\/p>\n<ul>\n<li><code>display: flex;<\/code> enables a flexible box layout.<\/li>\n<li>There are games that explain flexbox, so if you&#8217;re curious go check <a href=\"https:\/\/www.google.com\/search?q=games+that+teach+you+flexbox\">them<\/a> out.<\/li>\n<\/ul>\n<h4>Responsive Design Basics<\/h4>\n<p>In order to adapt the site to various screen sizes and make sure it looks good on an iPhone as well as on your laptop, we can use the so-called <strong>media queries<\/strong>:<\/p>\n<p><code><code>@media (max-width: 600px) { body { background-color: lightblue; }}<\/code><\/code><\/p>\n<p>This changes the background color on screens smaller than 600px.<\/p>\n<h4>Advanced Selectors<\/h4>\n<ul>\n<li>\n<p>Pseudo-classes<br \/>\n<code>a:hover { color: red; }<\/code> changes link color on hover<\/p>\n<\/li>\n<li>\n<p>CSS Transitions<br \/>\n<code>transition: background-color 0.5s ease;<\/code><\/p>\n<\/li>\n<li>\n<p>CSS Animations<\/p>\n<pre><code>@keyframes example {\nfrom {background-color: red;}\nto {background-color: yellow;}\n}\ndiv {\nanimation-name: example;\nanimation-duration: 4s;\n}<\/code><\/pre>\n<\/li>\n<\/ul>\n<h4>Advanced CSS<\/h4>\n<p>If you&#8217;re curious enough, please explore <a href=\"https:\/\/sass-lang.com\/\">SASS<\/a> (CSS with superpowers) on your own.<\/p>\n<h3>Part 2: JavaScript &#8211; Bringing Interactivity to Your Website<\/h3>\n<p>JavaScript is the scripting language that lets you create dynamic content.<\/p>\n<h4>Basic Syntax and Concepts<\/h4>\n<p><strong>Variables<\/strong><br \/>\n<code>let message = &#039;Hello, world!&#039;;<\/code> &#8211; with this we defined the variable called &#8216;message&#8217; and saved in it the string &#8216;Hello, world!&#8217;.<\/p>\n<p>You may come across the keyword <code>var<\/code>, but that was an old keyword that is no longer suggested to be used because of the <a href=\"https:\/\/stackoverflow.com\/questions\/762011\/what-is-the-difference-between-let-and-var\">unintended consequences<\/a>.<\/p>\n<p><strong>Functions<\/strong><\/p>\n<pre><code>function greet(name) {\n  alert(&#039;Hello, &#039; + name);\n}<\/code><\/pre>\n<p>With this we created a function called <code>greet<\/code> that takes a parameter called <code>name<\/code> and outputs <code>Hello name<\/code> via an alert (OFC, name is replaced with whatever you pass into the function as a parameter).<\/p>\n<p><strong>Events<\/strong><br \/>\n<code>&lt;button onclick=&quot;greet(&#039;Alice&#039;)&quot;&gt;Greet&lt;\/button&gt;<\/code><\/p>\n<p>With <code>onclick<\/code> we define what function is going to be run when the button is clicked.<\/p>\n<p><strong>DOM Manipulation<\/strong><\/p>\n<ul>\n<li>\n<p>Change text<\/p>\n<pre><code>document.getElementById(&#039;myText&#039;).innerHTML = &#039;New Text&#039;;<\/code><\/pre>\n<\/li>\n<li>\n<p>A button that changes a theme color<\/p>\n<pre><code>&lt;button onclick=&quot;changeColor()&quot;&gt;Change Theme&lt;\/button&gt;\n&lt;script&gt;\nfunction changeColor() {\ndocument.body.style.backgroundColor = &#039;lightblue&#039;;\n}\n&lt;\/script&gt;<\/code><\/pre>\n<\/li>\n<li>\n<p>Changing styles on hover using JavaScript<\/p>\n<pre><code>document.getElementById(&#039;myElement&#039;).onmouseover = function() {\nthis.style.color = &#039;red&#039;;\n};<\/code><\/pre>\n<\/li>\n<li>\n<p>Interactive portfolio enhancements<\/p>\n<pre><code>document.getElementById(&#039;project1&#039;).onmouseover = function() {\nthis.style.transform = &#039;scale(1.1)&#039;;\n};\ndocument.getElementById(&#039;project1&#039;).onmouseout = function() {\nthis.style.transform = &#039;scale(1)&#039;;\n};<\/code><\/pre>\n<\/li>\n<li>\n<p>Handling a click event<\/p>\n<pre><code>document.getElementById(&#039;myButton&#039;).addEventListener(&#039;click&#039;, function() {\nalert(&#039;Button clicked!&#039;);\n});<\/code><\/pre>\n<\/li>\n<\/ul>\n<h4>Advanced topics<\/h4>\n<p><strong>Using <code>fetch<\/code> to load data<\/strong><\/p>\n<pre><code>fetch(&#039;https:\/\/api.example.com\/data&#039;)\n  .then(response =&gt; response.json())\n  .then(data =&gt; console.log(data));<\/code><\/pre>\n<p>I wrote a three series blog post on doing so-called AJAX calls (fetching data from some API), check them out here:<\/p>\n<ul>\n<li><a href=\"https:\/\/nikola-breznjak.com\/blog\/javascript\/making-ajax-calls-pure-javascript\/\">Making AJAX calls in pure JavaScript<\/a><\/li>\n<li><a href=\"https:\/\/nikola-breznjak.com\/blog\/javascript\/making-ajax-calls-using-jquery\/\">Making AJAX calls using jQuery<\/a><\/li>\n<li><a href=\"https:\/\/nikola-breznjak.com\/blog\/javascript\/making-ajax-calls-using-jquery\/\">Making AJAX calls using the Fetch API<\/a><\/li>\n<\/ul>\n<p><strong>JavaScript ES6<\/strong><br \/>\nThere are some awesome things that have been added to the &#8216;new&#8217; flavor of JavaScript, called ES6. I won&#8217;t go into the details here, but use Google (or ChatGPT, or Bard, or &#8230;) and look into these topics:<\/p>\n<ul>\n<li>Arrow Functions: <code>const add = (a, b) =&gt; a + b;<\/code><\/li>\n<li>Template Literals\n<pre><code>const greeting = `Hello, ${name}!`;<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>And that&#8217;s a wrap! We&#8217;ve just scratched the surface of the amazing world of CSS and JavaScript. Remember, practice is key, and the web is your canvas. Go out there and create something awesome!<\/p>\n<p>In the <a href=\"https:\/\/nikola-breznjak.com\/blog\/javascript\/getting-started-with-vue-js-3-by-building-a-pokemon-search-application\/\">next blog post<\/a> we&#8217;ll cover frameworks for CSS and JavaScript, pick one for each and create a Pokemon search application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TL;DR This is the second part of the &quot;This is How Your Parents Used to Build Websites&quot;. The first blog post is here. Now, if you thought our&hellip;<\/p>\n","protected":false},"author":1,"featured_media":4649,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-4648","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/4648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=4648"}],"version-history":[{"count":0,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/4648\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/4649"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=4648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=4648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=4648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}