简体   繁体   English

CSS选择器:如何在div中引用属性

[英]Css selector : How to reference a attribute inside a div

<div id ="group">
    <img src= ' ' id = '1'>
    <img src= ' ' id = '2'>
    .......
</div>

I would like to reference all the img in the div id 'group' , how to mark it in css? 我想引用div id'group'中的所有img,如何在CSS中对其进行标记? Also, Which is better? 另外,哪个更好? Assign all to img / create a class eg. 全部分配给img /创建一个类,例如。 #img.groupItem and reference it everytime? #img.groupItem并每次引用? Thanks 谢谢

The source code: 源代码:

<div id="book">
    <img alt="flip book" src="demo/medium/Web081112_P001_medium.jpg">
    <img alt="flip book" src="demo/medium/Web081112_P002_medium.jpg">
    <img alt="flip book" src="demo/medium/Web081112_P003_medium.jpg">
</div>

After running the javascript: 运行javascript之后:

<div id="page" style="display: block; width: 1180px; height: 767px; left: 50%; top: 50%; margin: -413.5px auto 0px -590px; cursor: default;">
    <div id="book" style="position: relative; width: 1180px; height: 767px; transform: translate3d(0px, 0px, 0px);">
        <div class="turn-page-wrapper" page="1" style="position: absolute; overflow: hidden; width: 590px; height: 767px; top: 0px; right: 0px; left: auto; bottom: auto; z-index: 0; display: none;">
            <div id="cover" class="turn-page p1" style="width: 590px; height: 767px;">
            </div>
        </div>
        <div class="turn-page-wrapper" page="2" style="position: absolute; overflow: hidden; width: 590px; height: 767px; top: 0px; left: 0px; right: auto; bottom: auto; z-index: 7;">
            <div style="position: absolute; top: 0px; left: 0px; overflow: hidden; z-index: auto; width: 968px; height: 968px;">
                <div class="turn-page p2" style="width: 590px; height: 767px; position: absolute; top: 0px; left: 0px; bottom: auto; right: auto;">
                    <img alt="flip book" src="demo/medium/Web081112_P001_medium.jpg">
                </div>
            </div>
        </div>
    </div>
</div>

The html code is like that for a single img , I used the answer provided here but still not work. html代码类似于单个img的代码,我使用了此处提供的答案,但仍然无法正常工作。 I believe this is the problem caused by generate html code? 我相信这是由生成html代码引起的问题吗?

CSS was meant to apply global styling with the help of selectors . CSS的目的是在选择器的帮助下应用全局样式。

The descendant selector is what you are looking for: 后代选择器是您要寻找的:

#group img {
    // code
}

You can create a selector to reference those, yes: 您可以创建一个选择器来引用这些选择器,是的:

#group img { width: 32px; height: 32px }

However, if possible, it's better to just create a class for what you want to do: 但是,如果可能的话,最好只是为您想要做的事情创建一个类:

.icon { width: 32px; height: 32px }

Basically you want to decide if this really is something you only want to apply at that location. 基本上,您想确定这是否确实是您只想在该位置应用的内容。 Is that HTML structure how it always is going to be? HTML结构会一直保持下去吗? What if you change the ID? 如果您更改ID怎么办? What if you use a div with a background image instead of an img tag? 如果您将div与背景图片一起使用而不是img标签,该怎么办? Those are the sorts of questions you want to be asking yourself. 这些是您想问自己的问题。

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

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