我有一个这样的 div,我希望文本和图像水平对齐,其中 div 左侧和 div 右侧的空间相等。这是我拥有的当前代码,尽管它绝对不是最佳的:
.window{
position:absolute;
width: 400px;
height: 300px;
background-color:#424242;
}
.content{
padding-top:50px;
width: 50%;
position:relative;
vertical-align: top;
margin: 0 auto;
display:flex;
}
img {
height: 32px;
width: 32px;
min-width: 32px;
min-height: 32px;
position: relative;
float: left;
}
.textcontent{
margin-top: auto;
margin-bottom: auto;
margin-left: 16px;
display: block;
line-height: 182%;
}
.text{
font-size: 14px;
}Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="window">
<div class="content">
<img src="https://cdn4.iconfinder.com/data/icons/family-and-home-collection/110/Icon__grandfather-32.png" />
<div class="textcontent">
<div class="text"> Some Centered Text. </div>
<div class="text"> …Run Code Online (Sandbox Code Playgroud)我正在为我的 MMO RPG 游戏制作我的物品法典……我完成了数据加载器的设置,该加载器设置了包含我的物品数据的表格。
一切看起来都很好,除了我实例化我的项目信息的表格居中。
因为它是动态的,它的居中是基于单元格中文本/数据的长度,这导致了下图中这种丑陋的效果。
我试图找到一种方法,通过连接两侧的边框将表格居中,如图所示,红色边框是它应该放置的位置,但不是。
我可以达到这种效果的最佳方法是什么?
在此处查找实时 URL 版本。
我想做的只是使用 CSS 将 div 居中。我无法理解它。它根本不起作用。我使用了以下代码:
<style type="text/css">
<!--
div.test {
width: 300px;
margin-right: auto;
margin-left: auto;
border: 1px solid black;
}
-->
</style>
<div class='test'>
hello
</div>
Run Code Online (Sandbox Code Playgroud)
这在 IE 上不起作用,但在其他任何东西上都起作用(包括我的 iPhone,这让它非常令人沮丧)。div 只是保持在右侧对齐。
现在来说说(简单!)解决方案,这花费了我很多时间,而且我无法在搜索引擎中搜索问题找到该解决方案。只需添加文档类型即可:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
Run Code Online (Sandbox Code Playgroud)
为什么解决了这个问题?
这必须是可能的!
我已经阅读了本网站上发布的数十个问题以及盲目搜索Google.我在容器内有一个容器DIV和两个DIV.我希望容器内的所有东西都居中,但容器内的两个DIV彼此相邻.
我看到实现这一目标的唯一方法是将两个DIV浮动到容器内.但是,如果我漂浮他们,那么我不能让他们居中.并且通过居中我的意思是我希望左侧DIV的右边界接触(或几乎触碰)右侧DIV的左边界.
有人请教育我!我发誓,我会回到桌子!我没有发布任何代码,因为我没有任何代码
感谢任何能帮助我的人!