Hello Lili
Hello Lili

Reputation: 1587

Dynamic left margin

How can I set a dynamic left margin of a div depending on $(window).width()?

For example, for a width of 1886px the margin should be 20%, but for a width of 1280px the margin should change to 10%.

Upvotes: 1

Views: 419

Answers (2)

Jamie Barker
Jamie Barker

Reputation: 8246

CSS Media Queries:

div {
     margin-left: 20%; /* Default */
}
@media (max-width: 1280px) { /* Apply these styles to 1280px width and under */
     div {
          margin-left: 10%;
     }
}

Upvotes: 2

Andrejs Cainikovs
Andrejs Cainikovs

Reputation: 28434

CSS media queries is a way to go:

@media (max-width:1280px) {
    margin-left: 10%;
}
@media (min-width:1281px) {
    margin-left: 20%;
}

Upvotes: 1

Related Questions