search
HomeWeb Front-endHTML TutorialInsert content around text using CSS

Insert content around text using CSS

Jul 18, 2017 pm 05:23 PM
csscontent

CSS solution for inserting content around text

This article will discuss how to insert icons around text, how to control the positional relationship between them, through the rationality of HTML structure and the use of CSS attributes To compare the advantages and disadvantages of the effects achieved by different solutions.

Common design draft requirements

  • Insert icons, lines, triangles, circles before, after, above and below the text

  • The inserted elements must achieve spacing, alignment (center, top, baseline) and other positional relationships with the text.

ethical knowledge

  • Flexibly use display, background and other attributes

  • Pass :before and :after cooperate with the content attribute to insert content.

  • Realize the positional relationship between text and symbols through absolute, vertical, margin, line-height and other attributes.

  • Graphics that can be drawn using CSS properties use CSS properties, otherwise use the background property to display the background image

Practical operation

  • Solution 1: Change the HTML structure by adding tags before or after text elements such as <span></span> and <i></i>

  • Solution 2: Directly use pseudo-elements: before and :after (not supported below Ie7)

    • Must have content attribute

    • The inserted element is an inline element, which needs to be explicitly defined as a block-level element before the height can be set , padding, margins, etc

Line

Insert content around text using CSS
Insert content around text using CSS

##html
<div>
    <h2>
        <span>前端技术</span><i>前端技术前端技术</i>
    </h2>
</div>
css
.article-block-title {
    height: 44px;
    /*实现文本与竖线对齐*/
    line-height: 44px;
    border-left: 3px solid #72b16a;
    padding-left: 20px;
}
Analysis
  • Directly use the container of the text to use border-left, border-right, border-top, and border-bottom to display only the text. Lines up, down, left, and right.

  • For inline, inline-block, etc., you can use line-height to center the text and vertical lines.

Insert content around text using CSS##html

<p>
    <i></i>resto restaurant home page website template<i></i>
</p>
css
.text-info .line {
    display: inline-block;
    width: 40px;
    border-top: 1px solid #fff;
    /*使横线居中*/
    vertical-align: middle;
    /*for IE7*/
    *margin-top: 22px;
}
Analysis
    Adding
  • i

    and span tags before and after the text is clearer than using pseudo-elements: before and :after.

  • vertical-align:middle achieves vertical centering of lines and text.
    • This attribute is invalid in ie7
    • can be implemented using margin-top (prerequisite is to know the parent-element height
html

<div>The menu</div>

css
.menu-tips:after {
    position: absolute;
    left: 0;
    bottom: 0;
    content: "";
    width: 0;
    height: 0;
    /*menu是156px宽,所以这里设置78px*/
    border-left: 78px solid transparent;
    border-right: 78px solid transparent;
    border-bottom: 10px solid #fff;
}
Analysis
    Passed The transparent attribute works with the border to implement the triangle.
  • Note that we can use the position attribute to insert :before and :after at any position, not just "before" or "after". The line on the right can be placed directly below the text "Become our volunteer"
  • ##circle

##html

<div>
    <a></a>
    <a></a>
    <a></a>
    <a></a>
</div>
Insert content around text using CSScss
.index-panel-header .btn-group {
    float: right;
    /*清除行内元素的4px空白间距*/
    font-size: 0;
}

.index-panel-header .btn {
    display: inline-block;
    margin-left: 11px;
    width: 9px;
    height: 9px;
    background: #dedede;
    /*画圆*/
    -moz-border-radius: 5px; /* Firefox */
    -webkit-border-radius: 5px; /* Safari 和 Chrome  */
    border-radius:5px; /* Opera 10.5+, 以及使用了IE-CSS3的IE浏览器 */
    /*for ie7、8*/
    position: relative;
    z-index:2;
    behavior: url(../ie-css3.htc); /* 通知IE浏览器调用脚本作用于'btn'类 */
}
Analysis
  • border-radius cannot be used below IE8 and needs to be forced

  • ##Trick1: Replace with image background.

    • ##Trick2: Call the script ie-css3.htc to make IE browser support css3 attributes
    • ##The current element. There must be positioning attributes, such as position:relative or position:absolute attributes.

    The z-index value must be higher than the surrounding elements
  1. ##.

    #Custom icon
  2. ##html
  3. <div>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
    </div>
css
.star-bar {
    font-size: 0px;
}

.star {
    display: inline-block;
    width: 10px;
    height: 10px;
    margin-right: 5px;
    background: url("../images/index-star.png") no-repeat;
}

.nostar {
    background-position: 0 -10px;
}

Analysis

Insert content around text using CSS

Here are some methods for scoring and other requirements, using the background attribute to display pictures
##Extended knowledge
    About how to center. Element, here is a cheat tool: How to center an element
  • Summary

    If there are multiple icon symbols in a row, use HTML tags to represent them.
  • If you are inserting a single symbol, use pseudo-element > to add additional HTML tags without considering compatibility.

The above is the detailed content of Insert content around text using CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the difference between an HTML tag and an HTML attribute?What is the difference between an HTML tag and an HTML attribute?May 14, 2025 am 12:01 AM

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

The Future of HTML: Evolution and TrendsThe Future of HTML: Evolution and TrendsMay 13, 2025 am 12:01 AM

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment