Using CSS to solve height-adaptive issues
I am very resistant to using js to solve the highly adaptive problem, because it is difficult to maintain and not natural enough. However, it is not easy to use pure CSS, such as the example I will talk about below.
Requirements:
1. The height of this rectangle is the same as the height of the browser window, and vertical scroll bars cannot appear
2. The height of the green part is fixed, such as 50px
3. Fill the remaining height of the purple part
The HTML structure is as follows:
Look first 1.
html, body {
height: 100%;
margin: 0px;
padding: 0px;
}
#main {
background-color: #999;
height: 100%;
}
Requirement 2 is also very Easy:
#nav {
background-color: #85d989;
height: 50px;
}
Requirement 3 Yes The most troublesome thing is that generally we will think of height:100%, but 100% is based on the height of the parent element. For example, the height of the parent element is 300px, #nav takes up 50px, and #content should be 250px, but Written as height: 100%, the result is that the height of #content also becomes 300%, and a vertical scroll bar appears that is not allowed by requirements.
Of course, it is quite simple to use js to solve this kind of problem, but this time I just don’t want to use js, so let’s try it next:
This demand really makes me collapse, look It seemed simple, but after trying n ways, they all felt unreliable. Finally, I found a method that is closest to the ideal effect, as follows
html, body {
height: 100%;
margin: 0px;
padding: 0px;
}
#main {
background-color: #999;
height: 100%;
}
#nav {
background-color: #85d989;
width: 100%;
height: 50px;
float: left;
}
#content {
background-color: #cc85d9;
height:100%;
}
Floating is used here, and the final result just looks fine. Of course, if you just simply display text and pictures, this kind of The method is enough, but if you want to use js to do some interaction, for example, there is an element that needs to be dragged inside #content, its top minimum value must not be 0, otherwise it will be blocked by #nav. The tragedy is me There was such a need, so I continued to try hard.
After a day of trying, and with the guidance of experts, I finally found the solution, and I burst into tears
#nav {
background-color: #85d989;
width: 100%;
height: 50px;
}
#content {
background-color: #cc85d9;
width: 100%;
position: absolute;
top: 50px;
bottom: 0px;
left: 0px;
}
The key point is to use top and bottom together. This is a very unconventional usage. It can forcefully define the area of the box model. It’s amazing.
The above is the detailed content of Using CSS to solve height-adaptive issues. For more information, please follow other related articles on the PHP Chinese website!

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

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

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-
