导航:首页 > 编程语言 > 怎么用js在div添加文字居中显示图片

怎么用js在div添加文字居中显示图片

发布时间:2023-03-31 11:12:53

『壹』 怎样用css只让div中的图片居中

1、打开在线写前端代码网站jsrun或者jsfiddle。

『贰』 在一个DIV中怎样让文字跟图片居中对齐

兄弟,我给你写了个示例,把下面代码复制过去看看吧:
DIV中怎样让文字跟图片居中对齐的键在于这个属性 vertical-align:middle;
------------------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C/氏宴/DTD XHTML 1.0 Transitional//EN" "清核羡http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>无标题页</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<答拍style>
body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, th, td { margin: 0; padding: 0; font-size:12px; }
.clearfix:after { content:'\0020'; display:block; height:0; clear:both; }
.clearfix { *zoom:1;
}
.test { height:100px; width:200px; line-height:100px; border:solid 1px #000; margin:100px auto; padding:0 30px; }
.test img{border:solid 1px #000; vertical-align:middle;}
</style>
</head>
<body>
<div class="test">
这个对齐了<img src="https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/logo-.gif" />
</div>
</body>
</html>

『叁』 怎样把字放在图片的右边,居中用div+css

不用CSS,直接用align="absmiddle"就可以了。

HTML:

<div>
袜亏谨<imgsrc="https://gss0..com/9vo3dSag_xI4khGko9WTAnF6hhy//pic/item/.jpg"align="absmiddle"/>空粗
Hello~I'mhere1
</div>

效告基果:

『肆』 js如何让文字居中显示

1、打开编辑器,新建test.html,用于学习今天的内容。

『伍』 怎么让DIV永远居中在浏览器的中央,用CSS或JS或是两者结合都行,高手请进呀!给具体例子,分少可以加

做个变换菜单的DIV居中问题搞了我几个小时,在网上搜到的都不是很理想,终于测试出来一个方法来,这个方法绝对有效,当你真的拿DIV没办法的时正穗候可以试试.

<style type="text/css">
#center {
position:absolute;
width:300px;
height:60px;
left:50%;
top:50%;
z-index:1;
background-color:#000;
color:fff;
margin-left:-150px;
margin-top:-32px
}
</style>
</head>
<body>
<div id="center">绝对在中间</div>
</body>

============以下是网上其他的一些方法,供参考==============

在div+css布局中,居中问题是每个初学者都会碰到的问题:

1.水平居中
举例代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>demo</title>举正卜
<style type="text/css">
body{
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
margin: 0;
padding: 0;/*--for opera--*/
text-align: center;/*--for IE5.0--*/
}

#layout{
margin: 0 auto;/*--居中 --*/
width: 760px;/*--宽度必须的--*/
text-align: left;
background: #F6F6F6;
}

pre{
padding: 15px;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>

<body>
<div id="layout"><pre>
body{
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
margin: 0;
padding: 0;/*--for opera--*/
text-align: center;/*--for IE5.0--*/
}

#layout{
margin: 0 auto;/*--居中 --*/
width: 760px;/*--宽度必须的--*/
text-align: left;
background: #F6F6F6;
}

pre{
padding: 15px;
}
</pre>
</div>
</body>
</html>

2.另外,经典论坛阿捷的清芹例子:

主要的样式定义如下:
body {TEXT-ALIGN: center;}
#center {
MARGIN-RIGHT: auto;
MARGIN-LEFT: auto;
}

说明:
首先在父级元素定义TEXT-ALIGN: center;这个的意思就是在父级元素内的内容居中;对于IE这样设定就已经可以了。但在mozilla中不能居中。解决办法就是在子元素定义时候设定时再加上“MARGIN-RIGHT: auto;MARGIN-LEFT: auto; ”

完整举例代码如下:

<html>
<head>
<style>
body{TEXT-ALIGN: center;}
#center{
MARGIN-RIGHT: auto;
MARGIN-LEFT: auto;
height:200px;
background:#F00;
width:400px;
}
</style>
</head>
<body>
<div id="center"></div>
</body></html>

3.垂直居中

若BOX的宽度和高度已知:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<style type="text/css">
body{
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
margin: 0;
padding: 0;/*--for opera--*/
}

#layout{
position: absolute;/*--绝对定位--*/
top: 50%;
left: 50%;
margin-top: -240px;/*--div高度的一半--*/
margin-left: -320px;/*--div宽度的一半--*/
width: 640px;
height: 480px;
background: #ECECEC;
}

pre{
padding: 15px;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>

<body>
<div id="layout"><pre> body{
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
margin: 0;
padding: 0;/*--for opera--*/
}

#layout{
position: absolute;/*--绝对定位--*/
top: 50%;
left: 50%;
margin-top: -240px;/*--div高度的一半--*/
margin-left: -320px;/*--div宽度的一半--*/
width: 640px;
height: 480px;
background: #ECECEC;
}

pre{
padding: 15px;
}
</pre></div>
</body>
</html>

解释:如果把屏幕中点设为坐标原点(0 0),X的正方向是屏幕的右边,Y的正方向屏幕的上边。那么在已知BOX宽度和高度的情况下,设BOX的属性为绝对定位,top:50% left:50%,这样的话,BOX的左上角就在坐标原点(0 0), 设margin-left:-320px(宽度的一半), 左上角的坐标(-320 0),再设margin-top:-240px(高度的一半),左上角的坐标现在是(-320 240),相对原点来说, BOX是完全居中了,四个顶点的值分比别是(320 240)Ⅰ, (-320 240)Ⅱ,(-320 -240)Ⅲ ,(320 -240)Ⅳ。

适用于首页带语言选择版本或者欢迎页。

单行文字可采用行高来实现垂直居中,或者用padding来调整。

4.另一个方法:
<div style="border: 1 solid red;position: absolute;top:expression((this.parentElement.offsetHeight-this.offsetHeight)/2);left:expression((this.parentElement.offsetWidth-this.offsetWidth)/2);">我站在中央了<br>center</div>
</div>

<div style="border: 1 solid #C0C0C0;"><br><br><br><br><br><br><br><br>
<div style="border: 1 solid red;position: absolute;top:expression((this.parentElement.offsetHeight-this.offsetHeight)/2);left:expression((this.parentElement.offsetWidth-this.offsetWidth)/2);">我站在中央了<br>center</div>

『陆』 通过CSS+DIV怎么将文字写在图片上方

HTML图片和文字是并列显示的。如下:

代码总汇

HTML

<div class="img-group"> <img src="img/snow.png">
<div class="img-tip">我是雪豹</div></div>

CSS

.img-group { position: relative; display: inline-block;
}
.img-tip { position: absolute; bottom: 0; background: #333; color: #fff; opacity: 0.6; display: none;
}
.img-group:hover .img-tip { display: block; width: 100%; text-align: center;
}

『柒』 想在一个div里面添加图片,用js怎么写啊

1、猛档新建一个HTML文件,保存为test.html,用于编写代码实现拖放功能 。

『捌』 css html 如何让div里边的图片和文字同时上下居中

1、首先先进行文本框的插入,在word文档编辑界面上,单击上方工具栏中的“插入”按钮,包括文本框,所有的插入选项都在这里。

『玖』 怎样在div中添加文字

div中添加文字的具体操作步骤如下:

1、打开Dreamweaver软件,新建一个html文件。

『拾』 js使用div内容居中

1、准备好一个空的html结构的文档。

阅读全文

与怎么用js在div添加文字居中显示图片相关的资料

热点内容
士官秦勇被小舅子取精 浏览:402
不死帝境坐哪一桌 浏览:427
都市绝世战神叶凌天 浏览:532
放屁影片 浏览:969
主角可以往返明末与现代 浏览:40
新三国和新三国大电影版哪个更值得看 浏览:392
能看黑片的网站 浏览:455
提取码:9981 浏览:525
重装系统后开机密码 浏览:83
承德电影院今日影讯 浏览:302
传说中的乡巴佬女切腹6部合集 浏览:10
香港打真军电影1001香港打真军电影 浏览:730
类似于天才枪手 浏览:41
韩国李采妮电影有哪些 浏览:255
天堂网站在线免费看 浏览:639
ck电影网免费 浏览:432
韩国电影帮邻居搬圣诞树的片段 浏览:467
如饥似渴电影完整手机观看 浏览:171
母乳汁理沦片 浏览:796

友情链接