【问题标题】:Centering an img居中 img
【发布时间】:2019-12-14 16:20:13
【问题描述】:

我需要将一个 img 居中(带有 class=“key”的那个,但浮动元素会阻止它。 我应该使用什么技术来居中?

谢谢!

GitHub 仓库:https://github.com/CalogerN/beta-sign-up-split-layout

实时预览:https://calogern.github.io/beta-sign-up-split-layout/

body {
  margin: 0px;
  padding: 0px;
  font-size: 16px;
}

header {
  background-image: url(../img/bg-intro.svg);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  height: 643px;
}

.logo {
  float: left;
}

.wlogo {
  fill: white;
}

.key {
  height: 234px;
  position: relative;
  top: 200px;
}

.key-wrapper {
  display: block;
  margin: auto;
}
<section class="section-a">

  <header>

    <svg width="120" height="35" class="logo" *numbers* </svg>

    <div class="key-wrapper">

      <img src="img/illustration-main.svg" alt="a man with a key" class="key">

    </div>

  </header>

【问题讨论】:

标签: html css


【解决方案1】:

试试看:

HTML 结构:

 <header>
    <div class="svg-wrap">
        <svg width="120" height="35" class="logo" *numbers*="" <="" svg=""></svg>
    </div>
    <div class="key-wrapper">
        <img src="img/illustration-main.svg" alt="a man with a key" class="key">
    </div>
</header>

CSS 结构:

  body {
  margin: 0px;
  padding: 0px;
  font-size: 16px;
}
header {
  background-image: url(../img/bg-intro.svg);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  height: 643px;
}
.wlogo {
  fill: white;
}
.svg-wrap {
  text-align: center;
  margin: 0 auto;
}
.key {
  height: 234px;
  position: relative;
  top: 200px;
}
.key-wrapper {
  display: block; 
  margin: auto;
}

注意:只需将应用 CSS 的 SVG 标记的 div 包装起来,必须将其设置为居中对齐。

【讨论】:

  • 你没看清楚这个问题。我想用“key”类而不是另一个用“logo”类来居中 svg。
  • 好的,亲爱的@CalogeroNapoli,请添加这个 CSS .key-wrapper { text-align: center;}
  • 我明白了。请在移动视图媒体查询中添加:.logo { float: none;边距:0 自动;显示:块; } 和 .logo { 浮动:无;边距:0 自动;显示:块; }
  • 问题是我需要 .logo 来浮动:左。但我试过了,img 居中,问题是它似乎是浮动的。
猜你喜欢
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
  • 1970-01-01
  • 2013-10-04
  • 2017-05-13
  • 2012-08-07
相关资源
最近更新 更多