How To Set Auto Width In Css
How to set div width to fit content using CSS ?
There are three ways to solve this trouble which are listed below:
- By default case
- Using inline-block property
- Using fit-content property in width and pinnacle
Default Case: HTML div is by default fit to content inside it. The example goes like this:
Instance 1:
<!DOCTYPE html>
<
html
lang
=
"en"
dir
=
"ltr"
>
<
head
>
<
meta
charset
=
"utf-8"
>
<
title
>GeeksforGeeks Case</
title
>
<
style
media
=
"screen"
>
trunk {
background: orange;
overflow: hidden;
color: white;
}
.GeeksForGeeks {
text-align: centre;
background: dodgerblue;
position: absolute;
peak: 50%;
left: 1%;
right: 1%;
}
</
style
>
</
caput
>
<
body
>
<
h1
way = "color:forestgreen; top:35%;
left: 35%; position:absolute; ">
Geeks For Geeks
</
h1
>
<
div
class
=
"GeeksForGeeks"
>
Cascading Style Sheets, fondly referred
to every bit CSS, is a just designed language
intended to simplify the procedure of
making web pages presentable. CSS allows
y'all to apply styles to web pages. More than
importantly, CSS enables you to practice this
independent of the HTML that makes up
each web page.
</
div
>
</
body
>
</
html
>
Output:
Using inline-block property: Use display: inline-block belongings to set a div size according to its content.
Example ii:
<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"utf-8"
>
<
title
>GeeksforGeeks Example</
title
>
<
style
media
=
"screen"
>
body {
groundwork: violet;
overflow: auto;
color: white;
}
.GeeksForGeeks {
text-align: middle;
background: dodgerblue;
position: absolute;
display: inline-block;
left: one%;
right: i%;
summit: fifty%;
}
</
fashion
>
</
caput
>
<
body
>
<
h1
mode="color: forestgreen;
top: 35%; left: 35%; position: absolute;">
Geeks For Geeks
</
h1
>
<
div
class
=
"GeeksForGeeks"
>
Cascading Mode Sheets, fondly referred
to as CSS, is a simply designed language
intended to simplify the procedure of
making web pages presentable. CSS allows
you to apply styles to web pages. More
importantly, CSS enables you to practice this
independent of the HTML that makes upwardly
each web page.
</
div
>
</
body
>
</
html
>
Output:
Using fit-content holding in width and summit: In this method, nosotros set the width and pinnacle property to fit-content value.
Case 3:
<!DOCTYPE html>
<
html
lang
=
"en"
dir
=
"ltr"
>
<
head
>
<
meta
charset
=
"utf-viii"
>
<
championship
>Geeks for Geeks Example</
title
>
<
mode
media
=
"screen"
>
body {
background: tomato plant;
overflow: subconscious;
color: white;
}
.GeeksForGeeks {
background: crimson;
position: accented;
width:fit-content;
pinnacle:fit-content;
left: 0;
top: 50%;
}
</
way
>
</
head
>
<
body
>
<
h1
way = "color: lime; superlative: 35%;
left: 35%; position: absolute;">
Geeks For Geeks
</
h1
>
<
div
course
=
"GeeksForGeeks"
>
Cascading Mode Sheets, fondly referred
to equally CSS, is a simply designed language
intended to simplify the process of
making web pages presentable. CSS allows
you to apply styles to spider web pages. More
importantly, CSS enables you to practice this
independent of the HTML that makes up
each spider web page.
</
div
>
</
body
>
</
html
>
Output:
HTML is the foundation of webpages, is used for webpage development by structuring websites and web apps.You tin can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.
CSS is the foundation of webpages, is used for webpage development by styling websites and web apps.Y'all can learn CSS from the basis up by following this CSS Tutorial and CSS Examples.
How To Set Auto Width In Css,
Source: https://www.geeksforgeeks.org/how-to-set-div-width-to-fit-content-using-css/
Posted by: boyerjact1960.blogspot.com
0 Response to "How To Set Auto Width In Css"
Post a Comment