CSS グリッドとフレックスボックスを使用したレスポンシブ Web デザイン

PHPz
リリース: 2024-08-05 21:34:52
オリジナル
729 人が閲覧しました

Responsive Web Design with CSS Grid and Flexbox

Responsive Web Design Using CSS Grid and Flexbox

Responsive web design is a way to develop web sites so that they work well on various kinds of devices and screen sizes. Instead of having to create multiple versions of a site for different devices, responsive design uses flexible grids and layouts, media queries, and fluid images to make the user experience better, across all platforms.

Why is Responsive Web Design Important?

As more and more people around the world are using cell phones and tablets to browse the internet, having a responsive website isn’t an option anymore—it's a necessity. A responsive design allows for greater usability by allowing consumers to access content seamlessly, regardless of the device they are using. It also improves the user experience by ensuring that content is visually coherent and easily readable across devices This can reduce frustration and encourage interaction. What’s more, responsive design future-proofs websites, letting them adapt to new devices without having to do extensive redesigns.

Today, we'll look at the basics of responsive web design and focus particularly on two powerful CSS techniques: Flexbox and CSS Grid. We will show how these layouts adapt to different screen sizes using a simple website with colorful boxes and numbers.

A Brief History of Responsive Web Design

Responsive web design has changed a lot since the early days of the internet. Media queries, which apply styles based on device characteristics, such as screen size, resolution, and orientation. were introduced in the early 2000s, Flexbox was launched in 2012, and CSS Grid was adopted in 2017. These innovations have allowed designers to create adaptable layouts on a number of different devices, providing a better experience for users.

Creating Responsive Layouts with CSS Grid and Flexbox

Now, let’s check out some practical examples that let us see how Flexbox and CSS Grid work.

Responsive Grid Layout: Color Grid

We'll create a simple layout using CSS Grid.

HTML for Grid Layout:

     Color Grid  
1
2
3
4
5
6
ログイン後にコピー

HTML:

  • The HTML markup creates a responsive container (grid-container) and colorful boxes (grid-item) with different numbers and background colors. The viewport meta tag allows the layout to scale on different devices.

CSS for Grid Layout:

/* styles.css */ body { margin: 0; font-family: Arial, sans-serif; background: #f0f0f0; } .grid-container { display: grid; grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); gap: 10px; padding: 20px; } .grid-item { display: flex; justify-content: center; align-items: center; height: 100px; color: #fff; font-size: 2em; border-radius: 8px; }
ログイン後にコピー

CSS:

  • The CSS for the grid layout uses different grid properties to be more responsive. The display: grid; declaration sets the element with the class grid-container as a grid container, so that we can take advantage of all that grids have to offer. The grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); property define how the columns in the grid are structured: it automatically adjusts the number of columns based on the available space, with each column having a minimum width of 100 pixels and a maximum of 1 fraction (1fr) of the available space. This design allows the grid to adapt to different screen sizes, so that the grid items don’t overflow or create unsightly gaps. The gap: 10px; property applies consistent spacing to the grid items. Lastly, we stack the grid-item elements with display: flex; and center their content with justify-content: center; and align-items: center. This way, each item is well-organized and balanced.

Example of Responsive Web Design: Grid Layout

This grid layout uses:

  1. Dynamic Column Count: The grid automatically adjusts the number of columns to fit the viewport width, with items taking a minimum of 100px.
  2. Flexible Sizing: auto-fit lets the boxes reflow and rearrange when needed, giving everything a more organized look.
  3. Media Queries: While not actually written here, the way the grid elements behave with different screen sizes demonstrates the concept of media queries by using responsive grid properties.

Responsive Flexbox Layout: Color Row

Next, let’s use Flexbox to create a simple row of colored boxes.

HTML for Flexbox Layout:

     Color Row  
1
2
3
4
ログイン後にコピー

HTML:

  • Similar to the grid example, the HTML here creates a flex-container with flex-item boxes that display colored numbers.

CSS for Flexbox Layout:

/* styles.css */ body { margin: 0; font-family: Arial, sans-serif; background: #f5f5f5; } .flex-container { display: flex; flex-wrap: wrap; justify-content: center; padding: 20px; gap: 10px; } .flex-item { display: flex; justify-content: center; align-items: center; height: 100px; width: 100px; color: #fff; font-size: 2em; border-radius: 8px; }
ログイン後にコピー

CSS:
The CSS here uses Flexbox properties to create a responsive layout that adapts to various screen sizes. The display: flex; in the .flex-container gives its child elements, or (flex items), Flexbox functionalities. The flex-wrap: wrap; property allows items to flow onto multiple lines if the container's width is exceeded. The justify-content: center; property centers the flex items along the main axis, so there is a balanced layout regardless of the number of items. The gap: 10px; property introduces uniform spacing between items, improving overall organization. Each .flex-item is also a flex container, using display: flex; to allow further alignment of its inner content, which is centered both vertically and horizontally using justify-content: center; and align-items: center;. The fixed dimensions of height: 100px; and width: 100px; provide uniformity, while the combination of these properties gives the layout a pleasant look while adapting to the needs of different devices.

Example of Responsive Web Design: Flexbox Layout

This flexbox layout demonstrates severalresponsive design characteristics.

  1. Flex Wrapping: The flex-wrap: wrap; property makes boxes move to the next line when they can't fit in the row, adapting to different viewport widths.
  2. Centered Items: Items remain centered regardless of the screen size, improving the overall presentation.
  3. Fixed Dimensions: The boxes have a specific size, but they wrap and readjust based on the available space, allowing for a responsive layout.

Comparing CSS Grid and Flexbox

When it comes to layout design in CSS, Grid and Flexbox are both great choices, but they serve different purposes. CSS Grid is a two-dimensional layout system that allows you to create complex grid structures with rows and columns, making it ideal for layouts where precise control over both dimensions is required, such as in web applications or dashboards. On the other hand, Flexbox is a one-dimensional layout model that is best at distributing space along a single axis—either horizontally or vertically—making it perfect for simpler layouts or smaller components like buttons or navigation menus. While you might choose Grid for a comprehensive, structured layout where elements need to align across both axes, Flexbox would be your go-to for an adaptive, fluid layout that needs to respond to content size. In the end, your choice should depend on the specific needs of your project; often, using both together, complementarily, can give you the best results.

Conclusion

With CSS Grid and Flexbox, you can create adaptable layouts that look great on any device. These examples illustrate how straightforward it can be to implement dynamic designs.

Now it's your turn! Experiment with these techniques, modify the colors and layout settings, and see how simple it is to create fun and responsive designs.
``
sources:
https://www.w3schools.com/css/css3_flexbox.asp
https://www.w3schools.com/css/css_grid.asp
https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout
https://kinsta.com/blog/responsive-web-design/#4-flexbox-layout
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
https://css-tricks.com/snippets/css/complete-guide-grid/
https://blog.logrocket.com/css-flexbox-vs-css-grid/#:~:text=For%20a%20major%20layout%20style,helpful%20when%20working%20with%20rows.

以上がCSS グリッドとフレックスボックスを使用したレスポンシブ Web デザインの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!