简体   繁体   English

如何将某些文本右对齐到其他居中的文本

[英]how do I right justify some text to other text that is centered

I want to have a customer testimonial quote centered in the middle of a page. 我希望在页面中间有一个客户推荐报价。 The quote might be arbitrary length, but doesn't span two lines. 引用可能是任意长度,但不跨越两行。 Then I want a new line and then the name of the person that provided the testimonial, just under the testimonial but right justified. 然后我想要一个新的行,然后是提供证词的人的名字,只是在证词下但是正确的。

<div class="quote">
 "Wow!  Thanks, create customer service."
</div>
<div class="source">
  -- John S. - California
</div> 

Styles: 样式:

.quote {text-align:center}
.source {text-align:right; padding-right:300px;}

How do I align the source so that I works for arbitrary length of quotes? 如何对齐源以便我可以使用任意长度的引号?

This will do, probably: 这可能会:

HTML: HTML:

<blockquote>
    <p>
        <span class="quote">
            "Wow!  Thanks, create customer service."
            <cite>
                 -- John S. - California
            </cite>
        </span> 
    </p>
</blockquote>

CSS: CSS:

blockquote {
    text-align: center;
}
.quote {
    position: relative;
}
cite {
     position: absolute;
     right: 0;
     bottom: -25px;
     text-align: right;
}

Change your markup a bit and nest the source into the quote. 稍微更改标记并将源嵌套到引号中。

<div class="quote">
    "Wow!  Thanks, create customer service."
    <div class="source">
         -- John S. - California
    </div> 
</div>

The CSS for it: 它的CSS:

.quote { 
  display:table;
  text-align: center;
  margin:0 auto;
}

.quote .source {
  text-align:right;
}

Here is the fiddle for it. 这是它的小提琴

Basically: you can't without going too ugly and hack-y with your markup. 基本上:你不能不用你的标记太丑陋和黑客。 (See THiCE's answer for such a solution). (参见THiCE对这种解决方案的回答)。

I do not recommend this, but if you're okay with using JavaScript, then this is fairly simple: (below uses jQuery, but can be achieved without it) 推荐这个,但如果你对使用JavaScript没问题,那么这很简单:(下面使用jQuery,但没有它可以实现)

$(".quote").each(function() {
    var $this = $(this);
    $this.next().css({"padding-right": ($this.parent().width() - $this.width()) / 2});
});

If you don't want to use JavaScript and don't want to go hack-y, I suggest you rethink your layout just a bit. 如果您不想使用JavaScript并且不想破解,我建议您重新考虑一下您的布局。

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

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