HAKATEMIA
18CSS basics

CSS and Responsive Design

Easy20MIN

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:

CSS
1div {
2  width: 100px;
3  height: 100px;
4  background-color: blue;
5}
6
7/* Media query */
8@media screen and (min-width: 600px) {
9  div {
10    width: 200px;
11    height: 200px;
12  }
13}

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:

CSS
1div {
2  width: 50%;
3  height: 50%;
4  background-color: red;
5}

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

TRY ITREPLIT

NOTE

Valitettavasti Replit-palvelu on muuttunut 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

Learn to hack — start here

Hundreds of interactive courses, virtual labs and CTF challenges in your browser. Start a free trial — no card required.