【问题标题】:Using flex-box to align layout使用 flex-box 对齐布局
【发布时间】:2021-07-13 16:31:48
【问题描述】:

我正在开发一个项目,使用我想要实现如图中的布局的地方。这是我到目前为止所做的。我能够将侧边栏对齐到左侧,将标题对齐到顶部(在侧边栏后面)。但是,我无法将 contentContainer 与侧边栏对齐并位于顶部下方。我正在考虑将容器和侧边栏按 width: 20% 和 width: 80% 对齐,但侧边栏必须具有固定的宽度大小,因为我不希望在调整应用程序大小时改变宽度。

我在 contentContainer 中也有问题。我已经把它做成了一个列方向的弹性盒子。尝试将 headContent 放在 flex-start 上,将 mainContent 放在 center 上,将 footerContent 放在 flex-end 上。然而它只是行不通。供您参考,如果它很重要,它是一个 reactjs 应用程序。这是我尝试过的代码。


       <div>
          <div className="Topbar">
            <svg viewBox="10 0 50 80">
              <text y="50">Header Text</text>
            </svg>
          </div>
    
          <div className="Sidebar">
            <svg
              id="menuitem1"
              width="70"
              height="100"
              viewBox="0 0 70 100"
              fill="#767765"
              xmlns="http://www.w3.org/2000/svg"
            >
              <path d="M22.4478 65.0703L21.7393 69.9057L26.6989 69.2151L47.1497 49.2776L42.8982 45.1328L22.4478 65.0703Z" />
              <path d="M52.0738 44.4765C53.2459 43.3339 53.2459 41.4745 52.0736 40.3317C51.506 39.7783 50.7509 39.4735 49.9482 39.4735C49.1455 39.4735 48.3904 39.7785 47.8226 40.3319L47.1487 40.9889L51.4 45.1335L52.0738 44.4765Z" />
            </svg>
            <svg
              id="menuitem2"
              width="75"
              height="78"
              viewBox="0 0 75 78"
              fill="#767765"
              xmlns="http://www.w3.org/2000/svg"
            >
              <path d="M22.4478 65.0703L21.7393 69.9057L26.6989 69.2151L47.1497 49.2776L42.8982 45.1328L22.4478 65.0703Z" />
              <path d="M52.0738 44.4765C53.2459 43.3339 53.2459 41.4745 52.0736 40.3317C51.506 39.7783 50.7509 39.4735 49.9482 39.4735C49.1455 39.4735 48.3904 39.7785 47.8226 40.3319L47.1487 40.9889L51.4 45.1335L52.0738 44.4765Z" />
          </div>
    
          <div className="contentContainer">
            <div className="headContent"/>
            <div className="mainContent">
              <input
                type="text"
                placeholder="Enter text here.."
                className="textBox"
              />
            </div>
            <div className="footerContent" />
          </div>
        </div>

还有 CSS 文件:

    .contentContainer {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      background: red;
      width: 100%;
      height: 100%;
      position: fixed;
      top: 45px;
      bottom: 50;
    }
    
    .Topbar {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    width: 100%;
    top: 0%;
    height: 45px;
    position: absolute;
    z-index: 2;
    
    -webkit-app-region: drag;
    background:transparent;
    }
    
    svg{
      font   : bold 26px Century Gothic, Arial;
      width  : 100%;
      height : 100px;
      z-index: 3;
      filter: drop-shadow(3px 3px 3px rgba(161, 160, 160, 0.25));
      }
    
    text{
      /* fill:  #de8356b8; */
      fill: #f0f0ec;
      stroke: rgb(40, 40, 40);
      stroke-width: 0.5px;
      stroke-linejoin: bevel;
    }
    
    
    .Sidebar {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content:flex-start;
    -webkit-app-region: drag;
    
    width: 80px;
    height: 100%;
    position:fixed;
    top: 0%;
    left: 0%;
    z-index: 2;
    
    background: #faf9f9;
    /* background: transparent; */
    border-right: 0.2px solid #abababba;
    /* filter: drop-shadow(0.5px 0px 1px rgba(31, 31, 31, 0.25)); */
    }
    
    #menuitem1 {
      width: 30px;
      height: 50px;
      padding-top: 50pt;
    }
    
    #menuitem2 {
      width: 30px;
      height: 50px;
      padding-top: 30pt;
    }
    
    .textBox {
      width: 180px;
      height: 180px;
      filter: drop-shadow(3px 3px 3px rgba(161, 160, 160, 0.25));
    }
    
    
    .Content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center ;
    
    position: fixed;
    width: 80%;
    height: 100%;
    bottom: 30;
    right: 0%;
    
    background: #F8F8F8;
    }
    
    .headContent {
      width: 100%;
      height: 20%;
      align-self: flex-start;
      top: 20;
      background-color: black;
    }
    
    .mainContent {
      display: flex;
      flex-direction: row;
      position: fixed;
      align-self: center;
      justify-content: center;
      align-items: center;
      width: 100%;
      height: 70%;
      background-color: red;
    }
    
    .footerContent {
      position: fixed;
      align-self: flex-end;
      align-items: flex-end;
      width: 100%;
      height: 50px;
      bottom: 0;
      background-color: blue;
    }

请注意,我希望侧边栏的顶部栏具有固定宽度和固定高度。关于contentContainer,我希望它可以拉伸和调整大小。此外,contentContainer 中的每个孩子都应该在行轴上拥有自己的 flexbox。你可以看到我已经在上面提供的代码中尝试过了。我可能很接近,但在这里和那里遗漏了几点。感谢您的帮助。

Layout wanted

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    如我所见,您几乎已经完成了您想要的。除了未关闭的&lt;svg&gt; 标签外,一切都很好。唯一的问题是课程。您使用了className 属性而不是class 属性。这是主要问题。如果不是,请看下图并告诉我您还需要什么:

    The "picture below"

    正如您在 cmets 中看到的那样,我们进行了一次对话,并且我已经修复了它。所以,现在您首先需要将className 更改为class,然后您需要将您的CSS 文件更改为:

        body{
            overflow-x: hidden;
        }
        .contentContainer {
          display: flex;
          flex-direction: column;
          justify-content: flex-start; /*start from the top and end in the bottom, for the headContainer*/
          align-items: center;
          background: red;
          width: calc(100% - 80px); /*Changed from 100%*/
          height: 100%;
          position: fixed;
          top: 45px;
          bottom: 50;
          left: 80px; /*Added to move to the right because of the sidebar*/
        }
        
        .Topbar {
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: center;
        width: 100%;
        top: 0%;
        height: 45px;
        position: absolute;
        z-index: 2;
        -webkit-app-region: drag;
        background:transparent;
        }
        
        svg{
          font   : bold 26px Century Gothic, Arial;
          width  : 100%;
          height : 100px;
          z-index: 3;
          filter: drop-shadow(3px 3px 3px rgba(161, 160, 160, 0.25));
          }
        
        text{
          fill: #f0f0ec;
          stroke: rgb(40, 40, 40);
          stroke-width: 0.5px;
          stroke-linejoin: bevel;
        }
        
        
        .Sidebar {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content:flex-start;
        -webkit-app-region: drag;
        
        width: 80px;
        height: 100%;
        position:fixed;
        top: 0%;
        left: 0%;
        z-index: 2;
        
        background: #faf9f9;
        border-right: 0.2px solid #abababba;
        }
        
        #menuitem1 {
          width: 30px;
          height: 50px;
          padding-top: 50pt;
        }
        
        #menuitem2 {
          width: 30px;
          height: 50px;
          padding-top: 30pt;
        }
        
        .textBox {
          width: 180px;
          height: 180px;
          filter: drop-shadow(3px 3px 3px rgba(161, 160, 160, 0.25));
        }
        
        
        .Content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center ;
        
        position: fixed;
        width: 80%;
        height: 100%;
        bottom: 30;
        right: 0%;
        
        background: #F8F8F8;
        }
        
        .headContent {
          width: 100%;
          height: 20%;
          align-self: flex-start;
          top: 20px;
          background-color: black;
          color:white;
        }
        
        .mainContent {
          display: flex;
          flex-direction: row;
          align-self: center;
          justify-content: center;
          align-items: center;
          width: 100%;
          height: 70%;
          background-color: red;
        }
        
        .footerContent {
          position: fixed;
          align-self: flex-end;
          align-items: flex-end;
          width: 100%;
          height: 50px;
          bottom: 0;
          left: 80px;
          background-color: blue;
          color:white;
        }
    

    (我隐藏了正文的overflow-x,因为您的内容溢出了它。如果您有问题,只需将其删除)。

    您的主要问题是类属性,但样式表中的另一个更改是将 contentContainer 向右移动了侧边栏的宽度(+padding、+margin、+border,但您没有它们中的任何一个),这在我们的例子中,宽度是 80 像素。然后,它的宽度需要为 100% 减去 80 像素,并证明内容是 flex-start 的,这样它们就可以从顶部开始然后移动到底部。不过,你做得很好。

    【讨论】:

    • 但我没有,因为 headContent 没有显示在 topBar 下方。事实上,当我将它的背景颜色设置为黑色时,我根本看不到它。我还希望 mainContent 中的内容,实际上是整个 contentContainer 的中心,位于 contentContainer 的中心,而不是整个应用程序本身的中心。
    • 是的,它更接近,但我希望 mainContent 中的项目位于 mainContent 的中心,而不是应用程序本身的中心。如果你们看我上传的图片,那么你应该明白我想要什么
    • 我对其进行了一些改进,但是,您仍然希望您的输入对齐在中心还是底部?
    • 输入应该在 mainContent 内,在 mainContent 框的中心。现在它以整个应用程序的宽度为中心,但我希望它仅以 mainContent 的宽度为中心..
    • 是的,你解决了这个问题,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 2017-12-30
    相关资源
    最近更新 更多