css - 如何在匹配基线时左右对齐两个不同大小的文本?

标签 css wordpress alignment

我正在尝试更改标题和标语在 the Coraline WordPress theme 中的显示方式.我希望它们彼此相邻,在基线上对齐,左右对齐。我是 CSS 新手。

我想出了一个解决方案,但它似乎在 Safari 8 中不起作用。在所有主流浏览器的最新版本中都可以使用的可靠替代方案是什么?

这是我在 Chrome 和 Firefox 中的尝试:

#container {
  width: 400px;
  background: #eee;
  /* This below is the key, doesn't work in Safari 8 */
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

#title {
  font-size: 32pt;
  background: #aee;
}

#tagline {
  font-size: 12pt;
  background: #eea;
}
<div id="container">
  <div id="title">Site Title</div>
  <div id="tagline">This is the tagline</div>
</div>

最佳答案

Safari 目前只支持 flex带前缀。

#container {
  display: -webkit-flex; /*new*/
  display: flex;
  -webkit-justify-content: space-between; /*new*/
  justify-content: space-between;
  -webkit-align-items: baseline; /*new*/
  align-items: baseline;
}

更新的演示:

#container {
  width: 400px;
  background: #eee;
  /* This below is the key, doesn't work in Safari 8
     Edit, added -webkit-* prefix */
  display: -webkit-flex;
  display: flex;
  -webkit-justify-content: space-between;
  justify-content: space-between;
  -webkit-align-items: baseline;
  align-items: baseline;
}

#title {
  font-size: 32pt;
  background: #aee;
}

#tagline {
  font-size: 12pt;
  background: #eea;
}
<div id="container">
  <div id="title">Site Title</div>
  <div id="tagline">This is the tagline</div>
</div>

或者,您可以使用inline-block 和一些技巧,如下所示。

#container {
  width: 400px;
  background: #eee;
  text-align: justify;
  font-size: 0; /*fix for white space*/
}

#container:after{
  content: "";
  width: 100%;
  display: inline-block;
}

#title, #tagline {
  display: inline-block;
  vertical-align: bottom;
}

#title {
  font-size: 32pt;
  background: #aee;
}

#tagline {
  font-size: 12pt;
  background: #eea;
}
<div id="container">
  <div id="title">Site Title</div>
  <div id="tagline">This is the tagline</div>
</div>

关于css - 如何在匹配基线时左右对齐两个不同大小的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29688607/

相关文章:

jquery - 我想用 jQuery 在悬停时对 div 产生发光效果

html - 如何将 HTML 代码拆分为右侧图像的两列?

wordpress - 被黑的 WordPress - 如何摆脱 "casino ..."

css - 菜单移动到较小尺寸的下一行

css - Bootstrap 导航栏在 css 中的定位

xcode - 如何在 Xcode 中获取智能选项卡 ("indent with tabs, align with spaces") 行为?

javascript - 如何使用样式标签在js中使用变量

html - CSS - 将div插入无序列表的中间

css - 有什么方法可以得到只有两个元素的简单两列布局?

css - 如何根据需要使用 CSS 设置此菜单样式?