[英](CSS) stretching div but content in the center
For example: I have a div with a height of, let's say, 100px. 例如:我有一个div,高度为100px。 In that div is link ( a
-tag). 在该专区链接( a
-标签)。
My CSS: 我的CSS:
a:hover, a:focus {
background-color: gray;
}
That means, I want the background of the link beign gray when it is focused. 这就是说,我希望链接的背景在聚焦时呈灰色。
But stop, it's not that easy. 但是停下来,这并不容易。 I want also: 我也要:
I know I can do these to things with display: flex;
我知道我可以通过display: flex;
来做这些事情display: flex;
in the div, but not at the same time. 在div中,但不能同时显示。 (For stretching I need align-items: stretch;
and for vertically centering I need align-items: center;
. Obviously I can't use them both. (对于拉伸,我需要align-items: stretch;
对于垂直居中,我需要align-items: center;
;。显然,我不能同时使用它们。
You can make the link 100% height and vertically centered with flex
, height
, and align-items
您可以将链接设置为100%高度,并使用flex
, height
和align-items
垂直居中
div { height: 50vh; } a { display: inline-flex; background: #eee; height: 100%; align-items: center; } a:hover, a:focus { background-color: gray; }
<div> <a href="#">link</a> </div>
If your div is, let's say, 100px then all you need to do is set your nested a element to display: inline-block and set its line-height to that of div, which is 100px. 如果您的div是100px,那么您需要做的就是将嵌套元素设置为显示:inline-block并将其line-height设置为div的line-height,即100px。
You can try something like this. 您可以尝试这样。
div { height: 100px; border: thin solid grey; } div a { display: inline-block; line-height: 100px; background-color: silver; color: white; text-decoration: none; } div a:hover { background-color: grey; }
<div><a href="https://google.com">Clicl Me!</a></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.