简体   繁体   English

如何在锚标签内居中放置文本?

[英]How to center the text inside the anchor tag?

I have arranged the anchor tag with text with borders. 我已经用带有边框的文本排列了锚标签。 I want to make the text to center inside the block. 我想使文本在块内居中。 I have tried padding, margin left, float but nothing is working. 我已经尝试过填充,左边距剩余,浮动,但是没有任何效果。

 body { background-color:black; background-repeat: no-repeat; background-size: 100% 100%; } .title{ height: 17%; width: 100%; margin: 0%; text-align: center; font-size: 2rem; color: white; } .subjects{ color: white; width: 28%; height: 7%; border: 2px solid white; font-size: 2rem; border-radius:20px; margin-top: 86px; margin-left: 8px; text-align:center; vertical-align: middle; } .maths{ margin-left: 24%; margin-top: 41px; } .maths a{ text-decoration: none; color: white; border: 2px solid white; padding: 4px; padding-right: 140px; font-size: 2rem; border-radius: 20px; cursor: pointer; vertical-align:middle; display: inline-block; } .physics{ margin-left: 24%; margin-top: 41px; } .physics a{ text-decoration: none; color: white; border: 2px solid white; padding: 4px 103px; padding-right: 206px; font-size: 2rem; border-radius: 20px; cursor: pointer; vertical-align:middle; display: inline-block; } .chemistry{ margin-left: 24%; margin-top: 41px; } .chemistry a{ text-decoration: none; color: white; border: 2px solid white; padding: 4px; padding-right: 169px; font-size: 2rem; border-radius: 20px; cursor: pointer; vertical-align:middle; display: inline-block; } 
 <div class="title"> <h1>Syllabus</h1> </div> <div class="subjectsList"> <div class="subjects"> Syllabus <span class="triangle"></span> </div> <div class="maths"> <a href="mathsinfo.html" target="_blank"> <!--<img src="images/maths.jpg" height="50px" width="50px" alt="Mathematics Icon">--> Mathematics</a> </div> <div class="physics"> <a href="physicsinfo.html" target="_blank">Physics</a> </div> <div class="chemistry"> <a href="chemistryinfo.html" target="_blank">Chemistry</a> </div> </div> 

that physics text must be at middle.Can anyone help me out to solve this problem. 物理课必须在中间。任何人都可以帮我解决这个问题。

Just change the padding (left and right padding to half of what you had for right padding): 只需更改填充(左和右填充为右填充的一半)即可:

 .physics{ margin-left: 24%; margin-top: 41px; } .physics a{ text-decoration: none; color: black; border: 2px solid black; padding: 4px 103px; font-size: 2rem; border-radius: 20px; cursor: pointer; vertical-align:middle; display: inline-block; } 
 <div class="physics"> <a href="physicsinfo.html" target="_blank">Physics</a> </div> 

remove padding-right:206px; 删除padding-right:206px; add text-align:center 添加text-align:center

.physics a{
                text-decoration: none;
                color: black;
                border: 2px solid black;
                padding: 4px 0px;
                font-size: 2rem;
                border-radius: 20px;
                cursor: pointer;
                vertical-align:middle;
                display: inline-block;    
                text-align:center;
            }

remove margin-left from .physicsc and padding-right from .physics a and set .physicsc移除margin-left并从.physics a移除padding-right并设置

.physics {       
   text-align: center;
}
.physics a {
  width:30%; // or anything you need
}

Also check out centering guide . 另请查阅对中指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM