2017-01-13 36 views
0

裏面我有一個輸入字段和一個提交按鈕,它看起來像一個問號由於CSS樣式。這是它目前的樣子:廣場問號提交按鈕輸入文本字段

enter image description here

我如何定位輸入字段中的問號是這樣的:

enter image description here

div { 
 
    position: relative; 
 
    top: 50%; 
 
    transform: translateY(50%); 
 
} 
 
div h1 { 
 
    text-align: center; 
 
    margin: 0px; 
 
} 
 
div p { 
 
    text-align: center; 
 
} 
 
.text { 
 
    display: block; 
 
    margin: 0 auto; 
 
    width: 50%; 
 
    padding-left: 8px; 
 
    padding-right: 8px; 
 
    height: 35px; 
 
} 
 
.submit { 
 
    border: none; 
 
    background: none; 
 
    position: absolute; 
 
    top: 2px; 
 
    right: 0px; 
 
}
<div> 
 
    <h1>Heading</h1> 
 
    <p>Sub-Heading</p> 
 
    <form> 
 
    <input type="text" name="username" class="text"> 
 
    <input type="submit" class="submit" value="?"> 
 
    </form> 
 
</div>

的jsfiddle :https://jsfiddle.net/ea1vrume/

我曾嘗試提交按鈕下面的造型,但它從那裏我希望它是地方它的出路:

.submit { 
    position: absolute; 
    top: 2px; 
    right: 0px; 
} 

回答

0

要獲得文本框裏面的?按鈕:

  • 風格position: relative適用於您的<form>
  • 風格right: 25%適用於您的?按鈕

div { 
 
    position: relative; 
 
    top: 50%; 
 
    transform: translateY(50%); 
 
} 
 
div h1 { 
 
    text-align: center; 
 
    margin: 0px; 
 
} 
 
div p { 
 
    text-align: center; 
 
} 
 
.text { 
 
    display: block; 
 
    margin: 0 auto; 
 
    width: 50%; 
 
    padding-left: 8px; 
 
    padding-right: 22px; 
 
    height: 35px; 
 
    box-sizing: border-box; 
 
} 
 
.submit { 
 
    border: none; 
 
    background: none; 
 
    position: absolute; 
 
    top: 9px; 
 
    right: 25%; 
 
} 
 
form { 
 
    position: relative; 
 
}
<div> 
 
    <h1>Heading</h1> 
 
    <p>Sub-Heading</p> 
 
    <form> 
 
    <input type="text" name="username" class="text"> 
 
    <input type="submit" class="submit" value="?"> 
 
    </form> 
 
</div>

+0

有沒有辦法垂直居中提交按鈕? –

+0

@TheCodesee一種方法是將按鈕的頂部樣式改爲12px。檢查我的更新代碼。 –

+0

任何機會,我們可以阻止文本重疊提交按鈕? –

1

絕對定位將拉動這一關。將position: relative;指定給父級,並且我將其設置爲inline-block,以便它僅將內容包裝在內部,而不是100%寬度的頁面,然後將.submit按鈕絕對放置在文本輸入上。

div { 
 
    position: relative; 
 
    top: 50%; 
 
    transform: translateY(50%); 
 
} 
 

 
div h1 { 
 
    text-align: center; 
 
    margin: 0px; 
 
} 
 

 
div p { 
 
    text-align: center; 
 
} 
 

 
.submit { 
 
    border: none; 
 
    background: none; 
 
    position: absolute; 
 
    top: 2px; 
 
    right: 0px; 
 
} 
 
form { 
 
    text-align: center; 
 
} 
 
form > div { 
 
    display: inline-block; 
 
    width: 50%; 
 
    position: relative; 
 
} 
 
.submit { 
 
    position: absolute; 
 
    right: 0; top: 50%; 
 
    transform: translateY(-50%); 
 
} 
 
.text { 
 
    width: 100%; 
 
    box-sizing: border-box; 
 
    padding-left: 8px; 
 
    padding-right: 8px; 
 
    height: 35px; 
 
}
<div> 
 
    <h1>Heading</h1> 
 
    <p>Sub-Heading</p> 
 
    <form> 
 
    <div> 
 
     <input type="text" name="username" class="text"> 
 
     <input type="submit" class="submit" value="?"> 
 
    </div> 
 
    </form> 
 
</div>

+0

您好,謝謝您的回答。我注意到輸入字段不再居中。 –

0

看是否有此幫助https://jsfiddle.net/ea1vrume/1/

HTML

<div> 
    <h1>Heading</h1> 
    <p>Sub-Heading</p> 
    <form> 
    <input type="text" name="username" class="text"> 
    <input type="submit" class="submit" value="?"> 
    </form> 
</div> 

CSS:

div { 
    position: relative; 
    top: 50%; 
    transform: translateY(50%); 
    text-align: center; 
} 

div h1 { 
    text-align: center; 
    margin: 0px; 
} 

form { 
    position: relative; 
    width: 50%; 
    display: inline-block; 
} 

div p { 
    text-align: center; 
} 

.text { 
    display: block; 
    margin: 0 auto; 
    width: 100%; 
    box-sizing: border-box; 
    padding-left: 8px; 
    padding-right: 8px; 
    height: 35px; 
} 

.submit { 
    border: none; 
    background: none; 
    position: absolute; 
    top: 0; 
    transform: translateY(50%); 
    right: 0px; 
}