【问题标题】:How to effectively float an image or background image如何有效地浮动图像或背景图像
【发布时间】:2015-03-02 12:28:33
【问题描述】:

我需要动态显示 2 个表示开头和结尾引号的图形图像文件,如下面的示例屏幕截图所示。

引号需要出现在上方内容块的左右两侧,如图所示。页面上的内容块宽度会有所不同。

我试过浮动和背景图片。有没有人有提示或技巧来正确、动态和灵活地定位 2 个图像文件?

这是我在与@Utkanos 合作后所得到的答案:

HTML

<div class="postsPage_item_content postsPage_item_quote"><?php the_content();?></div>

CSS

div#maincontentcontainer div#primary div div.postsPage_item_content {
  position: relative;
  text-align: center;
}
div#maincontentcontainer div#primary div div.postsPage_item_quote::before, div#maincontentcontainer div#primary div div.postsPage_item_quote::after {
  background-image: url('../images/QUOTE1.png');
  content: '';
  display: block;
  left: 20%;
  height: 28px; /* background-image natural height is 28px */
  position: absolute;
  top: calc(50% - 50px);
  width: 36px; /* background-image natural width is 36px */
}
div#maincontentcontainer div#primary div div.postsPage_item_quote::after {
  background-image: url('../images/QUOTE2.png');
  left: auto;
  right: 20%;
}

显示

期望的结果是 (1) 每个动态渲染的引号都与内容块的顶部对齐,并且 (2) 引号动态定位到内容块的左侧和右侧,边距填充,如红色所示箭头。

【问题讨论】:

  • 虽然可以这样做,但使用 css content 属性和 :before 和 :after 创建它们不是更好吗?
  • 啊。我不熟悉那些CSS样式。我会调查他们。谢谢@MightyPork

标签: css image css-float background-image css-position


【解决方案1】:

伪元素非常适合这种事情。

HTML:

<div id='my_div'>
    <p>Content here.</p>
    <p>Etc.</p>
</div>

CSS:

#my_div {
    position: relative;
}
#my_div::before, #my_div::after {
    content: '';
    width: 50px;
    height: 50px;
    position: absolute;
    display: block;
    background: url('path/to/open_quote_img.png');
    left: 5%;
    top: calc(50% - 25px);
}
#my_div::after {
    background: url('path/to/close_quote_img.png');
    left: auto;
    right: 5%;
}

该代码假定您的报价图形的宽度和高度为 50 像素 - 根据需要进行修改。

最后,为确保您的内容不会覆盖引用图像,请在容器上设置适当的padding-leftpadding-right(在我的示例中为div),以便将内容充分推离它们.

【讨论】:

  • 谢谢@Utkanos,我将尝试您的回答并回复。这对我来说将是一个很好的学习。
  • p/s:请记住使用 :: 而不是 : 作为伪元素。后者用于伪类,例如:hover.
  • @H.Ferrence - 哎呀,我忘记了 left / top 样式 - 请参阅修改后的答案。
  • 不客气,@H.Ferrence。恐怕要进一步收紧此解决方案,使引号与文本的第一行和最后一行的开头和结尾相距一定距离,这就是您更新后的 OQ 似乎根据需要描述的内容,这将很困难。我已经编写了许多这些引用的东西,而且说实话,引用永远不会完全在我希望它们出现的位置。通常,最难正确的是结束报价的位置。最后两个位:将- 50px 更改为- 18px(图像高度的一半)并为此考虑使用blockquote 标签而不是div
  • 虽然结果不是我想要的,但它们已经足够接近了,所以我现在接受这是“最佳答案”,因为 CSS3 中似乎没有办法处理这个。
【解决方案2】:

另一种可能性是在relative 容器内使用absolute 定位。例如:

.container { width:300px; position:relative;padding:20px}
    .left-quote {position:absolute; top:10px; left:10px; font-size:30px;}
    .right-quote {position:absolute; bottom:20px; right:10px; font-size:30px;}
<div class="container">
      <span class="left-quote">"</span>
      <span class="right-quote">"</span>
      <p>is one of the smartest and most dedicated people that I know... he helped the company achieve incredible share of voice in key publications such as...</p>
    </div>

【讨论】:

  • 我正在尝试使用伪 CSS,但我仔细查看了您的答案 @luke2012,并且您的结尾引用出现在内容之后。我需要图形显示在内容的右侧,并带有 vertical-align:top
猜你喜欢
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
  • 2014-10-25
  • 1970-01-01
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多