<div style='width:10px'>
<a style="z-index : 1;cursor: pointer; text-decoration: underline; padding: 0px 3px 0px 0px; float: left;">
<img src='myimage.png' />
</a>
<a style="z-index : 1;margin-left: 4px; padding: 0px 3px 0px 0px; float: left;" >
<img src='myimage2.png' />
</a>
</div>
http://jsfiddle.net/n5q06r59/
With this code, the two images are displayed below each other. If I increase the width of the main div, the images are correctly displayed beside each other.
How do I force the elements to be always displayed beside each other, getting rather overlapped than drawn below in case of limited space?
You can use display table/table-cell to display beside. like:
div{
display:table;
}
a{
display:table-cell;
}
And remove float:left from anchor tag.
Check Fiddle Here.
If I understand you correctly, you want the images to display next to each other and when there are more images, you want them to overlap horizontally to fit in the given space.
Use javascript to accomplish what you want.
The overall logic would be,
Get the maximum width of the div
Divide the width by number of images you have (if all the images have same width), say that value is imgPosVal.
Place the images in such a way that you increment imgPosVal for each image on iteration.
To make this work successful, you have to position images as absolute;
Hope this helps. Let me know if you don't get it.
Sample Code: (You may have to tweak little bit according to your requirement)
var allImagesWidth = 100;
placeImages(["image1.png", "image2.png", "image3.png"]);
function placeImages(imageList) {
var parentDiv = $("#div");
var parentDivWidth = parentDiv.width(); //Say this is 200.
var imgPosVal = parentDivWidth/imageList.length; //66.6
for(var i=0; i<imageList.length; i++) {
var imgObj = $("<img src=\""+imageList[i]+"\" >");
parentDiv.append(imgObj);
imgObj.css({"position":"absolute", "left": (imgPosVal*i) + "px"});
}
}
Solution using Flexbox
.main {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: space-around;
}
a {
width: calc(100% - 4px);
margin: 0px 2px;
}
img {
width: 100%;
}
<div class="main">
<a><img src='http://lorempixel.com/400/200/sports/1/' /></a>
<a><img src='http://lorempixel.com/400/200/sports/2/' /></a>
<a><img src='http://lorempixel.com/400/200/sports/3/' /></a>
</div>
Jsfiddle Demo
Flex Guide
You can use postion: absolute attribute along with z-index:199 to keep top of other. The maximum value of z-index represent top position.
<a style="position:absolute;z-index : 0;cursor: pointer; text-decoration: underline; padding: 0px 3px 0px 0px; float: left;">
<img src='myimage.png' /> </a>
<a style="position:absolute;z-index : 1;margin-left: 4px; padding: 0px 3px 0px 0px; float: left;" >
<img src='myimage2.png' /> </a>
Related
I am trying to create the following design
Where the left side is an image followed by a tag with words.
on the right are the menu options which should look like they are clickable. The slashes that separate them should never move change color etc. However "About" "Contact" and "The Future" will eventually look different when hovered over and will take you to different pages on the site.
So when I attempted to create this I made the following HTML and CSS (make sure the window is wide when you run it)
.modernShadow {
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.16), 0 2px 10px 0 rgba(0, 0, 0, 0.12);
}
.fittingObject {
max-width: 100%;
max-height: 100%;
}
#topdiv {
position: fixed;
z-index: 2;
background-color: #4FBEA9;
width: 100%;
height: 44pt;
padding-top: 3pt;
padding-bottom: 3pt;
padding-left: 3pt;
padding-right: 3pt;
}
#topdiv * {
display: inline;
vertical-align: middle;
}
.title {}
.titleLogo {}
.titleHere {
color: white;
}
.rightList {
float:right;
vertical-align: middle;
}
<header>
<!-- Displays in tab bar -->
<title>Logo Here</title>
<div id="topdiv" class="modernShadow">
<img src="https://cdn1.iconfinder.com/data/icons/computer-system-files-essential-glyph/48/Sed-40-512.png" class="fittingObject" />
<h1 class="title">
<h1 class="titleLogo">Logo</h1>
<h1 class="titleHere">Here</h1>
</h1>
<nav class="rightList">
<ul>About</ul>
/
<ul>Contact</ul>
/
<ul>The Future</ul>
</nav>
</div>
</header>
And the result is this
And I can't explain why because I don't understand what is happening to make it this way.
Why are components under float:right not vertically aligned like everything else? I believe #topdiv * gives every subcomponent the style vertical-align: center so why isn't that coming into play?
Where is the spacing between the words and the "/"s coming from?
I am not sure it isn't just a fluke that "Logo Here" is appearing vertically centered... Maybe that is just the font size? But that is set by the .fittingObject css. (the title logo should be as big as it can be)
The end goal is essentially the picture above. The font size of the right items is variable and the text should just always be vertically centered inside of the containing div.
Answers:
Float doesn't work with vertical align, read more about it here; CSS Vertical align does not work with float
UL's have the property padding-inline-start which has a default of 40px and is pushing content to the right.
Max width and Max height properties only specify the limit of a certain element, it doesn't indicate their actual width and height values.
I modified your html to include wrapper divs; classes with left and right. In the right div I placed your rightList which has line-height css property and is an alternative to vertical-align:middle.
.modernShadow {
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.16), 0 2px 10px 0 rgba(0, 0, 0, 0.12);
}
.fittingObject {
max-width: 100%;
max-height: 100%;
}
#topdiv {
position: fixed;
z-index: 2;
background-color: #4FBEA9;
width: 100%;
height: 44pt;
padding-top: 3pt;
padding-bottom: 3pt;
padding-left: 3pt;
padding-right: 3pt;
}
#topdiv * {
display: inline;
vertical-align: middle;
}
.title {}
.titleLogo {}
.titleHere {
color: white;
}
.left,
.right {
width: 49%;
display: inline-block;
}
.rightList {
width:230px;
float: right;
line-height: 60px;
}
.rightList ul {
padding-inline-start: 0px;
}
<header>
<!-- Displays in tab bar -->
<title>Logo Here</title>
<div id="topdiv" class="modernShadow">
<div class="left">
<img src="https://cdn1.iconfinder.com/data/icons/computer-system-files-essential-glyph/48/Sed-40-512.png" class="fittingObject" />
<h1 class="title">
<h1 class="titleLogo">Logo</h1>
<h1 class="titleHere">Here</h1>
</h1>
</div>
<div class="right">
<nav class="rightList">
<ul>About</ul>
/
<ul>Contact</ul>
/
<ul>The Future</ul>
</nav>
</div>
</div>
</header>
do you have any particular reason to use <ul> for all the navs?
if you use
<ul>
<li>About</li> /
<li>Contact</li> /
<li>The Future</li>
</ul>
it will generate the display as you wanted.
the <ul> has default user agent stylesheet.
and for the vertically centered text, if you already set the height for the blocks, you can always use the same statement for line-height (in this case is 44pt) to make it vertically centered.
I'm attempting to place a 'notification' style badge over an images. I am using Twitters Bootstrap as a base framework and creating a custom CSS class called notify-badge. But I cannot get anything to line up properly.
Through the magic of Photoshop, here is what I am trying to accomplish.
Here is my CSS code.
.notify-badge{
position: absolute;
background: rgba(0,0,255,1);
height:2rem;
top:1rem;
right:1.5rem;
width:2rem;
text-align: center;
line-height: 2rem;;
font-size: 1rem;
border-radius: 50%;
color:white;
border:1px solid blue;
}
I would like to be able to place any small about of text in the badge and it expand the red circle to fit.
Here is my HTML code.
<div class="col-sm-4">
<a href="#">
<span class="notify-badge">NEW</span>
<img src="myimage.png" alt="" width="64" height="64">
</a>
<p>Some text</p>
</div>
Bunch of different ways you can accomplish this. This should get you started:
.item {
position:relative;
padding-top:20px;
display:inline-block;
}
.notify-badge{
position: absolute;
right:-20px;
top:10px;
background:red;
text-align: center;
border-radius: 30px 30px 30px 30px;
color:white;
padding:5px 10px;
font-size:20px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-sm-4">
<div class="item">
<a href="#">
<span class="notify-badge">NEW</span>
<img src="https://picsum.photos/200" alt="" />
</a>
</div>
</div>
Addendum (from the Asker #user-44651)
(moved from the question)
Here is the result of applying this answer.
Adding margin-top:-20px; to .item fixed the alignment issue.
The idea here is to overlay an absolute container on top of a relative one. Here's a similar example:
<div class="image">
<img src="images/3754004820_91a5c238a0.jpg" alt="" />
<h2>A Movie in the Park:<br />Kung Fu Panda</h2>
</div>
The CSS:
.image {
position: relative;
width: 100%; /* for IE 6 */
}
h2 {
position: absolute;
top: 200px;
left: 0;
width: 100%;
}
This is going to put our text right up on top of the image nicely, but it doesn't accomplish the box we want to achieve behind the text. For that, we can't use the h2, because that is a block level element and we need an inline element without an specific width. So, wrap the h2 inside of a span.
<h2><span>A Movie in the Park:<br />Kung Fu Panda</span></h2>
Then use that span to style and text:
h2 span {
color: white;
font: bold 24px/45px Helvetica, Sans-Serif;
letter-spacing: -1px;
background: rgb(0, 0, 0); /* fallback color */
background: rgba(0, 0, 0, 0.7);
padding: 10px;
}
For ideas on how to ensure proper spacing or to use jQuery to cleanup the code a bit by allowing you to remove some of the tags from the code and jQuery them back in, check the source.
Here's a fiddle I made with the sample code:
https://jsfiddle.net/un2p8gow/
I changed the notify-badge span into a div. I saw no reason it had to be a span.
I changed the position to relative. Edit - you could actually keep the attribute position: absolute; provided you know what you're doing with it. Guy in the comments was right.
You had the attribute right: 1.5rem; and I simply changed it to left because it was being inset in the opposite direction of your example.
You can tweak it further but in a vacuum this is what you want.
I've got multiple div elements nested in a div. What I want is for the #title-text div to be next to the image - in this case using a float.
The problem is that the text in the div spaces out the div when it has no more room in width as you can see in this jsFiddle.
I want to use floats because there will be multiple tiles using the same classes, and those tiles have different sizes and images etc. Also I don't want to have the #title-text to have a fixed width because of multiple tiles using it and thus having different widths.
Here's the HTML:
<div id="tile-wrapper">
<div id="category-text">
<p class="category-content">Smartphones / software</p>
</div>
<div id="tile-image">
<img src="images/wp10.jpg" class="tile-image" name="title" />
</div>
<div id="title-text">
Placeholder Text placeholder text placeholder text placeholder text
</div>
<div id="date-time-text">
<p class="date-time">3 minutes ago.</p>
</div>
</div>
Okay, so first I added display inline to your classes like this
.tile-image {
margin: 0 auto;
display:inline;
}
.title-text {
margin: 0 5px 5px 0px;
font-weight: 300;
text-shadow: 1px 1px 3px rgba(0,0,0,.3);
text-decoration: none;
font-size: 22px;
color: black;
display:inline;
}
then I put a width and alignment on #title-text
#title-text {
float: left;
margin-left: 5px;
margin-bottom:10px;
text-align:left;
width:50%;
}
I found the answer to my question with Jquery. Here's the solution if anyone wonders.
$(document).ready(function () {
var imageWidth = $('#tile-image').width();
var titleTextWidth = $('#title-text').width();
$('#title-text').width(imageWidth);
$('#tile-wrapper').width(imageWidth * 2 + 30); // +30 for margins.
});
I’m trying to recreate this sort of layout:
This is the code I’m currently using to accomplish it:
<table style="border:0px;">
<tbody>
<tr style="border:0px;">
<td><img src="twophones.jpg" alt="" /></td>
<td>
<table style="border:0px;">
<tbody>
<tr width="100%" style="border:0px;">
<td width="100%">
<center>
<h11>DISCOVER THE BRANDS AND STYLES DESIGNED FOR YOU</h11>
<br>
<h33>Coming soon to the App Store and Google Play.</h33>
<table style="border:0px; width:410px;">
<tr style="border:0px;"><td style="border:0px;"><img src="dot.png"></td></tr>
<tr style="border:0px;" width="410">
<td style="border:0px;"><img src="app.jpg" alt="" /></td>
<td><img src="android.jpg" alt="" /></td>
</tr>
</table>
</center>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
Unfortunately, I’m sick of maintaining this table gunk. How can I maintain the same layout, but using standard CSS techniques?
Here are a couple of my attempts:
<div id="parent"> <div id="viewport">
<a href="#">
<img src="twophones.jpg" style="float:left;> <img src="twophones.jpg" alt="" />
<h11 style="width:100%;float:right; display: table-cell; vertical-align: middle;">DISCOVER THE BRANDS AND STYLES DESIGNED FOR YOU</h11>
<span><h11>DISCOVER THE BRANDS AND STYLES DESIGNED FOR YOU</h11><br>
<h33>Coming soon to the App Store and Google Play.</h33>
<br>
<h33 style="width:100%;float:right; display: table-cell; vertical-align: middle;">Coming soon to the App Store and Google Play.</h33>
</span>
</a>
</div> </div>
<div id="parent"> <div id="parent">
<img src="twophones.jpg" style="float:left;"> <img src="twophones.jpg" style="float:left;>
<div style="width:65%;float:right;"> <div style="width:65%;float:right;">
<h11>DISCOVER THE BRANDS AND STYLES DESIGNED FOR YOU</h11>
<h11>DISCOVER THE BRANDS AND STYLES DESIGNED FOR YOU</h11>
<br> <br>
<h33>Coming soon to the App Store and Google Play.</h33>
<h33>Coming soon to the App Store and Google Play.</h33>
</div> </div>
First thing you want do to when doing a layout with CSS is, well, not touching the CSS and dealing purely with the content. How best could we represent this content? I think this includes all the content rather semantically:
<section>
<img src="twophones.jpg" alt="">
<h2>Discover the brands and styles designed for you</h2>
<p>Coming soon to the App Store and Google Play</p>
<ul>
<li class="iphone">
<a href="#">
Available on the
<strong>App Store</strong>
</a>
</li>
<li class="android">
<a href="#">
Available on the
<strong>Android Market</strong>
</a>
</li>
</ul>
</section>
It contains all the content, but it doesn’t look great. It looks sort of like this:
(picture of two phones)
Discover the brands and styles designed for you
Coming soon to the App Store and Google Play
Available on the App Store
Available on the Android Market
Your layout doesn’t quite look like that. First big difference is that nothing’s centered here, but that’s trivial to fix: (take a look)
section {
text-align: center;
}
And what about those buttons? Well, each one functions sort of as a blocky part of the page, but we still want it to be inline, so we’ll apply a display of inline-block. Furthermore, we want the bolded part to be on another line, so we’ll set its display to block, which should force that. Lastly for now, we know it’s got a orangish background and border, and looks like it’s got a little shadow on the text, so putting all this together:
section li a {
display: inline-block;
background: orange; /* fallback for browsers that
don't support gradients */
background: linear-gradient(#f9a60d, #f37111);
color: white;
text-shadow: 0 0 -1px 0 black;
border: 1px solid #e79d48;
border-top-color: #ffe37d;
border-radius: 5px;
box-shadow: 0 5px 0 #a95511;
padding: 8px;
text-decoration: none; /* no underlines on our link, please */
text-align: left; /* within the button, left-aligned */
}
section li a strong {
display: block;
}
Nice buttons! But we could still use some icons on them—fortunately, that’s easy: just add a little more padding on the left and apply a background image: (try it)
section li a {
padding-left: 50px;
}
section li.iphone a {
background: orange url(iphone-icon.png) no-repeat 10px 10px;
background: linear-gradient(#f9a60d, #f37111), url(iphone-icon.png) no-repeat 10px 10px;
}
/* similar for Android */
Now how do you get the buttons to appear in a line? Fortunately, that’s simple. First, remove any margins and padding on the list, then make each item inline-block (try it):
section ul {
margin: 0;
padding: 0;
}
section li {
display: inline-block;
}
Now how about that image on the side? It turns out CSS has us covered. We just tell it we want to float it to the left. As a common trick, we’ll also set an overflow: hidden on the container, so the float is entirely contained within the container. (You can’t see it standalone, but you may see the effect if you try to embed it in a larger web page.)
section {
overflow: hidden;
}
section img {
float: left;
}
Try it. Then we have just one minor visual tweak: we want the header to be uppercased. Fortunately, CSS has us covered there, too! Just apply
section h2 {
text-transform: uppercase;
}
And we’re done. Of course, there’s more you could do: adjust the margins and/or padding to change the spacing; change the font if necessary, etc., etc., but I’ve explored a few techniques that are generally applicable:
Floats are used and abused all the time in CSS. They’re useful.
Changing display can be useful to force elements to display in or out
of a line.
Playing with background can put icons on things.
I don’t mean for this to be a huge code dump; rather, I’d hope you’d learn something out of it, and be able to do similar things yourself.
I don't think I can go any more in-depth or explain anything better than the fantastic answer by icktoofay, but here is a simple layout that could also get you started.
Here is the demo.
Let's start with the basic HTML layout:
<div class="wrap">
<div class="image">
<img src="http://www.placehold.it/400X500" />
</div>
<div class="information">
<h1>DISCOVER THE BRANDS AND STYLES DESIGNED FOR YOU</h1>
<h2>Coming soon to the App Store and Google Play.</h2>
<a class="storeLinks">Play store</a>
<a class="storeLinks">APP store</a>
</div>
</div>
Now let's add in some CSS to layout your HTML elements. In this example:
display: table-cell; can be used to vertically align our content in conjunction with vertical-align: middle; and place our image to the left of the text.
html,body { height: 100%; } allows us to give our wrapping .wrap div a height of 100% so that all the content contained within <div class="wrap"> can be vertically centered.
.wrap > div will target only the divs that are directly after <div class="wrap">.
margin: 0 auto;, along with a fixed width, keep all our content horizontally centered.
* {
margin: 0;
padding: 0;
}
html,body {
height: 100%;
}
.wrap {
display: table;
height: 100%;
width: 900px;
margin: 0 auto;
}
.wrap > div {
display: table-cell;
height: 100%;
vertical-align: middle;
}
.image {
width: 400px;
}
.information {
width: 500px;
text-align: center;
}
h1 {
text-align: center;
padding: 10px;
margin: 10px;
}
h2 {
padding: 10px;
margin: 10px;
}
.storeLinks {
display: inline-block;
padding: 20px;
background: #DDD;
padding: 10px;
}
I am using Joomla, Phoca Gallery Image Component and Phoca Callery module. It is not actaully the question about Joomla, but about CSS. Plugin creates gallery with 4 images. Those images should create 2 x 2 grid, using float:left.
Here is what I have as a result:
http://jsfiddle.net/qAx7c/ (original link: http://renathy.woano.lv/index.php/lv/par-mums-2)
.block {
border:1px solid #342e2b;
border-radius:7px;
padding: 12px 22px 12px 22px;
}
.block-box2 div.content-main {
width:50%;
display:inline-block;
float:left;
}
.block-box2 div.content-sidebar2 {
width:49.99%;
float:right;
}
/* float clearing for IE6 */
* html .clearfix{
height: 1%;
overflow: visible;
}
/* float clearing for IE7 */
*+html .clearfix{
min-height: 1%;
}
/* float clearing for everyone else */
.clearfix:after{
clear: both;
content: ".";
display: block;
height: 0;
visibility: hidden;
font-size: 0;
}
/* FIXes */
#phocagallery-module-ri .phocagallery-box-file {
padding: 0 !important;
background: none !important;
}
#phocagallery-module-ri .phocagallery-box-file-first {
background: none !important;
}
#phocagallery-module-ri {
margin-left: 40px !important;
}
#phocagallery-module-ri div.mosaic a img {
border: 1px solid #342e2b !important;
/*border: none !important;*/
}
#phocagallery-module-ri div.mosaic a img, #phocagallery-module-ri div.mosaic img {
-webkit-box-shadow: none !important;
box-shadow: none !important;
}
<div class="block block-box2 clearfix">
<div class="content-main">
<div class="item-page">
<h2>Par mums</h2>
Some text here
Some text here
</div>
</div>
<div class="content-sidebar2">
<div id="phocagallery-module-ri" style="text-align:center;">
<center style="padding:0px;margin:0px;">
<div class="mosaic" style="float:left;padding:5px;width:170px">
<a class="modal-button" title="Atmosfēra" href="">
<img src="phoca_thumb_m_parmums_telpas.jpg" alt="Atmosfēra" width="170" height="150">
</a>
</div>
<div class="mosaic" style="float:left;padding:5px;width:170px">
<a class="modal-button" title="Par mums" href="#">
<img src="phoca_thumb_m_parmums_atmosfera.jpg" alt="Par mums" width="170" height="149">
</a>
</div>
<div class="mosaic" style="float:left;padding:5px;width:170px">
<a class="modal-button" title="Par mums" href="#">
<img src="phoca_thumb_m_parmums_dzerieni.jpg" alt="Par mums" width="170" height="150">
</a>
</div>
<div class="mosaic" style="float:left;padding:5px;width:170px">
<a class="modal-button" title="Par mums ārpusē" href="#">
<img src="phoca_thumb_m_parmums_izskats.jpg" alt="Par mums ārpusē" width="170" height="150">
</a>
</div>
</center>
</div>
<div style="clear:both"></div>
</div>
As you see, one image is not floating correctly. The code of div phocagallery-module-ri is generated automatically.
I tried to change width, marings, paddings of images and divs, but nothing helps - one image is floating incorrectly, however it seems that everything should be fine.
Can you, please, give me some ideas, why this floating is broken?
The first image's code is :
<img src="/images/phocagallery/par_mums/thumbs/phoca_thumb_m_parmums_telpas.jpg" alt="Atmosfēra" width="170" height="150">
And the second image's code is :
<img src="/images/phocagallery/par_mums/thumbs/phoca_thumb_m_parmums_atmosfera.jpg" alt="Par mums" width="170" height="149">
They have different height ( 150 and 149 ), this is the reason.
Changing the second image's height to 150 will works fine.
The issue is that the second image is less tall than the first. Therefore, the second floats next to the first, but the third one also floats left to the first, leaving a gap. The fourth one doesn't fit next to the third, so it wraps to a new line.
So that's the cause. Now for the solution, I'm not a CSS professional, so I cannot say which of the following solutions is best, nor if there is another, better one.
One solution would be to embed each image in a container that has a fixed height, or at least has the same height for each of them.
Other possible solutions would be to use a CSS table way of styling.
Thirdly, adding a clear:both element after each second image (since you only want two on a row) will break the floating.
Given the nature of the site and the pictures in the gallery, you may also choose to make each thumbnail image the same size. That will also solve it, by taking away the trigger of the problem.