Home Web Front-end HTML Tutorial Code sharing of Html+CSS layout techniques

Code sharing of Html+CSS layout techniques

Mar 25, 2017 am 09:52 AM

Single column layout horizontally centered

Horizontally centeredPage layout中The most common form of layout, which mostly appears in titles and the organization of content areas. Here are four ways to achieve horizontal centering (Note: What is implemented in each example below is the alignment operation of the child element. The parent container of the child element It is the parent element)

Use inline-block and text-align to achieve

.parent{text-align: center;}.child{display: inline-block;}
Copy after login

Advantages: good compatibility;
Disadvantages: need to set child elements and parent elements at the same time

Use margin:0 auto to implement

.child{width:200px;margin:0 auto;}
Copy after login

Advantages: good compatibility
Disadvantages: need to specify the width

Use table to implement

.child{display:table;margin:0 auto;}
Copy after login

Advantages: only need to specify the width of itself Setting up
Disadvantage: IE6,7 needs to adjust the structure

Use absolute positioningAchievement

.parent{position:relative;}/*或者实用margin-left的负值为盒子宽度的一半也可以实现,不过这样就必须知道盒子的宽度,但兼容性好*/
.child{position:absolute;left:50%;transform:translate(-50%);}
Copy after login

Disadvantage: Poor compatibility, available for IE9 and above

Practical flex layout implementation

/*第一种方法*/.parent{display:flex;justify-content:center;}
/*第二种方法*/.parent{display:flex;}.child{margin:0 auto;}
Copy after login

Disadvantages: Poor compatibility, if a large-area layout is performed, efficiency may be affected

Vertical centering

vertical-align

We all know that everyone has different hobbies. Some people like to eat sweets, some people like to eat spicy things, some people don’t like to eat celery, and some people don’t like it. Eat mutton, etc. The same is true for some elements in CSS. Some are only interested in milk, and some only like to eat nuts and jelly, but hate milk. Vertical-align is a picky eater and only likes milk. Eat jelly. I grew up eating jelly. Without jelly, it will get angry and ignore you. I call it "jelly-dependent element", also called "inline-block dependent element". In other words, only if an element belongs to the inline or inline-block (table-cell can also be understood as the inline-block level) level, the vertical-align attribute on it will work. -Some understanding and understanding of -vertical-align

When using vertical-align, since the alignment baseline is marked by the line-height baseline, you need to set line-heightOr set display:table-cell;

/*第一种方法*/.parent{display:table-cell;vertical-align:middle;height:20px;}/*第二种方法*/.parent{display:inline-block;vertical-align:middle;line-height:20px;}
Copy after login

Practical absolute positioning

.parent{position:relative;}
.child{positon:absolute;top:50%;transform:translate(0,-50%);}
Copy after login

Practical flex implementation

.parent{display:flex;align-items:center;}
Copy after login

Horizontal and vertical centering

Use vertical-align , text-align, inline-block implementation

.parent{display:table-cell;vertical-align:middle;text-align:center;}.child{display:inline-block;}
Copy after login

Using absolute positioning to implement

.parent{position:relative;}.child{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);}
Copy after login

Using flex to implement

.parent{display:flex;justify-content:center;align-items:center;}
Copy after login

Multi-column layout, the left column has a fixed width, and the right column is adaptive

This layout method is very common. It is suitable for layouts where the fixed-width side is often navigation and the adaptive side is content.
Code sharing of Html+CSS layout techniques

Using float+margin to implement

.left{float:left;width:100px;}.right{margin-left;margin-left:100px;}
Copy after login

Note: IE6 will have a 3px bug

Use float+margin(fix) to implement

Code sharing of Html+CSS layout techniques

<p class="parent">
 <p class="left"></p>
 <p class="right-fix">
   <p class="right"></p>
  </p>
</p>
Copy after login
.left{width:100px;float:left;}
.right-fix{width:100%;margin-left:-100px;float:right;}
.right{margin-left:100px;}
Copy after login

Use float+overflow to implement

.left{width:100px;float:left;}
.right{overflow:hidden;}
Copy after login

overflow:hidden, triggers bfc mode, floating cannot affect, isolate other elements, IE6 does not support it, set margin-left on the left as the margin between left and right, right Use overflow:hidden to form bfc mode
If we need to set the two columns to equal heights, we can use the following method to set the "background" to equal heights. In fact, it is not the equal heights of the content

.left{width:100px;float:left;}
.right{overflow:hidden;}
.parent{overflow:hidden;}
.left,.right{padding-bottom:9999px;margin-bottom:-9999px;}
Copy after login

Use table implementation

.parent{display:table;table-layout:fixed;width:100%;}
.left{width:100px;}
.right,.left{display:table-cell;}
Copy after login

Practical flex implementation

.parent{display:flex;}
.left{width:100px;}
.right{flex:1;}
Copy after login

Use the flex:1 of the right container to evenly divide the remaining width and achieve the same effect. The default value of align-items is stretch, so the heights of the two are equal

The right column has a fixed width and the left column is adaptive

Practical float+margin implementation

.parent{background:red;height:100px;margin:0 auto;}
.left{background:green;margin-right:-100px;width:100%;float:left;}
.right{float:right;width:100px;background:blue;}
Copy after login

Use table to implement

.parent{display:table;table-layout:fixed;width:100%;}
.left{display:table-cell;}
.right{width:100px;display:table-cell;}
Copy after login

Practical flex implementation

.parent{display:flex;}
.left{flex:1;}
.right{width:100px;}
Copy after login

Two columns with fixed width and one column with adaptive width

Code sharing of Html+CSS layout techniques

The basic html structure is that the parent container is parent, and since The containers are left, center, and right. Among them, left and center have fixed widths, and right is adaptive

Use float+margin to implement

.left,.center{float:left:width:200px;}
.right{margin-left:400px;}
Copy after login

Use float+overflow to implement

.left,.center{float:left:width:200px;}
.right{overflow:hidden;}
Copy after login

Use table implementation

.parent{display:table;table-layout:fixed;width:100%;}
.left,.center,.right{display:table-cell;}
.left,.center{width:200px;}
Copy after login

Use flex to implement

.parent{display:flex;}
.left,.center{width:100px;}
.right{flex:1}
Copy after login

Fixed width on both sides, adaptive middle column

Code sharing of Html+CSS layout techniques

Use float+margin to implement

.left{width:100px;float:left;}
.center{float:left;width:100%;margin-right:-200px;}
.right{width:100px;float:right;}
Copy after login

Use table to implement

.parent{width:100%;display:table;table-layout:fixed}
.left,.center,.right{display:table-cell;}
.left{width:100px;}
.right{width:100px;}
Copy after login

Use flex to implement

.parent{display:flex;}
.left{width:100px;}
.center{flex:1;}
.right{width:100px;}
Copy after login

One column has variable width and one column is adaptive

Code sharing of Html+CSS layout techniques

Use float+ overflow implementation

.left{float:left;}.right{overflow:hidden;}
Copy after login

Use table implementation

.parent{display:table;table-layout:fixed;width:100%;}
.left{width:0.1%;}
.left,.right{display:table-cell;}
Copy after login

Use flex implementation

.parent{display:flex;}
.right{flex:1;}
Copy after login

Multi-column equal distribution layout

多列等分布局常出现在内容中,多数为功能的,同阶级内容的并排显示等。

Code sharing of Html+CSS layout techniques

html结构如下所示

<p class="parent">
<p class="column">1</p>
<p class="column">1</p>
<p class="column">1</p>
<p class="column">1</p>
</p>
Copy after login

实用float实现

.parent{margin-left:-20px}/*假设列之间的间距为20px*/
.column{float:left;width:25%;padding-left:20px;box-sizing:border-box;}
Copy after login

利用table实现

.parent-fix{margin-left:-20px;}
.parent{display:table;table-layout:fixed;width:100%;}
.column{display:table-cell;padding-left:20px;}
Copy after login

利用flex实现

.parent{display:flex;}
.column{flex:1;}
.column+.column{margin-left:20px;}
Copy after login

九宫格布局

使用table实现

<p class="parent">
  <p class="row"><p class="item"></p><p class="item"></p><p class="item"></p></p>
  <p class="row"><p class="item"></p><p class="item"></p><p class="item"></p></p>
  <p class="row"><p class="item"></p><p class="item"></p><p class="item"></p></p>
</p>
Copy after login
.parent{display:table;table-layout:fixed;width:100%;}
.row{display:table-row;}
.item{display:table-cell;width:33.3%;height:200px;}
Copy after login

实用flex实现

<p class="parent"><p class="row"><p class="item"></p><p class="item"></p><p class="item"></p>
</p><p class="row"><p class="item"></p><p class="item"></p><p class="item"></p>
</p><p class="row"><p class="item"></p><p class="item"></p><p class="item"></p></p></p>
Copy after login
.parent{display:flex;flex-direction:column;}
.row{height:100px;display:flex;}
.item{width:100px;background:red;}
Copy after login

全屏布局

Code sharing of Html+CSS layout techniques

利用绝对定位实现

<p class="parent"><p class="top">top</p><p class="left">left</p><p class="right">right</p><p class="bottom">bottom</p></p>
Copy after login
html,body,parent{height:100%;overflow:hidden;}
.top{position:absolute:top:0;left:0;right:0;height:100px;}
.left{position:absolute;top:100px;left:0;bottom:50px;width:200px;}
.right{position:absolute;overflow:auto;left:200px;right:0;top:100px;bottom:50px;}
.bottom{position:absolute;left:0;right:0;bottom:0;height:50px;}
Copy after login

利用flex实现

<p class="parent"><p class="top">top</p><p class="middle"><p class="left">left</p><p class="right">right</p></p><p class="bottom">bottom</p></p>
Copy after login
.parent{display:flex;flex-direction:column;}
.top{height:100px;}
.bottom{height:50px;}
.middle{flex:1;display:flex;}
.left{width:200px;}
.right{flex:1;overflow:auto;}
Copy after login

响应式布局

meta标签的实用

设置布局宽度等于设备宽度,布局viewport等于度量viewport

<meta name="viewport" content="width=device-width,initial-scale=1">
Copy after login

媒体查询

HTML 4和CSS 2目前支持为不同的媒体类型设定专有的样式表, 比如, 一个页面在屏幕上显示时使用无衬线字体,
而在打印时则使用衬线字体, screen 和 print 是两种已定义的媒体类型, 媒体查询让样式表有更强的针对性,
扩展了媒体类型的功能;媒体查询由媒体类型和一个或多个检测媒体特性的条件表达式组成,
媒体查询中可用于检测的媒体特性有width、height和color(等), 使用媒体查询, 可以在不改变页面内容的情况下,
为特定的一些输出设备定制显示效果。

语法

@media screen and (max-width:960px){....}<link rel="stylesheet" media="screen and (max-width:960px)" href=&#39;xxx.css&#39; /
Copy after login

The above is the detailed content of Code sharing of Html+CSS layout techniques. For more information, please follow other related articles on the PHP Chinese website!

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 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
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
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

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.

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

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

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 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.

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

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: 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.

Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Apr 18, 2025 am 09:24 AM

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

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.

See all articles