简体   繁体   English

一个好的做法是使用填充而不是宽度(或高度)来构建div?

[英]Is a good practice use padding instead of width (or height) to build a div?

I have the next css 我有下一个CSS

.button {
width:0; 
height:18px; 
padding: 8px 0px 2px 30px; 
overflow:hidden;
transition: padding .5s ease;
}
.button:hover {
padding: 8px 170px 2px 35px
}

And i have this simple html 我有这个简单的HTML

<div class="button"> A text </div>

I'm looking for create in only one div, a white box that if you mouse over, this is expanded to the right to display a text smoothly. 我正在寻找只在一个div中创建,一个白色的盒子,如果你将鼠标移开,它会向右扩展以平滑地显示文本。

And that is the question: 这就是问题:

Is a good practice replace padding with width/height in these cases? 在这些情况下,一个好的做法是替换宽度/高度的填充物吗? Or is it better put the text in a different div and use the corresponding width and height instead of padding? 或者更好地将文本放在不同的div中并使用相应的宽度和高度而不是填充?

I don't know if i explain me good 我不知道我是否解释得很好

It is normally better to use padding since the button will support different text lengths and let the padding control the width and height depending on the text within the button, However if you want your buttons to be one size and one size only for example: A button with Submit, and a button with Download will be 2 different sizes and if you are displaying these along side each other you may want them to be the same size as one another, in this case then a width, height set because more ideal. 通常最好使用填充,因为按钮将支持不同的文本长度,并且填充控制宽度和高度取决于按钮内的文本,但是如果您希望按钮只有一个尺寸和一个尺寸,例如:A带有提交的按钮,带有下载的按钮将有2种不同的尺寸,如果你沿着彼此显示它们,你可能希望它们的尺寸彼此相同,在这种情况下,然后设置宽度,高度因为更理想。

I personally prefer padding but again if you want all buttons the same size then manually set these. 我个人更喜欢填充,但如果你想要所有按钮大小相同,那么手动设置这些。

By the way the way you shouldn't need to use height:18px, or Width:0; 顺便说一下你不需要使用高度的方式:18px,或宽度:0; Just delete these values and set the padding to control these and use a span within the button. 只需删除这些值并设置填充以控制这些值并使用按钮内的跨度。

Example: 例:

<p>Simple - One Button</p>
<div class="button"><span>A Button</span></div>

<p>3 Simple Buttons with Float Elements</p>
<div class="button floatleft"><span>A Button</span></div>
<div class="button floatleft"><span>A Button</span></div>
<div class="button floatleft"><span>A Button</span></div>
<div class="clearfix"> </div>

p {padding:15px 5px;}
.button {position:relative;transition:padding .5s ease;padding:5px;}
.button.floatleft {float:left;}
.button span:hover {padding:5px 20px 5px;}
.button span {background:#ccc;padding:5px;color:white;}
.clearfix {clear:both;}

Here's something I made 5mins ago to show you: JsFiddle 这是我5分钟前做的事情向你展示: JsFiddle

Hope this helps. 希望这可以帮助。

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

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