首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CSS文本元素破坏div定位

CSS文本元素破坏div定位
EN

Stack Overflow用户
提问于 2018-01-12 14:10:03
回答 3查看 79关注 0票数 1

我正在处理网页,并在添加文本之前将其分成几个div。

但是当我在其中一个div中放入一些文本时,所有div的位置都会折叠。

我已经编写了简单的代码来显示问题。

代码语言:javascript
复制
html{
  height: 100%;
}

body{
  height: 100%;
}

#wrapper{
  height: 100%;
}

.box{
  display: inline-block;
  position: relative;
  background: black;
}

#box-1{
  top: 5%;
  left: 5%;

  height: 35%;
  width: 35%;
}

#box-2{
  top: 5%;
  left: 10%;

  height: 35%;
  width: 50%;
}

#box-3{
  top: 10%;
  left: 5%;

  height: 50%;
  width: 35%;
		}

#box-4{
  top: 10%;
  left: 10%;

  height: 50%;
  width: 50%;
}
代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
	<title>Issue</title>
</head>
<body>
	<div id="wrapper">
		<div id="box-1" class="box">
			
		</div>
		<div id="box-2" class="box">
			
		</div>
		<div id="box-3" class="box">
			
		</div>
		<div id="box-4" class="box">
			
		</div>
	</div>
</body>
</html>

正如你所看到的,它划分得很好,没有问题(至少在我看来是这样)。

但是一旦你在div中添加了文本,它就会崩溃,我不明白为什么。

代码语言:javascript
复制
html{
  height: 100%;
}

body{
  height: 100%;
}

#wrapper{
  height: 100%;
}

.box{
  display: inline-block;
  position: relative;
  background: black;
}

#box-1{
  top: 5%;
  left: 5%;

  height: 35%;
  width: 35%;
}

#box-2{
  top: 5%;
  left: 10%;

  height: 35%;
  width: 50%;
}

#box-3{
  top: 10%;
  left: 5%;

  height: 50%;
  width: 35%;
}

#box-4{
  top: 10%;
  left: 10%;

  height: 50%;
  width: 50%;
}

p{
  color: white;
}
代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
	<div id="wrapper">
		<div id="box-1" class="box">
			<p>Hi</p>
		</div>
		<div id="box-2" class="box">
			
		</div>
		<div id="box-3" class="box">
			
		</div>
		<div id="box-4" class="box">
			
		</div>
	</div>
</body>
</html>

无论p标签是否存在,它都会折叠。

Div用%unit定位,并且是相对的。

因此,在我看来,它应该只与它的父母和兄弟相关。

我应该重新分配所有的绝对位置吗?

如果你知道为什么,或者有解决方案,请教我。谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-01-12 14:55:25

默认情况下,将vertical-align: top;放到.box中,它有vertical-align: baseline;

代码语言:javascript
复制
.box {
    display: inline-block;
    vertical-align: top;
    position: relative;
    background: black;
}
票数 2
EN

Stack Overflow用户

发布于 2018-01-12 15:07:36

Ismail Farooq答案之外的另一种选择

代码语言:javascript
复制
.box {
    display: block;
    position: relative;
    background: black;
    float: left;
}
票数 0
EN

Stack Overflow用户

发布于 2018-01-12 15:29:41

我正在使用flexbox链接是Flexbox here

代码语言:javascript
复制
<html>
<style>
html{
  height: 100%;
}

body{
  height: 100%;
}

#wrapper{
  height: 100vh;
   display: flex;
    flex-direction: row;
    flex-wrap: wrap;
}

.box{
    display: flex;
    background: black;
    margin: 0.5%;
}

#box-1{
    height: 50%;
    width: 49%;
}

#box-2{
    height: 50%;
    width: 49%;
}

#box-3{
    height: 50%;
    width: 49%;
}

#box-4{
    height: 50%;
    width: 49%;
}

p{
  color: white;
}
</style>
<body>
<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
	<div id="wrapper">
		<div id="box-1" class="box">
			<p>Hi</p>
		</div>
		<div id="box-2" class="box">
			
		</div>
		<div id="box-3" class="box">
			
		</div>
		<div id="box-4" class="box">
			
		</div>
	</div>
</body>
</html>
</body>
</html>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48220518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档