Как центрировать «позицию: абсолютный» topnav с выпадающим - PullRequest
0 голосов
/ 24 сентября 2019

Я использую чистое меню CSS из этого сайта .Это работает нормально, но если я попытаюсь центрировать или выровнять меню по правому краю, когда оно не в режиме «меню гамбургера», я не могу заставить его работать.

Я пробовал решения из этогоstackoverflow вопрос , но он портит меню.Элементы li размещаются под друг другом, и / или выпадающие меню появляются в неправильных местах.

Так как же я могу расположить это меню по центру или по правому краю, не испортив его?Любая помощь приветствуется!

/*Strip the ul of padding and list styling*/
ul {
    list-style-type:none;
    margin:0;
    padding:0;
    position: absolute;
}
/*Create a horizontal list with spacing*/
li {
    display:inline-block;
    float: left;
    margin-right: 1px;
}
/*Style for menu links*/
li a {
    display:block;
    min-width:140px;
    height: 50px;
    text-align: center;
    line-height: 50px;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    color: #fff;
    background: #2f3036;
    text-decoration: none;
}
/*Hover state for top level links*/
li:hover a {
    background: #19c589;
}
/*Style for dropdown links*/
li:hover ul a {
    background: #f3f3f3;
    color: #2f3036;
    height: 40px;
    line-height: 40px;
}
/*Hover state for dropdown links*/
li:hover ul a:hover {
    background: #19c589;
    color: #fff;
}
/*Hide dropdown links until they are needed*/
li ul {
    display: none;
}
/*Make dropdown links vertical*/
li ul li {
    display: block;
    float: none;
}
/*Prevent text wrapping*/
li ul li a {
    width: auto;
    min-width: 100px;
    padding: 0 20px;
}
/*Display the dropdown on hover*/
ul li a:hover + .hidden, .hidden:hover {
    display: block;
}
/*Style 'show menu' label button and hide it by default*/
.show-menu {
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    text-decoration: none;
    color: #fff;
    background: #19c589;
    text-align: center;
    padding: 10px 0;
    display: none;
}
/*Hide checkbox*/
input[type=checkbox]{
    display: none;
}
/*Show menu when invisible checkbox is checked*/
input[type=checkbox]:checked ~ #menu{
    display: block;
}
/*Responsive Styles*/
@media screen and (max-width : 760px){
    /*Make dropdown links appear inline*/
    ul {
        position: static;
        display: none;
    }
    /*Create vertical spacing*/
    li {
        margin-bottom: 1px;
    }
    /*Make all menu links full width*/
    ul li, li a {
        width: 100%;
    }
    /*Display 'show menu' link*/
    .show-menu {
        display:block;
    }
}
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS Only Navigation Menu</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <label for="show-menu" class="show-menu">Show Menu</label>
    <input type="checkbox" id="show-menu" role="button">
        <ul id="menu">
        <li><a href="#">Home</a></li>
        <li>
            <a href="#">About ↓</a>
            <ul class="hidden">
                <li><a href="#">Who We Are</a></li>
                <li><a href="#">What We Do</a></li>
            </ul>
        </li>
        <li>
            <a href="#">Portfolio ↓</a>
            <ul class="hidden">
                <li><a href="#">Photography</a></li>
                <li><a href="#">Web & User Interface Design</a></li>
                <li><a href="#">Illustration</a></li>
            </ul>
        </li>
        <li><a href="#">News</a></li>
        <li><a href="#">Contact</a></li>
    </ul>
</body>
</html>

Ответы [ 2 ]

0 голосов
/ 25 сентября 2019

Вы можете использовать этот код

body {
            margin: 0;
        }
        /*Strip the ul of padding and list styling*/        
        ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
            position: absolute;
        }
        /*Create a horizontal list with spacing*/        
        li {
            display: inline-block;
            float: left;
            margin-right: 1px;
        }
        /*Style for menu links*/        
        li a {
            display: block;
            min-width: 140px;
            height: 50px;
            text-align: center;
            line-height: 50px;
            font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
            color: #fff;
            background: #2f3036;
            text-decoration: none;
        }
        /*Hover state for top level links*/        
        li:hover a {
            background: #19c589;
        }
        /*Style for dropdown links*/        
        li:hover ul a {
            background: #f3f3f3;
            color: #2f3036;
            height: 40px;
            line-height: 40px;
        }
        /*Hover state for dropdown links*/        
        li:hover ul a:hover {
            background: #19c589;
            color: #fff;
        }
        /*Hide dropdown links until they are needed*/        
        li ul {
            display: none;
        }
        /*Make dropdown links vertical*/        
        li ul li {
            display: block;
            float: none;
        }
        /*Prevent text wrapping*/        
        li ul li a {
            width: auto;
            min-width: 100px;
            padding: 0 20px;
        }
        /*Display the dropdown on hover*/        
        ul li a:hover + .hidden,
        .hidden:hover {
            display: block;
        }        
        ul.hidden {
            width: 20%;
        }        
        ul.hidden li a {
            display: block;
            height: auto;
            text-align: center;
            line-height: normal;
            padding: 15px 10px;
        }
        /*Style 'show menu' label button and hide it by default*/        
        .show-menu {
            font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
            text-decoration: none;
            color: #fff;
            background: #19c589;
            text-align: center;
            padding: 10px 0;
            display: none;
        }
        /*Hide checkbox*/        
        input[type=checkbox] {
            display: none;
        }
        /*Show menu when invisible checkbox is checked*/        
        input[type=checkbox]:checked ~ #menu {
            display: block;
        }
        /*Responsive Styles*/        
        @media screen and (max-width: 760px) {
            /*Make dropdown links appear inline*/
            ul {
                position: static;
                display: none;
            }
            /*Create vertical spacing*/
            li {
                margin-bottom: 1px;
            }
            /*Make all menu links full width*/
            ul li,
            li a {
                width: 100%;
            }
            /*Display 'show menu' link*/
            .show-menu {
                display: block;
            }
            ul.hidden {
                width: 100%;
            }
        }
<label for="show-menu" class="show-menu">Show Menu</label>
    <input type="checkbox" id="show-menu" role="button">
    <ul id="menu">
        <li><a href="#">Home</a></li>
        <li>
            <a href="#">About ↓</a>
            <ul class="hidden">
                <li><a href="#">Who We Are</a></li>
                <li><a href="#">What We Do</a></li>
            </ul>
        </li>
        <li>
            <a href="#">Portfolio ↓</a>
            <ul class="hidden">
                <li><a href="#">Photography</a></li>
                <li><a href="#">Web & User Interface Design</a></li>
                <li><a href="#">Illustration</a></li>
            </ul>
        </li>
        <li><a href="#">News</a></li>
        <li><a href="#">Contact</a></li>
    </ul>
0 голосов
/ 25 сентября 2019

Вы можете использовать flexbox для выравнивания контента:

@media (min-width: 768px) {    
    #menu {
        width: 100%;
        display: flex;
        justify-content: center; /* or use end */
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...