简体   繁体   English

我该如何设定两种不同的风格 <ul> HTML和CSS中的标签?

[英]How do I style two different <ul> tags in HTML & CSS?

I have a 'ul' tag that already styles something else. 我有一个'ul'标签,该标签已经具有其他样式。 I need to create another 'ul' tag to stylize something else in my HTML page. 我需要创建另一个'ul'标签,以对HTML页面中的其他内容进行样式化。

This is the first ul style: 这是第一个ul样式:

ul {
    list-style: none;
    padding: 0px;
    margin: 0px;
    font-family: arial;
    color:white;
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
}

I need to style another ul where I create a list of items. 我需要在创建项目列表的另ul设置样式。 I want the default CSS settings for the ul tags, but I don't know how to make that work. 我想要ul标签的默认CSS设置,但是我不知道如何使它工作。

Here is the HTML code for the ul list: 这是ul列表的HTML代码:

<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>

What code will make the above ul tag go back to the default CSS settings? 什么代码会使上面的ul标签返回到默认CSS设置?

Better assign a class for the first ul and add styles for that CSS class, then other ul in the page will not get affected. 最好为第一个ul分配一个类,并为该CSS类添加样式,然后页面中的其他ul不会受到影响。

 .first-ul { list-style: none; padding: 0px; margin: 0px; font-family: arial; color: #000; text-align: center; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } 
 <ul class="first-ul"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> 

You could wrap the two inside of a div and style using accordingly. 您可以将两者包装在div和样式中,并使用相应的样式。

simple markup 简单标记

<div class="somediv">
    <ul></ul>
    <ul></ul>
</div>

simple css 简单的CSS

.somediv ul:first-child {
    background: blue
}

.somediv ul:nth-child(2) {
    background: red
}

You can use different class names (one common and one different name) to the UL and give as many properties as you wish. 您可以对UL使用不同的类名称(一个通用名称和一个不同名称),并根据需要提供任意数量的属性。

 .common{ list-style: none; padding: 0px; margin: 0px; font-family: arial; color:white; text-align: center; } .red { color:red; } .green { color:green; } 
 <ul class="common red"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> <ul class="common green"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> 

<div class="first">
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        </ul>
</div>
<div class="scnd">
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
    </ul>
</div>

add like this in your stylesheet 在样式表中添加这样的内容

.first ul
{
    Your style here
}
.scnd ul
{
    Your style here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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