jz22
jz22

Reputation: 2638

Styling of an input type file field does not properly work

I added a custom styling to a input field by putting it in a label tag.

However it doesn't work as expected. I want it to have a width of 100px and a height of 25px. In addition I want the text to be centered.

HTML:

.fileContainer {
  overflow: hidden;
  position: relative;
}

.fileContainer [type=file] {
  cursor: inherit;
  display: block;
  font-size: 999px;
  filter: alpha(opacity=0);
  min-height: 100%;
  min-width: 100%;
  opacity: 0;
  position: absolute;
  right: 0;
  text-align: right;
  top: 0;
}

.fileContainer {
  height: 25px;
  width: 100px;
  border: 1px solid #6C7A89;
  border-radius: 20px;
  background-color: transparent;
  font-size: 14px;
  color: #6C7A89;
}

.fileContainer [type=file] {
  cursor: pointer;
}
<label class="fileContainer">
Select file
<input name="filetoupload" type="file">
</label>

Fiddle

Upvotes: 1

Views: 275

Answers (5)

Ehsan
Ehsan

Reputation: 12959

Try This:

.fileContainer {
  display: inline-block;<---------Added[Need for set width and height]
  line-height: 25px;<------------Added[Center in the vertical axis]
  text-align: center;<------------Added[Center in the horizontal axis]
  //more code...
}

.fileContainer {
  overflow: hidden;
  position: relative;
}

.fileContainer [type=file] {
  cursor: inherit;
  display: block;
  font-size: 999px;
  filter: alpha(opacity=0);
  min-height: 100%;
  min-width: 100%;
  opacity: 0;
  position: absolute;
  right: 0;
  text-align: right;
  top: 0;
}

.fileContainer {
  height: 25px;
  line-height: 25px;
  width: 100px;
  border: 1px solid #6C7A89;
  border-radius: 20px;
  background-color: transparent;
  font-size: 14px;
  color: #6C7A89;
  display: inline-block;
  text-align: center;
}

.fileContainer [type=file] {
  cursor: pointer;
}
<label class="fileContainer">
    Select file
    <input name="filetoupload" type="file">
</label> 

Upvotes: 1

dippas
dippas

Reputation: 60563

It is not happening anything regards to width and height because by default label is an inline element level.

You can add this display: flex; to the .filecontainer to make a block-level element.

and this to center the text inside it :

justify-content: center;
align-items: center;

snippet

.fileContainer {
  overflow: hidden;
  position: relative;
}

.fileContainer [type=file] {
  opacity: 0;
  position: absolute;
  cursor: pointer;
}

.fileContainer {
  height: 25px;
  width: 100px;
  border: 1px solid #6C7A89;
  border-radius: 20px;
  background-color: transparent;
  font-size: 14px;
  color: #6C7A89;
  display: flex;
  justify-content: center;
  align-items: center;
}
<label class="fileContainer">
Select file
<input name="filetoupload" type="file">
</label>

Upvotes: 1

Rafiqul Islam
Rafiqul Islam

Reputation: 961

Try This:

.fileContainer {
    position: relative;
}
.fileContainer input {
    position: absolute;
    z-index: 2;
    opacity: 0;
    width:100%;
    height:100%;
}

.fileContainer {
    border: 1px solid #6C7A89;
    border-radius: 20px;
    padding: 2px 15px;
    margin: 2px;
    background: transparent;
    display: inline-block;
}
<div class="fileContainer">
    <input name="filetoupload" type="file"/>
    <span>Select file</span>
</div>

Upvotes: 2

Amit Kumar
Amit Kumar

Reputation: 314

plz update your css "fileContainer"

.fileContainer {
    overflow: hidden;
    position: relative;
}

.fileContainer [type=file] {
    cursor: inherit;
    min-height: 100%;
    min-width: 100%;
    opacity: 0;
    position: absolute;
    right: 0;
    text-align: right;
    top: 0;
}

.fileContainer {
  height:25px;
	width:100px;
	border:1px solid #6C7A89;
	border-radius:20px;
	background-color:transparent;
	font-size:14px;
	color:#6C7A89;
  padding-left: 21px;
  padding-right: 21px;
  padding-top: 5px;
  padding-bottom: 5px;
}

.fileContainer [type=file] {
    cursor: pointer;
}
.file{
  width: 100px;
  height: 100px;
  text-align: center;
}
<label class="fileContainer">
Select file
<input name="filetoupload" type="file" class="file">
</label>

<br><br><br>

<p>
/* add this css into label css "fileContainer" */ <br>

</p>
<code>
.fileContainer{
  padding-left: 21px;
  padding-right: 21px;
  padding-top: 5px;
  padding-bottom: 5px;
}
</code>

Upvotes: 0

soumya ranjan hota
soumya ranjan hota

Reputation: 1

add this

.fileContainer{display:block; text-align:center;}

Upvotes: 0

Related Questions