Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reducing the width of div moves to left side in css

Tags:

html

css

Here is the

http://jsfiddle.net/vicky081/jmGwX/5/ I want to change the width of the div which is now 100% when i change the width to 50% the div appear left side. How can i make that div as center by this when i reduce the size of the div it appear from the center is it possible? I know the width of the div is 100% then it appear like from left 0 to right 100 if i change the width to 50 also it appear from left to 50% right Is there is a way to show it from center when i change the width it appear both for example: if i reduce the width of the div is 50% then it appear like 25% from center of left and 25% center of right

Here is the code CSS:

.notify {
    background: #FFFFFF;
    width:50%;
    position: relative; 
    margin:-13px 0px 0px -5px;
    padding: 2px 0px 0px 0px;   
    border-bottom:2px solid #CC0000;
    box-shadow: 0px 4px 5px #AAAAAA;
    font-size:14px;
    font-family: 'Lato Regular', Arial;
    text-transform:uppercase;
    text-align:center;
}
like image 889
Vignesh Pichamani Avatar asked Oct 16 '25 14:10

Vignesh Pichamani


2 Answers

Edit your CSS like here:

.notify {
    ....
    margin: 0 auto 0 auto;
    ....
}

http://jsfiddle.net/wRM8j/

EDIT:

If position is fixed add

.notify {
    ....
    position: fixed;
    left: 0;
    right: 0;
    ....
}

http://jsfiddle.net/vZexH/

like image 152
Michal Avatar answered Oct 18 '25 07:10

Michal


The first Solution is perfect but in case you want to play with that using javascript. Just include this line.

<script>
var decider=parseInt(document.defaultView.getComputedStyle(document.getElementsByTagName("div")[0]).width)/2;
document.getElementsByTagName("div")[0].style.left=(window.innerWidth/2)-decider;
</script>
like image 27
chetan mehta Avatar answered Oct 18 '25 07:10

chetan mehta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!