只在DIV内的文本上应用样式。

huangapple go评论83阅读模式
英文:

Apply styling only on the text inside a DIV

问题

我有一个在服务器端动态创建的HTML中的div。我只想在该div上应用CSS(前端),仅当它具有某些内容时。如果它没有任何内容,那么我就不需要应用新的样式。

HTML代码示例如下:

<div class="attr-marker">
    Some-text-content        <!-- 在此应用新样式 -->
</div>

<div class="attr-marker">
    <!-- 不需要新样式 -->
</div>

<div class="attr-marker">
    <!-- 不需要新样式 -->
    <i class="fas fa-car" style="color:#d42424;font-size:px"></i>
</div>

我尝试过的CSS如下,但失败了:

.attr-marker text {
    display: block;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    line-height: 12px;
    font-size: 9px;
    text-align: center;
    background: #000;
    color: #fff;
}

我可以使用JavaScript来实现,但我希望纯粹使用CSS来解决,这样可以帮助我减少代码量。

英文:

I'm having a div in HTML which is dynamically creating from the server side. I want to apply css in HTML(front-end) only on that div if and only if its having some-content. If it doesn't have any content then I have no need to apply the new styling.

The sample of HTML code is:

&lt;div class=&quot;attr-marker&quot;&gt;
    Some-text-content        &lt;!-- Apply New Styling on it --&gt;
&lt;/div&gt;

&lt;div class=&quot;attr-marker&quot;&gt;
    &lt;!-- No need of new styling --&gt;
&lt;/div&gt;

&lt;div class=&quot;attr-marker&quot;&gt;
    &lt;!-- No need of new styling --&gt;
    &lt;i class=&quot;fas fa-car&quot; style=&quot;color:#d42424;font-size:px&quot;&gt;&lt;/i&gt;
&lt;/div&gt;

And the CSS which I tried but failed is:

.attr-marker text {
    display: block;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    line-height: 12px;
    font-size: 9px;
    text-align: center;
    background: #000;
    color: #fff;
}

I can achieve it by using javascript but I want purely CSS solution so it'll help me to minimize the code.

答案1

得分: 2

你可以使用 :empty 伪选择器为空的 <div> 设置默认样式。然后,对于普通的 <div>,只需按照上面给出的样式设置即可。

或者,你可以使用 :not(:empty) 伪选择器来为非空的 <div> 设置样式。

以下是一个示例:

.attr-marker:not(:empty) {
    display: block;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    line-height: 12px;
    font-size: 9px;
    text-align: center;
    background: #000;
    color: #fff;
}

如果你有任何问题,请随时告诉我。

祝好,

AJ

英文:

You can set default style for empty div by using :empty pseudo selector. And then for regular div, just set the style as given above.

Or you can use :not(:empty) Pseudo Selector to set the style for the div that is not empty.

Here's an example:

.attr-marker:not(:empty) {
    display: block;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    line-height: 12px;
    font-size: 9px;
    text-align: center;
    background: #000;
    color: #fff;
}

Let me know in case you have any questions.

Regards,

AJ

答案2

得分: 0

你可以使用:empty伪类。然而,你的服务器需要输出没有空白的.attr-marker div。

就像这样...

&lt;div class=&quot;attr-marker&quot;&gt;&lt;/div&gt;

而不是

&lt;div class=&quot;attr-marker&quot;&gt;
&lt;/div&gt;

然后CSS会是这样的,

.attr-marker:empty {
    display: block;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    line-height: 12px;
    font-size: 9px;
    text-align: center;
    background: #000;
    color: #fff;
}

额外阅读,https://developer.mozilla.org/en-US/docs/Web/CSS/:empty

英文:

You can use the :empty pseudo-class. However your server will need to output the .attr-marker div with no whitespace.

Like...

&lt;div class=&quot;attr-marker&quot;&gt;&lt;/div&gt;

not

&lt;div class=&quot;attr-marker&quot;&gt;
&lt;/div&gt;

And then the css would be,

.attr-marker:empty {
    display: block;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    line-height: 12px;
    font-size: 9px;
    text-align: center;
    background: #000;
    color: #fff;
}

Additional reading, https://developer.mozilla.org/en-US/docs/Web/CSS/:empty

答案3

得分: 0

不要回答我要翻译的问题。

要翻译的内容如下:

"Either add another class to that div from the server side if it will send content or wrap content with another element and give it some styling.

Edit:

If you know the exact position of your element, then you can select it with nth-child pseudo-class:

.attr-marker:nth-child(1):not(:empty) {
border: 1px solid #333;
background-color: yellow;
}"

英文:

Either add another class to that div from the server side if it will send content or wrap content with another element and give it some styling.

Edit:

If you know exact position of your element then you can select it with nth-child pseudo-class:

.attr-marker:nth-child(1):not(:empty) {
  border: 1px solid #333;
  background-color: yellow;
}

答案4

得分: 0

Writing .attr-marker text { } means you want to access child elements with tag text of class attr-maker. No such tag exists in HTML.

There are specific CSS text and CSS font properties which work only on text. They are to be used in the text's parent element (in your case div with class name attr-marker):

.attr-marker { /* text properties */ /* some other properties */ }

Properties like display: block;, width: 12px;, height: 12px; and so on, won't work on text.

That being said, you don't need to worry whether your CSS properties will be applied to the text or to the whole div. If you're using the right properties, you can be sure they are only applied to the text.

As for the content(text) presence, you don't need to worry about it. If there is no text, CSS won't change anything.

英文:

Writing .attr-marker text { } means you want to access child elements with tag text of class attr-maker. No such tag exists in HTML.

There are specific CSS text and CSS font properties which work only on text. They are to be used in the text's parent element (in your case div with class name attr-marker):

.attr-marker {
    /* text properties */
    /* some other properties */
}

Properties like display: block;, width: 12px;, height: 12px; and so on, won't work on text.

That being said, you don't need to worry whether your CSS properties will be applied to the text or to the whole div. If you're using the right properties, you can be sure they are only applied to the text.


As for the content(text) presence, you don't need to worry about it. If there is no text, CSS won't change anything.

答案5

得分: 0

如果这些标记是块级渲染元素,浏览器不应该显示它们,除非它们有内容,因此您可以信任浏览器不会渲染没有内容的元素,请使用下面的max-width和max-height属性:

.attr-marker {
    display: block;
    max-width: 12px;
    max-height: 12px;
    border-radius: 50%;
    line-height: 12px;
    font-size: 9px;
    text-align: center;
    background: #000;
    color: #fff;
    /*如果需要的话*/
    overflow: hidden;
}
英文:

If these markers are block rendered elements, the browser should not display them, unless they have content, therefore you can trust the browser to not render the elements with no content, use the max-width and max-height properties below:

.attr-marker {
    display: block;
    max-width: 12px;
    max-height: 12px;
    border-radius: 50%;
    line-height: 12px;
    font-size: 9px;
    text-align: center;
    background: #000;
    color: #fff;
    /*If required*/
    overflow:hidden
}

huangapple
  • 本文由 发表于 2020年1月3日 18:21:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定