这些span元素之间将有一个4像素宽的空间:
跨度{
显示:inline-block;
宽度:100 px;
背景颜色:浅紫红;
}
< p >
<span> Foo </span>
<span> Bar </span>
< / p >
小提琴演示
我知道我可以通过删除HTML中span元素之间的空白来摆脱这个空间:
<p>
<span> Foo </span><span> Bar </span>
</p>
我正在寻找一个CSS解决方案,不涉及:
修改HTML。
JavaScript。
在CSS中有一个简单的解决方案。例如:
HTML
<p class="parent">
<span class="children"> Foo </span>
<span class="children"> Bar </span>
</p>
CSS
.parent {
letter-spacing: -.31em;
*letter-spacing: normal;
*word-spacing: -.43em;
}
.children {
display: inline-block;
*display: inline;
zoom: 1;
letter-spacing: normal;
word-spacing: normal;
}
在我看来,在像em这样的项目中使用font-size: 0是不安全的,所以我更喜欢purecss的解决方案。
你可以在这个链接中检查这个框架。享受:)
.row {
letter-spacing: -.31em;
*letter-spacing: normal;
*word-spacing: -.43em;
/* For better view */
background: #f9f9f9;
padding: 1em .5em;
}
.col {
display: inline-block;
*display: inline;
zoom: 1;
letter-spacing: normal;
word-spacing: normal;
/* For better view */
padding: 16px;
background: #dbdbdb;
border: 3px #bdbdbd solid;
box-sizing: border-box;
width: 25%;
}
<div class="row">
<div class="col">1</div>
<div class="col">2</div>
<div class="col">3</div>
<div class="col">4</div>
</div>
我在React和Sass的一个Free Code Camp项目中尝试了字体大小:0的解决方案来解决类似的问题。
它确实有效!
首先,脚本:
var ActionBox = React.createClass({
render: function() {
return(
<div id="actionBox">
</div>
);
},
});
var ApplicationGrid = React.createClass({
render: function() {
var row = [];
for(var j=0; j<30; j++){
for(var i=0; i<30; i++){
row.push(<ActionBox />);
}
}
return(
<div id="applicationGrid">
{row}
</div>
);
},
});
var ButtonsAndGrid = React.createClass({
render: function() {
return(
<div>
<div id="buttonsDiv">
</div>
<ApplicationGrid />
</div>
);
},
});
var MyApp = React.createClass({
render: function() {
return(
<div id="mainDiv">
<h1> Game of Life! </h1>
<ButtonsAndGrid />
</div>
);
},
});
ReactDOM.render(
<MyApp />,
document.getElementById('GoL')
);
然后是萨斯:
html, body
height: 100%
body
height: 100%
margin: 0
padding: 0
#mainDiv
width: 80%
height: 60%
margin: auto
padding-top: 5px
padding-bottom: 5px
background-color: DeepSkyBlue
text-align: center
border: 2px solid #381F0B
border-radius: 4px
margin-top: 20px
#buttonsDiv
width: 80%
height: 60%
margin: auto
margin-bottom: 0px
padding-top: 5px
padding-bottom: 0px
background-color: grey
text-align: center
border: 2px solid #381F0B
border-radius: 4px
margin-top: 20px
#applicationGrid
width: 65%
height: 50%
padding-top: 0px
margin: auto
font-size: 0
margin-top: 0px
padding-bottom: 5px
background-color: white
text-align: center
border: 2px solid #381F0B
border-radius: 4px
margin-top: 20px
#actionBox
width: 20px
height: 20PX
padding-top: 0px
display: inline-block
margin-top: 0px
padding-bottom: 0px
background-color: lightgrey
text-align: center
border: 2px solid grey
margin-bottom: 0px
这是相同的答案,我给了相关的:显示:内联块-什么是空间?
实际上,有一种非常简单的方法可以从内联块中删除空白,既简单又语义化。它被称为具有零宽度空格的自定义字体,它允许您使用非常小的字体在字体级别上折叠空白(当它们在单独的行上时,由浏览器为内联元素添加)。一旦你声明了字体,你只需要在容器上改变font-family,然后在子容器上再改变,瞧。是这样的:
@font-face{
font-family: 'NoSpace';
src: url('../Fonts/zerowidthspaces.eot');
src: url('../Fonts/zerowidthspaces.eot?#iefix') format('embedded-opentype'),
url('../Fonts/zerowidthspaces.woff') format('woff'),
url('../Fonts/zerowidthspaces.ttf') format('truetype'),
url('../Fonts/zerowidthspaces.svg#NoSpace') format('svg');
}
body {
font-face: 'OpenSans', sans-serif;
}
.inline-container {
font-face: 'NoSpace';
}
.inline-container > * {
display: inline-block;
font-face: 'OpenSans', sans-serif;
}
很合口味。这里是我刚刚在font-forge中制作的字体下载,并使用FontSquirrel webfont generator进行转换。我只花了5分钟。css @font-face声明包括:压缩零宽度空格字体。它在谷歌驱动器,所以你需要点击文件>下载保存到你的电脑。如果您将声明复制到主css文件中,您可能还需要更改字体路径。
我想我应该在这个问题上添加一些新的东西,尽管目前提供的许多答案都是足够的和相关的,有一些新的CSS属性可以实现非常干净的输出,在所有浏览器上完全支持,几乎没有“黑客”。这确实远离了内联块,但它给你的结果与问题要求的相同。
这些CSS属性是网格
CSS网格是高度支持的(CanIUse),除了IE,它只需要一个-ms-前缀来允许它工作。
CSS Grid也是高度灵活的,它把表格、flex和内联块元素的所有好的部分都集中到一个地方。
创建网格时,可以指定行与列之间的间隔。默认间距已经设置为0px,但您可以将此值更改为任何您喜欢的值。
长话短说,这里有一个相关的工作示例:
身体{
背景:lightblue;
字体类型:无衬线;
}
.container {
显示:网格;
Grid-template-columns: 100px;
grid-column-gap: 0;/*不需要但有用,例如*/
grid-row-gap: 0;/*不需要但有用,例如*/
}
.box {
背景:红色;
}
.box: nth-child(甚至){
背景:绿色;
}
< div class = "容器" >
< div class = "盒子" >
你好
< / div >
< div class = "盒子" >
测试
< / div >
< / div >