Webdev

CSS Layout:

1. Box Model

The CSS box model is fundamental to layout. It consists of content, padding, border, and margin.

.box {
  width: 200px;
  height: 100px;
  padding: 20px;
  border: 2px solid black;
  margin: 10px;
}

Explanation:

Box-sizing

The box-sizing property changes how the total width and height of elements are calculated.

* {
  box-sizing: border-box;
}

This makes padding and border included in the element’s total width and height, which is often more intuitive for layout purposes.

2. Display Property

The display property determines how an element is treated in the layout flow.

Block-level Elements

div {
  display: block;
}

Block-level elements:

Inline Elements

span {
  display: inline;
}

Inline elements:

Inline-Block

.inline-block {
  display: inline-block;
}

Inline-block elements:

3. Positioning

The position property specifies the type of positioning method used for an element.

Static Positioning

.static {
  position: static;
}

This is the default. Elements are positioned according to the normal flow of the document.

Relative Positioning

.relative {
  position: relative;
  top: 10px;
  left: 20px;
}

The element is positioned relative to its normal position. Other elements are not affected.

Absolute Positioning

.absolute {
  position: absolute;
  top: 50px;
  right: 30px;
}

The element is positioned relative to its nearest positioned ancestor or the initial containing block.

Fixed Positioning

.fixed {
  position: fixed;
  bottom: 0;
  right: 0;
}

The element is positioned relative to the browser window and doesn’t move when scrolled.

4. Flexbox

Flexbox is a one-dimensional layout method for arranging items in rows or columns.

.container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.item {
  flex: 1;
}

Explanation:

5. CSS Grid

CSS Grid is a two-dimensional layout system, allowing you to layout items in rows and columns simultaneously.

.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 20px;
}

.grid-item {
  grid-column: span 2;
}

Explanation:

6. Float

While less common in modern layouts, float is still used for wrapping text around images.

.float-left {
  float: left;
  margin-right: 15px;
}

.clear {
  clear: both;
}

Explanation:

7. Multi-column Layout

For text-heavy content, multi-column layout can improve readability.

.multi-column {
  column-count: 3;
  column-gap: 40px;
  column-rule: 1px solid #ccc;
}

Explanation:

8. Responsive Layout

Media queries allow you to apply different styles for different screen sizes.

.container {
  width: 1200px;
  margin: 0 auto;
}

@media screen and (max-width: 1200px) {
  .container {
    width: 100%;
    padding: 0 20px;
  }
}

@media screen and (max-width: 768px) {
  .grid-container {
    grid-template-columns: 1fr;
  }
}

Explanation:

9. CSS Custom Properties for Responsive Design

Custom properties (CSS variables) can be particularly useful in responsive design.

:root {
  --main-width: 1200px;
}

.container {
  width: var(--main-width);
  margin: 0 auto;
}

@media screen and (max-width: 1200px) {
  :root {
    --main-width: 100%;
  }
}

This approach allows you to define key layout values in one place and easily adjust them for different screen sizes.


next -> Responsive web design

go back