HTML如何将按钮移动到右侧

13

抱歉,如果这个问题很愚蠢,因为我只是 HTML 的初学者。如何将我的按钮移到右边?我已经尝试了一些方法,但好像没有起作用。这是我的代码。

<!DOCTYPE html>
<html>
<head>
<title>Sample Dashboard</title>
<style type="text/css">

p.pos_right {
    position: relative;
    left: 20px;
}

 .button_example{
 border:1px solid #f9f68a; -webkit-border-radius: 3px; -moz-border-radius:      3px;border-radius: 3px;font-size:12px;font-family:arial, helvetica, sans-serif;   padding: 10px 10px 10px 10px; text-decoration:none; display:inline-block;text-  shadow: -1px -1px 0 rgba(0,0,0,0.3);font-weight:bold; color: #FFFFFF;
 background-color: #fcfac0; background-image: -webkit-gradient(linear, left  top, left bottom, from(#fcfac0), to(#f6f283));
 background-image: -webkit-linear-gradient(top, #fcfac0, #f6f283);
 background-image: -moz-linear-gradient(top, #fcfac0, #f6f283);
 background-image: -ms-linear-gradient(top, #fcfac0, #f6f283);
 background-image: -o-linear-gradient(top, #fcfac0, #f6f283);
 background-image: linear-gradient(to bottom, #fcfac0,  #f6f283);filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#fcfac0, endColorstr=#f6f283);
}

.button_example:hover{
 border:1px solid #f7f25f;
 background-color: #faf68f; background-image: -webkit-gradient(linear, left  top, left bottom, from(#faf68f), to(#f3ed53));
 background-image: -webkit-linear-gradient(top, #faf68f, #f3ed53);
 background-image: -moz-linear-gradient(top, #faf68f, #f3ed53);
 background-image: -ms-linear-gradient(top, #faf68f, #f3ed53);
 background-image: -o-linear-gradient(top, #faf68f, #f3ed53);
 background-image: linear-gradient(to bottom, #faf68f,  #f3ed53);filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startC     olorstr=#faf68f, endColorstr=#f3ed53);
}
</style>
</head>
<body>

<p class="pos_right">Most Number of Referrals for the month of </p>
<a class="button_example" href="#"><font color="black">UPDATE</font></a>


</body>
</html>

你试过使用属性 float: right 吗? - Pavel Pája Halbich
1
这就是教程应该被使用的地方,而不是Stack Overflow。 - Angry 84
7个回答

21
许多方法可以实现这一点,例如float
.button_example {float: right}

2
问题是,如果我只想把它向右移动一点呢? - marse

6

通过CSS的浮动属性,您可以相应地对齐元素。演示

.button_example{
    float: right;
}

3
只需将其添加至

标签中


.button_example {
  position:absolute;
  top: 10px;
  right:0;
}

这是一段代码片段。

p.pos_right {
    position: relative;
    left: 20px;
}

 .button_example {
         position: absolute;
         right: 0;
         top: 15px;
         border:1px solid #f9f68a; 
         -webkit-border-radius: 3px; 
         -moz-border-radius: 3px; 
         border-radius: 3px;
         font-size:12px;font-family:arial, helvetica, sans-serif;   
         padding: 10px 10px 10px 10px; 
         text-decoration:none; 
         display:inline-block;
         text-shadow: -1px -1px 0 rgba(0,0,0,0.3);
         font-weight:bold; 
         color: #FFFFFF;
         background-color: #fcfac0; 
         background-image: -webkit-gradient(linear, left  top, left bottom, from(#fcfac0), to(#f6f283));
         background-image: -webkit-linear-gradient(top, #fcfac0, #f6f283);
         background-image: -moz-linear-gradient(top, #fcfac0, #f6f283);
         background-image: -ms-linear-gradient(top, #fcfac0, #f6f283);
         background-image: -o-linear-gradient(top, #fcfac0, #f6f283);
         background-image: linear-gradient(to bottom, #fcfac0,  #f6f283);
         filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#fcfac0, endColorstr=#f6f283);
}

.button_example:hover{
   border:1px solid #f7f25f;
   background-color: #faf68f; background-image: -webkit-gradient(linear, left  top, left bottom, from(#faf68f), to(#f3ed53));
   background-image: -webkit-linear-gradient(top, #faf68f, #f3ed53);
   background-image: -moz-linear-gradient(top, #faf68f, #f3ed53);
   background-image: -ms-linear-gradient(top, #faf68f, #f3ed53);
   background-image: -o-linear-gradient(top, #faf68f, #f3ed53);
   background-image: linear-gradient(to bottom, #faf68f,  #f3ed53);filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startC     olorstr=#faf68f, endColorstr=#f3ed53);
}
<p class="pos_right">Most Number of Referrals for the month of </p>
<a class="button_example" href="#"><font color="black">UPDATE</font></a>


2

您可以通过将text-align: right;应用于按钮的父元素来将其移动到右侧。在您的情况下,它的父元素是body。

因此,您可以应用以下CSS:

body {
    text-align: right;
}

注意:它会使您的p标签也向右对齐。

2
根据您想要将其向右移动的空间大小,将此内容添加到 .button_example 中:
margin-left:100px;

0

我发现对于我的按钮,我必须使用:

right:-198px;
position: relative;

很明显,你需要根据自己的需求来调整正确的像素(px),并在浏览器的开发者工具中进行完美的调整,但这是对我有效的方法。


0

你也可以将其表示为百分比:

margin-left: 75%;

当调整页面大小时,它会移动,而使用像素时它会尝试保持在页面左侧100像素处。这完全取决于您在调整窗口大小时想要的结果。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接