Html 如何使用css将此按钮设置为x轴的中心?
我尝试使用css将此按钮设置为x轴的中心,但它不起作用 我该怎么做 这是html代码Html 如何使用css将此按钮设置为x轴的中心?,html,css,Html,Css,我尝试使用css将此按钮设置为x轴的中心,但它不起作用 我该怎么做 这是html代码 <div class="one"> <div class="two">start now</div> </div> margin:0自动 需要一个宽度 此解决方案用于将dom居中: 1. set the wrapper `text-align:center;` 2. and then set the dom `margin:0 auto;`
<div class="one">
<div class="two">start now</div>
</div>
margin:0自动代码>
需要一个宽度
此解决方案用于将dom居中:
1. set the wrapper `text-align:center;`
2. and then set the dom `margin:0 auto;`
3. At last need to set the width of the dom.
在代码中,您应该设置.one
和浮点:left的宽度代码>在中。两个影响样式,只需将其删除。
在第一类中添加宽度:25%
和边距:自动
.one{
height: 66px;
line-height: 36px;
text-align: center;
width:25%;
margin:auto;
}
从第二类中删除float:left
.two{
background-color: #3498db;
color: white;
font-size: 24px;
padding-top: 15px;
padding-bottom: 15px;
padding-left: 20px;
padding-right: 20px;
}
这将完成以下工作指定宽度并移除浮动:
试试这个:
您需要添加width:fit内容代码>在一个类中。
.one{
高度:66px;
线高:36px;
显示:块;
保证金:0自动;
宽度:适合的内容;
文本对齐:居中;
}
.二{
浮动:左;
背景色:#3498db;
颜色:白色;
字体大小:24px;
填充顶部:15px;
垫底:15px;
左侧填充:20px;
右边填充:20px;
}
从现在开始
要使div居中,请拆下浮子
要使宽度自动适合文本的大小,请执行以下操作:显示:内联块李>
无需更改.one中的任何内容
运行代码段以查看它的运行情况
.one{
高度:66px;
线高:36px;
文本对齐:居中;
保证金:0自动;
}
.二{
背景色:#3498db;
颜色:白色;
字体大小:24px;
填充顶部:15px;
垫底:15px;
左侧填充:20px;
右边填充:20px;
显示:内联块;
}
从现在开始
宽度自动更改:,例如
另一个文本较长的div
删除代码中的浮动:左侧
.one{
高度:66px;
线高:36px;
文本对齐:居中;
保证金:0自动;
}
.二{
背景色:#3498db;
颜色:白色;
字体大小:24px;
填充顶部:15px;
垫底:15px;
左侧填充:20px;
右边填充:20px;
}
从现在开始
不能定位浮动的元素。从按钮中移除float:left
。当从按钮中移除float left时,我的按钮仍然很长。对于IE,您可以使用我现在添加的第三个解决方案,使用flex。您可以在此处看到flex支持:
.two{
background-color: #3498db;
color: white;
font-size: 24px;
padding-top: 15px;
padding-bottom: 15px;
padding-left: 20px;
padding-right: 20px;
}
.one{
height: 66px;
line-height: 36px;
text-align: center;
margin: 0 auto;
width: 150px;
}
.two{
background-color: #3498db;
color: white;
font-size: 24px;
padding-top: 15px;
padding-bottom: 15px;
padding-left: 20px;
padding-right: 20px;
}