- HTML Tag
- Syntax
- Example of the HTML tag:
- Example of the HTML tag with the class attribute:
- Attributes
- How to style tag?
- Common properties to alter the visual weight/emphasis/size of text in tag:
- Coloring text in tag:
- Text layout styles for tag:
- Other properties worth looking at for tag:
- How to Set Width and Height of Span in CSS
- What is “display” Property in CSS?
- Example 1: Setting Width and Height of Span Using “block”
- HTML
- CSS
- Example 2: Setting Width and Height of Span Using “inline-block”
- Conclusion
- About the author
- Sharqa Hameed
- Set width and height of a span in CSS
- Set the Width and Height of a Span
- Use display: block to Make Width and Height Effective
- Conclusion
- Related posts:
HTML Tag
The HTML tag is used to define a small piece of content or text within a larger document that needs to be styled differently than the surrounding text. It is an inline element that can be used to apply styles, such as color or font, to a specific section of text.
The tag does not have any semantic meaning on its own, but it can be used in conjunction with other tags, such as or
, to give them additional styling or functionality. It is a useful tool for web designers and developers who need to make small adjustments to text without affecting the overall structure of the page.
For example, you can use the tag to highlight a specific word within a paragraph or to apply a different font size to a single character. The tag can also be used with CSS to create hover effects, animations, and other dynamic features on a web page.
The span tag is somehow similar to the div tag. But there are some differences. While the HTML tag is used to define a small piece of content or text within a larger document that needs to be styled differently than the surrounding text, the div tag is used to define a larger section or block of content that can contain other HTML elements. The div tag is a container element that is often used to group related content together and apply styling to the entire block. It is a block-level element, which means it takes up the full width of its parent container and forces a line break before and after the element.
Syntax
The tag comes in pairs. The content is written between the opening () and closing () tags.
In the example below we gave a style just inside the tag.
Example of the HTML tag:
html> html> head> title>Title of the document title> head> body> p>My cat has span style="color:#8ebf42;">green span> eyes. p> body> html>
Let’s see another example where we added class attribute to the tag and gave styles to the content of the tag separately.
Example of the HTML tag with the class attribute:
html> html> head> title>Title of the document title> style> .letter < color: red; font-size: 300%; /* Font size in percents */ position: relative; /* Relative positioning */ top: 7px; /* Move from above */ > style> head> body> p> span class="letter">О span> She brought in disgusting, disturbing yellow flowers in her hands. And these flowers stood out on her black coat. p> p>Michael Bulgakov p> body> html>
Attributes
Global Attributes refer to attributes that are used on any HTML element. These attributes are common for all elements in HTML.
An event occurs when a browser reacts to a particular user’s action. The user generates an event when clicking on a mouse, playing video, uploading a document or an image, or performing any other action on the website.
How to style tag?
Common properties to alter the visual weight/emphasis/size of text in tag:
- CSS font-style property sets the style of the font. normal | italic | oblique | initial | inherit.
- CSS font-family property specifies a prioritized list of one or more font family names and/or generic family names for the selected element.
- CSS font-size property sets the size of the font.
- CSS font-weight property defines whether the font should be bold or thick.
- CSS text-transform property controls text case and capitalization.
- CSS text-decoration property specifies the decoration added to text, and is a shorthand property for text-decoration-line, text-decoration-color, text-decoration-style.
Coloring text in tag:
- CSS color property describes the color of the text content and text decorations.
- CSS background-color property sets the background color of an element.
Text layout styles for tag:
- CSS text-indent property specifies the indentation of the first line in a text block.
- CSS text-overflow property specifies how overflowed content that is not displayed should be signalled to the user.
- CSS white-space property specifies how white-space inside an element is handled.
- CSS word-break property specifies where the lines should be broken.
Other properties worth looking at for tag:
- CSS text-shadow property adds shadow to text.
- CSS text-align-last property sets the alignment of the last line of the text.
- CSS line-height property specifies the height of a line.
- CSS letter-spacing property defines the spaces between letters/characters in a text.
- CSS word-spacing property sets the spacing between words.
How to Set Width and Height of Span in CSS
In HTML, Span is an inline container used for texture content and styling them. But you can not set the width and height of the span as normally set for the other HTML elements. In such a scenario, use the “display” property of CSS and then set the width and height of the span according to your requirements.
In this manual, we will discuss how to adjust the height and width of a span in CSS.
What is “display” Property in CSS?
The “display” property is used to set the display behavior of the HTML element. This CSS property can be utilized to specify whether an element should be treated as inline or block or for setting the layout of its children. To be more specific, you can utilize it to adjust the width and height of the span in CSS.
The syntax of the display property is:
Here is the description of the above-given values:
- block: It is used to set the width and height of the element.
- inline-block: It is used to set the margins and padding of the element.
Now, move to the examples in which we will set the width and height of the span using the block and inline-block values of the display property.
Example 1: Setting Width and Height of Span Using “block”
To set the width and height of the span, first create a span in HTML using the tag:
HTML
In CSS, set the width and height of the span using the “display” property. To do so, use the “span” to access it. After that, set the value of the display property as “block” and its width and height as “400px” and “150px”. Moreover, set the background color of the span as “rgb(11, 235, 243)” and the border of the span as “5px” width, “solid” shape, and “rgb(47, 0, 255)” color.
CSS
background : rgb ( 11 , 235 , 243 ) ;
border : 5px solid rgb ( 47 , 0 , 255 ) ;
As you can see, we have successfully set the width and height of the span in CSS:
Let’s move to the next example
Example 2: Setting Width and Height of Span Using “inline-block”
In this example, we will use the “inline-block” value of the display property to adjust the height and width of the span.
For this purpose, we will create a span in the HTML same as the previous example, and then move to the CSS and set the value of the display property as “inline-block” and set the width and height of the span as “400px” and “150px”. Moreover, set the background color of the span as “rgb(209, 173, 95)” and the border of the span as “5px”, “solid”, and “rgb(255, 166, 0)”:
background : rgb ( 209 , 173 , 95 ) ;
border : 5px solid rgb ( 255 , 166 , 0 ) ;
This will give us the following outcome:
From above given examples, it can be observed that using the “block” and “inline-block” values of the display property, the height and width properties of CSS can be adjust.
Conclusion
By using the display property’s “block” and “inline-block” values, the height and width of the span can be adjusted. You can utilize one of them according to your choice; both give the same result. In this manual, we have explained the procedure related to setting the width and height of the span using the CSS display property.
About the author
Sharqa Hameed
I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.
Set width and height of a span in CSS
When building a website, we often need to set the width and height of elements to fit them in the layout. But, you might have at least once faced a problem where you need to set the width and height of a but it didn’t work.
The reason why the width and height properties do not affect a element is that the element is an inline element. An inline element does not start on a new line and only takes up as much space as required.
Have a look at the below image:
As you can see from the above image, the span element only takes up as much space(width) as required to fit its text. Whereas, the paragraph element takes up the full width of its parent element even if that much space was not required to fit its text.
This is because the paragraph is a block-level element whereas the is an inline-level element.
Set the Width and Height of a Span
As previously said, the is an inline-level element, therefore it does not support the width and height properties.
So, if we anyhow want to give the width and height to a span, we have to change its default display type and set it to either inline-block or block .
The inline-block elements do also not start on a new line like the inline elements, but they do support the width and height properties.
So, to set the width and height of a span element, we will first change its display type to inline-block using the display property and then apply the desired width and height.
Let’s say we have a span element in our HTML document and we want to give it a width of 300px and height of 100px:
Now, set its display property to inline-block and apply the width and height properties.
/*Set width and height of span*/ .mySpan
Below is the outcome of the above code:
Use display: block to Make Width and Height Effective
If you set an element’s display property to block , by default, it takes up the full width of its parent element irrespective of the content it has.
Such elements also support the width and height properties. So, if you set the display property of the span element to block , the width and height properties become effective.
Let’s say we have the following span in our HTML document:
To set its width and height, apply display: block; and then use the width and height properties.
/*Set width and height of span*/ .mySpan
After running the above code, you will get the following output:
Conclusion
In this article, we learned why the width and height properties do not work on a span element and how can we make them effective.
The span is an inline element and only takes up as much space as it requires to fit its content. Which is why the width and height properties become ineffective.
So, if we want to set the width and height of a span element, we have to first change its display type to either inline-block or block . Which we can do using the display property. After that, you can easily set the width and height of the span.