【问题标题】:How to vertically center align 3 divs at extreme left, extreme right and absolute centre in a wrapper div with dynamic dimensions?如何在具有动态尺寸的包装器 div 中将 3 个 div 垂直居中对齐在极左、极右和绝对中心?
【发布时间】:2020-09-24 13:30:17
【问题描述】:

我是 Svelte 的新手,我非常喜欢它,在为我的项目设计顶部导航栏时,由于 Svelte 的 JS DOM 样式方法,我无法达到预期的结果。如果你能指导我,那就太好了。

标记如下

 <div class="top-nav">
        <div class="width-restriction">
            <div id="hamburger-icon">
                <HamburgerIcon /> <!-- Imported component, essentially an icon, doesn't affect styling; needs to be at the extreme left -->
            </div>
            <h1 class="head" id="logo">VKYD</h1> <!-- Logo; needs to be centered -->
            <div id="shopping-cart-icon">
                <ShoppingCartIcon /> <!-- Imported component, essentially an icon, doesn't affect styling; needs to be at the extreme right -->
            </div>
        </div>
    </div>

【问题讨论】:

  • 到目前为止,您对 css 有什么尝试?你应该发布那个。

标签: javascript html css svelte


【解决方案1】:

您可以在.width-restriction 上使用flexbox 轻松实现此目的:

.width-restriction {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
<div class="top-nav">
  <div class="width-restriction">
    <div id="hamburger-icon">
      icon-hamburger
      <!-- Imported component, essentially an icon, doesn't affect styling; needs to be at the extreme left -->
    </div>
    <h1 class="head" id="logo">VKYD</h1>
    <!-- Logo; needs to be centered -->
    <div id="shopping-cart-icon">
      icon-shop
      <!-- Imported component, essentially an icon, doesn't affect styling; needs to be at the extreme right -->
    </div>
  </div>
</div>

在 Svelte 中,您需要像这样在 style 标签中定义样式:

<style>
    .width-restriction {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
</style>

 <div class="top-nav">
     <div class="width-restriction">
         <div id="hamburger-icon">
             icon-hamburger <!-- Imported component, essentially an icon, doesn't affect styling; needs to be at the extreme left -->
         </div>
         <h1 class="head" id="logo">VKYD</h1> <!-- Logo; needs to be centered -->
         <div id="shopping-cart-icon">
             icon-shop <!-- Imported component, essentially an icon, doesn't affect styling; needs to be at the extreme right -->
         </div>
     </div>
</div>

检查REPL

【讨论】:

  • 没问题啊 ;) 请不要忘记验证答案。
【解决方案2】:

我不熟悉 Svelte,但通常情况下,使用这种类型的放置很容易实现

display: flex;
justify-content: space-between; 

您可以在此处查看该属性的快速演示(见页面底部):https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2013-09-13
    • 1970-01-01
    • 2012-03-26
    • 2023-03-16
    • 2014-10-19
    • 1970-01-01
    • 2011-02-05
    • 2013-09-10
    相关资源
    最近更新 更多