-2

I have an element .els with div .number (fixed width) and input .things. I want .els to be responsive. Width should be dependent on the screen. But when i test it in responsive mode in browser, .els have the same width all the time, no matter what. I think it's a problem of input. I read every thread in stackoverflow.com, I've tried everything and nothing works.

https://jsfiddle.net/bugs55/qtdLxv5o/15/

#elements {
  overflow: auto;
  background: #1d1e2e;
}
#elements .els {
  margin: 0 auto 10px auto;
  overflow: auto;
  box-sizing: border-box;
  padding: 10px;
  border-radius: 15px;
  width: 400px;
  display: flex;
}
#elements .els .number,
#elements .els input {
  float: left;
}
#elements .els .number {
  font-size: 20px;
  color: #dedede;
  width: 25px;
  height: 25px;
  padding: 5px;
  border-radius: 100px;
  line-height: 25px;
  margin: 0 10px 0 5px;
  background: rgba(0, 0, 0, 0.25);
}

#elements .els input {
  line-height: 29px;
  flex: 1;
  min-width: 0;
  box-sizing: border-box;
  border: none;
  background-image: none;
  background-color: transparent;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
  padding: 3px 0 1px 0;
  font-size: 20px;
  color: #dedede;
}
#elements .els .clear {
  clear: both;
  display: none;
}

@media only screen and (max-width: 430px) {
  .els {
    width: 100%;
  }
  .els:focus-within,
  .els:hover {
    transition: none;
    width: 0;
  }
}
<div id="elements">
  <div class="els" style="background:#00bfff">
    <div class="number" style="">1</div>
    <input type="text" class="things">
    <div class="clear"></div>
  </div>
</div>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
bugs55
  • 3
  • 2

1 Answers1

0

As stated in the comments, because of css specificity, your .els nodes inside #elements are always going to have a width of 400px due to your rule #elements .els.

Try changing your width from 400px to auto and set a max-width of 400px, and remove the .els width rule inside your media query:

#elements .els {
  margin: 0 auto 10px auto;
  overflow: auto;
  box-sizing: border-box;
  padding: 10px;
  border-radius: 15px;
  width: auto;
  max-width: 400px;
  display: flex;

}

Here's a (forked) fiddle showcasing how it'll look: https://jsfiddle.net/wf5z2vkj/

Barn on a Hill
  • 359
  • 3
  • 10