Put text in div html

HTML — Insert text inside div container

I am using div because I want to colour not the whole border but the external corner, it works but now what I want to do is to add a text/link at the centre of the four corners. If I am going to do that the text stays quadruple on each corner and change even the position of the div corner. Is there a better way to do that? It seems simple as it is but I have been in stock with this for days, this is my code in a snippet:

html, body < color: #fff; background: #000; width: 100%; >.wholeDivDom < margin: 0.7%; >.outer < width: 350px; height: 120px; background: black; position: relative; >div:before, div:after < content: ""; position: absolute; height: 20%; width: 10%; >.outer:after < content: ""; right: 0; border-right: 3px solid #fff; border-top: 3px solid #fff; >.outer:before < border-left: 3px solid #fff; border-top: 3px solid #fff; >.inner:before < bottom: 0; border-left: 3px solid #fff; border-bottom: 3px solid #fff; >.inner:after

1 Answer 1

Simply use a span tag to add your text and apply position: absolute to it. Then giving it left:50% and top:50% with transform: translate(-50%,-50%); should do the trick.

html, body < color: #fff; background: #000; width: 100%; >.wholeDivDom < margin: 0.7%; >.outer < width: 350px; height: 120px; background: black; position: relative; >div:before, div:after < content: ""; position: absolute; height: 20%; width: 10%; >.outer:after < content: "Hello"; right: 0; border-right: 3px solid #fff; border-top: 3px solid #fff; >.outer:before < border-left: 3px solid #fff; border-top: 3px solid #fff; >span < position: absolute; transform: translate(-50%,-50%); left: 50%; top: 50%; >.inner:before < bottom: 0; border-left: 3px solid #fff; border-bottom: 3px solid #fff; >.inner:after

Источник

Читайте также:  Создание массива массивов php

How To Style the HTML element with CSS

This tutorial will introduce you to styling the HTML Content Division element—or element—using CSS. The element can be used to structure the layout of a page and break up a webpage into separate components for individual styling. In this tutorial, you will create and style elements, as well as learn how to add and style other elements inside a container. These skills will prepare you to use elements as layout tools later on in the series when you begin recreating the demonstration website.

The

element is used by adding opening and closing

tags to an HTML document. On its own, the

element typically has little visual effect on the presentation of a webpage. To specify the size, color, and other properties of a
element, you can assign it style rules using CSS.

Prerequisites

To follow this tutorial, make sure you have set up the necessary files and folders as instructed in a previous tutorial in this series How To Set Up You CSS and HTML Practice Project.

Exploring the Element in Practice

Let’s try a hands-on exercise to study how the element works. Erase everything in your styles.css file (if you added content from previous tutorials). Next, add the following CSS rule for the tag selector:

div  background-color: green; height: 100px; width: 100px; > 

Save the styles.css file. Next, return to your index.html file, erase everything that’s there (except for the first line of code: ) and add the following code snippet:

Notice that the element has opening and closing tags but does not require any content. Save the index.html file and reload it in your browser. (For instructions on loading an HTML file, please visit our tutorial step How To View An Offline HTML File In Your Browser).

Your webpage should display a green box 100 pixels wide and 100 pixels tall as specified by the CSS rule:

Webpage with green <div data-lazy-src=

Note that you have added the class as an attribute to the tag by adding the class attribute and class name to each opening tag. Save the file and reload it in your browser. You should receive something like this:

Adding and Styling Text in a Container

You can put text inside a container by inserting text in between the opening and closing tags. Try adding text inside each of the elements in your index.html file:

div class="div-1">Bluediv> div class="div-2">Reddiv> div class="div-3">Yellowdiv> 

Save the file and reload it in your browser. You should now have text displayed in each of your containers:

Webpage with elements containing text

You can add additional HTML elements to your text inside the elements. For example, try adding the HTML heading tags ( to ) to your text inside the tags in your index.html file:

div class="div-1">h2>Blueh2>div> div class="div-2">h3>Redh3>div> div class="div-3">h4>Yellowh4>div> 

Save the file and reload it in your browser. The text inside the containers should now be styled according to the default properties of the to tags:

Webpage with header text inside containers

Note that the elements have also adjusted their positions slightly. This repositioning is caused by the default margin properties of the through elements. You’ll learn more about margins in the next tutorial on the CSS Box Model, but for now it is fine to ignore them

To style text inside the containers, you can specify text property values in the rulesets for your classes. Try adding the properties and values to your rulesets in your styles.css file as highlighted in the in the following code snippet:

.div-1  background-color: blue; height: 50px; width: 50px; font-size: 10px; color: white; iu > .div-2  background-color: red; height: 100px; width: 100px; font-size: 20px; color: yellow; > .div-3  background-color: yellow; height: 200px; width: 200px; font-size:30px; color: blue; > 

Save your styles.css file and reload the index.html file in your browser. The text inside the containers should now be styled according to the CSS rules in your styles.css file:

Webpage with styled header text inside <div data-lazy-src=

How to put text content into div

I have tried a design as like in below image. clip It’s all done except one little part. You can see the text «best value» on top right corner. The text was overlapping the element. When i check with inspect element result shows like in below image. result How to get the text into that .triangle-skewed class. Thanks in advance.

.selected < box-shadow: 0px 0px 2px 0px rgba(0, 0, 0, 0.75); background: #00ADEF; text-align:center; text-transform: uppercase; >.selected .plan-title < background: #f47129; border-bottom: 1px solid #fff; padding: 6px 2px 6px 2px; position: relative; >.selected .plan-title h1 < color: #fff; margin-top: 13px; margin-bottom: 12px; font-size: 40px; font-weight: 400; >.selected .plan-title .triangle-skewed < border-left: 100px solid transparent; border-right: 0px solid transparent; border-top: 80px solid #000000; border-bottom: transparent; position: absolute; top: 0; width: 0; height: 0; right: 0; >.selected .plan-title .triangle-skewed p < color: #fff; font-size: 14px; >.selected .plan-content < background: #00ADEF; >.selected .plan-content .h1-powof < font-weight: 400; font-size: 74px; color: #fff; >.selected .plan-content .h1-powof span < font-weight: 500; font-size: 40px; color: #fff; vertical-align: top; margin-right: 2px; margin-top: 12px; display: inline-block; >.selected .plan-content .month < color: #050505; font-weight: 400; font-size: 23px; >.selected .plan-content .content-p < color: #050505; font-weight: 400; font-size: 14px; >.selected .plan-content .content-p span < color: #fff; >.selected .plan-content .content-p-margin < color: #050505; font-weight: 400; margin-bottom: 9px; font-size: 14px; >.selected .plan-content .content-p-margin span < color: #fff; >.selected .plan-content .content-p-shipping < color: #050505; font-weight: 400; font-size: 23px; padding-top: 8%; >.selected .plan-content .content-p-shipping-no-space < color: #050505; font-weight: 400; font-size: 23px; padding-top: 3px; >.selected .plan-content .pick-btn < text-transform: uppercase; margin-bottom: 3%; background: #f47129; border-radius: 4px; width: 80%; color: #fff; border: none; font-weight: 400; font-size: 24px; padding: 12px 10px 12px 10px; box-shadow: 0px 0px 1px 0px #050505; transition: .2s; >.selected .plan-content .pick-btn:hover
 

ruby

best value

$49

per month

4x 15ml bottles of ejuice

guaranteed 60ml of ejuice

1 pack royal wires by dryx

$66 retail value

free shipping

Источник

How to append text to a div element?

Danger, Will Robinson! This adds HTML, not text. If your Extra Stuff is provided by the user, you’ve just introduced a security vulnerabilty. Better to use @Chandu’s answer below.

Yes, it’s an XSS vulnerability. You’re far better off creating a text node with the content instead, as describe in the answer below.

This also woudn’t work in case div contains elements with event listeners or inputs with user-entered text. I recommend the answer by Chandu.

Yes, I definitely do not recommend this as this will destroy state of any checkboxes, event listeners.

var theDiv = document.getElementById(""); var content = document.createTextNode(""); theDiv.appendChild(content); 

Using innerHTML:
This approach will remove all the listeners to the existing elements as mentioned by @BiAiB. So use caution if you are planning to use this version.

var theDiv = document.getElementById(""); theDiv.innerHTML += ""; 

I use this method with my «contenteditable» element with angularjs binding, and everything work correctly!

Should be the accepted answer indeed. Not only a beautiful way, but innerHTML will rebuild the DOM, and that is just not a good solution. Use appendChild().

This is better, but createTextNode won’t work if you are loading HTML. If you wanted to add list items, for example, this wouldn’t work. That is pretty limiting.

@Jake If HTML needs to be inserted instead of plain text, then don’t use createTextNode . There are several other methods for DOM manipulation related to this, e.g. insertAdjacentHTML , insertAdjacentElement , etc. Can’t call it “limiting” if you’re using the wrong tools. The question, though, could be clearer as to what is meant by “data”.

Beware of innerHTML , you sort of lose something when you use it:

theDiv.innerHTML = theDiv.innerHTML + 'content'; 

Which will destroy all nodes inside your div and recreate new ones. All references and listeners to elements inside it will be lost.

If you need to keep them (when you have attached a click handler, for example), you have to append the new contents with the DOM functions(appendChild,insertAfter,insertBefore):

var newNode = document.createElement('div'); newNode.innerHTML = data; theDiv.appendChild(newNode); 

yes but this will cause there to be an extra div inside the parent div which is not needed and might mess up some css styles

@Neal no it’s not. It’s neither correct or incorrect. It just depends on what the OP needs to append: text, html code or something else.

@Neal this is a perfectly good way of appending the data, and is more versatile than document.createTextNode() .

If you want to do it fast and don’t want to lose references and listeners use: .insertAdjacentHTML();

«It does not reparse the element it is being used on and thus it does not corrupt the existing elements inside the element. This, and avoiding the extra step of serialization make it much faster than direct innerHTML manipulation.»

Supported on all mainline browsers (IE6+, FF8+,All Others and Mobile): http://caniuse.com/#feat=insertadjacenthtml

// 
one
var d1 = document.getElementById('one'); d1.insertAdjacentHTML('afterend', '
two
'); // At this point, the new structure is: //
one
two

Источник

Оцените статью