3. CSS
CSS (Cascading Style Sheets) is a powerful style sheet language that is used to control the layout and styling of web pages. Here are some of the most important concepts in CSS:
1. Here are some basic steps to use CSS in your web pages:
Create a new CSS file: You can create a new CSS file using a plain text editor like Notepad, or a specialized code editor like Visual Studio Code.
Link the CSS file to your HTML document: To link the CSS file to your HTML document, you can use the
<link>
tag in the<head>
section of your HTML document, as described in the previous answer.Write CSS code: In your CSS file, you can write CSS code to style your HTML document. CSS uses selectors to target HTML elements, and then applies properties to those elements. For example:
2. How to connect your CSS file with your HTML document?
To connect your CSS file with your HTML document, you can use the <link>
tag in the <head>
section of your HTML document. Here's an example:
In this example, we've included a link to our styles.css
file using the <link>
tag. The href
attribute specifies the location of the CSS file, relative to the location of the HTML file.
By including the CSS file in this way, all of the styles defined in the CSS file will be applied to the HTML document.
3. How to style each tag? --> Selectors
Selectors are used to target HTML elements and apply styles to those elements. There are many types of selectors, including element selectors, class selectors, ID selectors, and more.
Element selectors target specific HTML elements by their tag name. For example, to target all <p>
elements, you would use the p
selector.
Class selectors target elements based on their class
attribute. For example, to target all elements with the highlight
class, you would use the .highlight
selector.
ID selectors target elements based on their id
attribute. IDs are unique, so there should only be one element with a particular ID on a page. To target an element by its ID, use the #
symbol followed by the ID value.
4. What are some common attributes to style?
CSS has a wide variety of attributes, but some of the most common ones include:
color
- Sets the text color.background-color
- Sets the background color of an element.font-size
- Sets the size of the font.font-family
- Sets the font family for the text.padding
- Sets the padding around an element.margin
- Sets the margin around an element.border
- Sets the border around an element.text-align
- Aligns text within an element.display
- Controls how an element is displayed (e.g. block, inline, flex).position
- Controls the position of an element relative to its parent.
These attributes are just a few examples of the many different ways CSS can be used to style and layout web pages.
5. Box model:
The CSS box model is a way of representing the layout of an HTML element. Every element on a web page can be thought of as a box with four parts: content, padding, border, and margin.
The content area is where the actual content of the element is displayed. Padding is the space between the content area and the border. Border is a line around the content and padding. Margin is the space between the border and the neighboring elements.
Here's an example of how the box model works:
Every HTML tag is automatically treated as a box and can be associated with the box model. Each box has a width, height, and is built from the inside out: content, padding, border, and margin. The box model can be controlled with CSS by defining the appropriate properties such as width, height, padding, border, and margin. For example, we can define a box with a width and height of 200 pixels, 20 pixels of padding, a 1-pixel border, and a 10-pixel margin on all sides using CSS. These properties can be adjusted as needed to control the appearance of the box.
6. Cascading and Specifity - What happens if multiple styles are applied to an element? Which style "wins"?
Cascading is the process by which multiple CSS styles are combined to determine the final style of an element. When multiple styles apply to an element, the rules of specificity come into play.
CSS selectors have different levels of specificity, which determines which styles will take precedence. The most specific selector wins.
Here's an example of how specificity works:
In this example, we've defined a style for all <p>
elements to be red. However, we've also defined a style for elements with the highlight
class to be blue. Since the .highlight
selector is more specific than the p
selector, the text will be blue.
Selectors can be classified into four types, ordered by increasing specificity:
Type selectors and pseudo-elements (e.g. div, span, ::before, ::after)
Class selectors, attribute selectors, and pseudo-classes (e.g. .my-class, [type="text"], :hover)
ID selectors (e.g. #my-id)
Inline styles (e.g. style attribute in HTML)
When multiple selectors target the same element and have conflicting styles, the selector with the highest specificity will win. For example, if we have the following styles:
And we apply both of these styles to an element with id="my-id" class="my-class"
, the color of the text will be red because the ID selector has a higher specificity than the class selector.
7. Layout - How to make your webpage responsive? --> Flexbox
CSS provides a variety of layout techniques to create complex and responsive layouts for web pages. The most common techniques include:
Box layout: The default layout of elements is based on the box model, which we discussed earlier. You can use the
display
property to control how boxes are laid out on the page.Flexbox layout: Flexbox is a powerful layout technique that allows you to create flexible and responsive layouts. You can use the
display: flex
property on a container element to turn it into a flex container. Flexbox allows you to easily control the alignment and spacing of elements.
Here's an example of an easy layout of three boxes next to each other using flexbox:
HTML:
CSS:
In this example, we have a container element that uses display: flex
to enable flexbox layout. We then have three child elements with the class .box
, each with a different background color. We use the flex
property to make the boxes equal width, and set a fixed height of 100px
for each box.
This will result in three boxes next to each other that are equally spaced and fill the available width of the container.