简体   繁体   English

如何在div左右两侧的中间对齐2个按钮

[英]How can I align 2 buttons on the middle of the left and right sides of a div

I have a frame containing a picture, I would like to add 2 buttons on the left and right side of it so users can click on those to view different pictures. 我有一个包含图片的框架,我想在它的左侧和右侧添加2个按钮,以便用户可以单击它们以查看不同的图片。 I can handle JavaScript but I can't figure out a way to align these 2 buttons in proper positions. 我可以处理JavaScript,但无法找到将这2个按钮对齐到正确位置的方法。 Thank you. 谢谢。

 .frame { background: #e0e0e0; width: 300px; height: 350px; border: 1px solid #b9b9b9; display: flex; justify-content: center; align-items: center; } .content { background: #FFF; width: 200px; height: 200px; } .btn { width: 15px; height: 20px; } .btnLeft { float: left; background: red; } .btnRight { float: right; background: blue; } 
 <div class="frame"> <div class="content"></div> </div> <div class="btn btnLeft"></div> <div class="btn btnRight"></div> 

在此处输入图片说明

You were on the right track but needed justify-content: space-between; justify-content: space-between;正确的轨道上,但需要justify-content: space-between; not justify-content: center; 没有justify-content: center; and you needed to put .btnLeft and .btnRight inside .frame . 你需要把.btnLeft.btnRight.frame

Here's what different values of justify-content do: justify-content不同值如下:

在此处输入图片说明

Image from CSS-Tricks 图片来自CSS-Tricks

 .frame { background: #e0e0e0; width: 300px; height: 350px; border: 1px solid #b9b9b9; display: flex; justify-content: space-between; /* not center */ align-items: center; } .content { background: #FFF; width: 200px; height: 200px; } .btn { width: 15px; height: 20px; } .btnLeft{ background: red; } .btnRight { background: blue; } 
 <div class="frame"> <div class="btn btnLeft"></div> <div class="content"></div> <div class="btn btnRight"></div> </div> 

You may want to put the buttons inside the frame so you can reference the left and right positions. 你可以把它放到按钮的框架内,所以你可以参考的leftright的位置。 Then make those buttons position:absolute then set left and right position for each buttons 然后,让这些按钮position:absolute然后设置leftright位置的每个按钮

Code: 码:

.frame {
  background: #e0e0e0;
  width: 300px;
  height: 350px;
  border: 1px solid #b9b9b9;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}
.btn{
  position: absolute;
  top: 50%;
  transform:translateY(-50%);
}
.btnLeft{
  left: 0;
}
.btnRight{
  right: 0;
}

Updated fiddle: https://jsfiddle.net/y0ty7t80/3/ 更新的小提琴: https : //jsfiddle.net/y0ty7t80/3/

First you could set the position of the frame to relative so it gets set as a root for following positionings. 首先,您可以将框架的位置设置为相对,以便将其设置为后续定位的根。 You could then set the positions of the two buttons both to "absolute" and put them inside of your frame so they get taken out of the text flow. 然后,您可以将两个按钮的位置都设置为“绝对”,并将它们放置在框架中,以使它们脱离文本流。 By setting both to a left/right property of 0 and a top property of 50% they get placed exactly in the middle of the frame. 通过同时将left / right属性设置为0和top属性设置为50%,它们将被精确放置在框架的中间。 Heres an example of what i mean: 这是我的意思的一个例子:

 .frame { position:relative; background: #e0e0e0; width: 300px; height: 350px; border: 1px solid #b9b9b9; display: flex; justify-content: center; align-items: center; } .content { background: #FFF; width: 200px; height: 200px; } .btn { width: 15px; height: 20px; } .btnLeft{ position:absolute; top:50%; left:0; background: red; } .btnRight { position:absolute; top:50%; right:0; background: blue; } 
 <div class="frame"> <div class="btn btnLeft"></div> <div class="btn btnRight"></div> <div class="content"></div> </div> 

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

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