


As a front-end novice, this was one of the questions in my interview for a front-end position. I had no practical experience, so I hit it by mistake, and ended up getting it wrong!
I rarely have time today to think about it seriously. The answer is not necessarily the best solution, but it can achieve the same effect.
Problem description: Place three horizontally aligned DIVs inside a DIV with an uncertain width. The width of the left and right DIVs is fixed at 200px, and the middle DIV fills the remaining width.
This question was the first question I did at that time. After reading the question, I wrote out the answer: isn’t it just a matter of float:left;! (Practice gives true knowledge, and I was defeated by the facts!)
The code I submitted for the written test:
<meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <style type="text/css"> .mainwrap{ width: 1000px; margin: 0 auto; height: 300px; } .left{ width: 150px; height: 100%; background: yellow; float: left; } .right{ width: 150px; height: 100%; background: pink; float: left; } .center{ width: auto; height: 100%; background: green; float: left; } </style> <div class="mainwrap"> <div class="left"></div> <div class="center"></div> <div class="right"></div> </div>The interviewer’s evaluation: This code is a must achieve your target results.
At that time, he did not pursue the transfer issue further. Later, I tried to run the code on the computer and the results are as follows:
center is missing !
It is indeed not the target effect! Suddenly I didn’t know what happened! But I understand the interviewer's evaluation: This code will not achieve your target effect (you are not deep enough in HTML CSS)!
But I am glad that the question is so simple. After seeing this result, I realized that I had fallen into a trap designed by the interviewer without even knowing it!
It has been almost 5 months since the interview. When I encountered similar problems in the project, I could only treat the middle div as a fixed div. I have never seriously looked for a solution to this problem. Always using novice as an excuse, I really missed the opportunity to solve many classic problems. Back to the topic, I thought about it carefully today. This is not necessarily the best solution, but it can achieve the target effect. The code is as follows:
<meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <style type="text/css"> .mainwrap{ width: 1000px; margin: 0 auto; height: 300px; } .left{ width: 150px; height: 100%; background: yellow; float: left; } .right{ width: 150px; height: 100%; background: pink; float: right; position: relative; top: -100%; } .center{ width: auto; height: 100%; background: green; /*float: left;*/ } </style> <div class="mainwrap"> <div class="left"></div> <div class="center"></div> <div class="right"></div> </div>
Running effect:
There is a stretching problem with this problem; on the contrary, the middle div is fixed and the remaining width is divided equally between the left and right.
In fact, it can solve the above problem. This problem is very open. There are many ways to achieve it, such as:
<meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <style type="text/css"> .mainwrap{ width: 1000px; margin: 100px auto; height: 300px; border: 1px solid #ff0000; } .left{ width: 50%; height: 100%; background: yellow; float: left; } .right{ width: 50%; height: 100%; background: pink; float: right; position: relative; top: -100%; } .center{ width: 600px; margin: 0 auto; height: 100%; background: green; /*float: left;*/ position: relative; z-index: 1000; } </style> <div class="mainwrap"> <div class="left"></div> <div class="center"></div> <div class="right"></div> </div>
Done!
Who else has a better solution? Please enlighten me!
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit


Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
