我想使用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必需的字段验证器消息框但它不适合我,所以我创建一个完全一样的。 提前谢谢。
答案 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 Reddy的答案中的CSS,这是你更新的jsfiddle。 https://jsfiddle.net/352k4ope/3/
修改2
在白色三角形周围添加了橙色边框。 https://jsfiddle.net/352k4ope/5/
答案 1 :(得分: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>
答案 2 :(得分: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 );
答案 3 :(得分: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;
}