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 - How to make divs wrap *only* at their max-width, and not because its parent container is being overflown?

I have a parent container with a fixed size, and absolutely positioned child divs inside it. The position of the child divs have dynamic text content and a max-width and can move around freely, and extend outside of the parent, which is set to overflow: hidden. See the snippet at the bottom for illustration.

This works fine except for one small problem: If a child partially sticks outside the parent container to the right, the text inside of it will wrap, trying to stay inside of the parent container. I do explicitly not want this - if a child moves out of the parent, it should not change its wrapping or size because of it, it should just move out of view. The parent should simply act as a "window" of sorts through which the children are viewed.

I've tried different combinations of applying white-space: nowrap to the parent and/or children, none of which have worked for me so far. Is this just not possible?

This snippet demonstrates the problem:

/* Relevant parent and child styles */

.parent {
  position: relative;
  width: 500px;
  height: 400px;
  overflow: hidden;
}

.child {
  position: absolute;
  max-width: 250px;
}


/* Individual positioning of child elements */

#fine {
  top: 30px;
  left: 100px;
}

#wrapped {
  top: 90px;
  left: 400px;
}

#unwrapped {
  top: 330px;
  left: 170px;
  white-space: nowrap;
}


/* The rest is only styles to make the example easier on the eye */

body {
  background-color: hsl(0, 0%, 90%);
  margin: 0;
  padding: 24px;
}

.parent {
  background-color: white;
  border: 2px solid grey;
}

.child {
  padding: 12px;
  background: yellowgreen;
}
<div class="parent">
  <div id="fine" class="child">
    Text should be wrapped normally with the max-width of the child, like this
  </div>
  <div id="wrapped" class="child">
    This text will be wrapped much earlier though because it is running out of the parent container
  </div>
  <div id="unwrapped" class="child">
    "white-space: nowrap" only prevents *all* wrapping
  </div>
</div>
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Don't use left to position the element but consider translate instead. The left property will restrict the width of your element.

/* Relevant parent and child styles */

.parent {
  position: relative;
  width: 500px;
  height: 400px;
}

.child {
  position: absolute;
  max-width: 250px;
}


/* Individual positioning of child elements */

#fine {
  top: 30px;
  transform: translateX(100px);
}

#wrapped {
  top: 90px;
  transform: translateX(400px);
}

#unwrapped {
  top: 200px;
  left: 170px;
}
#extra {
  top: 300px;
  left: 170px;
}



/* The rest is only styles to make the example easier on the eye */

body {
  background-color: hsl(0, 0%, 90%);
  margin: 0;
  padding: 24px;
}

.parent {
  background-color: white;
  border: 2px solid grey;
}

.child {
  padding: 12px;
  background: yellowgreen;
}
<div class="parent">
  <div id="fine" class="child">
    Text should be wrapped normally with the max-width of the child, like this
  </div>
  <div id="wrapped" class="child">
    This text will be wrapped much earlier though because it is running out of the parent container
  </div>
  <div id="unwrapped" class="child">
    "white-space: nowrap" only prevents *all* wrapping
  </div>
  
   <div id="extra" class="child">
    short text
  </div>
</div>

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

...