Table of Contents
回复讨论(解决方案)
Home Web Front-end HTML Tutorial A piece of div css code to arrange the two divs on the page left and right. How to implement it without using float? _html/css_WEB-ITnose

A piece of div css code to arrange the two divs on the page left and right. How to implement it without using float? _html/css_WEB-ITnose

Jun 24, 2016 pm 12:08 PM

页面里有一个container, 里面有两个div





已知container的宽是800
让leftDiv宽为200, rightDiv宽为600 横向排列起来,请问要怎么写?

如果不使用float应该怎么实现?(我的页面比这个复杂很多,用了float后ie和firefox显示差异太大,我想知道有没有什么好的技巧能够代替float,所以提了这个问题) 


回复讨论(解决方案)

1,页面再复杂,各种浏览器对float解释的还是不错的。
2,不用float的话,我唯一知道的就是绝对定位了。。。一侧用绝对定位,另外一侧用margin 赋值。
3,推荐还是用float,绝对定位会越搞越麻烦。
4,float的话,左右都要float,下面用clear清除一下,就不会有问题了。ie6 下的双倍边距用_display:inline解决。firefox下的清除,可以写#container:after{overflow:hidden;display:block,content:"";height:0;line-height:0;}就可以解决了。

几点额外的建议:
1. 不要认为你把container宽度设为800,leftDiv设为200,rightDiv设为600就能拥有“实诚”的布局。
实际上,有些宽度是你很容易忽略的,比如border,padding,margin,这些都是很容易忽略的,建议把leftDiv和rightDiv按百分比设定,并且加起来小雨100%。比如:leftDiv = 24%, rightDiv = 74%。
2. 不用一味地float:left; 不要忘记还有float:right。给rightDiv设定float:right有时会有意想不到的收获。

简单地,可以这样写:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb_2312" /><title>No Title</title></head><style>#container { width:800px; padding:0; margin:0 auto; height:100%;}#leftDiv { width:24%; float:left; text-align:center; line-height:600px; height:600px; display:block; background-color:#ccc; color:#f00;}#rightDiv { width:74%; float:right; text-align:center; line-height:600px; height:600px; display:block; background-color:#c00; color:#fff;}</style><body><div id="container"> 	<div id="leftDiv">	leftDiv	</div> 	<div id="rightDiv">	rightDiv	</div> </div> </body></html>
Copy after login


table
布局格式已经固定的话 最外面直接包TABLE一行2列 何必写这么多CSS搞自己头晕 而且里面2个DIV是独立的以后修改也不麻烦

display:inline吧







float这里就不贴代码了,不用float的话,可以用定位来做,这种方法的优点是可以按DIV的顺序来决定优先显示的顺序,代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"  xml:lang="zh-CN" lang="zh-CN"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /></head><body><style type="text/css">body {font-size:9pt;text-align:center;}#container {position:relative;margin:auto;width:800px;height:200px;}#leftDiv {position:absolute;top:0;left:0;width:200px;height:200px;background:#ddd;}#rightDiv {position:absolute;top:0;right:0;width:600px;height:200px;background:#eee;}</style>1、先显示leftDiv<div id="container"> <div id="leftDiv">leftDiv</div> <div id="rightDiv">rightDiv</div> </div>2、先显示rightDiv<div id="container"> <div id="rightDiv">rightDiv</div> <div id="leftDiv">leftDiv</div> </div></body></html>
Copy after login

说白了就是如果不想用float定位,那么就请使用position:absolute吧
使用时请注意在父层div中设定position:relative
然后left和right两个子层position:absolute

多说一句,子层这时候会脱离文本流,但是如果想让此div中数据能够自动撑开div,那么请在该层最下面添加一个清除浮动的元素如 

float这里就不贴代码了,不用float的话,可以用定位来做,这种方法的优点是可以按DIV的顺序来决定优先显示的顺序,代码如下:
HTML code
body{
font-size:9pt;
text-align:center;}
#container{
position:relative;
margin:auto;
width:800px;
height:200px;}
#leftDiv{
position:absolute;
top:0;
left:0;
width:200px;
height:200px;
background:#ddd;}
#rightDiv{
position:absolute;
top:0;
right:0;
width:600px;
height:200px;
background:#eee;}

1、先显示leftDivleftDiv

rightDiv


2. Display rightDiv firstrightDiv< /div>leftDiv

If floating positioning is not allowed, let’s use absolute positioning

The default div is a row-level tag. If you do not use float, you can display it: inline and then set the width of the two divs appropriately

In summary
When you start learning css,
Playing with float:left ,right is a big deal...
Let’s study the next few templates

In summary
When I started learning css,
Play with float:left , right It’s so confusing...
Let’s study the next few templates
Not bad, I’ve studied them all,,,

Use positioning or TBALE

float:right;

Just use float, the effect of each browser can be set almost, it is impossible to be exactly the same

1. float
2. position
3. diaplay: inline
4 , table
5. ....is there any?

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
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 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
1671
14
PHP Tutorial
1276
29
C# Tutorial
1256
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: 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 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 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.

What is the difference between <strong>, <b> tags and <em>, <i> tags? What is the difference between <strong>, <b> tags and <em>, <i> tags? Apr 28, 2025 pm 05:42 PM

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

See all articles