search
HomeWeb Front-endHTML Tutorial移除行块级元素之间的空格(译文)_html/css_WEB-ITnose

引言

我现在仍然记得作为一个IE6时代的初级开发者拼命的渴望IE拥有display: inline-block;这个属性。当我们想控制间距和填充在inline元素儿不用block and float的时候,inline-block这个属性是及其的有用。
然而,使用inline-block的时候有一个问题,视觉上两个元素之间会出现空白。
有很多办法移除这些恼人的空白

方法 0

唯一一个百分之百解决这个问题的方案是这样:不要在这些元素的源码之间出现空白。

<style type="text/css">ul,li{  margin: 0;  padding: 0;}li{  list-style: none;  display: inline-block;  border: 1px solid black;  padding: 2px;}</style><ul><li>Item</li><li>Item</li><li>Item</li></ul>

下面是效果:

当然了,这个维护起来比较麻烦,但他很实用、和逻辑,最重要的是比较可靠。

方法 1

最好的空白解决方案:设置行区块元素的父元素font-size: 0。因此如果你有一个

元素包裹了一堆
  • 元素,你可以这么做。

    <style type="text/css">ul,li{  margin: 0;  padding: 0;}li{  list-style: none;  display: inline-block;  border: 1px solid black;  padding: 2px;}.inline-block-list{  font-size: 0;}.inline-block-list li{  font-size: 14px;}</style><ul class="inline-block-list">  <li>Item</li>  <li>Item</li>  <li>Item</li></ul>

    这种情况一般情况下和方法 0是一样的结果,不过在IE8中,如果给行块级元素设置了position:absolute,这个会导致一个严重的问题,页面直接就是全部空白了。

    方法 2

    这种方法有点取巧的意思,但仍然可以用。在行块元素之间使用HTML注释,这个原理也是在元素源码之间没有空格

    <style type="text/css">ul,li{  margin: 0;  padding: 0;}li{  list-style: none;  display: inline-block;  border: 1px solid black;  padding: 2px;}.inline-block-list{  font-size: 0;}.inline-block-list li{  font-size: 14px;}.three{  position: absolute;  margin-left: 20px;}</style><ul class="inline-block-list">  <li>Item</li><!-- --><li>Item</li><!-- --><li class="three">Item</li></ul>

    用一个单词形容这个。。。流氓(gross),两个单词形容这种方法。。。真流氓(really gross)。用三个单词。。。就是——你可以使用它(you get it)。他可以正常的显示。

    方法 3

    和方法 2类似,这个方法也有点令人遗憾。你可以利用行块元素的灵活性,用负的间距抵消空白

    <style type="text/css">ul,li{  margin: 0;  padding: 0;}li{  list-style: none;  display: inline-block;  border: 1px solid black;  padding: 2px;}.inline-block-list li{  margin-left: -4px;}</style><ul class="inline-block-list">  <li>Item</li>  <li>Item</li>  <li>Item</li></ul>

    这是一个很差劲的方案,你必须计算这个负间距是多少,有时候还会有不可预料的空白。所以,尽量避免使用这种来解决行块元素之间的空白。

    方法 4

    另外一个基于HTML的hack解决方案就是——只需要将闭合标签>放到下一个标签的开始:

    <style type="text/css">ul,li{  margin: 0;  padding: 0;}li{  list-style: none;  display: inline-block;  border: 1px solid black;  padding: 2px;}.inline-block-list{  font-size: 0;}.inline-block-list li{  font-size: 14px;}</style><ul class="inline-block-list">  <li>Item</li  ><li>Item</li  ><li>Item</li></ul>

    跟HTML注释这种方式相比,没有那么难看,但我知道,我可以移除空白,但没有去考虑这些空白为什么会存在。

    所有的这些解决方案没有一个是完美的,唯一可以选择的在HTML中不要使用空格和缩进也是一个不太好的方案。这不是告诉你“注意使用什么方案”,因为inline-block仍然非常的有用,但当开发者使用它的时候知道如何处理空白非常的重要。

    方法 my

    我的方法是使用浮动,这个在IE7中也是ok的,前面的这些方法在IE系列中某些低版本可能会失效(inline-block不起作用)

    <style type="text/css">ul,li{  margin: 0;  padding: 0;}ul:after{  content: '.';  height: 0;  visibility: hidden;  display: block;  clear: both;}li{  list-style: none;  display: inline-block;  border: 1px solid black;  padding: 2px;  float: left;}</style><ul class="inline-block-list">  <li>Item</li>  <li>Item</li>  <li>Item</li></ul>

    LAST

    author: 燕睿涛
    email: ritoyan@163.com

  • 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
    What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

    The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

    What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

    The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

    What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

    The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

    What is the viewport meta tag? Why is it important for responsive design?What is the viewport meta tag? Why is it important for responsive design?Mar 20, 2025 pm 05:56 PM

    The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

    What is the purpose of the <iframe> tag? What are the security considerations when using it?What is the purpose of the <iframe> tag? What are the security considerations when using it?Mar 20, 2025 pm 06:05 PM

    The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

    How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

    This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

    What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

    Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

    How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

    The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

    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)
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    EditPlus Chinese cracked version

    EditPlus Chinese cracked version

    Small size, syntax highlighting, does not support code prompt function

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    VSCode Windows 64-bit Download

    VSCode Windows 64-bit Download

    A free and powerful IDE editor launched by Microsoft

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools