CSS按钮对齐问题

huangapple go评论68阅读模式
英文:

CSS Buttons align issue

问题

.btn-prev, .btn-next {
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.7);
    display: inline-block;
    border: none;
    line-height: 40px;
    margin: 0 auto;
    top: 30%;
    z-index: 2;
    font-size: 30px;
    font-weight: bold;
    border-radius: 50%;
    font-family: monospace;
    cursor: pointer;
}

.btn-prev:hover, .btn-next:hover {
    background: rgb(238, 236, 236);
}

.btn-prev {
    left: 10%;
    margin: 0 auto;
    text-align: center;
    border: none;
}

.btn-next {
    right: 10%;
    margin: 0 auto;
    text-align: center;
    border: none;
}
<section>
    <div id="btn-prev" class="btn-prev">&#60;</div>
    <div id="btn-next" class="btn-next">&#62;</div>
    <div class="center_part">
        <div class="zoom">
        </div><br><br>
        <button class="button btnleer">Leer</button>
    </div>
</section>

(Note: I have removed the unnecessary auto values from the left and right properties in the CSS to center the buttons, as per your request.)

英文:

I was making this web page (I am a beginner in CSS), and I was wondering if anyone could help me to put the buttons aside of the blue box thingy

.btn-prev, .btn-next {
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.7);
    display: inline-block;
    border: none;
    line-height: 40px;
    margin: 0 auto;
    top: 30%;
    z-index: 2;
    font-size: 30px;
    font-weight: bold;
    border-radius: 50%;
    font-family: monospace;
    cursor: pointer;
  }
  
  .btn-prev:hover, .btn-next:hover {
    background: rgb(238, 236, 236);
  }
  
  .btn-prev {
    left: 10% auto;
    right: 80%;
    margin: 0 auto;
    text-align: center;
    border: none;
    
  }
  
  .btn-next {
    right: 10% auto;
    left: 80%;
    margin: 0 auto;
    text-align: center;
    border: none;
  }

as y'all can see it's a bit of a mess because I was messing around and could use some help.

here is what it looks like
result
and I want the buttons aside from the blue box and centered in the middle, not flexed because it would f- up the position as I want it to move aside from the window, help 😥
needed result
here is how I would (theoretically) want it to look like, without further saying, thank you 😁

EDIT:
I will add the HTML for y'all to see

&lt;section&gt;
    &lt;div id=&quot;btn-prev&quot; class=&quot;btn-prev&quot;&gt;&amp;#60;&lt;/div&gt;
    &lt;div id=&quot;btn-next&quot; class=&quot;btn-next&quot;&gt;&amp;#62;&lt;/div&gt;
    &lt;div class=&quot;center_part&quot;&gt;
      &lt;div class=&quot;zoom&quot;&gt;
   &lt;/div&gt;&lt;br&gt;&lt;br&gt;
    &lt;button class=&quot;button btnleer&quot;&gt;Leer&lt;/button&gt;
  &lt;/div&gt;
  &lt;/section&gt;

I didn't put all the html, just the section where the buttons are 🫠

答案1

得分: 1

.center_part {
position: relative;
text-align: center;
}

.btn-prev,
.btn-next {
width: 40px;
height: 40px;
background: rgba(255, 255, 255, 0.7);
display: inline-block;
border: none;
line-height: 40px;
z-index: 2;
font-size: 30px;
font-weight: bold;
border-radius: 50%;
font-family: monospace;
cursor: pointer;
position: absolute;
top: 50%;
transform: translateY(-50%);
}

.btn-prev {
left: 10px;
}

.btn-next {
right: 10px;
}

.btn-prev:hover,
.btn-next:hover {
background: rgb(238, 236, 236);
}

英文:
.center_part {
  position: relative;
  text-align: center;
}

.btn-prev,
.btn-next {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.7);
  display: inline-block;
  border: none;
  line-height: 40px;
  z-index: 2;
  font-size: 30px;
  font-weight: bold;
  border-radius: 50%;
  font-family: monospace;
  cursor: pointer;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

.btn-prev {
  left: 10px;
}

.btn-next {
  right: 10px;
}

.btn-prev:hover,
.btn-next:hover {
  background: rgb(238, 236, 236);
}

I'm a noob in HTML, but I guess this may help

答案2

得分: 0

以下是代码的中文翻译:

将主要的 div(带有蓝色背景色的 div)设为相对位置,同时将按钮都设为绝对位置,可以根据需要相应地更改左右位置来设置按钮。

.btn-prev, .btn-next {
    position: absolute;
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.7);
    display: inline-block;
    border: none;
    line-height: 40px;
    margin: 0 auto;
    top: 30%;
    z-index: 2;
    font-size: 30px;
    font-weight: bold;
    border-radius: 50%;
    font-family: monospace;
    cursor: pointer;
}

.btn-prev:hover, .btn-next:hover {
    background: rgb(238, 236, 236);
}

.btn-prev {
    left: 10% auto;
    right: 100%;
    margin: 0 auto;
    text-align: center;
    border: none;
}

.btn-next {
    right: 10% auto;
    left: 100%;
    margin: 0 auto;
    text-align: center;
    border: none;
}
英文:

Giving the main div(the div with blue blackground-color) position relative and both the button position absolute can set the button by changing the left right position accordingly as you want.

.btn-prev, .btn-next {
        position:absolute;
        width: 40px;
        height: 40px;
        background: rgba(255, 255, 255, 0.7);
        display: inline-block;
        border: none;
        line-height: 40px;
        margin: 0 auto;
        top: 30%;
        z-index: 2;
        font-size: 30px;
        font-weight: bold;
        border-radius: 50%;
        font-family: monospace;
        cursor: pointer;
      }
      
      .btn-prev:hover, .btn-next:hover {
        background: rgb(238, 236, 236);
      }
      
      .btn-prev {
        left: 10% auto;
        right: 100%;
        margin: 0 auto;
        text-align: center;
        border: none;
        
      }
      
      .btn-next {
        right: 10% auto;
        left: 100%;
        margin: 0 auto;
        text-align: center;
        border: none;
      }

答案3

得分: 0

只需使用flex来将您的内容排成一行。您需要在HTML中移动一个按钮。但您将获得您想要的效果。比使用绝对定位要好得多,后者需要更多的包装元素来包含。

section {
  display: flex;
  flex-direction: row;
}
英文:

Just use flex to align your content into a row. You'll have to move a button in the HTML around. But you'll get the effect you want. Much better than using position absolute which would need more wrapping elements to contain.

section {
  display: flex;
  flex-direction: row;
}
&lt;section&gt;
  &lt;div id=&quot;btn-prev&quot; class=&quot;btn-prev&quot;&gt;&amp;#60;&lt;/div&gt;
  &lt;div class=&quot;center_part&quot;&gt;
    &lt;div class=&quot;zoom&quot;&gt;&lt;/div&gt;
    &lt;br&gt;&lt;br&gt;
    &lt;button class=&quot;button btnleer&quot;&gt;Leer&lt;/button&gt;
  &lt;/div&gt;
  &lt;div id=&quot;btn-next&quot; class=&quot;btn-next&quot;&gt;&amp;#62;&lt;/div&gt;
&lt;/section&gt;

答案4

得分: 0

据我所知,如果您想让HTML元素水平排列,可以使用flex容器和flex项目。尝试在Google上搜索FlexBox。

英文:

As I know if you want to let html elements order by horizontal you can use flex container with flex item. Try to search FlexBox on Google.

huangapple
  • 本文由 发表于 2023年6月13日 12:43:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定