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

Add a break to an unordered list with CSS when window width is less than 600px

If I have an unordered list that I am using for an icon menu like the menu seen in a word processor or text editor. When the window gets below say 600px the icons get hidden and I want them to break down onto the next line.

<ul>
<li>Icon One</li>
<li>Icon twi</li>
<li>Icon three</li>
<li>Icon four</li>
<li>Icon five</li>
</ul>

This list is an inline list and I need it to break after the 4th item into two rows IF the window is less than 600px.

Is there a css solution to this without having to create a separate list or add another row?

i.e. something like this

<style>
@media only screen and (max-width: 600px) {

.listing {
display:inline;
}

.breakitem {
display:breakhere;
}

}
</style>



 <ul class="listing">
    <li>Icon One</li>
    <li>Icon twi</li>
    <li>Icon three</li>
    <li class="breakitem">Icon four</li>
    <li>Icon five</li>
    </ul>
question from:https://stackoverflow.com/questions/65833721/add-a-break-to-an-unordered-list-with-css-when-window-width-is-less-than-600px

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

1 Reply

0 votes
by (71.8m points)

I hope this helps mate..

  .container {
  width: 100vw;
}

li {
  display: inline-block;
}

li:nth-child(4) {
  display: none;
}

@media only screen and (max-width: 600px) {
  li:nth-of-type(4) {
    display: block;
  }
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>


</head>

<body>
  <div class="container">
    <ul class="listing">
      <li>Item One</li>
      <li>Item twi</li>
      <li>Item three</li>
      <li></li>
      <li>Item four</li>
      <li>Item five</li>
    </ul>
  </div>
</body>

</html>

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

...