简体   繁体   中英

CSS: image & links verticaly aligned (center) AND responsive image swap

I have several problems with my css. As you can see in this fiddle , I would like the image to be perfectly and automatically vertically-aligned right smack the middle. The same goes for the menu links.

The other problem I have is that I would like to change the logo image to another source, when using small screens. I would preferably like to only use media queries (if possible).

SAMPLE HTML

<header>
    <nav id="menu-main" class="clearfix">
        <a id="logo-link" href="#"><img id="logo" 
    src="http://juvera.me/websites/openwork/beta/_img/logo-291x60.png" title="Open Work" alt="Open Work" /></a>

        <ul id="menu-links">
        <li class="link-active"><a class="link-active" href="/">Home</a></li>
        <li><a href="#about">About</a></li>
        <li><a href="#contact">Contact</a></li>
        </ul>
    </nav> 
</header>


<main>
  <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
</main>

CSS

body { font-family: Century Gothic, sans-serif; margin: 0em; padding: 0em; }
header, main { display: block; margin: 0em; padding: 0em; }
header {
    background-color: #292828;

  height: 70px;
    max-height: 70px;

    /* Overlay */
    width: 100%;
    position: absolute; left:0; right:0; z-index: 999;
}
header nav {
    height: 70px;
}



/* *************************************
Image Menu
************************************* */
#logo {
    margin: auto 0em auto 10%;
    vertical-align: middle;
}
#logo-link {
    height: 70px;
    vertical-align: middle;
}


/* *************************************
Links menu
************************************* */
#menu-links {
    float: right;
    margin: auto 10% auto 0em;
}
#menu-links li {
    display: inline-block;
}
#menu-links li a {
    color: #fff;
    font-size: 0.75em;
    text-decoration: none;
    text-transform: uppercase;

    height: 70px;
    padding: 0em 1em 0em 1em;
    display: inline-block;
    vertical-align:middle;
    text-align: center;
}


main { padding-top: 70px; background-color: #cccccc; }

@media (max-width:10em) { 
/* smartphones, iPhone, portrait 480x320 phones */ 
    #logo{ src:"http://juvera.me/websites/openwork/beta/_img/logo-150x31.png"; }
    main { font-size: 1em; }
}

Create another logo img that gets hidden by default, but on mobile versions , the other logo gets hidden, and the mobile logo is displayed instead. Also, use line-height to vertically center everything in the navbar ( See this answer ).

In your CSS :

To header , add line-height: 70px;

To #logo-link , add position: relative; top: -4px; position: relative; top: -4px;

Change your media query to:

@media (max-width: 10em) { 
    #logo { display: none; }
    #logo-mobile { display: inline; }
    main { font-size: 1em; }
}

And add #logo-mobile { display: none; } #logo-mobile { display: none; }

In your HTML , add another img next to #logo with the id of logo-mobile that has the src you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM