居中一些按钮

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

Centering some buttons

问题

  1. <section class="mainContent">
  2. <button id="result">结果</button>
  3. <button id="clear">C</button>
  4. <button id="one">1</button>
  5. <button id="two">2</button>
  6. <button id="three">3</button>
  7. <button id="four">4</button>
  8. <button id="five">5</button>
  9. <button id="six">6</button>
  10. <button id="seven">7</button>
  11. <button id="eight">8</button>
  12. <button id="nine">9</button>
  13. <button id="zero">0</button>
  14. <button id="plus">+</button>
  15. <button id="minus">-</button>
  16. <button id="multiply">*</button>
  17. <button id="divide">/</button>
  18. <button id="equal">=</button>
  19. <button id="comma">,</button>
  20. <input type="text" placeholder="您的计算">
  21. </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 -->

  1. .mainContent button {
  2. font-family: inherit;
  3. padding: 1em 2em;
  4. border: 3px solid black;
  5. width: 2em;
  6. background-color: #edf2fb;
  7. color: black;
  8. border-radius: 0.35em;
  9. font-size: 1.2em;
  10. margin: 0.1em auto;
  11. display: flex;
  12. justify-content: center;
  13. }

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

  1. &lt;section class=&quot;mainContent&quot;&gt;
  2. &lt;button id=&quot;result&quot;&gt;Result&lt;/button&gt;
  3. &lt;button id=&quot;clear&quot;&gt;C&lt;/button&gt;
  4. &lt;button id=&quot;one&quot;&gt;1&lt;/button&gt;
  5. &lt;button id=&quot;two&quot;&gt;2&lt;/button&gt;
  6. &lt;button id=&quot;three&quot;&gt;3&lt;/button&gt;
  7. &lt;button id=&quot;four&quot;&gt;4&lt;/button&gt;
  8. &lt;button id=&quot;five&quot;&gt;5&lt;/button&gt;
  9. &lt;button id=&quot;six&quot;&gt;6&lt;/button&gt;
  10. &lt;button id=&quot;seven&quot;&gt;7&lt;/button&gt;
  11. &lt;button id=&quot;eight&quot;&gt;8&lt;/button&gt;
  12. &lt;button id=&quot;nine&quot;&gt;9&lt;/button&gt;
  13. &lt;button id=&quot;zero&quot;&gt;0&lt;/button&gt;
  14. &lt;button id=&quot;plus&quot;&gt;+&lt;/button&gt;
  15. &lt;button id=&quot;minus&quot;&gt;-&lt;/button&gt;
  16. &lt;button id=&quot;multiply&quot;&gt;*&lt;/button&gt;
  17. &lt;button id=&quot;divide&quot;&gt;/&lt;/button&gt;
  18. &lt;button id=&quot;equal&quot;&gt;=&lt;/button&gt;
  19. &lt;button id=&quot;comma&quot;&gt;,&lt;/button&gt;
  20. &lt;input type=&quot;text&quot; placeholder=&quot;Your calculations&quot;&gt;
  21. &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 -->

  1. .mainContent button {
  2. font-family: inherit;
  3. padding: 1em 2em;
  4. border: 3px solid black;
  5. background-color: #edf2fb;
  6. color: black;
  7. border-radius: 0.35em;
  8. font-size: 1.2em;
  9. display: block;
  10. margin: 0.1em;
  11. }
  12. .mainContent {
  13. display: grid;
  14. grid-template-columns: auto auto auto auto;
  15. justify-content: center;
  16. }
  17. .calculation {
  18. display: flex;
  19. justify-content: center;
  20. }

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

  1. &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;
  2. &lt;/div&gt;
  3. &lt;section class=&quot;mainContent&quot;&gt;
  4. &lt;button id=&quot;result&quot;&gt;Result&lt;/button&gt;
  5. &lt;button id=&quot;clear&quot;&gt;C&lt;/button&gt;
  6. &lt;button id=&quot;one&quot;&gt;1&lt;/button&gt;
  7. &lt;button id=&quot;two&quot;&gt;2&lt;/button&gt;
  8. &lt;button id=&quot;three&quot;&gt;3&lt;/button&gt;
  9. &lt;button id=&quot;four&quot;&gt;4&lt;/button&gt;
  10. &lt;button id=&quot;five&quot;&gt;5&lt;/button&gt;
  11. &lt;button id=&quot;six&quot;&gt;6&lt;/button&gt;
  12. &lt;button id=&quot;seven&quot;&gt;7&lt;/button&gt;
  13. &lt;button id=&quot;eight&quot;&gt;8&lt;/button&gt;
  14. &lt;button id=&quot;nine&quot;&gt;9&lt;/button&gt;
  15. &lt;button id=&quot;zero&quot;&gt;0&lt;/button&gt;
  16. &lt;button id=&quot;plus&quot;&gt;+&lt;/button&gt;
  17. &lt;button id=&quot;minus&quot;&gt;-&lt;/button&gt;
  18. &lt;button id=&quot;multiply&quot;&gt;*&lt;/button&gt;
  19. &lt;button id=&quot;divide&quot;&gt;/&lt;/button&gt;
  20. &lt;button id=&quot;equal&quot;&gt;=&lt;/button&gt;
  21. &lt;button id=&quot;comma&quot;&gt;,&lt;/button&gt;
  22. &lt;/section&gt;

<!-- end snippet -->

答案2

得分: 0

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

  1. .mainContent button {
  2. font-family: inherit;
  3. border: 3px solid black;
  4. padding: 25px;
  5. margin: 10px;
  6. width: 10em;
  7. background-color: #edf2fb;
  8. color: black;
  9. border-radius: 0.35em;
  10. font-size: 1.2em;
  11. text-align: center;
  12. }
  13. .mainContent {
  14. display: flex;
  15. justify-content: center;
  16. flex-wrap: wrap;
  17. }
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <link rel="stylesheet" href="style.css" />
  7. <title>浏览器</title>
  8. </head>
  9. <body>
  10. <input type="text" placeholder="您的计算">
  11. <section class="mainContent">
  12. <button id="result">结果</button>
  13. <button id="clear">C</button>
  14. <button id="one">1</button>
  15. <button id="two">2</button>
  16. <button id="three">3</button>
  17. <button id="four">4</button>
  18. <button id="five">5</button>
  19. <button id="six">6</button>
  20. <button id="seven">7</button>
  21. <button id="eight">8</button>
  22. <button id="nine">9</button>
  23. <button id="zero">0</button>
  24. <button id="plus">+</button>
  25. <button id="minus">-</button>
  26. <button id="multiply">*</button>
  27. <button id="divide">/</button>
  28. <button id="equal">=</button>
  29. <button id="comma">,</button>
  30. </section>
  31. <script src="script.js"></script>
  32. </body>
  33. </html>

希望这能帮助您!

英文:

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

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

  1. .mainContent button {
  2. font-family: inherit;
  3. border: 3px solid black;
  4. padding: 25px;
  5. margin: 10px;
  6. width: 10em;
  7. background-color: #edf2fb;
  8. color: black;
  9. border-radius: 0.35em;
  10. font-size: 1.2em;
  11. text-align: center;
  12. }
  13. .mainContent {
  14. display: flex;
  15. justify-content: center;
  16. flex-wrap: wrap;
  17. }

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

  1. &lt;!--
  2. Online HTML, CSS and JavaScript editor to run code online.
  3. --&gt;
  4. &lt;!DOCTYPE html&gt;
  5. &lt;html lang=&quot;en&quot;&gt;
  6. &lt;head&gt;
  7. &lt;meta charset=&quot;UTF-8&quot; /&gt;
  8. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
  9. &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; /&gt;
  10. &lt;title&gt;Browser&lt;/title&gt;
  11. &lt;/head&gt;
  12. &lt;body&gt;
  13. &lt;input type=&quot;text&quot; placeholder=&quot;Your calculations&quot;&gt;
  14. &lt;section class=&quot;mainContent&quot;&gt;
  15. &lt;button id=&quot;result&quot;&gt;Result&lt;/button&gt;
  16. &lt;button id=&quot;clear&quot;&gt;C&lt;/button&gt;
  17. &lt;button id=&quot;one&quot;&gt;1&lt;/button&gt;
  18. &lt;button id=&quot;two&quot;&gt;2&lt;/button&gt;
  19. &lt;button id=&quot;three&quot;&gt;3&lt;/button&gt;
  20. &lt;button id=&quot;four&quot;&gt;4&lt;/button&gt;
  21. &lt;button id=&quot;five&quot;&gt;5&lt;/button&gt;
  22. &lt;button id=&quot;six&quot;&gt;6&lt;/button&gt;
  23. &lt;button id=&quot;seven&quot;&gt;7&lt;/button&gt;
  24. &lt;button id=&quot;eight&quot;&gt;8&lt;/button&gt;
  25. &lt;button id=&quot;nine&quot;&gt;9&lt;/button&gt;
  26. &lt;button id=&quot;zero&quot;&gt;0&lt;/button&gt;
  27. &lt;button id=&quot;plus&quot;&gt;+&lt;/button&gt;
  28. &lt;button id=&quot;minus&quot;&gt;-&lt;/button&gt;
  29. &lt;button id=&quot;multiply&quot;&gt;*&lt;/button&gt;
  30. &lt;button id=&quot;divide&quot;&gt;/&lt;/button&gt;
  31. &lt;button id=&quot;equal&quot;&gt;=&lt;/button&gt;
  32. &lt;button id=&quot;comma&quot;&gt;,&lt;/button&gt;
  33. &lt;/section&gt;
  34. &lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt;
  35. &lt;/body&gt;
  36. &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:

确定