ancho: 100% - relleno?
Tengo una entrada html.
La entrada tiene padding: 5px 10px;
Quiero que sea el 100% del ancho del div padre(que es fluido).
Sin embargo, usar width: 100%;
hace que la entrada sea 100% + 20px
¿cómo puedo evitar esto?
14 answers
box-sizing: border-box
es una manera rápida y fácil de arreglarlo:
This will work in all modern browsers, and IE8+.
Aquí hay una demostración: http://jsfiddle.net/thirtydot/QkmSk/301 /
.content {
width: 100%;
box-sizing: border-box;
}
Las versiones prefijadas del navegador (-webkit-box-sizing
, etc.) no son necesarios en los navegadores modernos.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-06 13:26:53
Esta es la razón por la que tenemos box-sizing
en CSS.
He editado tu ejemplo, y ahora funciona en Safari, Chrome, Firefox y Opera. Échale un vistazo: http://jsfiddle.net/mathias/Bupr3 / Todo lo que agregué fue esto:
input {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
Desafortunadamente los navegadores más antiguos como IE7 no soportan esto. Si estás buscando una solución que funcione en IEs antiguos, echa un vistazo a las otras respuestas.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-31 12:08:12
Use relleno en porcentajes también y elimine del ancho:
padding: 5%; width: 90%;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-07 12:00:55
Puede hacerlo sin usar box-sizing
y soluciones no claras como width~=99%
.
Demostración en jsFiddle:
- Mantener las entradas
padding
yborder
- Añadir a la entrada horizontal negativo
margin
=border-width
+horizontal padding
- Añadir a la envoltura de entrada horizontal
padding
igual amargin
del paso anterior
Marcado HTML:
<div class="input_wrap">
<input type="text" />
</div>
CSS:
div {
padding: 6px 10px; /* equal to negative input's margin for mimic normal `div` box-sizing */
}
input {
width: 100%; /* force to expand to container's width */
padding: 5px 10px;
border: none;
margin: 0 -10px; /* negative margin = border-width + horizontal padding */
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-28 05:54:09
Use css calc ()
Súper simple e impresionante.
input {
width: -moz-calc(100% - 15px);
width: -webkit-calc(100% - 15px);
width: calc(100% - 15px);
}
Como se ve aquí: Div ancho 100% menos cantidad fija de píxeles
Por webvitaly ( https://stackoverflow.com/users/713523/webvitaly)
Fuente Original: http://web-profile.com.ua/css/dev/css-width-100prc-minus-100px/
Acabo de copiar esto aquí, porque casi me lo pierdo en el otro hilo.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:54:48
Suponiendo que estoy en un contenedor con relleno de 15px, esto es lo que siempre uso para la parte interior:
width:auto;
right:15px;
left:15px;
Que estirará la parte interior a cualquier ancho que debe ser menos el 15px cada lado.
Salud
Andy
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-29 12:42:44
Puedes probar algunos trucos de posicionamiento. Puede poner la entrada en un div con position: relative
y una altura fija, luego en la entrada tiene position: absolute; left: 0; right: 0;
, y cualquier relleno que desee.
Ejemplo en vivo
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-07 11:58:13
Aquí está la recomendación de codeontrack.com , que tiene buenos ejemplos de solución:
En lugar de establecer el ancho del div en 100%, establézcalo en auto, y asegúrese de que
<div>
esté configurado para mostrar: bloque (predeterminado para<div>
).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-31 22:18:54
Mueva el relleno de la caja de entrada a un elemento wrapper.
<style>
div.outer{ background: red; padding: 10px; }
div.inner { border: 1px solid #888; padding: 5px 10px; background: white; }
input { width: 100%; border: none }
</style>
<div class="outer">
<div class="inner">
<input/>
</div>
</div>
Ver ejemplo aquí: http://jsfiddle.net/L7wYD/1/
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-07 12:16:25
Qué hay de envolverlo en un recipiente. El contenedor debería tener un estilo como:
{
width:100%;
border: 10px solid transparent;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-21 14:09:43
Tuve el mismo problema. Solucionarlo así:
width: 100%;
padding: 5px;
/*--------------Now remove from padding what you've added---------------*/
margin:-5px;
Salud
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-24 13:11:56
Prueba esto:
width: 100%;
box-sizing: border-box;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-03 15:55:39
Solo entiende la diferencia entre width: auto; y width:100%; Width: auto; calculará (AUTO)MÁTICAMENTE el ancho para que se ajuste exactamente al div de envoltura, incluido el relleno. Width 100% expande el ancho y agrega el relleno.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-15 17:18:46
Puedes hacer esto:
width: auto;
padding: 20px;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-21 03:15:37