Part 1 - Basic to Intermediate CSS
Cascading Style Sheets (CSS) is essential for web development, used to style and layout web pages. A solid understanding of CSS can elevate your website's design, and having a CSS cheat sheet can streamline the process. In this part, we’ll cover the foundational CSS concepts, properties, and how to apply them to style your HTML content.
CSS Syntax
The basic syntax for writing CSS is as follows:
selector {
property: value;
}
Selector: The HTML element you want to style.
Property: The style property (e.g.,
color
,font-size
).Value: The value you apply to the property (e.g.,
red
,16px
).
Basic Selectors
CSS allows you to target elements using various selectors:
Universal Selector:
*
targets all elements.Element Selector:
h1
targets all<h1>
elements.Class Selector:
.class-name
targets elements with a specific class attribute.ID Selector:
#id-name
targets a specific element with a given ID.Attribute Selector:
[type="text"]
targets input elements with a specific attribute value.
Styling Text
Text styling is crucial for improving readability and enhancing the user experience. Here are some essential text-related properties:
font-family: Sets the font of the text.
font-size: Specifies the size of the text.
font-weight: Controls the boldness of the text (e.g.,
normal
,bold
,lighter
).line-height: Defines the space between lines of text.
text-align: Aligns text horizontally (e.g.,
left
,center
,right
).text-transform: Alters the text case (e.g.,
uppercase
,lowercase
,capitalize
).letter-spacing: Adjusts the spacing between characters.
word-spacing: Controls the space between words.
Example:
p {
font-family: Arial, sans-serif;
font-size: 16px;
line-height: 1.5;
text-align: justify;
}
Colors and Backgrounds
CSS allows you to set background colors, images, and gradients to enhance the visual appeal of your website. You can use several methods to define colors:
Color Names: e.g.,
red
,blue
,green
.Hex Codes: e.g.,
#ff0000
for red.RGB: e.g.,
rgb(255, 0, 0)
for red.RGBA: Adds an alpha (opacity) value to RGB, e.g.,
rgba(255, 0, 0, 0.5)
.
Background properties:
background-color: Sets the background color of an element.
background-image: Adds a background image.
background-position: Defines the position of the background image.
background-size: Specifies the size of the background image.
Example:
div {
background-color: #f0f0f0;
background-image: url('background.jpg');
background-size: cover;
}
Box Model
The CSS box model is fundamental for layout design. It consists of the following components:
Content: The actual content of the element (e.g., text, images).
Padding: The space between the content and the border.
Border: The outer edge of the element, surrounding the padding.
Margin: The space between the border and surrounding elements.
To visualize it:
box-sizing: border-box;
This setting ensures that padding and border are included within the element's total width and height.
Example:
div {
width: 300px;
padding: 20px;
border: 1px solid black;
margin: 10px;
}
Flexbox Layout
Flexbox is a powerful layout model that makes it easier to align and distribute elements within a container, even when their sizes are unknown or dynamic.
Key properties for the container:
display: flex: Enables flexbox on the container.
flex-direction: Defines the direction of the flex items (e.g.,
row
,column
).justify-content: Aligns items along the main axis (e.g.,
center
,space-between
).align-items: Aligns items along the cross axis (e.g.,
center
,flex-start
).flex-wrap: Specifies whether the items should wrap to the next line.
Example:
.container {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
}
Positioning Elements
Positioning allows you to control the placement of elements on the page. There are several types of positioning:
static: Default positioning; elements are positioned according to the normal document flow.
relative: Positions an element relative to its normal position.
absolute: Positions an element relative to the nearest positioned ancestor (non-static).
fixed: Positions an element relative to the browser window.
sticky: Combines relative and fixed positioning; sticks to the top of the viewport when scrolling.
Example:
.element {
position: absolute;
top: 20px;
left: 50px;
}
In the next part of this CSS cheat sheet, we'll dive deeper into advanced CSS features, including animations, transitions, CSS Grid, and media queries. Stay tuned for more!