css - 以大写字母和负边距列出

标签 css margin

我正在编写以下列表,即使文本由多行组成,它也能保持大写字母和文本之间的距离。现在大写字母和后面的文本不在同一行上,尤其是在使用不同的浏览器(如 Safari 移动版或 OSX Safari)查看时。我认为负 margin-top 是这里的问题。您有改进此代码的想法吗?非常感谢!

<style type="text/css">

ul {
list-style: none;
margin:10px 0px 0px 0px;
padding: 0px;
}

ul li a {
background-color: #F7F7F7;
border: 1px solid #999999;
color: #000000;
display: block;
font-size: 16px;
margin-bottom: -1px;
padding: 12px 12px;
}

ul li:first-child a {
-webkit-border-top-left-radius: 8px;
-webkit-border-top-right-radius: 8px;
}

ul li:last-child a {
-webkit-border-bottom-left-radius: 8px;
-webkit-border-bottom-right-radius: 8px;
}


#inside {
margin:-22px 0px 0px 26px; 
}

h1 {
font-size: 20px;
font-weight:bold;
display:inline;
}

</style>

<ul>
<li><a href="#"><h1>A</h1><div id="inside">Some text</div></a></li>
<li><a href="#"><h1>B</h1><div id="inside">Some text</div></a></li>
<li><a href="#"><h1>C</h1><div id="inside">Some text</div></a></li>
</ul>

最佳答案

现在定义您的 h1 标签和#inside id display:inline-block;删除否定边距并根据您的设计定义您的 anchor 标记 line-height

习惯了这个css

ul {
list-style: none;
margin:10px 0px 0px 0px;
padding: 0px;
}

ul li a {
background-color: #F7F7F7;
border: 1px solid #999999;
color: #000000;
display: block;
font-size: 16px;
padding: 12px 12px;
    line-height:16px; // add this line
    border-bottom:0; // add this line
}

ul li:first-child a {
-webkit-border-top-left-radius: 8px;
-webkit-border-top-right-radius: 8px;
}

ul li:last-child a {
-webkit-border-bottom-left-radius: 8px;
-webkit-border-bottom-right-radius: 8px;
    border-bottom: 1px solid #999999; // add this line
}


h1, #inside {
margin:0; // add this line
    display:inline-block; // add this line
    vertical-align:top; // add this line
}

h1 {
font-size: 20px;
font-weight:bold;
    margin-right:5px; // add this line
}

Demo

关于css - 以大写字母和负边距列出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13798094/

相关文章:

css - Firefox 中负边距值的问题

html - 图片在源中,但第一次在浏览器中不显示

javascript - 当单击页面中的任何按钮时,将在使用 Jquery-File-Upload 时打开文件对话框

html - 将 DIV 定位在正确的位置

html - 为什么元素不在页面中心(边距 :auto)

css - 向 child 添加边距时容器移动

WPF:堆栈面板中元素之间的间距

javascript - 无法创建水平菜单下拉

CSS z-index 未按预期工作

html - 使用复选框在调整后的大小和带有滚动条的原始大小之间切换图像大小