Slides: https://tinyurl.com/y7rq7tpq
We will be using codepen for our practice problems!
All practice problems, solutions, and examples can be found here, but there will also be individual links to each practice problem. https://codepen.io/collection/AaWKGo/
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
Tell us about yourself.
Keep it simple!
From a non-technical (and non-linear) point of view...
Either in your own words or using google
define: attribute
Attribute: a quality or feature regarded as a characteristic or inherent part of someone or something (From Google)
Try to do this with all other elements and words you might not know in HTML/CSS
A website is like a human body!
Your ContentA website is a way to present your content to the world, using HTML and CSS to make it look good.
CSS is like the skin! It makes the website look beautiful and not like a creepy skeleton running around.
<p style="color: red; font-size: 20px; font-style: italic;">
This has inline styles
</p>
<style>
p {
color: red;
font-size: 20px;
font-style: italic;
}
</style>
You put this in the head of your document!
p {
color: red;
font-size: 20px;
font-style: italic;
}
In the beginning...
CSS didn't always exist!
Layout was initially a table and styles were added as attributes (sometimes there were even style HTML tags, such as for font!)
It's ok though! We got CSS in the end!
In order for the internet to be accessible to anyone and everyone, your elements should be as meaningful as possible.
Poor example:
<p>Welcome!</p>
<h2>
This is a lot of text and probably was not a
good idea to use a header for this.
</h2>
Welcome!
Don't style your <p> tags to look like <h1> tags!!
You wouldn't use a spreadsheet for your resume, so don't use a table for page layout.
When you write HTML, you need to check that you are writing the correct and allowed elements within other elements.
For example: you cannot place a <div> inside of a <p>
<p>
<div></div>
</p>
You can look up specific elements and their permitted content on the w3.org site.
List of ElementsStarting out
https://codepen.io/ivanaveliskova/pen/aRXLqmDifference between Responsive Design and Adaptive Design
HTML is already responsive!!! See this example!
Sometimes that's not enough...
We can use CSS to assist in making our pages remain responsive.
Percentage Margins!
Fun fact about using percentage margins and padding:
The actual margin/padding is determined/calculated by the WIDTH of the CONTAINER.
Feel free to use percentage left/right margin/padding on your elements, they will look significantly better than using it on top/bottom.
MOAR CSS!
Use percentage widths and min/max-width in combination for some c R a Z y stuff
img {
width: 50%; /* Images will be 50% of their container */
max-width: 250px; /* Until the image hits 250px
then it will remain at 250px */
min-width: 100px; /* If the image starts to shrink
and suddenly 50% is not legible,
or doesn't look good,
this is as small as it will get */
}
Responsive Gallery
https://codepen.io/ivanaveliskova/pen/NOoXvvIf you want to use a height, for something like a giant hero image, in that case you can use 'vh'!
vh is viewport height
For the image to take up the whole viewport size set height to 100vh
Margin
Border
Padding
Content
Margin
Border
Padding
Content
If we are trying to make pixel perfect responsive designs, percentage widths and padding can mess us up
How do we fix this in order to match our pixel perfect designs?
.selector {
box-sizing: border-box;
}
The box will recalculate itself if there is padding and border to remain the same width and height.
Codepen exampleThe height currently set is messing up the background image and the text.
This will be a quick one! Let's do it together!
Organizing your files
When you have a small site (e.g. a landing page, a CSS file, and maybe a js file, and some images), this isn't as big of a deal
BUT!
If you are consistent and do this no matter how small the site is, your sites will be easier to develop and you will have an easier time working on teams.
Examples
Basic
↳ /your_main_folder ↳ /images — me.jpg ↳ /javascripts — myjs.js ↳ /stylesheets — mystyles.css — index.html
More Advanced
↳ /your_main_folder ↳ /assets ↳ /fonts — icons.woff ↳ /images — me.jpg — logo.svg ↳ /javascripts — jquery.min.js — my_js.js ↳ /stylesheets — my_styles.css ↳ /views ↳ /any_individual_folders — index.html
body {}
div {}
ul {}
p {}
a {}
.header {}
.nav {}
.container {}
.content {}
.footer {}
#facebook-icon {}
#copyright {}
The more selectors we add onto our css selectors will make the stylesheet slower.
Fun fact, the browser reads CSS selectors from right to left.
So the less specific we are on the right (at the end of our selectors) the slower the browser is.
#footer .class a
div .class #link
One methodology for organizing CSS and naming classes is to use something called BEM
Block, Element, Modifier
.puppy {}
.puppy__eyes {}
.puppy__ears {}
.puppy__fur {}
.puppy__tail {}
.puppy__nose {}
.puppy__eyes--left {}
.puppy__eyes--right {}
Try not to use IDs for styling.
Typically IDs are used for JS hooks.
Also they are super specific and can't be reused in the future if you want to have similar styles somewhere else.
IDs can only be overridden with other IDs or more extreme methods such as inline styling or through the use of !important
This is really !important for you to remember:
Avoid using !important in your CSS
Sure, sometimes you can use it, but it overrides any other styles, and if you are trying to override styles and struggling with it, then you need to rethink your styling and not resort to !important
There are times when you might need to use it:
Some jobs might need it
Change all of the classes to use BEM!
Let's take a look at the CSS selectors we might already be familiar with:
.icon
within a
tagsBut CSS is magical and we can select way more stuff and be more specific without resorting to IDs!
div > .pretty {}
<div>
<span class="pretty">
This has the styles!
<span class="pretty">
But this doesn't!
</span>
</span>
</div>
div + .pretty {}
<div></div>
<p class="pretty">
This has the styles!
</p>
<p class="pretty">
But this doesn't!
</p>
div ~ .pretty {}
<div class="pretty">
This doesn't have the styles!
</div>
<p class="pretty">
This has the styles!
</p>
<p class="pretty">
And so does this!
</p>
Examples
Codepen ExamplesEvery element has this hidden space "before" and "after" it.
It can be manipulated a lot like regular elements!
Now a break from our regularly scheduled programming
Several ones you can use or learn about
Sass CSS
$red: #cf202d
$almost-white: #f1f2f2
$rhythm: 8px
body
padding: $rhythm
background: $almost-white
p
color: $almost-white
background: $red
.my-class
color: $red
body {
padding: 8px;
background: #f1f2f2;
}
body p {
color: #f1f2f2;
background: #cf202d;
}
.my-class {
color: #cf202d;
}
SCSS CSS
$red: #cf202d;
$almost-white: #f1f2f2;
$rhythm: 8px;
body {
padding: $rhythm;
background: $almost-white;
p {
color: $almost-white;
background: $red;
}
}
.my-class {
color: $red;
}
body {
padding: 8px;
background: #f1f2f2;
}
body p {
color: #f1f2f2;
background: #cf202d;
}
.my-class {
color: #cf202d;
}
LESS CSS
@red: #cf202d;
@almost-white: #f1f2f2;
@rhythm: 8px;
body {
padding: @rhythm;
background: @almost-white;
p {
color: @almost-white;
background: @red;
}
}
.my-class {
color: @red;
}
body {
padding: 8px;
background: #f1f2f2;
}
body p {
color: #f1f2f2;
background: #cf202d;
}
.my-class {
color: #cf202d;
}
For smaller projects it doesn't make sense to have this set up.
But in larger projects or projects worked on by several people or teams this is extremely helpful.
CSS prepropcessors allow us to write DRY code through the use of mixins which are sort of like functions.
Check out sass-lang for more info on Sass and SCSS and lesscss.org for more on LESS.
There is an upcoming class on Sass that you should definitely check out! (on Nov 27 and 29)
Now back to our regularly scheduled programming!
Block: most elements are set to this; div, p
this means that no matter how wide, they will clear each other and stack on top of one another
Inline-block: this will allow elements to stack up next to one another, as long as there is room, keeps height/width, padding/margin constraints
Inline: the element will only take up as much space as it needs, on spans, any styling elements (em, strong)
will ignore any top/bottom margin you add, will add top/bottom padding, but not move other elements out of the way, will also ignore width and height settings
Table: the elements behave like a table, used with display: table-cell/table-row and so on
None: will hide the element and remove it from affecting the document flow
Not the best rendering solution, you probably shouldn’t put the element in there if you don’t want it to show, usually used in conjunction with js, elements will still load (like images)
Flex: used on parent element to align the internal elements wherever you need them to be
Grid: also used on a parent element to position inner elements similarly to table. But allows for more granular positioning
Live in the inspector!
Every browser renders HTML elements differently
We use CSS to try to unify how things look across all the different browsers
Sometimes browsers won’t accept certain styles without the use of browser specific prefixes
-webkit-, -ms-, -moz-, -o-
But also make sure you also include the code without the prefix at the end!
Inspectors in various browsers are pretty robust!
Can’t access a certain browser or want to see how your code will look on a specific browser on a phone?
Browserstack (free 30min trial)
Microsoft Edge This takes a screen shot of your site on Edge or allows you to run a virtual windows machine
Codepen allows free crossbrowser testing of your code snippets
xcode can allow you to emulate iphones/ipads
Chrome has some robust mobile emulators
Not just for taking notes!
Sometimes things just won’t work no matter what you do. Maybe you are absolutely sure you wrote your markup or styles absolutely perfectly, spelled everything right and it’s still just not working, commenting out chunks of code and checking where it still works is the best way to debug something not working.
Speaking of comments!
Developers love comments! (But don't go overboard)
Comments help others understand your code if you do something a little out of the ordinary
Comments help you remember why you did something out of the ordinary
What is a linter?
A linter checks our code and lets us know of any if we took a wrong turn while writing our code
It will set some rules for your editor to follow and your editor can be set to autimatically fix those bad coding practices or to warn you that you did something bad.
There are some "rules" that are suggested that you can follow, but this is really just an opinion:
And if you don't know any Korean (I don't)
Most editors have plugins for these rules to make it easier for you!
White space isn't read by the browser. So make good use of it!
Use indentation!
add a space between a CSS selector and the opening of the rule block ( .some-class {} ) (the blue highlighted part)
Can't figure out a problem using any of these tips?
ASK REAL PEOPLE!
GDI has a Slack group! Ask in the #css-help channel
We also offer awesome Code & Coffee meetups once a month where you can ask real people real questions!
Also, please feel free to bug me at any time of day (morning, evening, 3am). I might not respond at 3am, but I will definitely respond within 24 hours.