search
HomeWeb Front-endHTML TutorialNegative margin realizes adaptive div left and right layout_html/css_WEB-ITnose

左右排版本来是可以这样实现的:两边都使用百分比,然后左右浮动。但是这样做满足不了下面这样的场景:

   在一个文章页面里面,分为左边的正文区域和右边的与文章关联的其他信息区域,我想让正文区域能随着用户的显示屏宽度变化而变化,这似乎百分比就能做到,但是如果使用百分比,左右两边都必须有一个固定的比值,那样右边也会随着用户显示屏的宽度变化,如果用户的显示屏很宽,那这右边就显得似乎国语宽了,正文无论多款是无所谓的,但是侧边最好是一个固定值。

   就像这种右图所示效果:    

   也就是说我需要一个左边自适应,而右边固定的左右布局,或者说某一边固定,另一边占据剩余部分,该如何做呢?

   我找到的最好的答案是使用负margin(配合浮动),下面概括一下原理:可以500%提高开发效率的前端UI框架!

   使用负margin可以使当前的div左边能容纳下面的div浮动上来,因此把右边的div摆在前面,左边的摆后面,右边的使用负margin就能让左边的浮上来,这样就遮住了右边的左半部分,只要右边再内部使用一个div,外左边距为左边的宽度就实现了左右的排版。


1、左边固定,右边自适应

   (1)右边使用margin-left值为200(刚好是左边的宽度)的宽度,那么左边就刚好能浮动上来。为什么说浮动上来呢,因为如果没有这个margin: 0 0 0 -200px;由于右边的宽度是100%,因此左边肯定是排在下面的。

   (2)左边虽然能浮动上来,但是右边和左边的内容是有重叠的

所以你还需要绿色部分的代码,右边再内部使用一个div,外左边距为左边的宽度



   

       

           这是右边部分
       

   

   

           这是左边部分
   

   这个产生的效果如下图:(图片区域宽度固定的,但是文字是占据这个div的剩余部分,这里右边没有紧贴边框是因为父div有较大padding的原因没能挤满,不要受误导)。可以500%提高开发效率的前端UI框架!

   提示:真正在实现的时候好像没必要左边必须写在后面,我试过,即使按从左到右书写也是可以的,但是很多大型网站都是反着写,我就按正规的来讲了。

 

 

2、右边固定,左边自适应

跟上一个最大的不同点就是,左右两个div容器的代码的前后位置换了,原因就是"float:rihgt"一定要在"float:left"前面。

.代码 

  1.   
  2.   
  3.             这是右边部分  
  
  •   
  •   
  •                 这是左边部分  
  •   
  •   

  • 3. Each of the left and right accounts for a certain percentage, this is simple, change any of the above to 200px A ratio of 20% is achieved.

    .code

    1. This is the right side Section
    2. This is the left part


    4. Add one more column to achieve a three-column layout on the left, middle and right. All we need to do is add another column, and the float is right and there are some empty spaces. Here is a proportional example

    . Code

    1. right; margin: 0 0 0 -40%; width: 100%;">
    2. This is the right part
    3. This is the left part
    4. This is the middle part

    5. To achieve the effect of a frame page (scroll bars can appear on the left and right respectively, and there are no scroll bars on the page): Just add position: absolute; overflow: scroll; height to the left and right divs. : 100%; Add left: 200px; to the left. That’s it. Absolute positioning is used here, so the float attribute can be eliminated. A front-end UI framework that can improve development efficiency by 500%!


    .code

    1. This is the right part
  • This is the left part
  • < ;/div>
  • Statement
    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
    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.

    HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 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.

    From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 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.

    Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

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

    The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

    The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

    HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

    HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

    HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

    HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

    The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 08, 2025 pm 07:05 PM

    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.

    See all articles

    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

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    MinGW - Minimalist GNU for Windows

    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.

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    Safe Exam Browser

    Safe Exam Browser

    Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)