html-如何在CSS中将锚点元素居中?
我只想将锚点水平放置在屏幕中间,该怎么办?
foreyez asked 2020-01-18T12:39:25Z
13个解决方案
74 votes
将text-align css属性添加到其父样式属性
例如:
example
或使用课程(推荐)
example
.my-class {
text-align: center;
}
参见以下工作示例:
text-align
text-align
问:为什么text-align样式没有应用到inline元素而不是block?
答:text-align样式描述了inline内容如何在block元素内对齐。 在这种情况下,div是一个块元素,它的内联内容是a。为进一步探讨这一点,请考虑将text-align样式应用于a元素(伴随更多文本)的意义不大
即使这里有换行符,text-align的所有内容都是内联内容,因此会产生类似以下内容:
纯文本是内联内容。 示例跨度也是内联内容
如果将text-align属性应用于这种情况,那么在这种情况下如何显示“示例”并没有多大意义。
JaredMcAteer answered 2020-01-18T12:41:18Z
18 votes
这样写:
的CSS
.parent{
text-align:center
}
sandeep answered 2020-01-18T12:41:42Z
13 votes
具有不同用途的两个选项:
HTML:
CSS:
.example { text-align: center; }
要么:
.example { display:block; width:100px; margin:0 auto;}
bookcasey answered 2020-01-18T12:42:15Z
3 votes
尝试环绕div并将这些样式添加到div中:
width: 100%;
text-align:center;
clem answered 2020-01-18T12:42:34Z
3 votes
尝试
margin: 0 auto;
display:table
希望能帮助到别人。
Eric K answered 2020-01-18T12:42:58Z
2 votes
Awaissoft answered 2020-01-18T12:43:14Z
1 votes
默认情况下,锚点是内联渲染的,因此请将text-align: center;设置为最近渲染为块的祖先。
Quentin answered 2020-01-18T12:43:34Z
1 votes
有很多方法。