컨테이너의 나머지 너비를 채우는 스타일 입력 요소
다음과 같은 HTML 스 니펫이 있다고 가정 해 보겠습니다.
<div style="width:300px;">
<label for="MyInput">label text</label>
<input type="text" id="MyInput" />
</div>
이것은 내 정확한 코드는 아니지만 중요한 것은 고정 너비 컨테이너의 동일한 줄에 레이블과 텍스트 입력이 있다는 것입니다. 줄 바꿈없이 레이블의 크기를 알지 않고 컨테이너의 나머지 너비를 채우도록 입력 스타일을 어떻게 지정할 수 있습니까?
모든 사람들이 레이아웃을 위해 테이블을 싫어하는 것처럼, 명시적인 테이블 태그를 사용하거나 display : table-cell을 사용하여 이와 같은 작업을 도와줍니다.
<div style="width:300px; display:table">
<label for="MyInput" style="display:table-cell; width:1px">label text</label>
<input type="text" id="MyInput" style="display:table-cell; width:100%" />
</div>
다음은 JavaScript 또는 테이블 레이아웃 해킹을 사용하지 않는 간단하고 깨끗한 솔루션입니다. 이 대답과 비슷합니다. 입력 텍스트 자동 너비가 다른 요소를 부동 상태로 100 % 채
입력 필드를 범위 인로 감싸는 것이 중요합니다 display:block. 다음은 버튼이 먼저오고 입력 필드가 두 번째로되어야한다는 것입니다.
그런 다음 버튼을 오른쪽으로 띄우면 입력 필드가 나머지 공간을 채 웁니다.
HTML
<form method="post">
<button>Search</button>
<span><input type="text" title="Search" /></span>
</form>
CSS
form {
width: 500px;
overflow: hidden;
background-color: yellow;
}
input {
width: 100%;
}
span {
display: block;
overflow: hidden;
padding-right:10px;
}
button {
float: right;
}
간단한 바이올린 : http://jsfiddle.net/v7YTT/90/
업데이트 1 : 귀하의 웹 사이트가 최신 브라우저만을 대상으로하는 경우 유연한 상자를 사용하는 것이 좋습니다 . 여기 에서 현재 지원을 확인할 수 있습니다 .
업데이트 2 : 입력 필드와 전체를 공유하는 여러 버튼 또는 기타 요소에서도 작동합니다. 여기 예가 있습니다.
Flexbox를 사용하는 것이 좋습니다.
그래도 적절한 공급 업체 접두사를 추가하십시오!
form {
width: 400px;
border: 1px solid black;
display: flex;
}
input {
flex: 2;
}
input, label {
margin: 5px;
}
<form method="post">
<label for="myInput">Sample label</label>
<input type="text" id="myInput" placeholder="Sample Input"/>
</form>
Please use flexbox for this. You have a container that is going to flex its children into a row. The first child takes its space as needed. The second one flexes to take all the remaining space:
<div style="display:flex;flex-direction:row">
<label for="MyInput">label text</label>
<input type="text" id="MyInput" style="flex:1" />
</div>
Easiest way to achieve this would be :
CSS :
label{ float: left; }
span
{
display: block;
overflow: hidden;
padding-right: 5px;
padding-left: 10px;
}
span > input{ width: 100%; }
HTML :
<fieldset>
<label>label</label><span><input type="text" /></span>
<label>longer label</label><span><input type="text" /></span>
</fieldset>
Looks like : http://jsfiddle.net/JwfRX/
Very easy trick is using a CSS calc formula. All modern browsers, IE9, wide range of mobile browsers should support this.
<div style='white-space:nowrap'>
<span style='display:inline-block;width:80px;font-weight:bold'>
<label for='field1'>Field1</label>
</span>
<input id='field1' name='field1' type='text' value='Some text' size='30' style='width:calc(100% - 80px)' />
</div>
you can try this :
div#panel {
border:solid;
width:500px;
height:300px;
}
div#content {
height:90%;
background-color:#1ea8d1; /*light blue*/
}
div#panel input {
width:100%;
height:10%;
/*make input doesnt overflow inside div*/
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
/*make input doesnt overflow inside div*/
}
<div id="panel">
<div id="content"></div>
<input type="text" placeholder="write here..."/>
</div>
If you're using Bootstrap 4:
<form class="d-flex">
<label for="myInput" class="align-items-center">Sample label</label>
<input type="text" id="myInput" placeholder="Sample Input" class="flex-grow-1"/>
</form>
Better yet, use what's built into Bootstrap:
<form>
<div class="input-group">
<div class="input-group-prepend">
<label for="myInput" class="input-group-text">Default</label>
</div>
<input type="text" class="form-control" id="myInput">
</div>
</form>
'development' 카테고리의 다른 글
| bash / shell 스크립트에서 http 응답 코드를 평가하는 방법은 무엇입니까? (0) | 2020.05.19 |
|---|---|
| 파이썬은 꼬리 재귀를 최적화합니까? (0) | 2020.05.19 |
| Struct와 OpenStruct는 언제 사용해야합니까? (0) | 2020.05.19 |
| 리포지토리와 서비스 계층의 차이점은 무엇입니까? (0) | 2020.05.19 |
| 팬더의 데이터 프레임에서 무한 값을 삭제합니까? (0) | 2020.05.19 |