css - 填充强制 block 元素换行

标签 css padding

HTML代码:

<h1><a href="#">Hello World! Hello World!</a></h1>

CSS 代码:

* {
 box-sizing: border-box; 
}

body {
 text-align: center;
}

h1 {
  display: inline-block;  /*For some known reasons*/
  color: white;
  background-color: gray;
}

h1 a {
  display: block;
  padding: 2%; /*Here is the problem, btw if i apply padding only to the sides (right and left) the phrase stay in one line ???*/
  text-decoration: none;
  color: inherit;
}

Preview

  1. 我不想删除通用选择器*规则
  2. 我不想将 H1 显示更改为内嵌
  3. 我不想使用空格:nowrap;

那么我怎样才能将短语保持在一行中呢?

最佳答案

对此感到抱歉,但我真的很生气,我浪费了这么多时间,最后问题出在使用百分比“%”上,我需要做的就是使用 em 或 rem 或 px,pfff 为什么会这样?

关于css - 填充强制 block 元素换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33725036/

相关文章:

css - JavaFX-2——设置几种样式

android - 在 Selector drawable 中更改 Padding

html - 向右浮动

android - Android Webview 是否支持 CSS3 WebKit 动画?

javascript - Greasemonkey - 图像不显示

html - Firefox:100% 和内部填充?

html - 如何使用 CSS 制作半透明边框?

css - 将填充添加到容器但在网格之外

html - css水平滚动从右到左对齐

css - jQm 以编程方式调整表单单选按钮显示