简体   繁体   English

居中引用,但保持文本左对齐

[英]Center blockquote but keep text left-aligned

I created a simple page with Bootstrap and CSS. 我使用Bootstrap和CSS创建了一个简单页面。 I would like my two blockquotes to be centered on the page but with the text kept left-aligned (which Bootstrap does by default for the classes selected). 我希望我的两个blockquotes在页面的中心,但文本保持左对齐(默认情况下,Bootstrap对所选类执行此操作)。

View CodePen for full code. 查看CodePen以获取完整代码。

This is the HTML of one of the blockquotes (as an example): 这是其中一个块引用的HTML(例如):

  <blockquote>
  <p>A wise man proportions his belief to the evidence.</p>
  <footer class="blockquote-footer"><cite>David Hume</cite></footer>
  </blockquote>

Here's the CSS I added for my blockquotes: 这是我为blockquotes添加的CSS:

blockquote {
  font-family: Georgia, serif;
  font-size: 18px;
  font-style: italic;
  width: 500px;
  margin: 3em 0;
  padding: 0.35em 40px;
  line-height: 1.45;
  position: relative;
  color: #383838;
}

blockquote:before {
  display: block;
  padding-left: 10px;
  content: "\201C";
  font-size: 80px;
  /* An element with position: absolute; is positioned relative to the nearest positioned ancestor, which is probably the blockquote */
  position: absolute;
  /* Offsets from the edges of element's containing block, ancestor to which the element is relatively positioned */
  left: -20px; /* Negative moves it left */
  top: -20px; /* Negative moves it toward the top */
  color: #7a7a7a;
}

blockquote cite {
  color: #999;
  font-size: 14px;
  margin-top: 5px;
}

As you've given the blockquote a width , you can use auto margins to horizontally center it. blockquote设置width ,您可以使用自动边距将其水平居中。

Updating your CSS*: 更新CSS *:

blockquote {
  margin: 3em auto !important;
}

updated codepen 更新的代码笔

*Note that your CSS is being overruled by bootstrap. *请注意,您的CSS被引导程序所取代。 You can use !important to rectify that 您可以使用!important来纠正

You can add auto to your margin attribute. 您可以将auto添加到margin属性。 Try this code. 试试这个代码。

blockquote {
  font-family: Georgia, serif;
  font-size: 18px;
  font-style: italic;
  width: 500px;
  margin: 3em auto;
  padding: 0.35em 40px;
  line-height: 1.45;
  position: relative;
  color: #383838;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM