Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background-color for a div around all content inside

Tags:

I'm working on a 'arrow'-div. It currently looks like this:

enter image description here

the div contains two other divs(two lines). And I want that the background is nearly wrapped around the lines. But the height of the yellow-background is a lot smaller than the height of the lines. I already tried 'height: auto'. I hope someone could help me out.

#lineAll {
  background-color: yellow;
  height: auto;
}

#line1 {
  height: 2px;
  background-color: black;
  transform: rotate(35deg);
  width: 40px;
}

#line2 {
  height: 2px;
  background-color: black;
  transform: rotate(-35deg);
  width: 40px;
  margin-top: 20px;
}
<div id="lineAll">
  <div id="line1"></div>
  <div id="line2"></div>
</div>

edit:

The width is also not the way I want it. It's currently 100%-width of the screen.

like image 366
Soccerlife Avatar asked Mar 03 '17 14:03

Soccerlife


People also ask

Can a div have a background color?

The default background color of a div is transparent . So if you do not specify the background-color of a div, it will display that of its parent element.

How do I set a background color for the width of text?

Put the text in an inline element, such as a <span> . And then apply the background color on the inline element. An inline element is as big as its contents is, so that should do it for you.


2 Answers

Try this:

<div style="background-color : yellow; padding: 15px 0px; width: 40px;">
    <div id="lineAll">
       <div id="line1"></div>
       <div id="line2"></div>
    </div>
</div>

<style>
#lineAll {
   background-color: yellow;
   height: auto;
}

#line1 {
  height: 2px;
  background-color: black;
  transform: rotate(35deg);
  width: 40px;
}

#line2 {
  height: 2px;
  background-color: black;
  transform: rotate(-35deg);
  width: 40px;
  margin-top: 20px;
}

like image 193
Toxide82 Avatar answered Sep 21 '22 10:09

Toxide82


You can do this with one element and :after pseudo-element. Just create smaller pseudo-element that has border-top and border-right and then rotate it for 45deg.

.element {
  width: 50px;
  height: 60px;
  background: yellow;
  position: relative;
}
.element:after {
  content: '';
  position: absolute;
  top: 15px;
  border-top: 1px solid black;
  border-right: 1px solid black;
  height: 30px;
  width: 30px;
  transform: rotate(45deg);
}
<div class="element"></div>

To create other button just rotate for -135deg and set right: 0px

.element {
  width: 50px;
  height: 60px;
  background: yellow;
  position: relative;
  display: inline-block;
  margin: 50px;
}
.element:after {
  content: '';
  position: absolute;
  top: 15px;
  border-top: 1px solid black;
  border-right: 1px solid black;
  height: 30px;
  width: 30px;
  transform: rotate(45deg);
}
.element.right:after {
  transform: rotate(-135deg);
  right: 0px;
}
<div class="element"></div>
<div class="element right"></div>
like image 28
Nenad Vracar Avatar answered Sep 22 '22 10:09

Nenad Vracar