Home Web Front-end CSS Tutorial CSS web page layout introductory tutorial 8: Three columns of floating middle column width adaptive_Basic tutorial

CSS web page layout introductory tutorial 8: Three columns of floating middle column width adaptive_Basic tutorial

May 16, 2016 pm 12:07 PM

Using the floating positioning method, fixed width and adaptation from one column to multiple columns can basically be completed easily, including fixed width of three columns. Here we are given a new requirement. We hope to have a three-column layout. The left column in the base requires a fixed width and is displayed on the left. The right column requires a fixed width and is displayed on the right. The middle column needs to be between the left column and the right column. The middle of the right column automatically adapts according to the change in spacing between the left and right columns. This puts forward a new requirement for layout, and it cannot be achieved simply by using float attributes and percentage attributes. CSS currently does not support percentage calculations that are accurate enough to take into account the space occupied by the left and right columns. If you use 100% for the middle column For width, it will use the width of the browser window instead of the middle spacing between the left column and the right column, so we need to rethink this issue.

Absolute positioning
Before starting such a three-column layout, it is necessary to understand a new positioning method - absolute positioning. The previous floating positioning method mainly allows the browser to automatically adjust the floating direction according to the content of the object. However, when this method cannot meet the positioning requirements, a new method is needed to achieve it. CSS provides another method besides floating positioning. The positioning method is absolute positioning, which is achieved using the position attribute.

position Used to set the positioning method of objects. Available values: static/absolute/relative

For every object in the page, the default position attribute is static.
If the object is set to position:absolute, the object will be repositioned according to the position of the entire page. When using this attribute, you can use top, right, bottom, left, that is, the distance values ​​​​in the four directions of top, right, bottom, and left. To determine the specific position of the object, see the following CSS:

#layout {
position:absolute;
top:20px;
left:0px;
}
If# When the layout uses position:absolute;, it will become an absolute positioning mode. At the same time, when setting top:20px;, it will always be 20px away from the top of the browser window, and left:0px; will ensure that it is away from the left margin of the browser. is 0px.

Note: If an object is set with position:absolute; it will be essentially separated from other objects. Its positioning mode will not affect other objects, nor will it be affected by the floating positioning of other objects. In a sense, after using absolute positioning, the object floats on the web page like a layer.

After the object is absolutely positioned, its floating relationship with the page will no longer be considered. You only need to set the values ​​​​of the object's top, right, bottom, and left directions.

In this case, using absolute positioning can solve the problem we raised well. Similarly, use 3 divs to form our three column structure:

Copy code The code is as follows:


#left {
background-color: #E8F5FE;
border: 1px solid #A9C9E2;
height: 400px; width: 200px;
position: absolute;
top: 0px;
left: 0px;
}
#right {
background-color: #FFE7F4;
border: 1px solid #F9B3D5;
height: 400px;
width: 200px;
position: absolute;
top: 0px;
right: 0px;
}

In this way, the left column will be displayed close to the left edge by left: 0px;, while the right column will be displayed by right: 0px; so that the right column will be displayed from the right, and the #center in the middle will use ordinary CSS Style:

Copy code The code is as follows:

#center {
background-color: #F2FDDB;
border: 1px solid #A5CF3D;
height: 400px;
margin-right: 202px;
margin-left: 202px;
}

For #center, we don’t need to set its floating method. We only need to let it have a left margin and always maintain the width of #lef and #right, thus achieving an adaptive width of 202px on both sides. , and the distance between the left and right sides is just enough for #left and #right to be displayed in this space, thus achieving the requirements.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and

See all articles