


How to use CSS and Flexbox to implement responsive layout of images and text at different screen sizes?
Apr 05, 2025 pm 06:06 PMCSS Responsive Layout: The perfect combination of pictures and text
Creating responsive web layouts that allow images and text to be perfectly presented at different screen sizes is a common challenge in front-end development. This article will explain in detail how to use CSS's Flexbox and media query to achieve the effect of displaying pictures and text side by side on larger screens, while on smaller screens (such as mobile phones), the images are on top and text are on bottom.
Requirement background
The goal is to use CSS and Flexbox to build a layout with fixed-size pictures (200x200 pixels) and a piece of text. On a large screen, text takes up the remaining space; on a small screen, the image is located above the text.
Problem and Solution: The Importance of Viewport
Previous attempts may have encountered the problem that the layout does not change with the screen width, which is usually due to the lack of viewport settings. The viewport is the area in the browser window that displays the content of the web page. Correct viewport settings ensure that web pages are rendered and scaled correctly on different devices.
Complete solution: HTML and CSS code
In order to achieve responsive layout, it is necessary to Partially add the viewport meta tag and adjust the CSS code:
<meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Flexbox layout examples</title> <style> .container { display: flex; } .image { width: 200px; height: 200px; } .text { flex: 1; /* 占據(jù)剩余空間 */ } /* 小屏幕媒體查詢(xún) (例如手機(jī)) */ @media (max-width: 600px) { .container { flex-direction: column; /* 垂直布局 */ } .text { flex: initial; /* 取消 flex 屬性,讓文本自適應(yīng)寬度 */ } } </style> <div class="container"> <div class="image">picture</div> <div class="text">Text content</div> </div>
Code explanation:
-
meta name="viewport"
: Set the viewport to make the page scale according to the device width. -
.container
: Use Flexbox layout. -
.image
: Set the image size. -
.text
:flex: 1;
Make text occupy the remaining space. -
@media (max-width: 600px)
: Media query. When the screen width is less than 600px, setflex-direction
tocolumn
to achieve vertical layout, and remove theflex
attribute of.text
to allow the text content to adapt to the width.
Through the above settings, the web page layout will be automatically adjusted according to the screen size to achieve a perfect responsive effect. Remember, you can adjust the value of max-width
according to actual needs.
The above is the detailed content of How to use CSS and Flexbox to implement responsive layout of images and text at different screen sizes?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Against the backdrop of violent fluctuations in the cryptocurrency market, investors' demand for asset preservation is becoming increasingly prominent. This article aims to answer how to effectively hedge risks in the turbulent currency circle. It will introduce in detail the concept of stablecoin, a core hedge tool, and provide a list of TOP3 stablecoins by analyzing the current highly recognized options in the market. The article will explain how to select and use these stablecoins according to their own needs, so as to better manage risks in an uncertain market environment.

The Virtual Currency Exchange APP is a professional digital asset trading application, providing users with safe and convenient digital currency trading services. The new v6.129.0 version has upgraded the performance and operation experience, aiming to bring a smoother trading experience.

This article will discuss the world's mainstream stablecoins and analyze which stablecoins have the risk aversion attribute of "gold substitute" in the market downward cycle (bear market). We will explain how to judge and choose a relatively stable value storage tool in a bear market by comparing the market value, endorsement mechanism, transparency, and comprehensively combining common views on the Internet, and explain this analysis process.

The latest official version of the virtual currency trading platform v6.129.0 is a professional and secure digital asset trading application created for Android users. It provides rich market conditions, convenient trading functions and multiple security protection, and is committed to providing users with a first-class trading experience.

As the market conditions pick up, more and more smart investors have begun to quietly increase their positions in the currency circle. Many people are wondering what makes them take decisively when most people wait and see? This article will analyze current trends through on-chain data to help readers understand the logic of smart funds, so as to better grasp the next round of potential wealth growth opportunities.

This article will introduce several mainstream stablecoins and explain in depth how to evaluate the security of a stablecoin from multiple dimensions such as transparency and compliance, so as to help you understand which stablecoins are generally considered relatively reliable choices in the market, and learn how to judge their "hazard-haven" attributes on your own.

Google Chrome is a free and fast multi-platform web browser developed by Google. It is known for its speed, stability and reliability. Chrome is based on the open source Chromium project and is widely used on devices such as desktops, laptops, tablets and smartphones. The browser has a clean interface and a wide range of customizable options, allowing users to personalize it according to their preferences. In addition, Chrome has a huge library of extensions that provide additional features such as ad blocking, password management and language translation, further enhancing the browsing experience.

The Virtual Digital Coin Exchange APP is a powerful digital asset trading tool, committed to providing safe, professional and convenient trading services to global users. The platform supports a variety of mainstream and emerging digital asset transactions, with a bank-level security protection system and a smooth operating experience.
