The Wayback Machine - https://web.archive.org/web/20240530105024/https://www.geeksforgeeks.org/how-to-set-width-and-height-of-span-element-using-css/
Open In App

How to Set Width and Height of Span Element using CSS ?

Last Updated : 05 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The <span> tag is used to apply styles or scripting to a specific part of text within a larger block of content. The <span> tag is an inline element, you may encounter scenarios where setting its width and height becomes necessary. This article explores various approaches to set the width and height of a <span> element using CSS.

Method 1: Using Inline Styles

The basic method is to set the width and height of a <span> element using inline styles directly within the HTML tag.

In this example, the style attribute is used to set the width to 200px and the height to 100px. Additionally, display: inline-block; is applied to allow the <span> to have width and height properties.

Example:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <title>
        Set Width and Height of 
        Span with Inline Styles
    </title>
</head>
  
<body>
    <span style="
        width: 200px; 
        height: 100px; 
        display: inline-block;
        background-color: rgb(6, 104, 42);">
        GeeksforGeeks
    </span>
</body>
  
</html>


Output:

span-element

Method 2: Using CSS Classes

To apply the same width and height styles to multiple <span> elements, you can define a CSS class and then use that class in your HTML.

Here, the CSS class .resizable-span is defined with the desired width, height, and other styles. Each <span> with the class resizable-span will inherit these styles.

Example:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <title>
        Set Width and Height of 
        Span with Inline Styles
    </title>
  
    <style>
        .resizable-span {
            width: 200px; 
            height: 100px; 
            display: inline-block;
            background-color: rgb(6, 104, 42);
        }
    </style>
</head>
  
<body>
    <span class="resizable-span">
        GeeksforGeeks
    </span>
</body>
  
</html>


Output:

span-element

Method 3: Using Flexbox

Flexbox is a powerful layout model in CSS, and it can be utilized to set the width and height of a <span> element while allowing for more complex layouts.

In this example, a <div> with the class flex-container is used as a flex container, and the <span> elements within it are given the class resizable-span. The display: flex; property on the container enables the use of Flexbox layout.

Example:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <title>
        Set Width and Height of 
        Span with Inline Styles
    </title>
  
    <style>
        .flex-container {
            display: flex;
        }
  
        .resizable-span {
            width: 200px;
            height: 100px;
            background-color: green;
            color: white;
        }
    </style>
</head>
  
<body>
    <div class="flex-container">
        <span class="resizable-span">
            Span 1
        </span>
          
        <span class="resizable-span">
            Span 2
        </span>
    </div>
</body>
  
</html>


Output:

span-element-2



Similar Reads

HTML width/height Attribute vs CSS width/height Property
In HTML 5, few elements follow the width and height attributes and maximum elements do not follow this attribute. Like img, iframe, canvas, and svg kind of elements follow the width and height attributes but div, span, article and section type of elements don't follow them.The width and height attributes are affected in img, svg tags, those are wea
3 min read
How to Set the Fixed Width of a Span Element in CSS ?
Setting a fixed width of a span element is useful when we need to control the size of a specific inline element. The span element is mostly used in styling specific parts of text and other inline elements. There are various approaches to setting a fixed width of a span element in CSS including, width property, max-width property, and min-width prop
4 min read
How to set width and height of background image in percent with respect to parent element in CSS ?
If we are using an image as a child and want to set the height and width in percentage then we need to set the parent element with some fixed size. Approach 1: Here we will use CSS inside the tags which are also known as inline CSS.For the parent div we will give a fixed size by giving height: 500px and width: 40% according to screen size, and we w
2 min read
How to add Background-Color for Text Width Instead of Entire Element Width using CSS ?
In this article, we will see how to add background color for the text width instead of the entire element width using CSS. The display: inline or display: inline-block property is used to set the background color of the text width. Syntax: display: inline; display: inline-block; Approach: We will create an element and then apply the display: inline
2 min read
How to dynamically set the height and width of a div element using jQuery ?
Set the height of a div element using jQuery The content height of a div can dynamically set or change using height(), innerHeight(), and outerHeight() methods depending upon the user requirement. If the user wants to change the content height of a div dynamically, it includes changing the actual height, actual height with padding, and actual heigh
7 min read
How to wrap or break long text/word in a fixed width span ?
In this article, we will see how to wrap long text/word in a fixed-width span. The word-wrap property in CSS is used to break long words and wrap them into the next line. It defines whether to break words when the content exceeds the boundaries of its container. Syntax: word-wrap: normal|break-word|initial|inherit; Example 1: The following code dem
1 min read
How to set the Width and Height in Tailwind CSS ?
Setting the width and height in Tailwind CSS involves using utility classes like w-[size] and h-[size]. These classes allow you to easily define the width and height of elements in your project. Setting WidthTo define the width of an element, employ the w-[size] classes, replacing [size] them To establish an element's heightwith options like 1/2, f
1 min read
How to set height equal to dynamic width (CSS fluid layout) ?
To create a fluid layout in CSS, set an element's height to the same value as its dynamic width. This makes the layout more adaptable and responsive since the element's height will automatically change depending on the viewport's width. It is possible to determine the element's height by using the padding by setting the height attribute to 0. Using
2 min read
How to Set Height Equal to Dynamic Width (CSS fluid layout) in JavaScript ?
A key component of constructing a contemporary website in web development is developing an adaptable and dynamic layout. Setting the height of an element equal to its dynamic width, often known as a CSS fluid layout, is a common difficulty in accomplishing this. When the screen size changes and you want to keep an element's aspect ratio, like that
4 min read
How to Set Viewport Height & Width in CSS ?
Set viewport height and width in CSS is essential for creating responsive and visually appealing web designs. We'll explore the concepts of setting viewport height and width by using various methods like CSS Units, Viewport-Relative Units, and keyframe Media Queries. Table of Content Using CSS UnitsUsing Viewport-Relative UnitsUsing CSS Media Queri
3 min read