简体   繁体   English

创建具有固定大小但居中元素的Css按钮

[英]Creating a Css Button with fixed size but centered elements

I'm trying to create a Button with an Image on the Right side. 我正在尝试创建一个右侧带有图像的按钮。 But when I set the width of my innerContainer to auto it choses the maximum width. 但是,当我将innerContainer的宽度设置为auto时,它将选择最大宽度。

To realize this, there was a need to create some wrappers. 为了实现这一点,需要创建一些包装器。

 div.container { max-width: 240px; width: auto; border: 5px solid #cd8102; border-radius: 3px; height: 36px; background: linear-gradient(#fcbf00, #f39600); /* Standard syntax */ font-family: 'PT Sans',sans-serif; color: black; font-size: 16px; text-decoration: none; } div.container:hover{ background: linear-gradient(yellow, #f39600); /* Standard syntax */ } div.innerContainer{ width: 90%; margin-left: auto; margin-right: auto; border: 2px solid black; } div.box1 { box-sizing: border-box; width: 207; max-width: 207px; /*border: 1px solid red; */ border: 1px solid yellow; float: left; height: 36px; text-align: center; padding-right: 6px; padding-left: 10px; } div.box2 { box-sizing: border-box; width: 20px; height: 20px; margin-top: 8px; text-align: center; border: 1px solid blue; float: left; justify-content: center; /* align horizontal */ align-items: center; /* align vertical */ } #TextInTheMiddle { text-align: center; display: flex; justify-content: center; /* align horizontal */ align-items: center; /* align vertical */ height: 36px; border: 1px dotted red; } 
 <a href="http://www.google.de" > <div class="container"> <div class="innerContainer"> <div class="box1"> <span id='TextInTheMiddle'>Weiter Einkaufen</span> </div> <div class="box2"> <span> <img id="img" src="file:///Y|/Button/images/icons/next.png" height="20" width="20"> </span> </div> <div style="clear:both;"></div> </div> </div> </a> 

The black border should ending with the blue one on the right side and also be centered within the button. 黑色边框应在右侧以蓝色边框结尾,并且也应在按钮内居中。 I can be viewed in the js fiddle Demo. 我可以在js小提琴演示中查看。

Jsfiddle Demo Jsfiddle演示

Try this: 尝试这个:

div.container {   
  text-align: center;
}
div.innerContainer{
  display: inline-block;
  width: auto; /* default value */
}

 div.container { max-width: 240px; width: auto; border: 5px solid #cd8102; border-radius: 3px; height: 36px; background: linear-gradient(#fcbf00, #f39600); /* Standard syntax */ font-family: 'PT Sans',sans-serif; color: black; font-size: 16px; text-decoration: none; text-align: center; } div.container:hover{ background: linear-gradient(yellow, #f39600); /* Standard syntax */ } div.innerContainer{ display: inline-block; margin-left: auto; margin-right: auto; border: 2px solid black; } div.box1 { box-sizing: border-box; width: 207; max-width: 207px; /*border: 1px solid red; */ border: 1px solid yellow; float: left; height: 36px; text-align: center; padding-right: 6px; padding-left: 10px; } div.box2 { box-sizing: border-box; width: 20px; height: 20px; margin-top: 8px; text-align: center; border: 1px solid blue; float: left; justify-content: center; /* align horizontal */ align-items: center; /* align vertical */ } #TextInTheMiddle { text-align: center; display: flex; justify-content: center; /* align horizontal */ align-items: center; /* align vertical */ height: 36px; border: 1px dotted red; } 
 <a href="http://www.google.de" > <div class="container"> <div class="innerContainer"> <div class="box1"> <span id='TextInTheMiddle'>Weiter Einkaufen</span> </div> <div class="box2"> <span> <img id="img" src="file:///Y|/Button/images/icons/next.png" height="20" width="20"> </span> </div> <div style="clear:both;"></div> </div> </div> </a> 

I'm guessing that's what you were looking for? 我猜这就是您要找的东西吗?

http://jsfiddle.net/zf48u7as/1/ http://jsfiddle.net/zf48u7as/1/

div.innerContainer{
    /* add this and remove width: */
    display: inline-block;
}

div.container {   
    /* add this: */
    text-align: center;
}

Sounds like you want the picture outside of the box? 听起来像您要把图片放在盒子外面? This is what I got from it: 这是我从中得到的:

div.innerContainer{

    margin: auto;
    border: 2px solid black;
    width: 270px;
}
div.box2 {
    box-sizing: border-box;
    width: 20px;
    height: 20px;
    margin-top: 8px;
    text-align: center
    border: 1px solid blue;
    float: right;
    justify-content: center; /* align horizontal */
    align-items: center; /* align vertical */

}

Changed the width: 270px; 更改width: 270px; for the innerContainer and float: right; 对于innerContainerfloat: right; for the box2 jsfiddle 对于box2 jsfiddle

Instead of putting divs and spans you can customize the button like this : 您可以像这样自定义按钮,而不是放置div和span:

 input#button{ width: 75px; border: 0; border-radius: 4px; background-color: #838383; color: #fff; background-image: url('someImage'); background-repeat: no-repeat; background-position-x: 50px; text-indent: -20px; } 
 <input type="button" value="Click Me" id="button" /> 

That looks like an aweful lot of unnecessary code. 看起来很多不必要的代码。 I've reduced it as much as possible to match what I thought you were trying to achieve: 我已尽可能减少它,以符合我认为您要实现的目标:

To achieve this, there is not much to do: 要实现此目的,没有太多的事情要做:

  • center the text via an according line-height and text-align 通过相应的line-heighttext-align将文本居中
  • insert the image as background image with an appropriate padding to the parent element 将图像作为背景图像插入到父元素中并带有适当的填充

To further improve your code, you could use a button element (the appropriate element when you want to have a button) and some pseudo elements to position your image, but this solution works as well. 为了进一步改进代码,可以使用button元素(想要使用按钮时适当的元素)和一些伪元素来放置图像,但是这种解决方案也可以使用。

 div.container { display: inline-block; padding: 0 10px; border: 5px solid #cd8102; border-radius: 3px; height: 36px; background-image: linear-gradient(#fcbf00, #f39600); font-family: 'PT Sans', sans-serif; color: black; font-size: 16px; text-decoration: none; /* added for centering the text*/ text-align: center; line-height: 36px; } div.container:hover { background: linear-gradient(yellow, #f39600); } .container span { /* the image to the right of the text */ padding-right: 25px; background: url(http://lorempixel.com/output/sports-qc-20-20-6.jpg) right center no-repeat; } 
 <a href="http://www.google.de"> <div class="container"> <span> Weiter Einkaufen </span> </div> </a> 

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

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