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

How do you use html & css for conditional vertical wrap

I am new to html & css. I am wondering if it is possible to do this. I have not found a way so far. I want to arrange my elements in a column with wrapping like

flex-direction: column;
flex-wrap: wrap;

However, I only want to wrap the elements if there is enough horizontal room to do so without a horizonal scrollbar. Otherwise, I want a vertical scroll bar and the items to be in a single column.

question from:https://stackoverflow.com/questions/66056970/how-do-you-use-html-css-for-conditional-vertical-wrap

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

1 Reply

0 votes
by (71.8m points)

I asked a very similar question here Flexbox using align-items: flex-start together with align-content: center. The main difference between our questions is that I was working with row orientation.

Essentially, there is no way to dynamically detect "is there enough room". You have to tell the browser when there is or is not enough room.

So, media queries are your way to go.

You'll also see that I put a max-height in the CSS below. Somewhere in your markup, at some level, there is going to need to be a height constraint or the columns will not wrap.

I've setup a sandbox for you https://jsfiddle.net/w2rdbpo0/

.flex-container {
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  max-height: 600px;
  overflow: scroll;
}
.flex-container > div {
  overflow-wrap: break-word;
}

@media (min-width: 600px) {
  /* 600 pixels totally random  */
  /* just assuming 600px meets "enough horizontal room" requirement */
  
  .flex-container {
    flex-wrap: wrap;
    justify-content: flex-start;
  }
  .flex-container > div {
    max-width: 250px;
  }
}

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

...