Greg
Greg

Reputation: 3063

Place an image at the top of a DIV and center it

I have 2 containers, one grey, one white. I'd like to place an image (this will be a triangle down) to the very top of the white background and center it horizontally. How could I achieve this? I have tried the following but that doesn't work

See https://jsfiddle.net/63s86hf9/

enter image description here

HTML

<section id="services" class="padding-onlytop-lg">
     <div class="container"><img src="http://placehold.it/92x45" class="triangle" alt="triangle-greg" width="92" height="45">
         <div class="row col-md-10 col-md-offset-1 inner-content padding-onlybottom-lg">

CSS:

.triangle {
margin-left: 0;
margin-right: 0;
margin: auto;
margin-top: -80px;
}

Upvotes: 1

Views: 63

Answers (6)

Jakir Hossain
Jakir Hossain

Reputation: 2517

Add position: relative into .padding-onlytop-lg. Example

.padding-onlytop-lg {
    padding-top: 75px !important;
    position: relative;
}

And Write in the flowing way

.triangle {
    position: absolute;
    left: 0;
    right: 0;
    margin: 0 auto;
    top: 0;
}

Upvotes: 0

Nishant Dixit
Nishant Dixit

Reputation: 21

try this

#id{
    text-align: center;
    vertical-align: text-top;
}

or

#id {
    text-align: center;
   margin-top:0px;
}

change value of margin-top according to your requirement

Upvotes: 1

Ali Gamal
Ali Gamal

Reputation: 42

    .container{
position:relative;
}
.triangle {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    top: -130px;
}

Upvotes: 0

Alok Patel
Alok Patel

Reputation: 8022

Update your .triangle class to this. You need to make it display as block. And add bottom margin to add space between title and the image.

.triangle {
    margin: auto;
    margin-top: -76px;
    display: block;
    margin-bottom: 30px;
}

Upvotes: 1

C3roe
C3roe

Reputation: 96382

margin:auto only works on block elements.

.triangle {
  margin: auto;
  margin-top: -80px;
  display: block;
}

(Your declarations for margin-left and -right are superfluous, since you are overwriting them with margin: auto on the next line anyway.)

Upvotes: 0

Mohammad Usman
Mohammad Usman

Reputation: 39342

Please try this code..

.triangle {
    display: block;
    margin: -80px auto 0;
}

Upvotes: 2

Related Questions