Div Size Automatically size of content
As far as I know, display: inline-block
is what you probably need. That will make it seem like it's sort of inline but still allow you to use things like margins and such.
CSS Making the content DIV Auto size after the content within
Since your div#Content
is absolutely positioned within div#Container
, the container will ignore the height and width of the content when determining its own dimensions. Try giving div#Content
a position
value of relative
. Then, as mentioned in the comments above, switch its height
property to min-height
. If you need to stick with absolute positioning and still want a content-affected height, you'll have to use JavaScript to adjust the style.height
of the containing div when the height of the content div changes.
How to auto adjust the div height according to content in it?
Try with the following mark-up instead of directly specifying height:
.box-centerside { background: url("../images/greybox-center-bg1.jpg") repeat-x scroll center top transparent; float: left; min-height: 100px; width: 260px;}
<div class="box-centerside"> This is sample content<br> This is sample content<br> This is sample content<br> This is sample content<br> This is sample content<br> This is sample content<br> This is sample content<br> This is sample content<br> This is sample content<br> This is sample content<br> This is sample content<br> This is sample content<br></div>
How can I let a div automatically set it own width?
Solution with inline-block
You could try display: inline-block
and see if that works in your situation. However, you won't be able to center it using margin-left: auto
& margin-right: auto
because that technique requires a width value.
If possible, use display: inline-block
and set text-align: center
on it's container.
<div style="text-align: center">
<div style="display: inline-block;">
This will expand only as far as it needs to
</div>
</div>
Solution using Flexbox + container div
The original answer above was written in 2011, before flexbox was implemented. You can achieve a similar effect
<div style="display: flex; justify-content: center;">
<div>
This will expand only as far as it needs to
</div>
</div>
Solution without container div
There is another way to do this without a parent element.
- Set display to
inline-block
to make the div width fit to its content. - Use
position: relative
andleft: 50%
to position its left edge to 50% of its containing element. - Use
transform: translateX(-50%)
which will "shift" the element to the left by half its own width.
.center {
display: inline-block;
position: relative;
left: 50%;
transform: translateX(-50%);
}
Width equal to content
By default p
tags are block
elements, which means they take 100% of the parent width
.
You can change their display property with:
#container p {
display:inline-block;
}
But it puts the elements side by side.
To keep each element on its own line you can use:
#container p {
clear:both;
float:left;
}
(If you use float and need to clear after floated elements, see this link for different techniques: http://css-tricks.com/all-about-floats/)
Demo: http://jsfiddle.net/CvJ3W/5/
Edit
If you go for the solution with display:inline-block
but want to keep each item in one line, you can just add a <br>
tag after each one:
<div id="container">
<p>Sample Text 1</p><br/>
<p>Sample Text 2</p><br/>
<p>Sample Text 3</p><br/>
</div>
New demo: http://jsfiddle.net/CvJ3W/7/
Div height 100% and expands to fit content
Here is what you should do in the CSS style, on the main div
display: block;
overflow: auto;
And do not touch height
Make div width adapt automatically to contents within it
You have to clear the float after your div by adding style="clear: left;" on your consecutive element:
<div style="float: left;">
<table>...</table>
</div>
<div style="clear: left;">
...
</div>
How to auto adjust the div size for all mobile / tablet display formats?
This is called Responsive Web Development(RWD).
To make page responsive to all device we need to use some basic fundamental such as:-
1. Set the viewport meta tag in head:
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0"/>
2.Use media queries.
Example:-
/* Smartphones (portrait and landscape) ----------- */
@media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* Styles */
}
/* Smartphones (landscape) ----------- */
@media only screen
and (min-width : 321px) {
/* Styles */
}
/* Smartphones (portrait) ----------- */
@media only screen
and (max-width : 320px) {
/* Styles */
}
/* iPads (portrait and landscape) ----------- */
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* Styles */
}
/* iPads (landscape) ----------- */
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
/* Styles */
}
/* iPads (portrait) ----------- */
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {
/* Styles */
}
/* Desktops and laptops ----------- */
@media only screen
and (min-width : 1224px) {
/* Styles */
}
/* Large screens ----------- */
@media only screen
and (min-width : 1824px) {
/* Styles */
}
/* iPhone 4 ----------- */
@media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
}
3. Or we can directly use RWD framework:-
- Bootstrap
- Foundation 3 etc.
Some of good Article
Media Queries for Standard Devices - BY CHRIS COYIER
CSS Media Dimensions
4. Larger Device, Medium Devices & Small Devices media queries. (Work in my Scenarios.)
Below media queries for generic Device type: - Larger Device, Medium Devices & Small Devices.
This is just basic media types which work for all of scenario & easy to handle code instead of using various media queries just need to care of three media type.
/*###Desktops, big landscape tablets and laptops(Large, Extra large)####*/
@media screen and (min-width: 1024px){
/*Style*/
}
/*###Tablet(medium)###*/
@media screen and (min-width : 768px) and (max-width : 1023px){
/*Style*/
}
/*### Smartphones (portrait and landscape)(small)### */
@media screen and (min-width : 0px) and (max-width : 767px){
/*Style*/
}
Related Topics
How to Use Nth-Child Correctly
Flex-Basis for Wrapping Columns
How to Add Second Level to a Submenu in a Dropdown Navigation CSS
Is There a Way in CSS to Move an Element in The Dom
CSS: Is There Any Difference Between These Two Parts
Why Does Display Inline-Block Match Height of Text
Angular Materials Won't Apply Styles to Components
(CSS) How Position Text (With Background Color) Over <Img> Tag Without Absolute Positioning
What Are The Various Ways to Hide a <Div>
Overlaping Effect on Cards with Pure CSS
Anyone Know What The Purpose of Tagging a Class with *[Class]
Redmine 3.3.0 (Ruby on Rails 4.2.6) Stylesheets Not Generated/Included in Application.CSS
CSS Percentage Widths and Heights and Resolution Problems
CSS - Successive Indenting of Siblings After Headings
How to Create Text Border in React Native
How to Animate a Glowing Effect on Text
Why Bootstrap CSS Is Not Overriding in Other Project with The Same Code