Tom
Tom

Reputation: 345

Unsure how to position image over gradient correctly

I am generating a gradient on the page. How can I overlay an image in the center of this ribbon? I need to center the image and I cant seem to figure out how to make this work. I am using bootstrap and the page is responsive. So it needs to stay together when the page gets smaller. Any help is appreciated!

Desired Result:

enter image description here

#ribbon-background { 
	background: #ed1c24; /* Old browsers */
	background: -moz-linear-gradient(left,  #ed1c24 0%, #600000 50%, #ed1c24 100%); /* FF3.6-15 */
	background: -webkit-gradient(linear, left top, right top, color-stop(0%,#ed1c24), color-stop(50%,#600000), color-stop(100%,#ed1c24)); /* Chrome4-9,Safari4-5 */
	background: -webkit-linear-gradient(left,  #ed1c24 0%,#600000 50%,#ed1c24 100%); /* Chrome10-25,Safari5.1-6 */
	background: -o-linear-gradient(left,  #ed1c24 0%,#600000 50%,#ed1c24 100%); /* Opera 11.10-11.50 */
	background: -ms-linear-gradient(left,  #ed1c24 0%,#600000 50%,#ed1c24 100%); /* IE10 preview */
	background: linear-gradient(to right,  #ed1c24 0%,#600000 50%,#ed1c24 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
	filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ed1c24', endColorstr='#ed1c24',GradientType=1 ); /* IE6-9 */
	border-top: 3px solid #000;
	border-bottom: 3px solid #000;
	box-shadow: 0 7px 0 #FFF inset,
				0 -7px 0 #FFF inset;
    height: 65px;
    margin: 0 auto;
    width: 100%; 
	z-index: 99;
}
<div id="ribbon-background"></div>

Upvotes: 0

Views: 23

Answers (1)

AVAVT
AVAVT

Reputation: 7133

One way to do that is to give the ribbon a position: relative; and add the follow style to a child <img>:

  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

Demo:

#ribbon-background {
  background: #ed1c24;
  /* Old browsers */
  background: -moz-linear-gradient(left, #ed1c24 0%, #600000 50%, #ed1c24 100%);
  /* FF3.6-15 */
  background: -webkit-gradient(linear, left top, right top, color-stop(0%, #ed1c24), color-stop(50%, #600000), color-stop(100%, #ed1c24));
  /* Chrome4-9,Safari4-5 */
  background: -webkit-linear-gradient(left, #ed1c24 0%, #600000 50%, #ed1c24 100%);
  /* Chrome10-25,Safari5.1-6 */
  background: -o-linear-gradient(left, #ed1c24 0%, #600000 50%, #ed1c24 100%);
  /* Opera 11.10-11.50 */
  background: -ms-linear-gradient(left, #ed1c24 0%, #600000 50%, #ed1c24 100%);
  /* IE10 preview */
  background: linear-gradient(to right, #ed1c24 0%, #600000 50%, #ed1c24 100%);
  /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ed1c24', endColorstr='#ed1c24', GradientType=1);
  /* IE6-9 */
  border-top: 3px solid #000;
  border-bottom: 3px solid #000;
  box-shadow: 0 7px 0 #FFF inset, 0 -7px 0 #FFF inset;
  height: 65px;
  margin: 0 auto;
  width: 100%;
  z-index: 99;
  position: relative;
}

#ribbon-overlay-img {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<div id="ribbon-background">
  <img id="ribbon-overlay-img" src="http://www.iconsdb.com/icons/preview/orange/stackoverflow-4-xxl.png">
</div>

Upvotes: 1

Related Questions