Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

margin top to div inside another div

Tags:

html

css

I have two divs which have background images and I would like to add margin to the the 2nd div but it is creating white space.

How can I add a top margin while keeping the div inside the other div?

<div class="background">
    <div class="logo">
    </div> 
</div>

.logo {
   background-image: url(image/logo2.png);
   height:40px;
   width:400px;
   margin-left:100px;
   display:block;
   margin-top:20px;
}

.background {
   background-image: url(image/empback.png);
   width:100%;
   height:94px;
   min-width:1345px;
}
like image 404
amdvb Avatar asked Aug 21 '13 05:08

amdvb


People also ask

How do you margin a div inside a div?

To move the inner div container to the centre of the parent div we have to use the margin property of style attribute. We can adjust the space around any HTML element by this margin property just by providing desired values to it.

Can a div have a margin?

div s are used to create what used to be known as layers, and can be used as a replacement for tabled layout. We get down to that fully in CSS Layout. Default margins, borders and padding are all 0, so when you wrap a div around some text, there is no space between its edges and the text.

How do you put a div in front of another?

In order an element to appear in front of another you have to give higher z-index to the front element, and lower z-index to the back element, also you should indicate position: absolute/fixed... Save this answer.

Can we add margin to span tag?

The <span> tag is a inline element, it fits into the flow of the content and can be distributed over multiple lines. We can not specify a height or width or surround it with a margin.


1 Answers

You can use overflow: auto on the parent element

.background{
    background: #f00;
    width:100%;
    height:94px;
    min-width:1345px;
    overflow: auto;
}

Demo

Just got a link to share which is related to this issue.


Also, a better approach towards this is instead of using position: absolute; you should use position: relative; with top, left, right and bottom properties, which will not only keep the element in float, it will also save you from positioning other elements in the same block.

Demo 2

like image 88
Mr. Alien Avatar answered Oct 04 '22 22:10

Mr. Alien