CSS GRID , MEDIA QUERY and POSITIONING

·

4 min read

GRID LAYOUT

The CSS Grid Layout Module offers a grid-based layout system, with rows and columns, making it easier to design web pages without having to use floats and positioning.


<div class="grid-container">
  <div class="grid-item">1</div>
  <div class="grid-item">2</div>
  <div class="grid-item">3</div>
  <div class="grid-item">4</div>
  <div class="grid-item">5</div>
  <div class="grid-item">6</div>
  <div class="grid-item">7</div>
  <div class="grid-item">8</div>
  <div class="grid-item">9</div>
</div>
.grid-container {
  display: grid;
  grid-template-columns: auto auto auto;
  background-color: #2196F3;
  padding: 10px;
}
.grid-item {
  background-color: rgba(255, 255, 255, 0.8);
  border: 1px solid rgba(0, 0, 0, 0.8);
  padding: 20px;
  font-size: 30px;
  text-align: center;
}
</style>

Display Property

An HTML element becomes a grid container when its display property is set to grid or inline-grid. All direct children of the grid container automatically become grid items. The vertical lines of grid items are called columns.The horizontal lines of grid items are called rows.

<style>
.grid-container {
  display: grid;
  grid-template-columns: auto auto auto;
  background-color: #2196F3;
  padding: 10px;
}
</style>

Grid Gaps

The spaces between each column/row are called gaps. You can adjust the gap size by using one of the following properties:

column-gap row-gap gap


.grid-container {
  display: grid;
  column-gap: 50px;
}

Example The row-gap property sets the gap between the rows:

.grid-container {
  display: grid;
  row-gap: 50px;
}

Example The gap property is a shorthand property for the row-gap and the column-gap properties:

.grid-container {
  display: grid;
  gap: 50px 100px;
}

Example The gap property can also be used to set both the row gap and the column gap in one value:

Grid lines

The lines between columns are called column lines.

The lines between rows are called row lines.

Some Css Grid Properties

column-gap- Specifies the gap between the columns.

Gap- A shorthand property for the row-gap and the column-gap properties.

Grid-A shorthand property for the grid-template-rows, grid-template-columns, grid-template-areas, grid-auto-rows, grid-auto-columns, and the grid-auto-flow properties.

Grid- Column - A shorthand property for the grid-column-start and the grid-column-end properties.

Grid-column-end- Specifies where to end the grid item.

Grid-column-gap- Specifies the size of the gap between columns.

Grid-column-start- Specifies where to start the grid item.

Grid-gap -A shorthand property for the grid-row-gap and grid-column-gap properties.

ALL ABOUT POSITIONING IN CSS

The position property specifies the type of positioning method used for an element (static, relative, fixed, absolute or sticky).

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

There are five different position values:

static relative fixed absolute sticky Elements are then positioned using the top, bottom, left, and right properties. However, these properties will not work unless the position property is set first. They also work differently depending on the position value.

POSITION- STATIC

HTML elements are positioned static by default.

Static positioned elements are not affected by the top, bottom, left, and right properties.

An element with position: static; is not positioned in any special way; it is always positioned according to the normal flow of the page:

Here is the CSS that is used:


<head>
<style>
div.static {
  position: static;
  border: 3px solid #73AD21;
}
</style>
</head>

POSITION- RELATIVE

An element with position: relative; is positioned relative to its normal position.

Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.

Here is the CSS that is used.

div.relative {
  position: relative;
  left: 30px;
  border: 3px solid #73AD21;
}

POSITION-FIXED

An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element.

A fixed element does not leave a gap in the page where it would normally have been located.

Notice the fixed element in the lower-right corner of the page. Here is the CSS that is used:


div.fixed {
  position: fixed;
  bottom: 0;
  right: 0;
  width: 300px;
  border: 3px solid #73AD21;
}

POSITION- ABSOLUTE

An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).

However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.

Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.

div.relative {
  position: relative;
  width: 400px;
  height: 200px;
  border: 3px solid #73AD21;
}

div.absolute {
  position: absolute;
  top: 80px;
  right: 0;
  width: 200px;
  height: 100px;
  border: 3px solid #73AD21;
}

POSITION-STICKY

An element with position: sticky; is positioned based on the user's scroll position.

A sticky element toggles between relative and fixed, depending on the scroll position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).

div.sticky {
  position: -webkit-sticky; 
  position: sticky;
  top: 0;
  background-color: green;
  border: Black;
}