Table of Contents
box窗口居左
子窗口居左
子窗口居右
box窗口居右
Home Web Front-end HTML Tutorial 详解CSS定位原理_html/css_WEB-ITnose

详解CSS定位原理_html/css_WEB-ITnose

Jun 24, 2016 am 11:36 AM

现在有一个窗口绝对定位position:absolute;,设置css样式left: 0;right: 0;,可以实现窗口的宽度百分之百(100%)。

这时候,没有给窗口设置顶部top:0;,默认情况下窗口顶部为零,或者也可以说,默认情况下,窗口跟随着上一个窗口的底部bottom:0;。

如果上一个窗口是浏览器的边框,那么当前窗口就会紧贴着上一个窗口的边框,也就是浏览器的边框。 总结以上概述,当我们绝对定位一个窗口,设置窗口的左(left)右(right)都为零, 相对于上一个窗口(浏览器)而言,当前窗口的宽度为100%;

.box{    display: block;    position: absolute;    left: 0;    right: 0;} //browser => width:100%;  // box => width:100%;
Copy after login

box窗口居左

还是以上面窗口的定位为例,定义父窗口的类为class = "box",父窗口内部再加一个子窗口定义类为class = "content",然后给父窗口设置居左left:0;

.content{        display: block;        width: 100px;        height: 100px;        background-color: rgb(54, 171, 23); }
Copy after login

这时候,在默认情况下!我们看到的子窗口自动居左。也不排除父窗口绝对定位居左,使得子窗口也随着父窗口居左。 如果我不想子窗口随着上一个窗口的定位而改变子窗口的显示位置,于是我给子窗口进行绝对定位position:absolute; 那么子窗口会不会随着上一个窗口而改变呢?分析如下:

.content{        display: block;     position:absolute;        width: 100px;        height: 100px;        background-color: rgb(54, 171, 23); }
Copy after login

子窗口居左

于是,我在浏览器上看到子窗口设置居左的情况下,即使父窗口的定位居左,而且宽度为零;子窗口还是能显示出来。

.content{        display: block;        position: absolute;        <strong>left</strong>: 0;        width: 100px;        height: 100px;        background-color: rgb(54, 171, 23);    }
Copy after login

子窗口居右

当我给子窗口设置居右的情况下,原先父窗口的定位还是居左,而且宽度为零;子窗口却没有了效果,子窗口到底去哪里呢?

.content{        display: block;        position: absolute;<strong> right</strong>: 0;        width: 100px;        height: 100px;        background-color: rgb(54, 171, 23); }
Copy after login

猜想:子窗口是不是在父窗口的最右边?证明这一点很简单,我们只需给父窗口加上一个width:100%,或者给子窗口加一个width:inherit继承父窗口,亦或者修改content定位为position:relative都可以,提前是你的父窗口要有这个width:100%。此时,发现content窗口显示在最右边。

综上得出:

如果父窗口宽度为零

当我给父窗口(.box)定位居左,(content窗口)子窗口居左能显示子窗口全部效果。

当我给父窗口(.box)定位居右,不能显示全部效果。

如果目前窗口宽度不为零

子窗口定位居右能显示子窗口效果(不全部),居左也能显示子窗口全部效果。

宽度”为零“与”不为零“

简言之,父窗口定位居左,宽度为零,子窗口居左可见(visible),居右不可见(hidden)。父窗口定位居左,宽度不为零,子窗口居左可见(visible),居右也可见(visible)。

详细内容见下文!

其实真正要解决的不是上面的证明,而是子窗口(content)是不是随着父窗口(box)来改变的,还是content窗口随着box的父窗口改变(也就是说随着上上一个窗口改变)?因为box窗口和box父窗口的宽度width都是继承关系,会让我们误以为content窗口也会随着box父窗口而改变的。那么我就修改一下box窗口的宽度width:300px;这次box窗口的宽度就不继承box窗口父窗口的宽度,我发现content窗口是在box窗口的最右边。

.box{    display: block;    position: absolute;    right: 0;    width: 300px;    vertical-align: middle;    background-color: rgba(154, 171, 123,1);}
Copy after login

还是上面的例子,假如现在box父窗口的宽度小于conten子窗口的宽度,设置box父窗口的宽度为width:50px;和conten子窗口twidth:100px; 还是让box父窗口定位居左,当我定位content子窗口居左的时候,还是能看到完整的content子窗口大小,然而box不起作用。

当我定位content窗口居右的时候,看到的不是完整的content窗口的大小,而看到的是box窗口的大小。

总结:a:父窗口的宽度小于子窗口的宽度(子窗口的宽度大于父窗口的宽度),子窗口居左能显示完整的子窗口的大小(宽度),子窗口居右显示父窗口的大小(宽度)。

反之,b:父窗口的宽度大于子窗口的宽度(子窗口的宽度小于父窗口的宽度),子窗口居左和居右都能显示完整的父窗口的大小(宽度)。

分析a and b

如果a成立的话,我把父窗口定位的位置向右移动20像素,那么子窗口定位居左,显示的是子窗口的宽度。//content_long_a

如果a成立的话,我把父窗口定位的位置向右移动20像素,那么子窗口定位居右,显示的是父窗口的宽度width加上父窗口向右移动20像素的位移的值 // 可见区域 = box_long_a

如果b成立的话,我把父窗口定位的位置向右移动20像素,那么子窗口定位居左/居右,都显示的是父窗口的宽度width。 //可见区域content_long_b

如果a成立的话var box_long_a, content_long_a;var box_left = document.querySelector('.box');var content_left = box_left.querySelector('.content');box_long_a = box_left.style.left + box_left.style.width;content_long_a = box.style.width;如果b成立的话var  content_long_b;var box_left = document.querySelector('.box');var content_left = box_left.querySelector('.content');content_long_b = box_left.style.width;
Copy after login

box窗口居右

如果是box窗口居右的情况下,box窗口的宽度还是为零。

.box{    display: block;    position: absolute;    height: 100px;    right: 0;    vertical-align: middle;    background-color: rgba(154, 171, 123,1);}
Copy after login

子窗口还是绝对定位(父窗口的宽度小于子窗口的宽度的情况下。)如果我给子窗口设置居左,看到的效果是显示完整的子窗口的大小(宽度)。

不过这个子窗口的定位是根据父窗口的宽度为零的位置开始的路径。也就是说,子窗口的左边left的值相对于父窗口的left的来定位的,

并非是子窗口的左边left的值相对于父窗口的right来定位的。

 box{right:len;} <==> content{left:len2;}  // len2 = len;
Copy after login

如果我给子窗口设置居右,看到的效果是显示完整的子窗口的大小(宽度)。
这里子窗口的right是根据父窗口的right的值来定位,所以,子窗口从右向左移动多少像素、位移。

子窗口还是绝对定位(父窗口的宽度大于子窗口的宽度的情况下。)
此刻我们不管我们怎么定位子窗口的位置,都能看到子窗口的效果。

 

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

From Text to Websites: The Power of HTML From Text to Websites: The Power of HTML Apr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

See all articles