【问题标题】:How can I set the log image on header center vertical?如何在标题中心垂直设置日志图像?
【发布时间】:2021-01-10 18:40:06
【问题描述】:

我希望我的徽标垂直居中,但是,margin: auto 或 margin: center 不起作用。

我尝试了什么(但没有奏效) ・文本对齐:居中; ・边距:自动; ・边距: 中心: ・导航栏品牌中心

左上角的logo,用户点击logo图片,就是home.vue(route /)。

现在第二张照片是我使用的边距:-1.8% 0%。但我知道这不是一个优雅的解决方案。

如果你有任何想法,请给我建议。

    <b-navbar-brand :to="`/${$i18n.locale}/`">
         <img :src="image" class="logo"/>
     </b-navbar-brand>
.logo{
   width: 50px;
   position:absolute;
   height: 50px;
   margin:-1.8% 0%;  
}

我想从↓改成

到这里↓

下面的完整代码

<template>
  <div>
    <b-navbar toggleable="lg" type="light" id="header">

      <b-navbar-brand :to="`/${$i18n.locale}/`">
         <img :src="image" class="logo"/>
      </b-navbar-brand>

      <b-navbar-toggle target="nav-collapse"></b-navbar-toggle>

      <b-collapse id="nav-collapse" is-nav>
          <b-navbar-nav class="center" >
          <b-nav-item :to="`/${$i18n.locale}/`">{{ $t("header.home") }}</b-nav-item>
          <b-nav-item :to="`/${$i18n.locale}/about`">{{
            $t("header.about")
          }}</b-nav-item>
        </b-navbar-nav>

        <!-- Right aligned nav items -->
        <b-navbar-nav class="ml-auto">
            <b-nav-item v-if="!auth" :to="`/${$i18n.locale}/login`">{{
              $t("header.login")
            }}</b-nav-item>
            <b-nav-item v-if="auth" @click="logout()">{{
              $t("header.logout")
            }}</b-nav-item>
            <b-nav-item v-if="!auth" :to="`/${$i18n.locale}/signup`">{{
              $t("header.signup")
            }}</b-nav-item>
            <b-nav-item v-if="auth" :to="`/${$i18n.locale}/profile`">{{
              $t("header.profile")
            }}</b-nav-item>

          <b-nav-item-dropdown text="Lang" right>
            <b-dropdown-item @click="setLocale('en')">ENG</b-dropdown-item>
            <b-dropdown-item @click="setLocale('et')">EST</b-dropdown-item>
          </b-nav-item-dropdown>
        </b-navbar-nav>
      </b-collapse>
    </b-navbar>
  </div>
</template>

<script>
import image from "../assets/Logo.jpg"
export default {
  name: "Header",
  data() {
    return {
      image: image
    };
  },
  methods: {
    setLocale(locale) {
      this.$i18n.locale = locale;
      this.$router.push(
        {
          params: {
            lang: locale,
          },
        },
        () => {}
      );
      localStorage.setItem("lang", this.$i18n.locale);
    },
    logout() {
      this.$store.dispatch('logout')
    }
  },
  computed: {
  auth() {
    return this.$store.getters.isAuth
  }
  }
};
</script>

<style>
#header {
  background-color: cornflowerblue;
}
.logo{
   width: 50px;
   position:absolute;
   height: 50px;
   margin:-1.8% 0%;  
}

@media screen and (max-width: 891px) {
  .center {
    position: relative !important;
}

}

.center {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}
</style>

【问题讨论】:

  • 我可以看到b-navbar-brand上的css吗?
  • 不要使用绝对定位。快速解决方法是使用position: relative,然后设置bottom: 10px 或任何适当的数字。
  • 它工作,多么简单,谢谢!
  • @kazy 并没有真正响应,因为它是硬编码的。
  • 我认为处理这个问题的最好方法是使用display: flex 使导航菜单成为一个弹性容器,然后使用align-items: center 确保徽标与十字中心对齐-轴。 MDN - Cross Axis

标签: html css vue.js


【解决方案1】:

您可以使用弹性框。它应该工作。对齐项目中心是垂直对齐项目到中心。

    #header{ 
    display : flex;
align-items:center;
}

【讨论】:

    猜你喜欢
    • 2011-06-20
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    相关资源
    最近更新 更多