html - 父类具有“文本对齐:中心”,但子类保持左对齐
问题描述
这里我有 HTML 的片段
<header id="showcase">
<div class="showcase-content">
<h1>The Sky Is The Limit</h1>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Tempore facere adipisci corporis molestiae voluptatum natus.</p>
<a class = " readmore"href="#what">Read more</a>
</div>
</header>
下面是它的CSS
#showcase .showcase-content{
display: flex;
flex-direction: column;
text-align: center;
justify-content: center;
height: 100%;
padding: 4rem;
/* Overlay */
background-color: rgba(0,0,0,0.5);
}
.readmore{
display: inline-block;
width: 7rem;
color:white;
background: #93cb52;
left: 50%;
}
问题是readmore类有一个被覆盖的 display:inline-block; 并且.showcase-content类有text-align:center;但readmore仍然保持左对齐。
解决方案
设置margin: auto
为按钮。您为不包含行全宽的按钮指定了特定宽度。而且你给了left not apply,因为你可以给left、right、top、bottom属性加上位置。所以,左不工作。
#showcase .showcase-content{
display: flex;
flex-direction: column;
text-align: center;
justify-content: center;
height: 100%;
padding: 4rem;
/* Overlay */
background-color: rgba(0,0,0,0.5);
}
.readmore{
display: inline-block;
width: 7rem;
color:white;
background: #93cb52;
margin: auto;
}
<header id="showcase">
<div class="showcase-content">
<h1>The Sky Is The Limit</h1>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Tempore facere adipisci corporis molestiae voluptatum natus.</p>
<a class = " readmore"href="#what">Read more</a>
</div>
</header>
推荐阅读
- javascript - onClick 反应通知未显示,但它运行该功能
- nginx - NGINX 反向代理重写 SAML 回调
- azure - Kubernetes Zookeeper 集群设置/配置 YAML
- java - 如何打印/显示 MathML 表达式?
- javascript - 如何使用jquery css框架在jquery ui中的非活动选项卡的鼠标悬停时获取背景颜色?
- google-analytics - Google 分析上的推荐渠道上缺少某些 URL
- c# - System.InvalidOperationException: '在意外的时间调用了一个方法。(来自 HRESULT 的异常:0x8000000E)'
- javascript - 如何读取当前活动用户的 firebase 数据?
- javascript - 如何从数组中生成连续的数字
- bash - 如何根据bash中的日期对数据进行排序?