Compensating for Select Option loss

Asked

Viewed 54 times

4

I have a style I use for the fields of form:

.inputTextMedio {
    border: 1px solid rgb(0,0,0);
    border-radius: 10px;
    width: 250px;
    height: 30px;
    padding: 3px;
    display: inline-block;
}

The problem is I’m also using it for Select Options.

However, the Select Options end eating the pixels of margin and padding and in the end, instead of

width: 250px;
height: 30px;

They end up with

width: 242px; + 6 do padding + 2 do border = 250
height: 22px; + 6 do padding + 2 do border = 30

Is there a way to make up for that loss?

For example:

select {
  width  += 8px;
  height += 8px;
}

2 answers

4


The element select has by default box-sizing: border-box;. Suffice change to content-box.

Create one more style just for select and place box-sizing: content-box;:

select.inputTextMedio{
   box-sizing: content-box;
}

Example without:

.inputTextMedio {
    border: 1px solid rgb(0,0,0);
    border-radius: 10px;
    width: 250px;
    height: 30px;
    padding: 3px;
    display: inline-block;
}
<input class="inputTextMedio">
<br>
<select class="inputTextMedio">
   <option> Selecione</option>
</select>

Example with:

.inputTextMedio {
    border: 1px solid rgb(0,0,0);
    border-radius: 10px;
    width: 250px;
    height: 30px;
    padding: 3px;
    display: inline-block;
}

select.inputTextMedio{
   box-sizing: content-box;
}
<input class="inputTextMedio">
<br>
<select class="inputTextMedio">
   <option> Selecione</option>
</select>

  • thank you, that’s right!

  • Really the border-box "didn’t work" as expected! Good uncle tip!

2

You can use the cal function of CSS to compensate for this.

So if you’re like this:

width: 242px; + 6 do padding + 2 do border = 250
height: 22px; + 6 do padding + 2 do border = 30

Would look like this:

width: calc(242px + 8px); 
height: calc(22px + 8px); 

Tip: Here you can read more about css Calc(): https://developer.mozilla.org/en-US/docs/Web/CSS/calc

  • in the case of Calc does not work because width and heght I am taking a style class But the box-Sizing: content-box; worked

  • @Carlosrocha cool, so credit to Sam who gave the tip ;)

  • 1

    blz, I already gave him credit

Browser other questions tagged

You are not signed in. Login or sign up in order to post.