javascript - 用javascript附加后如何在div中对齐img?
问题描述
使用 javascript 将图像附加到 div 后,我想在其容器内对齐图像,但每次我top
在 div 中使用时,img {}
所有图像都会捕捉到容器的顶部,重叠。我创建了一个codepen:here。
由于主网站是在 wordpress 中构建的,因此我无法将主 div 设置为 flex 框,因为图标写在其 HTML 文件中的文本之前。他们设置它的方式是文本与中心对齐,图标向右浮动。
<div>
<span><i class="fa fa-angle-down"></i></span>
<a href="#">Text</a>
</div>
HTML:
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css">
</head>
<body>
<div id="1">
<a href="#">Nummer 1</a>
<span><i class="fa fa-angle-down"></i></span>
</div>
<div id="2">
<a href="#">Nummer 2</a>
<span><i class="fa fa-angle-down"></i></span>
</div>
</body>
</html>
CSS:
div {
background-color: red;
border: 5px solid white;
margin-bottom: 20px;
text-align: center;
padding: 20px;
}
div a {
text-decoration: none;
color: white;
font-size: 20px;
font-family: 'Arial', sans-serif;
text-transform: uppercase;
}
div img {
position: absolute;
left: 30px;
}
span {
color: white;
float: right;
}
JS:
window.onload=function(){
var elem = document.createElement("img");
elem.src = "https://lisaschumann.github.io/resources/images/Icon_Test.png";
elem.height = 40;
elem.width = 40;
elem.alt = "icon";
document.getElementById("1").appendChild(elem);
var elemTwo = document.createElement("img");
elemTwo.src = "https://lisaschumann.github.io/resources/images/Icon_Test.png";
elemTwo.height = 40;
elemTwo.width = 40;
elemTwo.style = "background-color: blue";
elemTwo.alt = "icon";
document.getElementById("2").appendChild(elemTwo);
}
解决方案
绝对位置适用于相对定位的父元素。所以你必须使包含的 div 相对定位:
position: relative;
您也可以使用 javascript 动态执行此操作:
elem.parentElement.style.position = 'relative';
elemTwo.parentElement.style.position = 'relative';
window.onload=function(){
var elem = document.createElement("img");
elem.src = "https://lisaschumann.github.io/resources/images/Icon_Test.png";
elem.height = 40;
elem.width = 40;
elem.alt = "icon";
document.getElementById("1").appendChild(elem);
var elemTwo = document.createElement("img");
elemTwo.src = "https://lisaschumann.github.io/resources/images/Icon_Test.png";
elemTwo.height = 40;
elemTwo.width = 40;
elemTwo.style = "background-color: blue";
elemTwo.alt = "icon";
document.getElementById("2").appendChild(elemTwo);
}
div {
background-color: red;
border: 5px solid white;
margin-bottom: 20px;
text-align: center;
padding: 20px;
position: relative;
}
div a {
text-decoration: none;
color: white;
font-size: 20px;
font-family: 'Arial', sans-serif;
text-transform: uppercase;
}
div img {
position: absolute;
top: 12px;
left: 30px;
}
span {
color: white;
float: right;
}
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css">
</head>
<body>
<div id="1">
<a href="#">Nummer 1</a>
<span><i class="fa fa-angle-down"></i></span>
</div>
<div id="2">
<a href="#">Nummer 2</a>
<span><i class="fa fa-angle-down"></i></span>
</div>
</body>
</html>
推荐阅读
- ios - 检测图像中的摩尔纹 - iOS Swift
- install4j - 为什么使用 Install4j 6 构建的 macOS 安装程序会在 JRE 10 中引发运行时异常?
- php - PHP,mysqli查询已执行但未更新数据库中的数据
- adal - AuthenticationContextIntegratedAuthExtensions.AcquireTokenAsync 现在在 4.0 预览版上失败,未知用户
- reactjs - 将 React 应用程序集成到 Struts 应用程序
- blockchain - 区块链中的交易验证
- c# - 更新服务器缓存,即使服务器正在获取任何请求 asp.net core 2.1
- laravel-5.5 - 如何使用具有功能的laravel eloquent的where条件(进入子表)
- ios - 如何使用 Swift 删除 Firebase 上超过 30 分钟的数据?
- python - 在apply方法中使用pandas中之前的计算行