Make div size as per contents and horizontally center inside it's parent - css

I have a div message which basically has a text and a link. I want its size to be changing based on the string inside it. Also I want this message div to be centered inside its container.
I have been playing with it for a while without much luck. Here is the link to JSfiddle: http://jsfiddle.net/pDYJ8/
Also I don't know how make that text and link appear one after other ( not on the new line )
Here is the code:
<div class="container">
<div class="message">
<p>do you want to </p>
<a href="somewhere" target="_blank">
buy this product
</a>
</div>
</div>
.container {
position: absolute;
background: #666;
top:25%;
width:100%;
font-size: 15px;
color: #e47911;
}
.message {
margin-right: auto;
margin-left: auto;
background: #ddd;
width:100px;
}
Tried display inline block to fit its content but then it wouldn't center it inside its parent.
Keeping width 100px for now just to mock my requirements

Just Tweak Some CSS
See the demo fiddle.
.container {
position: absolute;
background: #666;
top:25%;
width:100%;
font-size: 15px;
color: #e47911;
text-align: center; /*added*/
}
.container .message {
display: inline-block; /*added*/
text-align: left; /*added*/
background: #ddd;
}
.message p { /*added*/
display: inline-block;
}
Explanation
The text-align center causes the now inline-block display of .message to center, which is then reset to have its own text-align back at left (this is not necessary). To get the a on the same line, the p also needs to be some type of inline display, here I chose inline-block as well..

I think you are over complicating things. All you need is a text-align: centeron the container and a display: inline-block on the message:
.container {
background: #666;
font-size: 15px;
color: #e47911;
text-align: center;
}
.container .message {
background: #ddd;
display: inline-block;
}
http://jsfiddle.net/Pevara/pDYJ8/9/
The inline block makes the div act as a word inside text, and the text-align center makes the 'word' align to the center...

Here is a simplified approach to a couple of the answers given. It reduces the amount of HTML and CSS needed.
CSS
.container {
color: #e47911;
text-align: center;
}
.message {
display: inline;
background: #DDDDDD;
}
HTML
<div class="container">
<p class="message">
Do you want to buy this product?
</p>
</div>
I would definitely put your anchor tag, <a> inside the paragraph tag, <p>.
You could even remove display: inline; from .message if you made it a <span> rather than a <p>.

Check this out:
http://jsfiddle.net/pDYJ8/10/
Changes made to above link
.container .message {
margin: 0 auto;
width:auto;
}
span{
background: #ddd;
display:inline;
}

You can simplify it with display: table; and margin: 0 auto;
.container {
display: table;
margin: 0 auto;
background-color: #DDD;
}
<div class="container">
do you want to buy this product
</div>

Related

Link bigger than image

I looked around the boards a bit and couldn't find anything that worked for me, but basically when I center an image that has an anchor tag wrapped around it, the linkable area fills out to fit the entire width of the div, even when specifying a width. How can I get the link to only be around the image?
Link to JS Fiddle http://jsfiddle.net/jJ4hv/
HTML
<div id="right">
<div id="content">
<div class="title">
<h3>Spawn</h3>
<p><< Go Back</p>
</div>
<img src="images/spawnThumb.png" />
<p class="description">Spawn</p>
</div><!-- END content -->
</div><!-- END right -->
CSS
#content img {
display: block;
margin: 0 auto;
width: 400px;
}
.title {
border-bottom: thin solid #316b9c;
margin-bottom: 20px;
}
.title a {
font-size: 12px;
color: #bcbdbf;
}
.title a:hover {
color: #316b9c;
}
.title a:active {
color: #000;
}
.topImage {
margin-bottom: 10px;
}
p {
color: #bcbdbf;
}
Just add #content a to the #content img CSS.
#content img,
#content a{
display: block;
margin: 0 auto;
width: 400px;
}
http://jsfiddle.net/jJ4hv/2/
You could possibly apply
display: table;
margin: auto
to the [a] tag surrounding the image.
I fixed the same issue by adding text-align: center to the a tag!
#content a {
text-align: center;
}

Fix the alignment of two child divs

The project is to create a micro-blogging website similar to Twitter. I chose to name the site Chirper (how clever of me). Each post is structured by a parent div, an avatar div and a content div. The avatar and content divs are displayed inline, but they are not aligned properly. Any help is appreciated.
HTML:
<div class="chirp">
<div class="chirp_avatar_region">
<img src="img/avatar/default.png" alt="Avatar" width="64" height="64">
</div>
<div class="chirp_content">
<p>
USER
<span class="timeStamp">2013-11-22 16:43:59</span>
</p>
<p>
COMMENT
</p>
<p>
ReChirp!
</p>
</div>
The div's aren't aligned how I want them to be (level and 100% of the parent).
I can't post images, so here is a link to an imgur page: http://imgur.com/Mn9mE5q
Relevant CSS:
body {
font-family: Verdana, Geneva, sans-serif;
color: #000;
background-color: #666;
font-size: 1em;
}
/* Containers */
div {
margin-top: auto;
margin-left: auto;
margin-right: auto;
margin-bottom: 10px;
border-style: solid;
border-width: 3px;
border-color: #000;
padding: 10px;
}
div.pane {
width: 70%;
background-color: 0099FF;
}
div.chirp {
border-width: 1px;
margin-bottom: -1px;
width: 80%;
padding: 5px;
}
div.chirp_avatar_region {
display: inline-block;
width: 10%;
height: 100%;
text-align: center;
/*border-style: none;*/
}
div.chirp_content {
display: inline-block;
width: 80%;
height: 100%;
/*border-style: none;*/
}
div.chirp_avatar_region > img, div.chirp_content > p {
margin-top: 0;
vertical-align: middle;
}
You can either float your inner divs then clear the float following the container
or
use vertical-align:top to position your divs at the top of the container
Not entirely sure, but what I think is happening is that by defining position:inline-block, it's putting them on the same line, and making the line-height the height of the chirp_content container. In a sense anyway.
Set to vertical-align:top; and it should solve it.
Ex.
.chirp_content, .chirp_avatar_region{ vertical-align:top; }
JS Fiddle
Give to the avatar_region a float: left, and remove its width: and height: setting. Remove the chirp_content div, it circumvents the inlining.

How do I prevent images structured as a horizontal list from wrapping?

I have a row of distinct images that I want to use as a banner. Upon resizing, I don't want the last image(s) to wrap. I've done prerequisite google and search on stackoverflow; the examples I found were regarding text, not images.
Here's the jsfiddle: http://jsfiddle.net/pHytx/
The most relevant part of the code is probably the CSS:
#slidebanners {
width:100%;
}
#slidebanner ul{
padding: 0;
margin: 0;
overflow: hidden;
}
#slidebanner li{
float: left;
}
#slidebanner img{
height: 200px;
}
.page-header {
font-size: 2em;
padding: .5em;
margin-top: 15px;
}
The problem with this solution is that it doesn't get rid of the gap between the pictures; float gets rid of that gap. I could add a negative left margin, but that has weird effects because the negative margin gets applied unevenly (i.e. the rightmost image needs the most negative margin, but this affects the size of the leftmost image)
For this you can use the exotic display:table-cell;
See Demo:
http://jsbin.com/OxEPuJi/1/edit
Basically display:table-cell; prevents elements to drop on the next line. EVER!
And it also forces them to be next to each other without float.
Codes:
.img {
display:table-cell;
}
I apply this to the wrapper element which holds each image in my demo.
It's best to go for white-space: nowrap combined with display: inline-block, as shown in the accepted answer to the question you linked to.
The problem then becomes how to remove the gaps between the lis.
Here's a demo: http://jsfiddle.net/thirtydot/pHytx/4/
I went with removing the whitespace between the elements in the HTML.
Some people don't like editing their HTML to remove the gaps. To those people I say: deal with it. This is the easiest way to remove the gaps.
<ul>
<li>
<img src="media/images/slides/olympic-1.jpg" />
</li><li>
<img src="media/images/slides/olympic-2.jpg" />
</li><li>
<img src="media/images/slides/olympic-3.jpg" />
</li><li>
<img src="media/images/slides/olympic-4.jpg" />
</li><li>
<img src="media/images/slides/olympic-5.jpg" />
</li>
</ul>
#slidebanner ul {
white-space: nowrap;
padding: 0;
margin: 0;
list-style-type: none;
}
#slidebanner li {
display: inline-block;
vertical-align: top;
}
#slidebanner img {
height: 200px;
vertical-align: top;
}
To hopefully stop these pesky downvotes, here is a working implementation of the display: table-cell approach, thanks to user1721135 for the idea.
http://jsfiddle.net/thirtydot/pHytx/5/
#slidebanner {
float: left;
border: 1px solid red;
}
#slidebanner ul {
display: table;
width: 100%;
padding: 0;
margin: 0;
list-style-type: none;
}
#slidebanner li {
display: table-cell;
vertical-align: top;
}
#slidebanner img {
height: 200px;
vertical-align: top;
}
.page-header {
clear: both;
font-size: 2em;
padding: .5em;
margin-top: 15px;
}

Centering two divs in a div: one of fixed width and the other of variable width/height

I have a situation where I have one div of fixed width, containing an image pulled from Twitter, and another div of variable width containing user text of variable length. What I want to achieve is something like the following:
I can do this well enough with a single div that has background-image and padding-left. But I want to be able to apply border-radius to the img element, which simply won't be possible with a background-image.
If I do text-align: center on the outer div, it gets me halfway there. Here's a DEMO and a screenshot:
But this obviously isn't fully what I want.
How can I accomplish this?
Ask and you shall receive — a simplified jsFiddle example:
As an added bonus, the text is vertically centered too!
HTML:
<div class="logo">
<div class="logo-container">
<img src="http://img.tweetimag.es/i/appsumo_b.png" />
</div>
<div class="logo-name">
AppSumo is a really really long title that continues down the page
</div>
</div>
CSS:
.logo {
background-color: #eee;
display: table-cell;
height: 100px;
position: relative;
text-align: center;
vertical-align: middle;
width: 600px;
}
.logo-container {
background-color: #fff;
border-radius: 10px;
left: 10px;
position: absolute;
top: 10px;
width: 75px;
}
.logo-name {
font: bold 28px/115% Helvetica, Arial, sans-serif;
padding-left: 85px;
}
Would it be something like this?
http://jsfiddle.net/uPPTM/6/
.logo {
width:80%;
margin:auto;
background-color: red;
}
.logo-container {
border: 1px solid gold;
width:73px;
height: 73px;
display: inline-block;
vertical-align:middle;
}
.logo-name {
display: inline-block;
}
You can float the image container (or image itself without the container) to the left, clearing anything the left... and then float the text to the left, clearing anything to the right.
.logo-container{
float:left;
clear:left;
}
.logo-name{
float:left;
clear:right;
}
You can adjust the distance of the text using margins.
.logo-name{
float:left;
clear:right;
margin-top:10px;
margin-left:5px;
}
Use absolute positioning with a left position to push the title text past the image.
http://jsfiddle.net/uPPTM/9/
.logo { width: 50px; }
.title {
position: absolute;
top: 0;
left: 50px;
font-size: 32px;
text-align: center;
}
img {
border: 1px solid gray;
border-radius: 15px;
}
<div class="logo">
<div class="logo-container">
<img src="http://img.tweetimag.es/i/appsumo_b.png">
</div>
<div class="logo-name">AppSumo</div>
</div>

CSS: I can't put a button in the middle of a DIV element

I'm using CSS buttons from this tutorial:
http://www.oscaralexander.com/tutorials/how-to-make-sexy-buttons-with-css.html
I need to put a button in the middle of a DIV so it's centered. But I can't!
Here's the code of the button:
<a class="button" href="#"><span>Bring world peace</span></a>
And here's CSS:
.clear { /* generic container (i.e. div) for floating buttons */
overflow: hidden;
width: 100%;
}
a.button {
background: transparent url('bg_button_a.gif') no-repeat scroll top right;
color: #444;
display: block;
float: left;
font: normal 12px arial, sans-serif;
height: 24px;
margin-right: 6px;
padding-right: 18px; /* sliding doors padding */
text-decoration: none;
}
a.button span {
background: transparent url('bg_button_span.gif') no-repeat;
display: block;
line-height: 14px;
padding: 5px 0 5px 18px;
}
Here's the code I'm trying to use:
<div align="center"><a class="button" href="#"><span>Bring world peace</span></a></div>
the align attribute for the div element is deprecated. You're better off defining a class for that div, like so:
<div class="centerize">
<a class="button" href="#"><span>Bring world peace</span></a>
</div>
And the CSS:
.centerize {
text-align: center;
}
Note however that setting the text-align will only affect the content inside the div. The div itself (should be) a block element, and depending on where it sits in the document structure, may not be centered itself.
Just to make a little more certain, you can do something like this:
.centerize {
display: block;
margin: 0 auto;
text-align: center;
}
Now you can apply centerize to any element, and that element should take up the entire browser's width and center-align its content.
Modify the button class for these properties:
.button{
margin-left:50%;
margin-right:50%;
position: relative;
}
And wrap your link in the div like this:
<div align="center">
<a class="button" href="#"><span>Bring world peace</span></a>
</div>
The a.button is floated to the left. You could try float: none; on that element. margin: 0 auto; is also useful for center-aligning elements.
Does that help?

Resources