简体   繁体   English

CSS中的div标签和div周围的内嵌边框问题

[英]Problems with the div tag in CSS and inline borders that go around the div

This is my first time using HTML with CSS inline style. 这是我第一次将HTML与CSS内联样式一起使用。 Basically I am trying to use a div tag with an inline style to cause it to have a double lined border and one half of a letter width padding offsetting the text. 基本上,我试图使用带有内联样式的div标签,以使其具有双线边框和一半的字母宽度填充偏移文本。

I thought maybe I should start the div tag using an inline style rule, and a double border. 我想也许我应该使用内联样式规则和双边框开始div标签。 For example: 例如:

<div>
<div style=border:double;padding:0em,3px></div>

Is that a correct approach? 那是正确的方法吗?

You can use <hr> to create a horizontal rule and style it to make it double lined. 您可以使用<hr>创建水平线并设置其样式以使其成为双线。

hr {
    padding: 0em 3px;
    border: none;
    border-top: double #000;
    color: #000;
}

如果您想使用内联,请像这样使用

<div style="border:4px double black;width:200px;height:200px;"></div>

First of all, you should always put quotes around attribute values. 首先,应始终在属性值两边加上引号。 There is no reason not to. 没有理由不这样做。 (Originally, quotes were meant to be omittable around simple values, for instance height=30 , never around phrases that contained colons and semicolons and greater-than signs and stuff...) (最初,引号在简单的值(例如height=30周围是可省略的,绝不能在包含冒号,分号和大于号和其他东西的短语周围加引号。)

Also, you have an error in the paddings: those are not separated by commas. 此外,您在填充中有一个错误:填充之间没有用逗号分隔。

Then if you use the border notation, which is a shorthand for border-width , border-style and border-color , the browser will choose default values for the properties you don't specify. 然后,如果您使用border符号,这是border-widthborder-styleborder-color的简写,浏览器将为未指定的属性选择默认值。 In this case, medium for the border-width and currentColor for the border-color . 在这种情况下, border-widthmediumborder-colorcurrentColor If that is what you had planned, OK. 如果那是您计划的,那么好。 But if you don't know beforehand how thick a medium border is exactly, you should provide a width yourself. 但是,如果您事先不知道medium边框的确切厚度,则应该自己提供一个宽度。
See MDN . MDN

For double borders, multiples of 3px work best, since that's what you need minimally (1 px for each of the lines and 1 px in between). 对于双边框,最好使用3像素的倍数,因为这是您的最低要求(每行1像素,中间1像素)。 With other widths, different browsers may divide the widths differently, so that you may get thicker lines and less space in between on some browsers than on others. 对于其他宽度,不同的浏览器可能会对宽度进行不同的划分,因此,在某些浏览器上,您可能会比其他浏览器获得更粗的线条和更少的空间。

Edit: 编辑:
WRT the comment, if you want the div to take up the right half of the window, you can do something like this. WRT评论,如果您希望div占据窗口的右半部分,则可以执行以下操作。 Using a left margin of auto and a right margin of 0 , the div will be flushed to the right. 使用auto的左边距和0的右边距,div将被刷新到右边。
Note that I also used a padding of .5ch for "one half of a letter width", which is better than 3px hardcoded. 请注意,我还对.5ch的填充使用了“字母宽度的一半”,这比3px硬编码更好。

 .styled { border:3px double; width:50%; padding:.5ch 0; margin:0 0 1em auto; } 
 <div class="styled"></div> <hr/> 

Or, if using a float is more desirable: 或者,如果更希望使用浮点数:

 .styled { border:3px double; width:50%; float:right; padding:.5ch 0; margin-bottom:1em; } .styled + * { clear:right; } 
 <div class="styled"></div> <hr/> 

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

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