居中一些按钮

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

Centering some buttons

问题

<section class="mainContent">
      <button id="result">结果</button>
      <button id="clear">C</button>
      <button id="one">1</button>
      <button id="two">2</button>
      <button id="three">3</button>
      <button id="four">4</button>
      <button id="five">5</button>
      <button id="six">6</button>
      <button id="seven">7</button>
      <button id="eight">8</button>
      <button id="nine">9</button>
      <button id="zero">0</button>
      <button id="plus">+</button>
      <button id="minus">-</button>
      <button id="multiply">*</button>
      <button id="divide">/</button>
      <button id="equal">=</button>
      <button id="comma">,</button>
      <input type="text" placeholder="您的计算">
</section>
英文:

I am a beginner-ish front end Developer, and I have done something with my css stylesheet that whatever I do I cant seem to get my buttons on the page to be aligned as if it was a calculator? I know many of you would know this so, thank you for your help forward.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.mainContent button {
  font-family: inherit;
  padding: 1em 2em;
  border: 3px solid black;
  width: 2em;
  background-color: #edf2fb;
  color: black;
  border-radius: 0.35em;
  font-size: 1.2em;
  margin: 0.1em auto;
  display: flex;
  justify-content: center;
}

<!-- language: lang-html -->

&lt;section class=&quot;mainContent&quot;&gt;
  &lt;button id=&quot;result&quot;&gt;Result&lt;/button&gt;
  &lt;button id=&quot;clear&quot;&gt;C&lt;/button&gt;
  &lt;button id=&quot;one&quot;&gt;1&lt;/button&gt;
  &lt;button id=&quot;two&quot;&gt;2&lt;/button&gt;
  &lt;button id=&quot;three&quot;&gt;3&lt;/button&gt;
  &lt;button id=&quot;four&quot;&gt;4&lt;/button&gt;
  &lt;button id=&quot;five&quot;&gt;5&lt;/button&gt;
  &lt;button id=&quot;six&quot;&gt;6&lt;/button&gt;
  &lt;button id=&quot;seven&quot;&gt;7&lt;/button&gt;
  &lt;button id=&quot;eight&quot;&gt;8&lt;/button&gt;
  &lt;button id=&quot;nine&quot;&gt;9&lt;/button&gt;
  &lt;button id=&quot;zero&quot;&gt;0&lt;/button&gt;
  &lt;button id=&quot;plus&quot;&gt;+&lt;/button&gt;
  &lt;button id=&quot;minus&quot;&gt;-&lt;/button&gt;
  &lt;button id=&quot;multiply&quot;&gt;*&lt;/button&gt;
  &lt;button id=&quot;divide&quot;&gt;/&lt;/button&gt;
  &lt;button id=&quot;equal&quot;&gt;=&lt;/button&gt;
  &lt;button id=&quot;comma&quot;&gt;,&lt;/button&gt;
  &lt;input type=&quot;text&quot; placeholder=&quot;Your calculations&quot;&gt;
&lt;/section&gt;

<!-- end snippet -->

How do you do this? They are just lined up in the center one by one going down!

I tried all this justify content, text align, grid but I cant seem to get any to work

答案1

得分: 3

你可以使用display:grid;grid-template-columns属性来对mainContent类进行设置,以正确对齐按钮。此外,我将input元素放入具有calculation类的不同div中,并为其提供了内联CSS值。最后,我添加了display:flex;justify-content:center;属性,以使input元素(.calculation类)居中对齐。希望对您有帮助。

英文:

You can use display:grid; and grid-template-columns attribute to mainContent class not to .mainContent button class for aligning the buttons properly. Additionally i put the input element in to the different div which has calculation class and i gave it inline css values. Finally added display:flex; and justify-content:center; attributes for aligning the input element (.calculation class) to the center. I hope it works for you.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.mainContent button {
  font-family: inherit;
  padding: 1em 2em;
  border: 3px solid black;
  background-color: #edf2fb;
  color: black;
  border-radius: 0.35em;
  font-size: 1.2em;
  display: block;
  margin: 0.1em;
}

.mainContent {
  display: grid;
  grid-template-columns: auto auto auto auto;
  justify-content: center;
}

.calculation {
  display: flex;
  justify-content: center;
}

<!-- language: lang-html -->

 &lt;div class=&quot;calculation&quot;&gt;&lt;input style=&quot;width: 410px; height: 60px;&quot; type=&quot;text&quot; placeholder=&quot;Your calculations&quot;&gt;
    &lt;/div&gt;
    &lt;section class=&quot;mainContent&quot;&gt;
        &lt;button id=&quot;result&quot;&gt;Result&lt;/button&gt;
        &lt;button id=&quot;clear&quot;&gt;C&lt;/button&gt;
        &lt;button id=&quot;one&quot;&gt;1&lt;/button&gt;
        &lt;button id=&quot;two&quot;&gt;2&lt;/button&gt;
        &lt;button id=&quot;three&quot;&gt;3&lt;/button&gt;
        &lt;button id=&quot;four&quot;&gt;4&lt;/button&gt;
        &lt;button id=&quot;five&quot;&gt;5&lt;/button&gt;
        &lt;button id=&quot;six&quot;&gt;6&lt;/button&gt;
        &lt;button id=&quot;seven&quot;&gt;7&lt;/button&gt;
        &lt;button id=&quot;eight&quot;&gt;8&lt;/button&gt;
        &lt;button id=&quot;nine&quot;&gt;9&lt;/button&gt;
        &lt;button id=&quot;zero&quot;&gt;0&lt;/button&gt;
        &lt;button id=&quot;plus&quot;&gt;+&lt;/button&gt;
        &lt;button id=&quot;minus&quot;&gt;-&lt;/button&gt;
        &lt;button id=&quot;multiply&quot;&gt;*&lt;/button&gt;
        &lt;button id=&quot;divide&quot;&gt;/&lt;/button&gt;
        &lt;button id=&quot;equal&quot;&gt;=&lt;/button&gt;
        &lt;button id=&quot;comma&quot;&gt;,&lt;/button&gt;

    &lt;/section&gt;

<!-- end snippet -->

答案2

得分: 0

以下是您提供的代码的中文翻译:

.mainContent button {
    font-family: inherit;
    border: 3px solid black;
    padding: 25px;
    margin: 10px;
    width: 10em;
    background-color: #edf2fb;
    color: black;
    border-radius: 0.35em;
    font-size: 1.2em;
    text-align: center;
}

.mainContent {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style.css" />
    <title>浏览器</title>
</head>

<body>
    <input type="text" placeholder="您的计算">
    <section class="mainContent">
        <button id="result">结果</button>
        <button id="clear">C</button>
        <button id="one">1</button>
        <button id="two">2</button>
        <button id="three">3</button>
        <button id="four">4</button>
        <button id="five">5</button>
        <button id="six">6</button>
        <button id="seven">7</button>
        <button id="eight">8</button>
        <button id="nine">9</button>
        <button id="zero">0</button>
        <button id="plus">+</button>
        <button id="minus">-</button>
        <button id="multiply">*</button>
        <button id="divide">/</button>
        <button id="equal">=</button>
        <button id="comma">,</button>
    </section>
    <script src="script.js"></script>
</body>

</html>

希望这能帮助您!

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.mainContent button {
    font-family: inherit;
    border: 3px solid black;
    padding: 25px;
    margin: 10px;
    width: 10em;
    background-color: #edf2fb;
    color: black;
    border-radius: 0.35em;
    font-size: 1.2em;
    text-align: center;
}

.mainContent {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
}

<!-- language: lang-html -->

&lt;!-- 
Online HTML, CSS and JavaScript editor to run code online.
--&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot; /&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; /&gt;
  &lt;title&gt;Browser&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;input type=&quot;text&quot; placeholder=&quot;Your calculations&quot;&gt;
  &lt;section class=&quot;mainContent&quot;&gt;
        &lt;button id=&quot;result&quot;&gt;Result&lt;/button&gt;
        &lt;button id=&quot;clear&quot;&gt;C&lt;/button&gt;
        &lt;button id=&quot;one&quot;&gt;1&lt;/button&gt;
        &lt;button id=&quot;two&quot;&gt;2&lt;/button&gt;
        &lt;button id=&quot;three&quot;&gt;3&lt;/button&gt;
        &lt;button id=&quot;four&quot;&gt;4&lt;/button&gt;
        &lt;button id=&quot;five&quot;&gt;5&lt;/button&gt;
        &lt;button id=&quot;six&quot;&gt;6&lt;/button&gt;
        &lt;button id=&quot;seven&quot;&gt;7&lt;/button&gt;
        &lt;button id=&quot;eight&quot;&gt;8&lt;/button&gt;
        &lt;button id=&quot;nine&quot;&gt;9&lt;/button&gt;
        &lt;button id=&quot;zero&quot;&gt;0&lt;/button&gt;
        &lt;button id=&quot;plus&quot;&gt;+&lt;/button&gt;
        &lt;button id=&quot;minus&quot;&gt;-&lt;/button&gt;
        &lt;button id=&quot;multiply&quot;&gt;*&lt;/button&gt;
        &lt;button id=&quot;divide&quot;&gt;/&lt;/button&gt;
        &lt;button id=&quot;equal&quot;&gt;=&lt;/button&gt;
        &lt;button id=&quot;comma&quot;&gt;,&lt;/button&gt;
        
    &lt;/section&gt;
  &lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月17日 15:12:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269408.html
匿名

发表评论

匿名网友

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

确定