Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

html - Can't align element. What's the way to fix this?

I'm trying to make the icon for hamburger menu and align it with flexbox. But I can't. My icon is going to the left (beyond the menu and the browser screen), weird top indent appears.

There's my code:

* {
  margin: 0;
  padding: 0;
}

input {
  display: none;
}

.wrapper {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  position: relative;
  width: 100%;
  height: 100px;
  background: lightblue;
}

span {
  position: absolute;
  z-index: 1;
  width: 70px;
  height: 5px;
  background: #000;
  display: block;
}

span:nth-child(1) {
  top: 0px;
}
span:nth-child(2) {
  top: 30px;
}

span:nth-child(3) {
  top: 60px;
}

label {
  background: red;
  position: relative;
  display: block;
}
<div class="wrapper">
  <input type="checkbox" id="click">
  <label for="click">
    <span></span>
    <span></span>
    <span></span>
  </label>
</div>
question from:https://stackoverflow.com/questions/65648181/cant-align-element-whats-the-way-to-fix-this

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

1 Answer

0 votes
by (71.8m points)

In fact, the button for the hamburger menu can be made in different ways.

Check out my example. Now your tag <label> has a height and width, and this means that you can now adjust the height of the lines - <span>.

And now, you can see the background color - background: red.

Also, I added a rule right: 50px for label. Now you can adjust the padding.

I marked all the edits in css.

* {
  margin: 0;
  padding: 0;
}

input {
  display: none;
}

.wrapper {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  position: relative;
  width: 100%;
  height: 100px;
  background: lightblue;
}

span {
  /*position: absolute;*/ /*remove*/
  z-index: 1;
  /*width: 70px;*/ /*remove*/
  width: 100%; /*add*/
  height: 5px;
  background: #000;
  display: block;
}

span:nth-child(1) {
  top: 0px;
}
span:nth-child(2) {
  top: 30px;
}

span:nth-child(3) {
  top: 60px;
}

label {
  background: red;
  position: relative;
  /*display: block;*/ /*remove*/
  
  display: flex; /*add*/
  flex-direction: column; /*add*/
  justify-content: space-between; /*add*/
  width: 70px; /*add*/
  height: 50px; /*add*/
  right: 50px; /*add*/
}
<div class="wrapper">
  <input type="checkbox" id="click">
  <label for="click">
    <span></span>
    <span></span>
    <span></span>
  </label>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...