【问题标题】:Center navigation bar (Bootstrap) items horizontally水平居中导航栏(Bootstrap)项目
【发布时间】:2021-02-05 09:58:10
【问题描述】:

我有一个导航栏,上面有 2 个均匀分布的图标。

我想水平缩放它们,使它们相对于导航栏水平居中。

我认为 svg 的视图框会导致它看起来不在视觉中心。

我试过玩这个尺寸,但运气不佳。

我确实尝试过证明无效的内容。 然后我尝试为图标添加填充以允许它们移动。

代码:

.bar {
    display: flex;
    justify-content: center;
}

/* Style the navigation menu */
.navbar {
  width: 90%;
  background: #3CE18F;
  /* Forces the icons to not go outside the scrollbar. If we left it to invisble it would display 
  them outside. */
  overflow: auto;
  /* Curves the corners of the Nav Bar. */
  border-radius: 10px 10px 30px 30px;
  position: fixed;
  bottom: 3%;
  display: block;
  margin:auto;
}

/* Navigation links */
.navbar a {
  float: left;
  padding: 12px;
  color: white;
  text-decoration: none;
  font-size: 17px;
  width: 48.6%; /* Four equal-width links. If you have two links, use 50%, and 33.33% for three links, etc.. */
  text-align: center; /* If you want the text to be centered */
}

/* Add a background color on mouse-over */
.navbar a:hover {
  background-color: #000;
}

/* Style the current/active link */
.navbar a.active {
  background-color: #4CAF50;
}

.microphoneIcon {
/*  position: absolute;
    left: 65.36%;
    right: 26.29%;
    top: 54.22%;
    bottom: 0.46%;*/
    fill: #FFFFFF;
    stroke: #FFFFFF;
    -webkit-filter: drop-shadow( 3px 3px 2px rgba(0, 0, 0, .7));
    filter: drop-shadow( 3px 3px 2px rgba(0, 0, 0, .7));
  /* Similar syntax to box-shadow */
    /*background: #FFFFFF;
    box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);*/
}

.cameraIcon {
    fill: #FFFFFF;
    stroke: #FFFFFF;
    -webkit-filter: drop-shadow( 3px 3px 2px rgba(0, 0, 0, .7));
    filter: drop-shadow( 3px 3px 2px rgba(0, 0, 0, .7));
}
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
        <meta name="theme-color" content="#009578">
        <title>PWA</title>
        <link rel="stylesheet" href="src/navigationBar.css">
        <!-- Javascript Docs -->
        <script type="text/javascript" src="src/index.js"></script>
        <link rel="manifest" href="/manifest.json">
        <link rel="apple-touch-icon" href="images/logo192.png">
        
    </head>
    
    <body>
        <!-- Class for the Bottom Navigation Bar -->
        <div class="bar">
            <nav class="navbar fixed-bottom navbar-light bg-light nav-fill">
                <a href="#">
                    <svg class='microphoneIcon' id='microphoneIcon' width="42" height="46" viewBox="0 0 42 46" stroke="grey">
                        <path d='M21 23.7571C25.0314 23.7571 28.2857 21.1043 28.2857 17.8179V5.93928C28.2857 2.65288 25.0314 0 21 0C16.9686 0 13.7143 2.65288 13.7143 5.93928V17.8179C13.7143 21.1043 16.9686 23.7571 21 23.7571ZM18.5714 5.93929C18.5714 4.85043 19.6643 3.95953 21 3.95953C22.3357 3.95953 23.4286 4.85043 23.4286 5.93929V17.8179C23.4286 18.9067 22.3357 19.7976 21 19.7976C19.6643 19.7976 18.5714 18.9067 18.5714 17.8179V5.93929ZM21 27.7167C27.7029 27.7167 33.1429 23.282 33.1429 17.8179H38C38 24.8064 31.6614 30.5477 23.4286 31.5178V37.6155H18.5714V31.5178C10.3386 30.5477 4 24.8064 4 17.8179H8.85714C8.85714 23.282 14.2971 27.7167 21 27.7167Z'/>
                    </svg>
                </a>
                
                <a href="#">
                    <svg class ='cameraIcon' id='cameraIcon' xmlns="http://www.w3.org/2000/svg" width="50" height="46" viewBox="0 0 50 46">
                      <path d="M 45 36 a 3 3 90 0 1 -3 3 H 6 a 3 3 90 0 1 -3 -3 V 18 a 3 3 90 0 1 3 -3 h 3.516 a 9 9 90 0 0 6.36 -2.637 l 2.49 -2.484 A 3 3 90 0 1 20.481 9 h 7.032 a 3 3 90 0 1 2.121 0.879 l 2.484 2.484 A 9 9 90 0 0 38.484 15 H 42 a 3 3 90 0 1 3 3 v 18 z M 6 12 a 6 6 90 0 0 -6 6 v 18 a 6 6 90 0 0 6 6 h 36 a 6 6 90 0 0 6 -6 V 18 a 6 6 90 0 0 -6 -6 h -3.516 a 6 6 90 0 1 -4.242 -1.758 l -2.484 -2.484 A 6 6 90 0 0 27.516 6 H 20.484 a 6 6 90 0 0 -4.242 1.758 l -2.484 2.484 A 6 6 90 0 1 9.516 12 H 6 z M 24 33 a 7.5 7.5 90 1 1 0 -15 a 7.5 7.5 90 0 1 0 15 z m 0 3 a 10.5 10.5 90 1 0 0 -21 a 10.5 10.5 90 0 0 0 21 z M 9 19.5 a 1.5 1.5 90 1 1 -3 0 a 1.5 1.5 90 0 1 3 0 z"/>
                    </svg>
                </a>
            </nav>
        </div>
    </body>
</html>

【问题讨论】:

    标签: javascript html css bootstrap-4 uinavigationbar


    【解决方案1】:

    这是一种水平缩放它们的简单方法。导航栏的显示必须是 inline-flex。我已经添加了。

    正如您要求的那样垂直缩放它们。我更改了标签宽度的 CSS。

    请看一下 CSS。 @氮气

    .bar {
        display: flex;
        justify-content: center;
    }
    
    /* Style the navigation menu */
    .navbar {
      width: 90%;
      background: #3CE18F;
      /* Forces the icons to not go outside the scrollbar. If we left it to invisble it would display 
      them outside. */
      overflow: auto;
      /* Curves the corners of the Nav Bar. */
      border-radius: 10px 10px 30px 30px;
      position: fixed;
      bottom: 3%;
      display: block;
      margin:auto;
    }
    
    /* Navigation links */
    .navbar a {
      width: 100% !important;
      float: left;
      padding: 12px;
      color: white;
      text-decoration: none;
      font-size: 17px;
      width: 48.6%; /* Four equal-width links. If you have two links, use 50%, and 33.33% for three links, etc.. */
      text-align: center; /* If you want the text to be centered */
    }
    
    /* Add a background color on mouse-over */
    .navbar a:hover {
      background-color: #000;
    }
    
    /* Style the current/active link */
    .navbar a.active {
      background-color: #4CAF50;
    }
    
    .microphoneIcon {
    /*  position: absolute;
        left: 65.36%;
        right: 26.29%;
        top: 54.22%;
        bottom: 0.46%;*/
        fill: #FFFFFF;
        stroke: #FFFFFF;
        -webkit-filter: drop-shadow( 3px 3px 2px rgba(0, 0, 0, .7));
        filter: drop-shadow( 3px 3px 2px rgba(0, 0, 0, .7));
      /* Similar syntax to box-shadow */
        /*background: #FFFFFF;
        box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);*/
    }
    
    .cameraIcon {
        fill: #FFFFFF;
        stroke: #FFFFFF;
        -webkit-filter: drop-shadow( 3px 3px 2px rgba(0, 0, 0, .7));
        filter: drop-shadow( 3px 3px 2px rgba(0, 0, 0, .7));
    }
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
            <meta name="theme-color" content="#009578">
            <title>PWA</title>
            <link rel="stylesheet" href="src/navigationBar.css">
            <!-- Javascript Docs -->
            <script type="text/javascript" src="src/index.js"></script>
            <link rel="manifest" href="/manifest.json">
            <link rel="apple-touch-icon" href="images/logo192.png">
            
        </head>
        
        <body>
            <!-- Class for the Bottom Navigation Bar -->
            <div class="bar">
                <nav class="navbar fixed-bottom navbar-light bg-light nav-fill">
                    <a href="#">
                        <svg class='microphoneIcon' id='microphoneIcon' width="42" height="46" viewBox="0 0 42 46" stroke="grey">
                            <path d='M21 23.7571C25.0314 23.7571 28.2857 21.1043 28.2857 17.8179V5.93928C28.2857 2.65288 25.0314 0 21 0C16.9686 0 13.7143 2.65288 13.7143 5.93928V17.8179C13.7143 21.1043 16.9686 23.7571 21 23.7571ZM18.5714 5.93929C18.5714 4.85043 19.6643 3.95953 21 3.95953C22.3357 3.95953 23.4286 4.85043 23.4286 5.93929V17.8179C23.4286 18.9067 22.3357 19.7976 21 19.7976C19.6643 19.7976 18.5714 18.9067 18.5714 17.8179V5.93929ZM21 27.7167C27.7029 27.7167 33.1429 23.282 33.1429 17.8179H38C38 24.8064 31.6614 30.5477 23.4286 31.5178V37.6155H18.5714V31.5178C10.3386 30.5477 4 24.8064 4 17.8179H8.85714C8.85714 23.282 14.2971 27.7167 21 27.7167Z'/>
                        </svg>
                    </a>
                    
                    <a href="#">
                        <svg class ='cameraIcon' id='cameraIcon' xmlns="http://www.w3.org/2000/svg" width="50" height="46" viewBox="0 0 50 46">
                          <path d="M 45 36 a 3 3 90 0 1 -3 3 H 6 a 3 3 90 0 1 -3 -3 V 18 a 3 3 90 0 1 3 -3 h 3.516 a 9 9 90 0 0 6.36 -2.637 l 2.49 -2.484 A 3 3 90 0 1 20.481 9 h 7.032 a 3 3 90 0 1 2.121 0.879 l 2.484 2.484 A 9 9 90 0 0 38.484 15 H 42 a 3 3 90 0 1 3 3 v 18 z M 6 12 a 6 6 90 0 0 -6 6 v 18 a 6 6 90 0 0 6 6 h 36 a 6 6 90 0 0 6 -6 V 18 a 6 6 90 0 0 -6 -6 h -3.516 a 6 6 90 0 1 -4.242 -1.758 l -2.484 -2.484 A 6 6 90 0 0 27.516 6 H 20.484 a 6 6 90 0 0 -4.242 1.758 l -2.484 2.484 A 6 6 90 0 1 9.516 12 H 6 z M 24 33 a 7.5 7.5 90 1 1 0 -15 a 7.5 7.5 90 0 1 0 15 z m 0 3 a 10.5 10.5 90 1 0 0 -21 a 10.5 10.5 90 0 0 0 21 z M 9 19.5 a 1.5 1.5 90 1 1 -3 0 a 1.5 1.5 90 0 1 3 0 z"/>
                        </svg>
                    </a>
                </nav>
            </div>
        </body>
    </html>

    【讨论】:

    • 我刚试过这个,它不起作用。完全没有变化。
    • 查看它的工作原理。不要忘记为 inline-flex 添加一个重要的。
    • 我运行了 sn-p,图标没有水平居中。
    • 水平居中是什么意思?你能解释一下吗
    • @BathriNathan 你是说垂直的吗?因为它是水平居中而不是垂直居中。
    【解决方案2】:

    将 display:flex 放入导航栏 div

    .navbar {
      display:flex;
    }
    

    单独一行的按钮

    .navbar {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
    }
    

    【讨论】:

    • 不,这也不起作用。完全没有效果。
    猜你喜欢
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多