I'm attempting to get a div element to hug the contents as tightly as possible. This works fine for the width by using display:inline-block, but the height always has some extra padding/ margin/ border. How do I get the parent div to wrap the text exactly? I can get it by judicially choosing line-height, but this only works for a particular font and size.
How do I get the red box below to wrap the text as close as possible... for any input font or size?
Example:
jsfiddle, with css code
.outer {
display:block;
background:red;
}
.hug {
font-size:200%;
margin:0em;
border:0em;
padding:0em;
}
and html:
<div class="outer">
<div class="hug">
<h1>PERFECT FIT</h1>
</div>
</div>
<div class="outer">
<div class="hug">
<h2>PERFECT FIT</h2>
</div>
</div>
CSS, CCS3 solutions preferred over javascript, unless it isn't possible.
I ended up getting some decent results with this. If you alter the font size the line-height can stay as needed and hugs pretty darn close... Does it need to be to the pixel exactly?
.thisone{
display:inline-block;
background:#09f;
min-width:1px;
font-family:arial;
width:auto;
font-size:70px;
line-height:.70em;
}
DEMO
Here is something I've tried : http://jsfiddle.net/wared/CpZru/. My first attempt (pink) worked only with H2, so, I gave a closer look to this tag's default styles, and noticed that the font size was set to 1.5 (Chrome). Then I used this ratio for my second attempt (blue) : 1 / 1.5 = 0.666.... Although the result is more reliable, the ratio seems not to be valid with a different font weight (1 pixel overflows the P tag's line) or a different font family (green test).
You could calculate the ratio for each font weight/family since both parameters seem to affect the line height ratio. I'm not able to get something more interesting currently. Hope it can help in some way.
<div class="em1">
<h1>PERFECT FIT</h1>
<h2>PERFECT FIT</h2>
<p>PERFECT FIT</p>
</div>
<div class="em67">
<h1>PERFECT FIT</h1>
<h2>PERFECT FIT</h2>
<p>PERFECT FIT</p>
</div>
<div class="em67 arial">
<h1>PERFECT FIT</h1>
<h2>PERFECT FIT</h2>
<p>PERFECT FIT</p>
</div>
.em1 {
line-height: 1em;
}
.em1 * {
background: pink;
line-height: inherit;
}
.em67 * {
background: lightblue;
line-height: .67em;
}
.arial * {
font-family: Arial;
background: lightgreen;
}
.outer {
display:block;
background:red;
float:left;
clear:both;
}
.hug {
font-size:200%;
margin:0em;
border:0em;
padding:0em;
}
.hug h1,
.hug h2 {
margin:0;
padding:0;
height:1em;
line-height:1em;
}
Related
I'm trying to replace JPG images with some HTML code. The blank outline of the buttons will still be used for the standard button, plus the hover, but I want the text within the button to be handled via code.
But the issue is that some of the buttons have multiple words with multiple lines, where-as others are only a couple words.
I'd like to get the font-size for the buttons to be dynamic and not set and then also word-wrap and adjust accordingly.
I found this which sort-of does what I'd like:
Font-size depends on div width and height
But I need to the text to word-wrap.
Here's my example which I cant seem to get to work properly.
https://jsfiddle.net/5L39xq1n/
<div style="text-align:center;padding:20px 0;">DIV Button Test</div>
<div id="buttoncontainer">
<div id="leftsidebuttons" class="leftsidebuttons">
Multiple lines because of the number of words
</div>
<div id="leftsidebuttons" class="leftsidebuttons">
Single Line
</div>
<div id="leftsidebuttons" class="leftsidebuttons">
This is another multiple line button
</div>
</div>
#buttoncontainer { width:175px; height:46px; line-height:46px; }
#leftsidebuttons { white-space:nowrap; }
.leftsidebuttons { color:#d5a200 !important;
background-color:blue;
font-family: Arial, Helvetica, sans-serif;
font-style:italic;
font-weight:700;
margin:5px 0;
text-align:center;
word-wrap: break-word;
}
.leftsidebuttons:hover { color:#ffcc00 !important;
background-color:red;
text-align:center;
}
var container = $("#buttoncontainer"),
text_container = $("#leftsidebuttons"),
start_size = 16,
container_width = container.width();
text_container.css('font-size', start_size + 'px');
while (text_container.width() > container_width) {
text_container.css('font-size', start_size--+'px');
}
Removing white-space:nowrap should do the job.
you use #leftsidebuttons { white-space:nowrap; } and then {word-wrap: break-word;} for the class .leftsidebuttons!!
There are many problem that you would need to fix:
multiple element with same id
don't use div for button
that approach is very slow, basically it tries each font size until it fits.
if you change the div to button with auto it will work.
https://jsfiddle.net/357d2ka6/1/
Please run the demo:
* {
margin:0;
padding:0;
}
.body {
font-family:Microsoft Yahei;
font-size:16px;
background-color:lightblue;
height: 200px;
width:200px;
line-height:2;
}
.body span {
background-color:pink;
}
.body img {
width:50px;
height:50px;
}
.body .img-wrapper {
background-color:orange;
}
.body .img-wrapper {
vertical-align:middle;
}
<div class="body">
<span class="wrapper">
words-g words-g words-g
<span class="img-wrapper">
<img src="https://avatars3.githubusercontent.com/u/23273077" alt="">
s
</span>
words-g words-g words-g
</span>
</div>
The point is that I set
.body .img-wrapper {
vertical-align:middle;
}
I was expecting the red lines in below picture is in the same line:
According to the specification,
Align the vertical midpoint of the box with the baseline of the parent box plus half the x-height of the parent.
So,I think:
the vertical midpoint of the box is the first red line in the above picture and
the baseline of the parent box plus half the x-height of the parent = the second red line
But it turns out I am wrong and I guess the key is x-height of the parent.So,I found this:
So, I thought x-height of the parent in this case is not the second red line because of the existence of the image.
So,my question is :
how much is the x-height of the parent in this case? Is it changed because of the existence of the image?
Or something else is wrong?
Please notice:
I just want to get the x-height value in this case,so I can understand the vertical-align better.
I am not asking for a specific solution.
Whatever thanks for your help!
First the x-height of the element is not affected by the the image and is only defined by font-size and of course the font-family used. Then in order to get the value of the x-height you need to consider the ex unit.
Here is a better illustration taken for this answer
You may clearly see the difference between each value of vertical alignment and also notice the illustration of em and ex unit. Now in order to have the exact value of x-height, you simply need to use the ex unit.
Here is an example:
* {
margin:0;
padding:0;
}
body {
font-family:Microsoft Yahei;
font-size:16px;
background-color:lightblue;
line-height:2;
}
span {
background-color:pink;
border-right:1ex solid red;
border-left:1em solid red;
}
img {
width:50px;
height:50px;
}
<span>
words-g words-g words-g
</span>
<br>
<span>
words-g words-g words-g <img src="https://avatars3.githubusercontent.com/u/23273077" alt="">
</span>
As you can see I added a right and left border using ex and em units then if I check the computed value I can get the exact value. You will aslo notice that both span have the same value which indicate that the image has no impact on it.
Here is a prototype of what I am trying to implement
Here is what I currently have : JsFiddle
I am trying to get the picture of the guy on the laptop to align correctly with and to the right of the paragraph components - Business Traveller, Office Supply Purchases, etc...
What I've tried is using Align attribute, changing my img src code to
<img id="laptop" align="middle" src="zoom-39988392-3.JPG" height = "90" width ="90" />
but that didn't have any effect.
I also tried Float but that messed up my margins and the organization of my left components.
Is there a way I can do this without floating?
See the fiddle
The HTML and CSS that i've used is as follows. Used float:left
HTML
<div class="container">
<div id="choices">
<p class="choice">Business Traveller</p>
<p class="choice">Office Supply Purchases</p>
<p class="choice">Stay at home parent</p>
<p class="choice">Entertainment</p>
<p class="choice">Profile 6</p>
</div>
<div class="image"></div>
</div>
CSS
html, body, .container {
height:100%;
}
#choices {
width:30%;
float:left;
}
.choice {
margin-top:0px;
margin-left:20px;
text-align:center;
width:100%;
background-image: url("http://i.imgur.com/H43sVoi.png");
padding-top:15px;
padding-bottom:15px;
}
.image {
height:100%;
width:65%;
background-color:red;
float:left;
}
You will have to work with the height and width of each divs. I just made it roughly.
You have to create two columns. 1 column for the menu and the second column for the image. If you do this, you wont have trouble floating.
Background
I am creating a video gallery using the ShadowBox jQuery plugin. To do this, I am creating rows of inline images using display:inline-block. The user will be able to upload a video as well as thumbnail images to accompany the video. The thumbnail's max size is 240x160 pixels.
What I want to do is have a black border around each gallery thumbnail "slot" with the user's uploaded thumbnail residing inside of that "slot", so if the user uploads a 240x160 thumbnail, the thumbnail will fill up the "slot" completely, and if they upload a smaller image, the thumbnail will still be in the "slot" with some extra spacing around it.
Here's an example of where I am right now: http://jsfiddle.net/shaunp/HvZ5p/
The problem is that there is extra spacing below my thumbnails and I'm not sure why. If you inspect the code you will see that there is an extra 5 pixels lurking below the image and I'm not sure where it's coming from. The grey part below the image should be directly BEHIND the image so that in the case the user uploads a smaller thumbnail, there will be grey-background space around it, but for some reason it is too tall. Any suggestions?
HTML
<div class="inline">
<div class="bg-thumb">
<div class="cell-thumb">
<a href="#" rev="#nvcCaption#" class="shadow">
<img src="http://farm9.staticflickr.com/8330/8135703920_f2302b8415_m.jpg" class="thumbImg" alt="Thumb" />
</a>
</div>
</div>
<div class="vcCaption">Caption</div>
</div>
<div class="inline">
<div class="bg-thumb">
<div class="cell-thumb">
<a href="#" rev="#nvcCaption#" class="shadow">
<img src="http://farm9.staticflickr.com/8330/8135703920_f2302b8415_m.jpg" class="thumbImg" alt="Thumb" />
</a>
</div>
</div>
<div class="vcCaption">Caption</div>
</div>
CSS
body {
overflow:hidden;
margin:0 50px 0 50px;
}
.vcCaption {
text-align:center;
font-family:"HelveticaNeue-Light","Helvetica Neue",Helvetica,Arial,sans-serif;
font-size:14px;
color:#000;
margin-bottom:5px;
}
.inline {
display:inline-block;
}
.bg-thumb {
width:250px;
height:170px;
}
.bg-thumb {
text-align:center;
display:table;
margin-bottom:5px;
}
.cell-thumb {
display:table-cell;
vertical-align:middle;
border:5px solid #000;
background-color:#7f7f7f;
}
.thumbImg {
max-width:240px;
max-height:160px;
}
Add vertical-align:top to your thumbnails:
.thumbImg {
max-width:240px;
max-height:160px;
vertical-align:top;
}
jsFiddle example
The default value of vertical-align is baseline, but for your needs you'll want the images to align to the top.
Another option would be to set the font size to zero on the containing div like:
.cell-thumb {
display:table-cell;
vertical-align:middle;
border:5px solid #000;
background-color:#7f7f7f;
font-size:0;
}
jsFiddle example
Adding vertical-align: middle; to your image will solve that.
.thumbImg {
vertical-align: middle;
max-width:240px;
max-height:160px;
}
the anchor tag is by default an inline element which gives it extra spacing, set it to a block element and give it some width and height!
.cell-thumb a {
display: block;
width: 240px;
height: 160px;
}
Images will by default display as inline-block (http://dev.w3.org/html5/markup/img.html#img-display) meaning that they will sits on an inline level block - or text line if you prefer.
Either set the font-size and/or line-height to 0 or in this case simply set the image to display at block level (display: block;).
I'm new to web designing.
I started to designing a sample page but I have some challenges.
I have photoshop template like below image.
to convert it to HTML and CSS I fallowed these steps :
I separated background image with logo then I putted to background of body
I created main DIV as page container with relative position.
now I want to put texts to page ( OH BOY WE'RE LOST !) and ( THE RESOURCE YOU ARE LOOKING FOR MIGHT HAVE BEEN REMOVED, HAD ITS NAME CHANGED OR
TEMPORARILY UNAVAILABLE.)
I have some questions here :
How do I put them on Page ? both of them in one DIV ? or each one in separated DIV ?
How should I position them to having same position in picture and web site ? is there any specific technique exist ? or should I do i with test and try ?
Please explain me.
Fix the width width:100% of the page div. and give <h1> tag for "OH BOY WE'RE LOST!" with specified width and margin: 0 auto;.
and <p> tag for remaining text that "HE RESOURCE YOU ARE LOOKING FOR MIGHT HAVE BEEN REMOVED, HAD ITS NAME CHANGED OR TEMPORARILY UNAVAILABLE."
<div class="page">
<h1>OH BOY WE'RE LOST!</h1>
<p>HE RESOURCE YOU ARE LOOKING FOR MIGHT HAVE BEEN REMOVED, HAD ITS NAME CHANGED OR TEMPORARILY UNAVAILABLE.</p>
</div>
css :
.page {
position:relative;
width:100%;
height:auto;
}
.page h1 {
position:absolute;
top:0px;
left:0px;
font-size: /*your font size*/
font-family: /*your font family*/
width:100px; /*you can change the width as per your need*/
margin:0 auto;
}
.page p {
width:98%;
margin:0 auto;
}
How do I put them on Page ? both of them in one DIV ? or each one in separated DIV ?
Both of them in seperate div and enclosing them in the background image div. I personally prefer span than div because it is plain text.
<div class="backGroundImag">
<span class="text1"> Text goes here </span>
<span class="text2"> Text goes here </span>
</div>
You need to keep in mind the web is not a pixel perfect medium as there are so many variables in play: different browsers, operating systems, screen resolutions etc.
If you are new to web designing, start with something a little more simple. What you have there is a great design that may not immediately translate easily to the web (your search box for a start would provide some interesting implementation challenges).
Don't aim too high early on. Read, practice, repeat. Here is a good article on centered design techniques to get you started:
http://www.webdesignforidiots.net/2009/03/fixed-width-centered-website/
Marked as CW because this is advice and not an answer!
body { margin:0px; padding:0px; font-family:Arial, Helvetica, sans-serif; }
.wrapper { width:600px; margin:0px auto; background:url(images/bg.jpg) no-repeat; min-height:437px; }
.header { width:144px; margin:0px auto; padding: 25px 0 20px 0; height:130px; } /*If you want logo Seperatly use this step*/
.header h1 { margin:0px; padding:0px; } /*For Seo prupose*/
.content { margin:0px auto; padding:0px; width:480px; }
.content h1 { text-align:center; text-transform:uppercase; font-size:16px; margin:0px; padding:0px;}
.content h2 { font-size:12px; }
HTML
<div class="wrapper">
<div class="header"><h1><!--<img src="images/logo.png" alt="logo" />-->Logo</h1></div>
<div class="content">
<h1>OH BOY WE'RE LOST !</h1>
<h2>THE RESOURCE YOU ARE LOOKING FOR MIGHT HAVE BEEN REMOVED, HAD ITS NAME CHANGED OR TEMPORARILY UNAVAILABLE.</h2>
</div>
</div>