Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

html - Text align center or margin left and margin right not centering button in CSS

I added a button to my HTML, and wanted to center it. I tried the two ways in the title but both of them did not work. Im new to HTML and CSS. I'm centering it using CSS but if there is a way to do it using HTML, i'm fine with that.

div {
  text-align: center;
}

body {
  text-align: center;
}

input[type=text] {
  width: 110%;
  padding: 1%;
  background-color: rgb(255, 255, 255);
  border-radius: 10px;
}

h1 {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}

div {
  height: 10em;
  position: relative
}

html {
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  right: 50%;
  margin-right: -50%;
  transform: translate(-50%, -50%)
}

body {
  background: linear-gradient(-20deg, #e73c7e, #23a6d5, #23d5ab);
  background-size: 1000000% 1000000%;
}

button {
  margin-left: auto;
  margin-right: auto;
}
<div id="typing-area">
  <input type="text">
  <button>Enter</button>
  <h1>
    bocho is cool
  </h1>
</div>
question from:https://stackoverflow.com/questions/65851730/text-align-center-or-margin-left-and-margin-right-not-centering-button-in-css

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because you made the size of the input 110% and left the normal page size at 100%, it pushes out the input.

input[type=text] {
  width: 100%;
  padding: 1%;
  background-color: rgb(255, 255, 255);
  border-radius: 10px;            
}

margin-left: auto;
margin-right: auto;

you can use this instead of above

margin:0 auto

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...