ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Set Divs not larger than its contents using CSS

Here, we are going to learn how to set divs which are not larger than its contents using CSS?
Submitted by Anjali Singh, on February 08, 2020

Trivia:

No amount of discussion is enough when we are talking about divs. It is a very concept aspect of website or web page development and so are its properties and functions. What can you not do with divs? Every fundamental thing can be achieved if we make use of divs. The best part of divs is that it is not even that brain-wrecking to implement, you just have to create a “div” tag and there you go, do whatever you feel like with it. From creating a class to appending CSS and much more. Therefore, divs are very crucial elements of any website or web page and one must know how to use divs thoroughly if they wish to be a good developer. Now, since we have discussed so much about divs, why don't we move ahead with the article and discuss the topic at hand.

Discussion:

Now that you know the capabilities of using divs and what sorts of possibilities this tool has to offer, why don't we take this discussion a step ahead and dive more into the possibilities of divs? Therefore, in this section, we will be discussing how we can create divs larger than its contents using CSS? Well, CSS and divs are something inseparable, they go hand in hand. Using CSS we would be able to style our div elements and that would again help in creating a responsive website or web page. Now, the question is how do we create divs larger than its contents? To find the answer to that keep on reading!

Steps:

To achieve this task is not very tough rather it would seem to be very familiar to you, so all you gotta do is pay close attention and try to follow each step that is going to be mentioned below,

  1. First and foremost, go ahead and create a <div> tag with a class named box, then add <h2> tag in the div and write any content between the tags.
  2. The role of CSS steps in, choose colors for your text and maybe try to add some background color by using color properties.
  3. Next, you will need to make use of display property to define the type of box which you would be used for HTML. Suppose you take the inline-block value for the display property.
  4. Try to add some padding around the content.

Example:

<!DOCTYPE html>

<html>

<head>
    <style>
        .box {
            background-color: #f40;
            color: #fff;
            width: fit-content;
            height: fit-content;
        }
    </style>
</head>

<body>
    <center>
        <div class="box">
            <h1>This is IncludeHelp!</h1>
            <h2>IncludeHelp is a learning platform.</h2>
        </div>
    </center>
</body>

</html>
Set Divs not larger than its contents using CSS

In the above example, fit-content value is applied to both the height and width of the div.

Conclusion:

That's it! That is all to it. Pretty easy right? I told you you would be familiar and this is the way how you create divs not larger than its contents. You can also add width and height property and you can set it to fit-content.

CSS Tutorial & Examples »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.