用css

2016-07-14 44 views
0

創建一個警告消息框,我想創建一個使用CSS必填字段的警告消息框,這應該是完全一樣 - This is what i want用css

到目前爲止,我已經實現了這個有點像這個 - https://jsfiddle.net/payalsuthar/352k4ope/ 這裏是我的HTML代碼 -

Name:<input type="text" placeholder="name" id="name"><br> 
<div id="errname"><p id="sym">!</p>Please fill out this field.</div> 

Address:<textarea></textarea> 
<input type="submit" value="submit" /> 

這裏是我的CSS -

#errname{ 
    border:1px solid orange; 
    border-radius:4px; 
    width:250px; 
    margin-top=100px; 
    background-color:white; 
    font-size:15px; 
    padding:10px; 
} 
#sym{ 
    width:18px; 
    text-align:center; 
    background-color:darkorange; 
    color:white; 
    font-weight:bold; 
    font-size:14px; 
    border:1px solid white; 
} 

但我希望它是完全一樣上面的圖片,我不介意它是否與下一個字段重疊,它應該出現在上圖中的相同位置。我知道這是html必填字段驗證器消息框但它不能正常工作,所以我是創造一個完全一樣的。 在此先感謝。

+2

什麼你已經證明是當'required'屬性設置所顯示Chrome中的標準工具提示,但沒見過。你有沒有理由不能使用它? –

+0

...還有,這裏有什麼問題?我可以看到這是多個重複。 –

+0

我試圖用php桌面chrome應用程序來使用它,它完全正常運行.ieit不讓我提交表單,除非每個字段都被填充,但唯一的問題是它不顯示消息框。@ RoryMcCrossan –

回答

0

試試這個https://jsfiddle.net/352k4ope/4/

#name { 
    position: relative; 
} 

#name::before { 
    position: absolute; 
    top: 20px; 
    left: 60px; 
    content: ''; 
    display: block; 
    width: 0; 
    height: 0; 
    line-height: 0; 
    font-size: 0; 
    border-left: 8px solid transparent; 
    border-right: 8px solid transparent; 

    border-bottom: 10px solid white; 
} 

#name::after { 
    content: url('http://67.media.tumblr.com/fdeec9480199afb3c6ed9c24b2aec9c5/tumblr_oaayc9r25Z1vaudwko1_75sq.png')' Please fill out this field.'; 
    font-family: arial; 
    font-size: 14px; 
    line-height: 50px; 
    padding: 0 10px; 
    vertical-align: top; 
    width: 170px; 
    height: 50px; 
    position: absolute; 
    background-color: white; 
    top: 30px; 
    left: 50px; 
} 
+0

這正是我想要的,但唯一的問題是,我該如何添加邊界,以及該三角形整個塊。如果你幫助我@馬克佩雷拉,這將是一個很大的幫助.. –

+0

對不起,只能添加邊框到框中,而不是三角形,因爲三角形是邊框。請參閱https://jsfiddle.net/352k4ope/7/ –

+0

三角形是邊框 –

1

我想,你正在尋求CSS幫助。 而不是使用p標籤的嘗試使用span與下面的CSS

#sym { 
background-color: darkorange; 
color: white; 
font-weight: bold; 
font-size: 14px; 
border: 1px solid white; 
padding: 2px 5px 2px 5px; 
margin-right: 8px; 
} 

編輯

我已經從SRIKANTH雷迪的答案使用的CSS,這裏是你更新的jsfiddle。 https://jsfiddle.net/352k4ope/3/

編輯2

新增周圍的白色三角形橙色邊框。 https://jsfiddle.net/352k4ope/5/

+0

非常感謝,但我有一個問題,那個橙色填充在三角形內..我想它是白色的,它的邊框應該是橙色..看看你能不能幫我解決這個問題.. @ Hitesh Riziya –

+1

@payal_suthar你可以在那裏使用一個圖像。並調整其CSS。 – hriziya

+1

@payal_suthar檢查現在編輯2,它應該沒問題:) – hriziya

1

這裏的解決方案....

的CSS

.input-block { 
    position: relative; 
} 
.error-block { 
    position: absolute; 
    top: 100%; 
    left: 50px; 
    border:1px solid orange; 
    border-radius:4px; 
    width:250px; 
    background-color:white; 
    font-size:14px; 
    padding:10px; 
} 
.error-block::before { 
    content: ''; 
    position: absolute; 
    top: -20px; 
    left: 20px; 
    width: 0; 
    height: 0; 
    border-style: solid; 
    border-width: 10px; 
    border-color: transparent transparent orange; 
} 
.error-icon { 
    width:18px; 
    text-align:center; 
    background-color:darkorange; 
    color:white; 
    font-weight:bold; 
    font-size:14px; 
} 

的Html

<div class="input-block"> 
    Name:<input type="text" placeholder="name" id="name"> 
    <div class="error-block"><span class="error-icon">!</span>Please fill out this field.</div> 
</div> 
+0

..感謝它很大程度上幫助了我.... Stackoverflow需要像你一樣準備好幫助其他人的人... –

0

試試這個:

HTML:

<form> 
    <ul class="errorMessages"></ul> 

    <div> 
     <label for="name">Name:</label> 
     <input id="name" type="text" required> 
    </div> 

    <div> 
     <label for="comments">Address:</label> 
     <textarea id="comments" required></textarea> 
    </div> 

    <div class="buttons"> 
     <button>Submit</button> 
    </div> 
</form> 

jQuery的

var createAllErrors = function() { 
      var form = $(this), 
       errorList = $("ul.errorMessages", form); 

      var showAllErrorMessages = function() { 
       errorList.empty(); 

       // Find all invalid fields within the form. 
       var invalidFields = form.find(":invalid").each(function(index, node) { 

        // Find the field's corresponding label 
        var label = $("label[for=" + node.id + "] "), 
         // Opera incorrectly does not fill the validationMessage property. 
         message = node.validationMessage || 'Invalid value.'; 

        errorList 
         .show() 
         .append("<li><span>" + label.html() + "</span> " + message + "</li>"); 
       }); 
      }; 

      // Support Safari 
      form.on("submit", function(event) { 
       if (this.checkValidity && !this.checkValidity()) { 
        $(this).find(":invalid").first().focus(); 
        event.preventDefault(); 
       } 
      }); 

      $("input[type=submit], button:not([type=button])", form) 
       .on("click", showAllErrorMessages); 

      $("input", form).on("keypress", function(event) { 
       var type = $(this).attr("type"); 
       if (/date|email|month|number|search|tel|text|time|url|week/.test (type) 
        && event.keyCode == 13) { 
        showAllErrorMessages(); 
       } 
      }); 
     }; 

     $("form").each(createAllErrors); 

DEMO HERE

+0

..謝謝很多..但即使這與我使用的應用程序無法正常工作..任何方式謝謝回答.. –

+0

不客氣先生@ payal_suthar –

+0

sir ??? ..我是女性.. @ Ivin Raj –