gpt4 book ai didi

html - 创建一个具有固定大小但居中元素的 Css Button

转载 作者:行者123 更新时间:2023-11-28 09:16:28 30 4
gpt4 key购买 nike

我正在尝试创建一个右侧带有图像的按钮。但是当我将 innerContainer 的宽度设置为 auto 时,它会选择最大宽度。

要实现这一点,需要创建一些包装器。

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>

黑色边框应在右侧以蓝色边框结束,并且在按钮内居中。我可以在js fiddle Demo中查看。

Jsfiddle Demo

最佳答案

试试这个:

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>

关于html - 创建一个具有固定大小但居中元素的 Css Button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26281267/

30 4 0
文章推荐: javascript - 为什么 firebug 多次显示相同的脚本
文章推荐: html - 将
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com