如何为2个div使用相同的CSS?

时间:2011-10-04 15:53:23

标签: css html css3

我有两个我希望风格相同的div。一个叫做#popover,另一个称为#recordViewPopover。这是我的popover CSS。如何使#recordViewPopover使用与#popover相同的CSS?

#popover {
  -webkit-border-radius: 4px;
  -webkit-box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  -webkit-transition: opacity 0.25s linear;
  -moz-border-radius: 4px;
  -moz-box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  -moz-transition: opacity 0.25s linear;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  transition: opacity 0.25s linear; 
  border: 3px solid black;
  border-radius: 4px;
  background-color: white;
  color: black;
  cursor: auto;
  display: none;
  font-weight: normal;
  line-height: 1;
  position: absolute;
  right: 22px;
  z-index: 5000000;
}

#popover > .popover_triangle
{
  border-top: 16px solid rgba(0,0,0,0);
  border-left: 16px solid rgba(0,0,0,0);
  border-right: 16px solid rgba(0,0,0,0);
  border-bottom: 16px solid black; 
  font-size: 0px;
  line-height: 0%;
  position: absolute;
  top: -34px;
  left:150px;
  width: 0px;
}

#popover > .header
{
  background: #222;
  background: -moz-linear-gradient(top, #333 0%, #111 100%);
  background-image: -webkit-gradient(
    linear,
    left top,
    left bottom,
    from(rgba(255, 255, 255, 0.55)),
    color-stop(0.2, rgba(255, 255, 255, 0.35)),
    color-stop(0.5, rgba(255, 255, 255, 0.1)),
    color-stop(0.5, transparent),
    to(transparent));
  color: white;
  font-weight: bold;
  height: 27px;
  line-height: 25px;
  margin: 0;
  padding: 3px;
  text-shadow: rgba(0, 0, 0, 1) 0 -1px 0;
  text-align: center; 
}

#popover > .content
{
  font-size: 12px;
  max-height: 400px;
  overflow-x: hidden;
  overflow-y: auto;
  width: 190px;
}

#popover > .content ul
{
  list-style: none;
  margin: 0;  
  padding: 0;
}

#popover > .content ul li
{
  border-bottom: #ccc 1px solid;
  line-height: 25px;
}

#popover > .content ul li.item:hover
{
  border-bottom: #1443be 1px solid;
}

#popover > .content ul li h6
{
  background: #f3f3f3;
  display: block;
  color: #000;
  font-size: 13px;
  padding: 0 10px;
  text-shadow: rgba(255, 255, 255, 1) 0 -1px 0;
}

#popover > .content ul li a
{
  display: block;
  padding: 0 14px;
}

#popover > .content ul li a:link,
#popover > .content ul li a:visited
{
  text-decoration:none;color:#333;
}

#popover > .content ul li a:focus,
#popover > .content ul li a:hover
{
  background: #5396e3 url('../images/arrow-small.png') 97% center no-repeat;
  color: #fff;
  text-decoration: none;
  text-shadow: rgba(0, 0, 0, .55) 0 -1px 0;
}

这是我的divs

<div id='popover'> 
  <div class='popover_triangle'></div> 
  <div class='header'>OPTIONS</div> 
  <div class='content'> 
    <ul>
      <li><h6>OBJECT VIEW</h6></li> 
      <li class='item'><a onclick=\"$(document).trigger('objectViewFiltersEnabled', [ !filtersEnabled ]);\">Filters</a></li>
      <li class='item'><a href=\"#\" >Configuration</a></li>
      <li><h6>OBJECT</h6></li>
      <li class='item'><a href=\"#\" >Configuration</a></li>
      <li class='item'><a href=\"#\" >Documentation</a></li>
    </ul> 
  </div> 
</div>

<div id='recordViewPopover'> 
  <div class='popover_triangle'></div> 
  <div class='header'>OPTIONS</div> 
  <div class='content'> 
    <ul>
      <li><h6>Information</h6></li> 
      <li class='item'><a href='#' >Change Logs</a></li> 
      <li><h6>Actions</h6></li>
      <li class='item'><a href='#' >Assign</a></li> 
      <li class='item'><a href='#' >Share</a></li>
      <li class='item'><a href='#' >Convert</a></li> 
      <li class='item'><a href='#' >Copy</a></li> 
      <li class='item'><a href='#' >Clone</a></li> 
      <li class='item'><a href='#' >Merge</a></li>
      <li class='item'><a href='#' >Refresh</a></li>
      <li class='item'><a href='#' >Delete</a></li>    
    </ul> 
  </div> 
</div>

3 个答案:

答案 0 :(得分:9)

您可以为它们分配同一个类。为此,您将拥有:

<div id='popover' class='someName'>

<div id='recordViewPopover' class='someName'>

你的CSS将被声明:

.someName {
//Add styles here
}

答案 1 :(得分:4)

你可以group多个选择器:

#popover > .popover_triangle,
#recordViewPopover > .popover_triangle {

或者给元素一个类,并使用一个具有id选择器的类选择器。

.popover > .popover_triangle {

答案 2 :(得分:2)

#popover, #recordViewPopover {
    ...
}

哦,还有

#popover > .popover_triangle,
#recordViewPopover > .popover_triangle {
    ...
}

但是如果你希望有更多这样的div,那么为它们定义一个类确实会更好。