开发者

Style by tag in a div tag

开发者 https://www.devze.com 2023-02-20 20:26 出处:网络
not sure if this is possible but, can you style a single tag within a div tag? EG, could i put a <div> a开发者_Go百科round a section of my page then say make all <h2> tags green? is this p

not sure if this is possible but, can you style a single tag within a div tag? EG, could i put a <div> a开发者_Go百科round a section of my page then say make all <h2> tags green? is this possible or do you have to use classes? thanks


If I understand what you're asking correct, then yes, it is possible. Here's an example. For more information, read up on CSS selectors.

On re-reading the question, the only way to style an arbitrary element within a div is by using either classes or id (with CSS selectors). Here's another example styling a single item using selectors.


For any single arbitrary element you must use an ID or a single-use class and select that:

#green-headings h2 { color: green; }

However, if there is some structural information about your <div> that can be queryable with CSS selectors, you can use CSS pseudo-classes without needing to add an ID or class to your markup. For example, if your <div> is always the first child of body:

body > div:first-child h2 { color: green; }


background to green HTML

<div> 
   <p>hello everyone!</p>
   <div> hi. i'm a green bgd box</div>
   <p>nice. i don't have background.</p>
</div>

CSS

div > div {background-color: green}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号