html - 将链接对齐到 div 的底部并将其居中

标签 html css

我正在尝试将链接固定在 div 的底部中心并使其居中。

到目前为止,我已经想出了这个: http://jsfiddle.net/r494Lx0r/2/

div.container {
position: relative;
height: 110px;
width: 120px;
border: dashed 1px red;
}

div.container div.text {
position: absolute;
bottom: 0px;
border: solid 1px black;
}

现在我该如何让它居中呢?我尝试将 text-align:center;margin:0 auto; 添加到容器中,但它们都没有任何作用。

有人知道怎么做吗?

最佳答案

更新text-algin: center 添加到父级以使 anchor 居中并设置 border: solid 1px black; 到您的 anchor :

div.container {
	position: relative;
	height: 110px;
	width: 120px;
	border: dashed 1px red;
}

div.container div.text {
	position: absolute;
	bottom: 0px;
	right: 0;
    left: 0;
    text-align: center;
}

a{border: solid 1px black;}
<div class="container">
	<div class="text">
		<a href="#">Google.com</a>
	</div>

</div>

添加 Width: 100%text-align: center

div.container {
	position: relative;
	height: 110px;
	width: 120px;
	border: dashed 1px red;
}

div.container div.text {
	position: absolute;
	bottom: 0px;
    text-align: center;
    width:100%;
	border: solid 1px black;
}
<div class="container">
	<div class="text">
		<a href="#">Google.com</a>
	</div>

</div>

left: 0;, right: 0;text-align: center;

div.container {
	position: relative;
	height: 110px;
	width: 120px;
	border: dashed 1px red;
}

div.container div.text {
	position: absolute;
	bottom: 0px;
    left: 0;
    right: 0;
    text-align: center;
	border: solid 1px black;
}
<div class="container">
	<div class="text">
		<a href="#">Google.com</a>
	</div>

</div>

or you can combine `margin-left: 50%;` and `transform: translate(-50%)`

div.container {
	position: relative;
	height: 110px;
	width: 120px;
	border: dashed 1px red
}

div.container div.text {
	position: absolute;
	bottom: 0px;
	border: solid 1px black;
    margin-left: 50%;
    -webkit-transform: translate(-50%);
    -moz-transform: translate(-50%);
    transform: translate(-50%)
}
<div class="container">
	<div class="text">
		<a href="#">Google.com</a>
	</div>

</div>

关于html - 将链接对齐到 div 的底部并将其居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27198088/

相关文章:

javascript - 你如何在 php 中使用 echo 打印 html 中的文本?

javascript - 不要使用 Jquery 获取适当的 div

css - 使用相同的 css 属性设置按钮和 anchor 标记不起作用

html - 我不需要对旧版浏览器的支持,那么 IE11 是否需要 modernizr?

jquery - 当前链接不会保持突出显示

javascript - 通过 Js 检索 Sharepoint 列表

javascript - 使用 JavaScript 切换的 Canvas 外菜单是一种好习惯吗?

html - Div 背景阴影适合自定义 CSS 形状

css - css 中的手持媒体​​类型与支持移动站点的另一个域或子域?

html - 出现时如何使用css动画从右上角滑入框