سؤال

أحاول إعداد بنية علامة تبويب أفقية بسيطة لصفحة أعمل عليها، وأعيد إلى بعض المتاعب مع الفم العائم مع z-Index.

عرض التعليمات البرمجية التالية في المستعرض:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <style type="text/css">
        #main { width: 500px; z-index: 1;}

        .left { float: left; width: 96px; background-color: red; border: 2px solid orange; z-index: 2; margin-right: -2px }
        .right { float: left; width: 396px; background-color: #09c; border: 2px solid green; z-index: 3; }

        .clear { clear: both; }
</style>
</head>

<body>
    <div id="main">
        <div class="left">
            LEFT
        </div>
        <div class="right">
            RIGHT
            <br />
            RIGHT
        </div>
        <div class="clear"></div>
    </div>
</body>
</html>

لماذا لا يتداخل الحدود البرتقالية اليسرى من الحدود الخضراء للحف اليمنى؟

هل كانت مفيدة؟

المحلول

لن يتم تطبيق خاصية Z-Index على عناصر ثابتة. من أجل استخدام Z-Index يجب أن تتضمن CSS أيضا أي قيمة موضعية أخرى غير ثابتة (أي قريب، مطلق، ثابت).

.left { float: left; width: 96px; background-color: red; border: 2px solid orange; z-index: 3; margin-right: -2px; position: relative; }
.right { float: left; width: 396px; background-color: #09c; border: 2px solid green; z-index: 2; position: relative; }

سوف أعطيك ما تريد أعتقد. أضفت موقف: قريب؛ وقمت بتغيير مؤشر Z من .LET إلى 3 (من 2) وغير مؤشر Z من .RIGHT إلى 2 (من 3).

نصائح أخرى

z-index ليس له أي تأثير على العناصر غير المرفقة (على سبيل المثال position:absolute;)

استخدم ال position عقارات العليا العليا. إضافة position:relative ل .left.

سلبي margin-left?

.right { float: left; width: 396px; background-color: #09c; border: 2px solid green; z-index: 3; margin-left: -5px;}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top