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
226 views
in Technique[技术] by (71.8m points)

html - float div to left of parent div not working

With my current code, how do I make the purple div stick to the left side of the green div? I tried float:left which didn't work. I got close when adding position:absolute; and left:50%; to my purple div class but when the screen resized the div fell off screen. Is there a quick way to float the purple div to the left of the green one so it's not in the center?

html, body{
  margin:0;
  height:100%;
  width:100%;
}

#container{
  background-color:pink;
  height:91%;
  width:100%;
  display:flex;
}

#left{
  width:50%;
  background-color:lightblue;
  display:flex;
  position:relative;
}

#right{
  width:50%;
  background-color:lightgreen;
  display:flex;
}

#logo {
  left:0px;
  width: 100%;
  margin:auto;
  max-width:calc(80vh - 25px);
  background-color:purple;

}
#logo:before {
  content:"";
  display:flex;
  padding-top: 100%;
}
<div id="container">
  <div id="left"></div>
  <div id="right">
    <div id="logo"></div>
  </div>
</div>
question from:https://stackoverflow.com/questions/65645977/float-div-to-left-of-parent-div-not-working

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

1 Answer

0 votes
by (71.8m points)

Try like this.

html, body{
  margin:0;
  height:100%;
  width:100%;
}

#container{
  background-color:pink;
  height:91%;
  width:100%;
  display:flex;
}

#left{
  width:50%;
  background-color:lightblue;
  display:flex;
  position:relative;
}

#right{
  width:50%;
  background-color:lightgreen;
  display:flex;
}

#logo {
  left:0px;
  width: 100%;
  margin:auto;
  margin-left: 0;
  max-width:calc(80vh - 25px);
  background-color:purple;

}
#logo:before {
  content:"";
  display:flex;
  padding-top: 100%;
}
<div id="container">
  <div id="left"></div>
  <div id="right">
    <div id="logo"></div>
  </div>
</div>

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

...