居中img

时间:2019-08-07 05:23:38

标签: html css

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

谢谢!

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>

1 个答案:

答案 0 :(得分:0)

尝试一下:

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即可。