Table of Contents
水平居中设置-行内元素
水平居中设置-定宽块状元素
水平居中总结-不定宽块状元素方法
1.加入 table 标签
2.设置 display;inline 方法
3、设置 position:relative 和 left:50%;
垂直居中-父元素高度确定的单行文本
垂直居中-父元素高度确定的多行文本(方法一)
垂直居中-父元素高度确定的多行文本(方法二)
隐性改变display类型
Home Web Front-end HTML Tutorial HTML+CSS学习笔记 (15)

HTML+CSS学习笔记 (15)

Jun 21, 2016 am 08:54 AM

水平居中设置-行内元素

我们在实际工作中常会遇到需要设置水平居中场景,今天我们就来看看怎么设置水平居中的。

如果被设置元素为文本、图片等行内元素时,水平居中是通过给父元素设置 text-align:center 来实现的。如下代码:

html代码:

<body>  <div class="txtCenter">我是文本,哈哈,我想要在父容器中水平居中显示。</div></body>
Copy after login

css代码:

<style>  div.txtCenter{    text-align:center;  }</style>
Copy after login

水平居中设置-定宽块状元素

当被设置元素为块状元素时用text-align:center就不起作用了,这时也分两种情况:定宽块状元素不定宽块状元素。这一小节我们先来讲一讲定宽块状元素。

满足定宽块状两个条件的元素是可以通过设置“左右margin”值为“auto”来实现居中的。我们来看个例子就是设置 div 这个块状元素水平居中:

html代码:

<body>  <div>我是定宽块状元素,哈哈,我要水平居中显示。</div></body>
Copy after login

css代码:

<style>div{    border:1px solid red;/*为了显示居中效果明显为 div 设置了边框*/    width:500px;/*定宽*/    margin:20px auto;/* margin-left 与 margin-right 设置为 auto */}</style>
Copy after login

也可以写成:

margin-left:auto;margin-right:auto;
Copy after login

注意:元素的“上下 margin” 是可以随意设置的。

水平居中总结-不定宽块状元素方法

在实际工作中我们会遇到需要为“不定宽度的块状元素”设置居中,比如网页上的分页导航,因为分页的数量是不确定的,所以我们不能通过设置宽度来限制它的弹性。

不定宽度的块状元素有三种方法居中(这三种方法目前使用的都比较多):

1、加入 table 标签2、设置 display;inline 方法3、设置 position:relative 和 left:50%;

1.加入 table 标签

第一步:为需要设置的居中的元素外面加入一个 table 标签 ( 包括 、、 )。

第二步:为这个 table 设置“左右 margin 居中”(这个和定宽块状元素的方法一样)。

举例如下:

html代码:

<div><table>  <tbody>    <tr><td>    <ul>        <li><a href="#">1</a></li>        <li><a href="#">2</a></li>        <li><a href="#">3</a></li>    </ul>    </td></tr>  </tbody></table></div>
Copy after login

css代码:

<style>table{    margin:0 auto;}ul{list-style:none;margin:0;padding:0;}li{float:left;display:inline;margin-right:8px;}</style>
Copy after login

解释:table会根据内容自动生成宽度,由于li元素是块级元素,为了实现在同一行显示,因此需要设置为内联元素,为了让新添加的内容按照一定顺序排列,因此需要规定排列顺序为left,然后每个li距前一个li的间隔为8px,用外边距来实现

2.设置 display;inline 方法

改变块级元素的 display 为 inline 类型,然后使用 text-align:center 来实现居中效果。如下例子:

html代码:

<body><div class="container">    <ul>        <li><a href="#">1</a></li>        <li><a href="#">2</a></li>        <li><a href="#">3</a></li>    </ul></div></body>
Copy after login
Copy after login

css代码:

<style>.container{    text-align:center;}.container ul{    list-style:none;    margin:0;    padding:0;    display:inline;}.container li{    margin-right:8px;    display:inline;}</style>
Copy after login

这种方法相比第一种方法的优势是不用增加无语义标签,简化了标签的嵌套深度,但也存在着一些问题:它将块状元素的 display 类型改为 inline,变成了行内元素,所以少了一些功能,比如设定长度值。

3、设置 position:relative 和 left:50%;

通过给父元素设置 float,然后给父元素设置 position:relative 和 left:50%,子元素设置 position:relative 和 left:-50% 来实现水平居中。

代码如下:

<body><div class="container">    <ul>        <li><a href="#">1</a></li>        <li><a href="#">2</a></li>        <li><a href="#">3</a></li>    </ul></div></body>
Copy after login
Copy after login

css代码:

<style>.container{    float:left;    position:relative;    left:50%}.container ul{    list-style:none;    margin:0;    padding:0;    position:relative;    left:-50%;}.container li{float:left;display:inline;margin-right:8px;}</style>
Copy after login

这种方法可以保留块状元素仍以 display:block 的形式显示,优点不添加无语议表标签,不增加嵌套深度,但它的缺点是设置了 position:relative,带来了一定的副作用。

解释:1.向右移动 窗口宽度的50%;2.向左移动 元素宽度的50%;3.居中达成。

这三种方法使用得都非常广泛,各有优缺点,具体选用哪种方法,可以视具体情况而定。

垂直居中-父元素高度确定的单行文本

父元素高度确定的单行文本的竖直居中的方法是通过设置父元素的 height 和 line-height 高度一致来实现的。如下代码:

<div class="container">    hi,imooc!</div>css代码:<style>.container{    height:100px;    line-height:100px;    background:#999;}</style>
Copy after login

这样做只能是对单行元素进行居中设置,但是如果是多行要居中就不行了。这个方法虽然简单但是适用性不是很广。

垂直居中-父元素高度确定的多行文本(方法一)

父元素高度确定的多行文本图片块状元素的竖直居中的方法有两种:

方法一:使用插入 table (包括tbody、tr、td)标签,同时设置 vertical-align:middle。

说到竖直居中,css 中有一个用于竖直居中的属性 vertical-align,但这个样式只有在父元素为 td 或 th 时,才会生效。所以又要插入 table 标签了。下面看一下例子:

html代码:

<body><table><tbody><tr><td class="wrap"><div>    <p>看我是否可以居中。</p>    <p>看我是否可以居中。</p>    <p>看我是否可以居中。</p>    <p>看我是否可以居中。</p>    <p>看我是否可以居中。</p></div></td></tr></tbody></table></body>
Copy after login

css代码:

table td{height:500px;background:#ccc}
Copy after login

因为 td 标签默认情况下就默认设置了 vertical-align 为 middle,所以我们不需要显式地设置了。

垂直居中-父元素高度确定的多行文本(方法二)

在 chrome、firefox 及 IE8 以上的浏览器下可以设置块级元素的 display 为 table-cell,激活 vertical-align 属性,但注意 IE6、7 并不支持这个样式。

html代码:

<div class="container">    <div>        <p>看我是否可以居中。</p>        <p>看我是否可以居中。</p>        <p>看我是否可以居中。</p>        <p>看我是否可以居中。</p>        <p>看我是否可以居中。</p>    </div></div>
Copy after login

css代码:

<style>.container{    height:300px;    background:#ccc;    display:table-cell;/*IE8以上及Chrome、Firefox*/    vertical-align:middle;/*IE8以上及Chrome、Firefox*/}</style>
Copy after login

这种方法的好处是不用添加多余的无意义的标签,但缺点也很明显,它的兼容性不是很好,不兼容 IE6、7。

隐性改变display类型

有一个有趣的现象就是当为元素(不论之前是什么类型元素,display:none 除外)设置以下 2 个句之一:

position : absolutefloat : left 或 float:right
Copy after login

元素会自动变为以 display:inline-block的方式显示,当然就可以设置元素的 width 和 height 了且默认宽度不占满父元素。

如下面的代码,小伙伴们都知道 a 标签是行内元素,所以设置它的 width 是 没有效果的,但是设置为 position:absolute 以后,就可以了。

<div class="container">    <a href="#" title="">进入课程请单击这里</a></div>
Copy after login

css代码

<style>.container a{    position:absolute;    width:200px;    background:#ccc;}</style>
Copy after login

我的公众号二维码

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
1252
24
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.

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.

See all articles