【问题标题】:Divider with centred image in CSS?CSS中居中图像的分隔线?
【发布时间】:2013-12-05 12:22:51
【问题描述】:

如何在 CSS 的中心制作带有徽标的分隔线? !我一直在尝试,但还没有接近。实现这一目标的最佳方法是什么。

谢谢!

更新 这需要放置在背景图像的顶部,因此徽标周围的间隙必须是透明的。

对不起,伙计们,我知道这个有点棘手......

这是 PNG

【问题讨论】:

    标签: css


    【解决方案1】:

    好吧,如果你的背景是完全简单的,那么它就相对简单了。

    HTML

    <header>
      <div id="logo">
        <img src="http://placehold.it/200x100" alt="Placeholder Image" />
      </div>
    </header>
    

    CSS

    body {
      margin: 0;
      background: white;
    }
    
    #logo {
      width: 200px; /* Width of image */
      padding: 40px; /* Creates space around the logo */
      margin: 0 auto; /* Centers the logo */
      background: white; /* Must be same as body */
      position: relative; /* Brings the div above the header:after element */
    }
    
    #logo img {
      display: block;
    }
    
    /* :after pseudo element to create the horizontal line */
    header:after {
      content: '';
      display: block;
      width: 100%;
      height: 1px;
      background: #ccc;
      margin-top: -90px; /* Negative margin up by half height of logo + half total top and bottom padding around logo */
    }
    

    Working demo here.


    编辑

    对于正文(或包含 div)不是纯色的情况,请尝试以下操作:

    HTML

    <div id="header">
      <div id="logo">
        <img src="http://placehold.it/200x100" alt="Placeholder Image" />
      </div>
    </div>
    

    CSS

    body {
      margin: 0;
    }
    
    #logo {
      width: 100%;
    }
    
    #logo, #logo:before, #logo:after {
      float: left;
    }
    
    #logo:before, #logo:after {
      content: '';
      width: 50%;
      min-height: 100px; /* height of image */
      border-bottom: 1px solid #ccc;
      margin-top: -50px;
    }
    
    #logo:before {
      margin-left: -120px;
    }
    
    #logo:after {
      margin-right: -120px;
    }
    
    #logo img {
      float:left;
      padding: 0 20px;
    }
    

    Working demo here.


    或者甚至是基于display: table 的示例,但是在调整大小时这有点不稳定。

    http://jsbin.com/ITAQitAv/10/edit

    【讨论】:

    • 嗯,好东西,但问题是它位于背景图像之上 :( 谢谢
    • 抱歉误会了。会想出另一个选择
    • 几乎在那里,但是当放置在 bg img 的顶部时,徽标的 bg 颜色是可见的。我尝试将其设置为透明,但随后 hr 线完全可见
    • 非常感谢您的麻烦。非常感谢!
    【解决方案2】:

    这是一种方法:

    .hr {
       height: 50px; /* imageheight */
       background: #fff url(http://placekitten.com/100/50) no-repeat  center; 
    }     
    
    .hr hr {
       top: 50%; 
       position: relative;
    }
    
    <div class="hr"><hr /></div>
    

    这将是另一个:

    .hr2{
      display: block;
      border-top: 2px solid black; 
      height: 2px;
    }
    .hr2 img {
      display: block;
      margin: auto;
      margin-top: -31px; /*img-height /-2 + height / 2 */ 
      /* adjustments for 'margin' to border */
      padding: 0 20px; 
      background: #fff;
    }
    
    <div class="hr2"><img src ="http://placekitten.com/100/60"></div>
    

    演示:http://plnkr.co/edit/DznVp8qB9Yak8VfHVzsA?p=preview

    【讨论】:

    • 只需要在徽标周围设置一些左右边距。如果你在我的图片上注意到这条线没有穿过整个标志
    • 您可以为此使用另一个容器,也可以为其添加填充和背景颜色。
    • 我明白了 ;) 但问题是它位于 bg img 之上,所以我不能给它任何 bg 颜色 :( 干杯!
    • 这比较棘手。您应该更新问题并更具体一点。
    猜你喜欢
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2016-01-12
    • 2021-06-14
    • 2016-10-11
    • 1970-01-01
    相关资源
    最近更新 更多