如何将div中的按钮或文本水平居中?

时间:2016-08-03 06:41:08

标签: html css

我有这个HTML,它是使用display:table-cell的网格的一部分。 div的宽度比内容宽:

<div>123</div>
<div><button>1</button></div>

如何在较大的div中输入文本和按钮?

2 个答案:

答案 0 :(得分:2)

flex-box是最佳解决方案

.container{
	display: flex;
	align-items: center;
	justify-content: center; 
	flex-direction: column;
	height: 100vh;
	background: brown;
}
<div class="container">
	<p>Text at center</p>
	<button>button</button>
</div>

如果您不想要水平中心,可以移除align-items: center,并且可以移除您不想要垂直中心的justify-content: center

答案 1 :(得分:0)

public int getPartition(K2 key, V2 value,int numReduceTasks) {
  return (key.hashCode() & Integer.MAX_VALUE) % numReduceTasks;
}
.centeredFlex {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 600px; /* whatever you want... */
  width: 100%;   /* whatever you want... */
}

看一下flexbox:http://www.w3schools.com/css/css3_flexbox.asp