Html CSS - 为所有 div 添加边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4028153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:59:31  来源:igfitidea点击:

CSS - Add border to all divs

csshtml

提问by Trufa

I was wondering if there was any "easy" way to add through css lets say:

我想知道是否有任何“简单”的方式通过 css 添加可以说:

border: 1px solid red;

border: 1px solid red;

to all divs no matter what their id′s are.

对所有 div,无论它们的 ID 是什么。

I realize this might be a very very basic question (or no possible at all) and I hope it is clear enough.

我意识到这可能是一个非常基本的问题(或者根本不可能),我希望它足够清楚。

Just to clarify, lets say I′ve got:

为了澄清,让我们说我有:

HTML

HTML

<div id="one">

</div>

<div id="two">

</div>

and CSS

和 CSS

#one{
height: 10px;
width: 10px;
}

#two{
height: 10px;
width: 10px;
}

The result I actually want is:

我真正想要的结果是:

#one{

height: 10px;
width: 10px;
border: 1px solid red;
}

#two{
height: 10px;
width: 10px;
border: 1px solid red;
}

I want to achieve this without having to go one by one.

我要做到这一点,而不必一一进行。

Thanks in advance!!

提前致谢!!

Please ask for any clarification needed!

请要求任何需要的澄清!

回答by McAden

div {
    border: 1px solid #000000;
}

回答by code90

I agree with @McAden's answer. Alternatively, you can use jquery to add the style on the fly:

我同意@McAden 的回答。或者,您可以使用 jquery 动态添加样式:

<script type="text/javascript">
    $('div').css('border','1px solid #000');
</script>

回答by Ravi Chauhan

CSS Using
#one{
height: 100px;
width: 100px;
border:1px solid #000;
}
// you can use sepertely
#two{
height: 100px;
width: 100px;
border-bottom: 1px solid #000;
border-top: 1px solid #000;
border-left: 1px solid #000;
border-right: 1px solid #000;
}

JQuery Using

JQuery 使用

<script type="text/javascript">
    $('#one').css('border','1px solid #000');
</script>

回答by Sukesh Chand

$('div').css("border", "1px solid #CCC");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<div>
1
  <div>
    3
    </div>
<div>
2
</div>
</div>

回答by Damir Olejar

For all elements

对于所有元素

<style> * {
    border: 1px solid #000000;
}
</style>

回答by Oto Brglez

Perhaps this will help you:

也许这会帮助你:

div#one, div#two{
    border:1px solid red;
}

回答by sissonb

As McAden was saying, you may want to specify which divs you want to style. Instead of adding a class to each div you may want to try an approach like this,

正如 McAden 所说,您可能想要指定要设置样式的 div。您可能想尝试这样的方法,而不是为每个 div 添加一个类,

.theseDivs div{
    /*styles here*/
}

<div class="theseDivs">
    <div>Style applied here</div>
    <div>and here, </div>
</div>
<div>but not here</div>