1

I'm trying to adapt the images from the buttons (#but2, #but1) to their full height possible (in the div) and their corresponding width according to their height (width: auto).

I've tried with this code for the images from the buttons:

#but1 img, #but2 img{
    height: 100%;
    width: auto;
}

But I can't get the output I want. I share an image showing what's the output of that code and what's the output I want.

Click here for watching the image

Thanks a lot for your help!

#but1 {
  margin-left: auto;
  margin-right: 5px;
  background-color: transparent;
  border: 0;
}

#but2 {
  margin-left: 5px;
  margin-right: auto;
  background-color: transparent;
  border: 0;
}

#but1 img,
#but2 img {
  width: 100%;
  height: auto;
}

.button-container {
  background-color: #fff;
  width: 50%;
  height: 50px;
  border-radius: 125px;
  margin-left: auto;
  margin-right: auto;
  box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.5);
}

#but-cont-2 {
  margin-top: 25px;
  background-color: #79b2f7;
  position: relative;
}

#textarea {
  width: 85%;
  background-color: transparent;
  border: 0;
  height: 100%;
  outline: none;
  resize: none;
  float: left;
}

.text {
  width: 100%;
  background-color: transparent;
  float: right;
  border: 0;
  margin: 0;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  text-align: right;
  right: 21px;
}
<div>
  <div class="button-container" id="but-cont-1">
    <textarea id="textarea" name="prod"></textarea>
    <button onclick="sub()" id="but1">
        <img id="but1" src="https://cdn-icons-png.flaticon.com/512/861/861180.png" alt="">
    </button>
  </div>

  <div class="button-container" id="but-cont-2">
    <label id="cont" class="text"></label>
    <button id="but2">
        <img id="but2" src="https://cdn-icons-png.flaticon.com/128/1078/1078599.png" alt="">
    </button>
  </div>
</div>
isherwood
  • 58,414
  • 16
  • 114
  • 157
Tecnomagia
  • 13
  • 3
  • Could you please not abbreviate your selectors so much? I have to guess their meaning now. "but" can be "button", "bc" is probably "button-container", "ta-q" could be "textarea-question"? Who knows? – KIKO Software Dec 20 '22 at 18:58
  • @KIKOSoftware I've just changed them, I think now they are easier to know. I'm sorry! – Tecnomagia Dec 20 '22 at 19:03
  • You don't have to be sorry, it's just something I noticed. There just no need for such abbreviations, the only thing they accomplish is that they make your code harder to read. Using proper names, that convey real meaning, doesn't make your code slower or bigger. Remember that your server and browser will compress CSS for you on the fly. – KIKO Software Dec 20 '22 at 19:08
  • I fiddled a bit with your code and got this: [codepen.io](https://codepen.io/kikosoft/pen/MWBYyMv). I noticed some things: 1. You reuse id's, like "but1" and "but2", that's not valid. Id's have to be unique in HTML. 2. It's still a bit of mesh, that's why I didn't answer your question, but I hope this gets you one step further. – KIKO Software Dec 20 '22 at 19:35
  • https://stackoverflow.com/questions/3029422/how-do-i-auto-resize-an-image-to-fit-a-div-container Does this helps? – Code Assassin Dec 20 '22 at 19:49
  • Thanks, @KIKOSoftware, for the help. I've just solved it! Thanks a lot! – Tecnomagia Dec 20 '22 at 21:18

3 Answers3

0

Try using display: flex; for the button and try to resize the images with pixels like width: 20px; and height: auto; or verse versa, it should fix it.

0

Here is my idea of doing that: https://jsfiddle.net/L1ma5qrc/86/

*{
  margin: 0;
  padding: 0;
}

body{
padding: 20px
}
#but1 {
  /* margin-right: 5px; */
  /* background-color: transparent; */
  border: 0;
  background-color: #fff;
  width: 50%;
  height: 50px;
  border-radius: 125px;
  border: 1px solid lightgray;
  box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
}

#but1:before {
  content: "";
  display: block;
  background: url("https://cdn-icons-png.flaticon.com/128/1078/1078599.png") no-repeat;
  background-size: 50%;
  background-position: center right;
  height: 50px;
  width: 50px;
  margin-right: 16px;
  margin-left: auto;
}

#but2 {
  /* margin-right: 5px; */
  /* background-color: transparent; */
  border: 0;
  background-color: #fff;
  width: 50%;
  height: 50px;
  border-radius: 125px;
  border: 1px solid lightgray;
  box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
}

#but2:before {
  content: "";
  display: block;
  background: url("https://cdn-icons-png.flaticon.com/128/1078/1078599.png") no-repeat;
  background-size: 50%;
  background-position: center left;
  height: 50px;
  width: 50px;
  margin-right: auto;
  margin-left: 16px;
}
    <div>
      <div class="button-container" id="but-cont-1">
        <button id="but1">
        </button>
      </div>
      <div class="button-container" id="but-cont-2">
        <button id="but2">
        </button>
      </div>
    </div>
dzm11
  • 71
  • 6
0

I think I'd look at applying the images as backgrounds. It cleans up the markup quite a bit and makes positioning easier.

Other tips:

  • Don't use floats for alignment. They're an outdated layout technique and have very few appropriate uses anymore.
  • Avoid absolute positioning when possible. It can also be troublesome.
  • Floats don't work with absolute positioning. Use one or the other if you must.
  • Rely less on IDs in your CSS. Ideally everything is class-based so it's reusable.
  • Consider not removing outlines. They're important for accessibility.
  • Avoid using label elements other than with form inputs. That would be nonstandard and also a possible accessibility issue.

.button-container {
  background-color: #fff;
  width: 50%;
  height: 50px;
  border-radius: 125px;
  margin-left: auto;
  margin-right: auto;
  box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.5);
  position: relative;
  overflow: hidden;
}

.button-container.alt {
  margin-top: 25px;
  background-color: #79b2f7;
}

.button-container button {
  width: 100%;
    height: 100%;
    background-size: auto 60%;
    background-position: 93% 50%;
    background-repeat: no-repeat;
    border: 0;
}

.button-container button.icon-recycle {
  background-image: url("https://cdn-icons-png.flaticon.com/512/861/861180.png");
}

.button-container button.icon-trash {
  background-image: url("https://cdn-icons-png.flaticon.com/128/1078/1078599.png");
  background-position: 7% 50%;
}

#textarea {
  position: absolute;
  width: 85%;
  background-color: transparent;
  border: 0;
  height: 100%;
  outline: none;
  resize: none;
}

.text {
  width: 100%;
  background-color: transparent;
  border: 0;
  margin: 0;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  text-align: right;
  right: 21px;
}
<div>
  <div class="button-container">
    <textarea id="textarea" name="prod"></textarea>

    <button class="icon-recycle" onclick="sub()"></button>
  </div>

  <div class="button-container alt">
    <span class="text"></span>

    <button class="icon-trash"></button>
  </div>
</div>
isherwood
  • 58,414
  • 16
  • 114
  • 157