用div+css如何让字靠右对齐

html-css011

用div+css如何让字靠右对齐,第1张

可以用“text-align:right”让文字靠右对齐。

1、新建html文档,在body标签中添加div标签,为div设置一个id,这里以“demo”为例:

2、在div标签中添加文字,然后为div标签设置宽高和边框,这时默认情况下文字靠右对齐:

3、为div添加“text-align:right”,其中“text-align”指的是文本对齐方式,“right”指的是靠右对齐,这时网页中文字将会靠右对齐:

第一步,点击文本框,在属性栏中填写你需要的文字;见下图

第二步,为文本框文字添加css属性,点击文本框,可以在css面板中直接选择颜色或填写值;(可以在网页中直接定义css,也可以新建一个css文件)

显示效果

如果要实现鼠标点击灰色文字自动消失,需要使用js编程,可以在网上找js或jquery效果代码添加进页面内。

附上具体代码:

<!DOCTYPE html><html><head><style>.txt{    color:#ccc}.focus{    color:#333}</style><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script><script>$(document).ready(function(){    var str="我是灰色文字"    $(".txt").val(str)         $(".txt").focus(function(){        var v=this.value        if(v == str){            $(this).val("").addClass("focus")        }    }).blur(function(){        var v=this.value        if(v == ""){            $(this).val(str).removeClass("focus")                     }    })})</script></head><body><input class="txt" type="text" value="我是灰色文字"/></body></html>

需要准备的材料分别有:电脑、浏览器、html编辑器。

1、首先,打开html编辑器,新建html文件,例如:index.html。

2、在index.html中的<style>标签中,输入css代码:

div {position: relativeborder: 1px solid greenwidth: 200pxheight: 80px}

span {position: absolutebottom: 0}

3、浏览器运行index.html页面,此时成功实现了“文本”2个文字放到绿色div框的左下角。