Basics of web development

CSS and Responsive Design

Easy
20 min

CSS-responsive design is a technique that allows you to design websites that automatically scale according to different screen sizes, such as phones, tablets, and computers. This enables the optimal display of website content on different devices.

One way to implement responsive design with CSS is to use media queries. Media queries are CSS rules that check the user's device screen width and set the corresponding style rules. Below is an example of using a media query with CSS:

div {
  width: 100px;
  height: 100px;
  background-color: blue;
}

/* Media query */
@media screen and (min-width: 600px) {
  div {
    width: 200px;
    height: 200px;
  }
}

This CSS code defines that the width and height of the div element are initially 100 pixels and the background color is blue. In the media query, it checks if the screen width is at least 600 pixels, and if it is, the width and height of the div element are changed to 200 pixels.

Another way to implement responsive design with CSS is to use percentage values for the width and height of elements. Below is an example:

div {
  width: 50%;
  height: 50%;
  background-color: red;
}

This CSS code specifies that the width and height of the div element are 50% of the screen width and height. This means that the div automatically scales according to different screen sizes. The background color is red.

These are just a few examples of how you can implement responsive design with CSS. It is important to remember that responsive design allows your website to look good on different devices and improves the user experience.

Practice

Kokeile itse!

Valitettavasti Replit-palvelu on muuttnut lennosta eikä enää anna suorittaa näitä koodeja suoraan selaimessa. Voit klikata alla olevaa "Open in Replit" linkkiä ja avata koodin Replit-palvelussa. Meillä on työn alla etsiä Replitille korvaaja.

hakatemia pro

Ready to become an ethical hacker?
Start today.

As a member of Hakatemia you get unlimited access to Hakatemia modules, exercises and tools, and you get access to the Hakatemia Discord channel where you can ask for help from both instructors and other Hakatemia members.