首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅对于文本,将文本居中与背景色对齐

问仅对于文本,将文本居中与背景色对齐
EN

Stack Overflow用户
提问于 2018-04-03 03:01:29
回答 3查看 5.8K关注 0票数 0

如何将文本与屏幕中心对齐,并使链接的背景为蓝色。而无需更改HTML代码和仅更改CSS代码。我正在使用内联显示和对齐文本:居中,但它不工作。在链接部分,我想将它对齐到中心顶部,并且应该是固定的。

https://notepad.pw/css1234 (用于css文件)

https://notepad.pw/html1234 (用于html文件)

EN

回答 3

Stack Overflow用户

发布于 2018-04-03 12:55:21

你想要什么

  • 将文本与屏幕中心对齐,链接的背景为蓝色
  • 仅更改CSS代码
  • 链接应居中顶部对齐并固定

修改

在body{}

  • Remove width: 100%;

中添加

  • text-align: center;

解释

  • 这将在body
  • By的中心对齐width: 100%,背景颜色将只适合内容(本例中的链接)。

请运行下面的代码,看看这是否是您所要求的。

​

代码语言:javascript
复制
/* This is a partial blocks.css, you need to add more rules
   to this file to complete the assignment.  Do not add any CSS
   styling to the original index.htm / start.htm */

body {
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
	text-align: center;
}

/* Here is one of many  CSS rules you will need... */
#three{
    position: fixed;
    border: 5px solid #fea405;
    margin: 5px;
    padding: 5px;
    width: 25%;
    top: 0;
}
#two{
	position: fixed;
    border: 5px solid #048103;
    margin: 5px;
    padding: 5px;
    width: 25%;
    top: 0;
    right: 0;
}
#four{
	position: fixed;
	border: 5px solid #ffff00;
	margin: 5px;
	padding: 5px;
	width: 25%;
	bottom: 0;
}
#one{
	position: fixed;
	border: 5px solid #0201ff;
	margin: 5px;
	padding: 5px;
	width: 25%;
	bottom: 0;
	right: 0;
}
#link
{	
	text-align: center;
	color: white;
	background-color:#0201ff;
	display: inline;
}
代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
  <title>Blocks</title>
  <!-- Do not change this file - add your CSS styling 
       rules to the blocks.css file included below -->
  <link type="text/css" rel="stylesheet" href="style.css">
</head>
<body>
<div id="one">
Turns out you have a really fun time if you 
go to work every day and focus on being 
silly and funny and happy! - Hannah Murray
</div>
<div id="two">
All you need in this life is ignorance and confidence, 
and then success is sure. - Mark Twain
</div>
<div id="three">
Well, if crime fighters fight crime and fire fighters 
fight fire, what do freedom fighters fight? They never 
mention that part to us, do they? - George Carlin
</div>
<div id="four">
Great minds discuss ideas; average minds discuss events;
small minds discuss people. - Eleanor Roosevelt
</div>
<p id="link">
<a href="https://www.brainyquote.com/" target="_blank">
Brainy Quote
</a>
</p>
</body>
</html>

​

票数 2
EN

Stack Overflow用户

发布于 2018-04-03 03:20:45

css:

代码语言:javascript
复制
<style>
div
{
text-align:center; 
background-color:red;
}

.linkFrame
{
margin: 0 auto;
background-color: blue;
width:20%;
height: 100%;
}
.linkFrame a
{
color: white;
}
</style>

html:

代码语言:javascript
复制
<div><div class="linkFrame"><a href="#">YourText</a></div></div>

Tryit编辑器链接:https://www.w3schools.com/code/tryit.asp?filename=FPYX1DG3O9GQ

票数 0
EN

Stack Overflow用户

发布于 2018-04-03 14:11:32

你必须用下面的代码替换你为#link编写的小css代码:

代码语言:javascript
复制
#link{
   text-align: center;
}
#link a{
   background-color:#0201ff;
   color: white;
}

Refer the code with ouput

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49617163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档