简体   繁体   English

如何对齐面板右下角的按钮

[英]How to align buttons at the right bottom of panel

I have this CSS code: 我有这个CSS代码:

  .mainBody {
    border: 1px solid rgba(0, 0, 0, 0.87);
    width: 895px;
    position: relative;
    display: inline-block;
    padding-bottom: 8rem;
    top: 40px;
    height: 310px;
   }

 .label {
    display: inline-block;
    margin-left: 1%;
    position: relative;
    bottom: 10px;
    float: left;
    z-index: 2;
    background: white;
   }

  .dropdown {
    padding-left: 1%;
    width: 100%;
    margin: 1rem 0;
    display: flex;
    align-items: center;
   }

   .paragraph {
     position: relative;
     display: inline-block;
     width: 800px;
     padding-bottom: 30px;
     padding-left: 10px;
    }

    .expandedLink {
      display: flex;
      flex-direction: column;
      padding: 1rem 10rem;
    }

    .previousButton {
      position: relative;
      top: 77px;
      float: right;
    }

    .nextButton {
      position: relative;
      top: 77px;
      float: right;
    }

HTML:

   <div className="mainBody">
    <div className="label"></div>
    <div className="dropdown"></div>
    <div className="paragraph"></div>
    <div className="expandedLink"></div>
    <div className="nextButton"></div>
    <div className="previousButton"></div>
   <div>

I want to align the nextButton and previousButton at the bottom right of the mainBody. 我想在mainBody的右下角对齐nextButton和previousButton。 The problem is that I cannot use margin-top because I am using an expanded component and the content inside mainBody does not have a stable size. 问题是我无法使用margin-top,因为我使用的是扩展组件,并且mainBody内的内容大小没有稳定。 Any ideas? 有任何想法吗?

Add this lines to .mainBody 将此行添加到.mainBody

display: flex;
align-items: flex-end;
justify-content: flex-end;

Well you need to postion the .previousButton and .nextButton as absolute.Since the .mainBody is relative, an element with position: absolute; 好吧,您需要将.previousButton.nextButton放置为绝对位置。由于.mainBody是相对的,因此元素的位置为:absolute; is positioned relative to the nearest positioned ancestor. 相对于最近定位的祖先定位。

    .previousButton {
  position: relative;
  right: 0px;
  bottom:0px;
  float: right;
}

.nextButton {
  position: relative;
  right: 0px;
  bottom:0px;
  float: right;
}

Just set the buttonclasses to absolute, and add bottom: 0; right: 0; 只需将buttonclasss设置为absolute,然后添加bottom: 0; right: 0; bottom: 0; right: 0; . And maybe right for the other button a little bit higher. 也许恰恰是另一个按钮高一点。

Maybe you can do something by using flex with the following HTML: 也许您可以通过将flex与以下HTML结合使用来做一些事情:

<div className="mainBody">
    <div className="mainBodyContent">
        Main Body Content
    </div>
    <div className="buttonsContainer">
        <div className="nextButton">Next</div>
        <div className="previousButton">Prev</div>
    </div>
</div>

With this modified CSS: 通过修改后的CSS:

.mainBody {
  border: 1px solid rgba(0, 0, 0, 0.87);
  width: 895px;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  top: 40px;
  min-height: 310px;
}

.mainBodyContent {
  padding-bottom: 8rem; 
}

.buttonsContainer {
  align-self: flex-end;
  display: flex;
  flex-direction: row;
}

.previousButton {
  padding: 10px;
}

.nextButton {
  padding: 10px;

}

I have the example in the following JSFiddle for you to test: https://jsfiddle.net/9ey35uwt/ 我在以下JSFiddle中有示例供您测试: https ://jsfiddle.net/9ey35uwt/


Notes 笔记

  1. I modified the height property you had in .mainBody to min-height , because as I understood your question, the body's height is variable, and otherwise, the component wouldn't grow with the content, but if that's not the intention, you can change it back easily. 我将.mainBodyheight属性修改为min-height ,因为据我.mainBody ,主体的高度是可变的,否则该组件不会随内容的增长而增加,但是如果不是这样,则可以轻松将其改回。

  2. With the same intention, I had the padding moved to the content of your component. 出于同样的意图,我将padding移到了组件的内容上。

  3. I added some padding to the buttons to avoid them being right on the line and touching each other, but you may want to change that. 我在按钮上添加了一些填充,以避免它们在直线上彼此接触,但是您可能需要更改它。
  4. I grouped the buttons together in a single .buttonsContainer div to do the aligning of the buttons in a single place, instead of duplicating it in each of the buttons. 我将按钮分组到一个.buttonsContainer div中,以将按钮对齐在一个位置,而不是在每个按钮中都进行复制。
  5. I removed the top and position from the buttons, as they are unnecessary in the example shown using flex, but you should adjust the exact positioning of the buttons as you like by using padding and/or margin . 我从按钮上删除了按钮的topposition ,因为在使用flex的示例中它们是不必要的,但是您应该根据需要使用padding和/或margin来调整按钮的确切位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM