CSS:在div内放置符号。无法删除空间,位置在右上角

时间:2019-01-28 11:38:53

标签: html css

我有几个嵌套的div,最后一个div包含一个十字符号,该符号应该位于右上角。但是,它在div内水平居中,并且有页边距:https://codepen.io/anon/pen/QYEEjg

html

<div class="content">
  <div class="user">
    <div class="headline">
        <div class="username">some username
            <div class='cross'>
                <span class='cross-symbol'>
                    &times;              
                </span>
            </div>
        </div>
    <div>
  </div>
</div>

css

.content  {
    align: center;
    width: 660px;

    background-color: #c2f0c2;
    border: 2px solid black;    
}

.user  {
    text-align: center;
    width: 200px; 

    background-color: gray;
    margin: 8px;
    border: 1px solid gray;

    display: inline-block;

}

.headline {
    background-color: silver;
    border: 1px solid black;

    position: relative;
}

.username  {
    background-color: orange;
}

.cross  { 
    font-size: 28px; 
    color: #d00; 
    font-weight: bold; 
    font-family: helvetica,arial;       

    cursor: pointer;

    top: 0px;
    right: 0px;

    outline: 1px solid black;
    width: 28px;
    height: 25px;
    padding: 0px;

    position: absolute; 

} 

.cross-symbol  {

    top: 0px;
    margin: 0px;
    padding: 0px;
    background-color: green;
vertical-align: bottom;
}

enter image description here

我尝试将paddingmargin设置为0,但结果很少。任何帮助都将受到欢迎。

2 个答案:

答案 0 :(得分:1)

position:absolute添加到.cross-symbol,我添加了line-height

.content  {
	align: center;
	width: 660px;

	background-color: #c2f0c2;
	border: 2px solid black;

	
}

.user  {
	text-align: center;
	width: 200px; 

	background-color: gray;
	margin: 8px;
	border: 1px solid gray;

	display: inline-block;
	
}

.headline {
	background-color: silver;
	border: 1px solid black;

	position: relative;
}

.username  {
	background-color: orange;
}


.cross  { 
	font-size: 28px; 
	color: #d00; 
	font-weight: bold; 
	font-family: helvetica,arial; 		

	
	cursor: pointer;

	
	top: 0px;
	right: 0px;

	outline: 1px solid black;
	width: 28px;
	height: 25px;
	padding: 0px;

	position: absolute;	
	
} 

.cross-symbol  {
	position:absolute;
	top: 0px;
	margin: 0px;
	padding: 0px;
  right:0px;
  background:green;
  line-height:16px;
}
<div class="content">
  <div class="user">
    <div class="headline">
        <div class="username">some username
            <div class='cross'>
                <span class='cross-symbol'>
                    &times;              
                </span>
            </div>
        </div>
    <div>
  </div>
</div>

答案 1 :(得分:1)

请检查这是否是您要查找的内容。我已经更改了html并添加了一些CSS。我将描述这是否正确。谢谢

.content  {
	align: center;
	width: 660px;
	background-color: #c2f0c2;
	border: 2px solid black;

	
}

.user  {
	text-align: center;
	width: 200px; 

	background-color: gray;
	margin: 8px;
	border: 1px solid gray;

	display: inline-block;
	
}

.headline {
	background-color: silver;
	border: 1px solid black;

	position: relative;
}

.username  {
	background-color: orange;
}


.cross  { 
/* 	font-size: 28px;  */
    color: #d00;
    font-weight: bold;
    font-family: helvetica,arial;
    cursor: pointer;
    top: 50%;
    right: 0px;
    outline: 1px solid black;
    width: 22px;
    height: 20px;
    /* padding: 0px; */
    position: absolute;
    transform: translateY(-50%);	
	
} 

.cross-symbol  {
	
	top: 0px;
	margin: 0px;
	padding: 0px;
	background-color: green;
vertical-align: bottom;
}

.username{
  position:realtive;
}

.username p{margin:0}
<div class="content">
  <div class="user">
    <div class="headline">
        <div class="username">
          <p>some username</p>
            <div class='cross'>
                    &times;              
            </div>
        </div>
    <div>
  </div>
</div>