2

Is it possible to simulate a div around b and c items and to style it without changing this HTML?

I know there are ::before and ::after pseudo-elements but couldn't find any "wrapping" element. Maybe something like ::wrap(.b, .c).

<ul>
    <li class="a">a</li>
    <li class="b">b</li>
    <li class="c">c</li>
    <li class="d">d</li>
</ul>

A good example could be isolating and putting a single background image behind b and c.

Eduardo
  • 5,645
  • 4
  • 49
  • 57
  • Removed the "border" case. Intention is to group elements and not to put a border. Background image can be a good example if no specific solution is given for that. – Eduardo May 11 '15 at 19:19
  • 1
    There are no wrapping pseudo-elements. And adding a wrapper element to `.b` and `.c` (manually or with JavaScript) is impossible, because only `li` is valid as a child of `ul`. – Rick Hitchcock May 11 '15 at 20:27
  • Does this answer your question? [CSS container pseudo element?](https://stackoverflow.com/questions/11586192/css-container-pseudo-element) – Kal Jul 13 '21 at 01:49

5 Answers5

2

You can use the border property and apply it to the b and c class:

li.b {
    border:1pt solid black;
}

li.c {
    border:1pt solid black; 
}

http://jsfiddle.net/ykjv16d8/1/

From your comments:

You can set the border-bottom to none for b and the border-top to none for c

li.b {
    border:1pt solid black;
    border-bottom: none;
}

li.c {
    border:1pt solid black; 
    border-top:none;
}

http://jsfiddle.net/ykjv16d8/2/

Darren
  • 68,902
  • 24
  • 138
  • 144
2

Wouldn't you just do something like,

li.c,li.b { border: 1px solid black; }
Quintile
  • 335
  • 2
  • 6
  • Sorry I was not clear and just made an edit. I mean one border around both but my intention is not the border itself. I want to simulate a div around them. – Eduardo May 11 '15 at 18:58
1

Assuming no margin between list items, this will work:

li.b, li.c {
  border: 1px solid black;
}

li.b {
  border-bottom: none;
}

li.c {
  border-top: none;
}
<ul>
    <li class="a">a</li>
    <li class="b">b</li>
    <li class="c">c</li>
    <li class="d">d</li>
</ul>

If there are margins, you'll need to be a bit more creative:

li {
  margin: 20px;
  position: relative;
}

li.b::before, li.c::before {
  content: '';
  border: 1px solid black;
  position: absolute;
  width: 100%;
  height: calc(100% + 20px);
  top: -10px;
  left: -2px;
}

li.b::before {
  border-bottom: none;
}

li.c::before {
  border-top: none;
}
<ul>
    <li class="a">a</li>
    <li class="b">b</li>
    <li class="c">c</li>
    <li class="d">d</li>
</ul>
Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79
1

There is not (currently) a solution for this using good old CSS psuedo elements.

Mr. Hugo
  • 11,887
  • 3
  • 42
  • 60
Jicub
  • 21
  • 4
0

If i understand you want both b and c to be in one box??

try this

html

<ul>
  <li class="a">a</li>
  <li class="b bc">b</li>
  <li class="c bc">c</li>
  <li class="d">d</li>
</ul>

css

.bc {
   border: 1px solid red;
 }

.b.bc {
  border-bottom: none;
}

.c.bc {
 border-top: none;
}

is that what you mean? let me know

Abuasmaa
  • 11
  • 2
  • I confused everybody asking to put a border. I need to simulate a div around them without changing html. Try to put a background image using the same solution. – Eduardo May 11 '15 at 19:24