How to give a DIV the width of its content? - css

I would like to give a DIV the width of the picture in its content.
Here is my HTML :
<div id="container">
<div id="picandtext">
<img src="https://placehold.it/480x320" />
<p>Eodem tempore etiam Hymetii praeclarae indolis viri negotium est actitatum, cuius hunc novimus esse textum. cum Africam pro consule regeret Carthaginiensibus victus inopia iam lassatis, ex horreis Romano populo destinatis frumentum dedit, pauloque postea cum provenisset segetum copia, integre sine ulla restituit mora.</p>
</div>
</div>
Here is my CSS :
#container { height: 600px; display:table; width:1%; background: red; padding:10px; }
#picandtext { height: 400px; display:table; width:1%; background: green; padding:10px; }
#picandtext img { height: 180px; }
#picandtext p { height: 180px; }
I finally found a way to do it with display:table; width:1%; as you can see with this JSFiddle example : https://jsfiddle.net/Ls750c64/2/
But I would like to know if this way is the best one to make it ?

Given your existing markup, you can make #container display: inline-block; and absolutely position the p with text.
#container {
height: 600px;
display: inline-block;
background: red;
padding: 10px;
}
#picandtext {
height: 400px;
background: green;
padding: 10px;
position: relative;
}
#picandtext img {
height: 180px;
}
#picandtext p {
position: absolute;
left: 10px;
right: 10px;
}
<div id="container">
<div id="picandtext">
<img src="https://placehold.it/480x320" />
<p>Eodem tempore etiam Hymetii praeclarae indolis viri negotium est actitatum, cuius hunc novimus esse textum. cum Africam pro consule regeret Carthaginiensibus victus inopia iam lassatis, ex horreis Romano populo destinatis frumentum dedit, pauloque postea cum provenisset segetum copia, integre sine ulla restituit mora.</p>
</div>
</div>
And a less supported way to do it would be using intrinsic sizing. The browser support for it sucks though. http://caniuse.com/#feat=intrinsic-width
#container {
height: 600px;
display: inline-block;
background: red;
padding: 10px;
width: min-content;
}
#picandtext {
height: 400px;
background: green;
padding: 10px;
position: relative;
}
#picandtext img {
height: 180px;
}
<div id="container">
<div id="picandtext">
<img src="https://placehold.it/480x320" />
<p>Eodem tempore etiam Hymetii praeclarae indolis viri negotium est actitatum, cuius hunc novimus esse textum. cum Africam pro consule regeret Carthaginiensibus victus inopia iam lassatis, ex horreis Romano populo destinatis frumentum dedit, pauloque postea cum provenisset segetum copia, integre sine ulla restituit mora.</p>
</div>
</div>

Related

CSS: prevent item lowering with multiple text lines

First of all excuse me if the title is unclear. I dont really know how to describe the problem which also makes it harder for me to google for solutions.
I've inserted my code in a fiddle so I can explain it better:
https://jsfiddle.net/Lzbavyu9/2/
I want the X always to be on the same height as the first line of the text. I've tried things as vertical-align but that doesnt work. It always moves down with the last line of the text.
code is below
<div class="panel-title">
<h3>test sdfjsdf sdjfk sjkdf sdjk fjksd fjskdf jksdfj ksdfjk sfd jksd</h3>
<span class="collapse-button">
<i class="pull-right" aria-hidden="true">X</i>
</span>
</div>
CSS
.panel-title {
width: 200px;
}
.panel-title h3 {
margin: 8px 0 -1em 0;
display: block;
width: calc(100% - 30px);
}
.panel-title span.collapse-button {
padding: 0px;
height: 25px;
margin: 0px;
display: block;
}
.collapse-button i {
width: 25px;
height: 15px;
}
.pull-right {
float: right !important;
}
Does anybody have an idea?
Thanks in advance
div h3{
display: inline-block;
max-width: 10em;
vertical-align: top;
margin: 0;
}
<div>
<h3>test sdfjsdf sdjfk sjkdf sdjk fjksd fjskdf jksdfj ksdfjk sfd jksd</h3>
<i>X</i>
</div>
Flexbox can do that
* {
margin: 0;
padding: 0;
}
.panel-title {
width: 200px;
display: flex;
margin: 1em;
border: 1px solid grey;
}
.panel-title h3 {
display: block;
flex: 1;
}
.panel-title span.collapse-button {
padding: 0px;
height: 25px;
margin: 0px;
display: block;
}
.collapse-button i {
width: 25px;
height: 15px;
}
<div class="panel-title">
<h3>test sdfjsdf sdjfk sjkdf sdjk fjksd fjskdf jksdfj ksdfjk sfd jksd</h3>
<span class="collapse-button">
<i aria-hidden="true">X</i>
</span>
</div>
<div class="panel-title">
<h3>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Exercitationem alias est ex, reiciendis expedita natus distinctio quos ipsam cumque soluta, iusto incidunt molestias mollitia, ullam labore aliquid iure inventore voluptate!</h3>
<span class="collapse-button">
<i aria-hidden="true">X</i>
</span>
</div>
You should use position: absolute on your .collapse-button class. Add position: relative in your .panel-title class. If you want to adjust the position of the X, you can change the values of top, right, left and bottom properties in .collapse-button class
.panel-title {
width: 200px;
}
.panel-title h3 {
margin: 8px 0 -1em 0;
display: block;
width: calc(100% - 30px);
position: relative;
}
.panel-title span.collapse-button {
padding: 0px;
height: 25px;
margin: 0px;
display: block;
position: absolute;
top: 10px;
right: 0;
}
.collapse-button i {
width: 25px;
height: 15px;
}
.pull-right {
float: right !important;
}
<div class="panel-title">
<h3>test sdfjsdf sdjfk sjkdf sdjk fjksd fjskdf jksdfj ksdfjk sfd jksd</h3>
<span class="collapse-button">
<i class="pull-right" aria-hidden="true">X</i>
</span>
</div>
Put this:
.panel-title { position: relative; width: 200px; }
and
.collapse-button i { position: absolute; top: 5px; right: 0px; }

Center block in div with absolute position

I have a div with 2 blocks:
One with informations
Another absolute on the bottom (with variant height)
Problem: I would like to center image on the middle of the div, excluding absolute block.
What I have
What I want
My actual code:
https://jsfiddle.net/ph4kfuy9/5/
.block {
height: 400px;
background-color: #EEE;
margin: 20px 50px;
position: relative;
}
.text {
padding: 20px;
background-color: #CCC;
width: auto;
text-align: center;
width: 50%;
margin: 0 auto;
}
.absolute {
padding: 20px;
background-color: #555;
color: #FFF;
position: absolute;
bottom: 0;
width: 100%;
box-sizing: border-box;
}
<div class="block">
<div class="text">
<p>My text</p>
<div>Lorem Ipsum</div>
<p>Another text</p>
</div>
<div class="absolute">
My absolute block
<p>
Quam ob rem cave Catoni anteponas ne istum quidem ipsum, quem Apollo, ut ais, sapientissimum iudicavit; huius enim facta, illius dicta laudantur. De me autem, ut iam cum utroque vestrum loquar, sic habetote.
</p>
</div>
</div>
Thank
I would use flex for this
.block {
height: 400px;
background-color: #EEE;
margin: 20px 50px;
display:flex; /* make this flex */
flex-direction:column; /* line up chld elements in a column */
}
.text {
padding: 20px;
background-color: #CCC;
width: auto;
width: 50%;
margin: 0 auto;
flex-grow:1; /* make this take up remaining space that footer doesn't */
display:flex; /* make this flex */
flex-direction:column; /* line up chld elements in a column */
justify-content:center; /* vertical centre */
align-items:center; /* horizontal centre */
}
.footer { /* no need to be absolute */
padding: 20px;
background-color: #555;
color: #FFF;
width: 100%;
box-sizing: border-box;
}
<div class="block">
<div class="text">
<p>My text</p>
<div>Lorem Ipsum</div>
<p>Another text</p>
</div>
<div class="footer">
My footer block
<p>
Quam ob rem cave Catoni anteponas ne istum quidem ipsum, quem Apollo, ut ais, sapientissimum iudicavit; huius enim facta, illius dicta laudantur. De me autem, ut iam cum utroque vestrum loquar, sic habetote.
</p>
</div>
</div>

How to make background center in content in accordion menu

So I'm a self learner on this accordion menu from this site. My objective is to make the background image to be at the center of content area of slide-1. However, I'm unable to achieve this as the image does not centered on the content div. My current background image size 640x480 pixel. Appreciate any help on what went wrong
here is my html
<div id='h-accordion'>
<ul id='slides'>
<li class='slide open active' id='slide-1'>
<div class='text-box'>
<div class='slidebutton'>
<h3>Sign In</h3>
</div>
<div class='content'>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
</div>
</li>
<li class='slide open' id='slide-2'>
<div class='text-box'>
<div class='slidebutton'>
<h3>Reset Password</h3>
</div>
<div class='content'>
<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
</div>
</li>
<li class='slide open' id='slide-3'>
<div class='text-box'>
<div class='slidebutton'>
<h3>Register</h3>
</div>
<div class='content'>
<p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
</div>
</div>
</li>
<li class='slide open' id='slide-4'>
<div class='text-box'>
<div class='slidebutton'>
<h3>Useful Links</h3>
</div>
<div class='content'>
<div id='extlist'>
<ol class='liststyle'>
<li><a href='#' target='_blank'>Sub-Menu-1</a></li>
<li><a href='#' target='_blank'>Sub-Menu-2</a></li>
<li><a href='#' target='_blank'>Sub-Menu-3</a></li>
<li><a href='#' target='_blank'>Sub-Menu-3</a></li>
<li><a href='#' target='_blank'>Sub-Menu-4</a></li>
</ol>
</div>
</div>
</div>
</li>
</ul>
</div>
my css
#wrapper {
width: 100%;
margin: 0 auto;
}
#content {
width: 90%;
margin: 0 auto;
}
#extlist {
margin-left: 10px;
margin-top: 10px;
}
ul li {
list-style: none;
}
#h-accordion {
position: relative;
width: 900px;
height: 450px;
margin: 30px auto;
}
ul#slides {
list-style: none;
width: 900px;
overflow: hidden;
position: absolute;
height: 450px;
}
.text-box {
width:900px;
height:450px;
}
.slidebutton {
width:50px;
float:left;
height:450px;
background: #3399FF;
}
.content {
width:660px;
float:left;
padding-left:10px;
height:450px;
}
#slide-1 {
position: absolute;
top: 0;
left: 165px;
z-index: 4;
background: #FFFFFF url('images/login.jpg') no-repeat center center;
}
#slide-2 { position: absolute;
top: 0;
left: 110px;
z-index: 3;
background-color: #FFFFFF;
}
#slide-3 { position: absolute;
top: 0;
left: 55px;
z-index: 2;
background-color: #FFFFFF;
}
#slide-4 { position: absolute;
top: 0;
left: 0px;
z-index: 1;
margin-top:auto;
margin-bottom:auto;
background-color: #FFFFFF;
}
.slidebutton h3 {
transform: rotate(-90deg);
-webkit-transform: rotate(-90deg);
-moz-transform: rotate(-90deg);
-ms-transform: rotate(-90deg);
position:relative;
top: 50px;
left: -15px;
display:inline-block;
color:#fff;
text-transform:capitalize;
width: 150px;
height: 100px;
}
and the associated jQuery
$(document).ready(function() {
// Main Accordion
$('.slide')
.bind('open', function(){
if(! $(this).hasClass('open')){
$(this).next().trigger('open');
$(this).addClass('open');
$(this).animate({left: "-=680px"});
}
else{
$(this).prev().trigger('close');
}
$(this).siblings().removeClass('active');
$(this).addClass('active');
})
.bind('close', function(){
if($(this).hasClass('open')){
$(this).removeClass('open');
$(this).animate({left: "+=680px"});
$(this).prev().trigger('close');
}
});
$('.slidebutton')
.click(function(){
$(this).parent().trigger('open');
$('#content-' + $(this).parent().attr('id')).trigger('show');
});
});
edit to include link to JSFiddle
Try this... Add an image tag for each slide like so:
.cover {
width:100%;
height: 100%;
position: fixed;
left: 0;
top: 0;
right: 0;
bottom: 0;
z-index:1;
}
<img src="bg-image.jpg" class="cover">
This solution is great for all modern browsers (e.g. IE7+, Chrome, FF, Safari)

Positioning divs with css using absolute positioning

I know this is a noob question, but I just can't figure this out! I'm laying out a page for our intranet and all I need to do is position some divs side by side. Each container is a different item, but all containers have the same structure, a header, some descriptive text, and an image. I will be adding items as they are given to me. This is basically just a page i'm creating for employees to sell items. Here is my css and an image of what I'm trying to achieve. Please let me know if this doesn't make sense, but as smart as you guys have proven to be in the past, i'm sure you get the idea.
.wrapper {
width: 1260px;
height: 900px;
margin: 0px auto;
position: relative;
}
.container {
width: 400px;
height: 400px;
margin: 10px;
position: absolute;
}
.itemText {
width: 350px;
height: 190px;
padding: 0px;
position: absolute;
top: 25px;
left: 25px;
}
.itemHead {
padding: 0px;
margin: 0px;
}
.itemDesc {
padding: 10px;
margin: 0px;
}
.itemThumb {
width: 350px;
height: 150px;
position: absolute;
bottom: 25px;
left: 25px;
}
My HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/text.css">
<link rel="stylesheet" href="css/style.css">
<title>Document</title>
</head>
<body>
<div class="wrapper">
<div class="container">
<div class="itemText">
<div class="itemHead">
<p>Lorem ipsum.</p>
</div>
<div class="itemDesc">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto, placeat, aliquid tempore harum similique quo deleniti velit eum labore est?</p>
</div>
</div>
<div class="itemThumb"></div>
</div>
<div class="container">
<div class="itemText">
<div class="itemHead">
<p>Lorem ipsum.</p>
</div>
<div class="itemDesc">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto, placeat, aliquid tempore harum similique quo deleniti velit eum labore est?</p>
</div>
</div>
<div class="itemThumb"></div>
</div>
<div class="container">
<div class="itemText">
<div class="itemHead">
<p>Lorem ipsum.</p>
</div>
<div class="itemDesc">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto, placeat, aliquid tempore harum similique quo deleniti velit eum labore est?</p>
</div>
</div>
<div class="itemThumb"></div>
</div>
</div>
</body>
</html>
Don't position your containers absolutely.
.container {
width: 400px;
height: 400px;
margin: 10px;
position: relative;
float:left;
}
At the end of the last container div, you'll need a div to clear: left;
<div style="clear:left;"></div>
Now when you add more div's, they will auto float, and the container will get cleared.
Absolute positioning is in that case really useless.
try this:
.container {
position: relative;
float: left;
width: 400px;
height: 400px;
margin: 10px;
}
Your divs have the same height, so using float is pretty convenient. By giving your .container the attribute position: relative .itemThumb is positioned correctly.
This should work
I don't know if I understand exactly
but one way to get the containers to line up is to set the .container class to
.container {
width: 30%;
height: 400px;
margin: 10px;
display: inline-block;
}
Adding the display: inline-block;
And removing the position:absolute.
Setting your width for each container to around 30% of the wrapper will ensure that three containers get lined up before they go to a new line.
You will need to take off all the position: settings IN your css file so that all the information stays contained within the div.
e.g.
.wrapper {
width: 1260px;
height: 900px;
margin: 0px auto;
position: relative;
}
.container {
width: 30%;
height: 400px;
margin: 10px;
display: inline-block;
border: 1px solid black;
}
.itemText {
width: 350px;
height: 190px;
padding: 0px;
top: 25px;
left: 25px;
}
.itemHead {
padding: 0px;
margin: 0px;
}
.itemDesc {
padding: 10px;
margin: 0px;
}
.itemThumb {
width: 350px;
height: 150px;
bottom: 25px;
left: 25px;
}

center 3 divs in footer with dynamic with?

i got a tricky situation here. im trying to center 3 divs inside my footer and they need to have dynamic width, like min-width.
[cotainer [first] [second] [third] /container]
my setup is this
<footer>
<div id="container">
<div id="first"></div>
<div id="second"></div>
<div id="third"></div>
</div>
</footer>
footer #container { width: 800px; margin: 0 auto; }
#container #first,#container #second,#container #third
{
float: left;
min-width: 200px;
height: 25px;
background: /* image url */
padding: 4px;
margin: 0 20px 0 0;
}
#container #third { margin-right: 0; }
You should use display: table; and table-cell.
#container {
display:table;
}
#first, #second, #third {
display: table-cell;
width: 200px;
height: 40px;
border: 1px dashed #000;
}
Demo available here.
set container to display as:table and set it's margin to 0 auto.
#container {
display:table;
margn:0 auto;
whitespace: nowrap;
}
#first, #second, #third {
min-width: 200px;
float:left
...
}
Demo: http://jsfiddle.net/AZ4yT/1/
Edit: It gets left aligned in IE. so you might wanna use a workaround for that
What about using display: inline-block? You can see a jsFiddle of it here:
http://jsfiddle.net/S7bKT/1/
HTML
<div id="container">
<div id="first">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Aliquam scelerisque euismod auctor. Sed pulvinar nulla eu
lorem iaculis ultrices. Mauris
</div>
<div id="second">Lorem ipsum dolor sit amet</div>
<div id="third">Sed pulvinar nulla eu lorem iaculis ultrices</div>
</div>
CSS
#container {
width: 500px;
background: #dedede;
margin: 0 auto;
text-align: center;
}
#first, #second, #third {
display: inline-block;
min-width: 50px;
max-width: 120px;
min-height: 100px;
zoom: 1; /* Fix for IE */
_display: inline; /* Hack for IE */
margin-right: 20px;
vertical-align: top;
}
#first {
background: #f00;
}
#second {
background: #0f0;
}
#third {
background: #00f;
}
#container div:last-child {
margin-right: 0;
}

Resources