Custom graphical border on DIV with CSS - html
Ok, so this is a problem that has been nagging me for a while and I've seen a few good and bad solutions to it. But what is the best solution, and what is the pitfalls, drawbacks and big "No, Nos".
What I want is to create dynamic, flexible DIV-blocks with a custom graphical border. For example a DIV-box with shadows, but not necessarily shadows.
UPDATED:
As, #Jeroen stated bellow in a comment, I am not only asking for "the best way to make shadows". Any crazy custom graphical border.
I know there are some solutions with CSS3 (box-shadow, border-image and border-radius), but it is not 100% cross-browser, specially not if you have to work with one or two versions old browsers.
Example image of what i want to achieve:
or
The example above is actually done with one method I use frequently. It does the job and it does meet all the requirements.
It adapts to different sized DIV-blocks.
It uses custom graphics.
It works cross-browser and versions.
It is pretty easy and fast to apply.
It is JavaScript free, 100% CSS/HTML.
...but of course there are a few cons:
It requires 8 images.
It requires 8 extra DIV-blocks with no real content.
Not very pretty in the source.
HTML DIV-block example:
<div class="flowBox">
<h1>Header 1</h1>
Vivamus tincidun...
<div class="border_t"></div>
<div class="border_b"></div>
<div class="border_l"></div>
<div class="border_r"></div>
<div class="border_br"></div>
<div class="border_bl"></div>
<div class="border_tr"></div>
<div class="border_tl"></div>
</div>
CSS example:
<style type="text/css">
<!--
.flowBox {
background:#FFFFFF;
margin:10px;
float:left;
padding:10px;
width:250px;
position:relative;
}
.border_t {
background:url(border_t.png) repeat-x;
position:absolute;
top:-2px; left:0;
width:100%;
height:2px;
}
.border_b {
background:url(border_b.png) repeat-x;
position:absolute;
bottom:-6px; left:0;
width:100%;
height:6px;
}
.border_l {
background:url(border_l.png) repeat-y;
position:absolute;
top:0; left:-3px;
width:3px;
height:100%;
}
.border_r {
background:url(border_r.png) repeat-y;
position:absolute;
top:0; right:-6px;
width:6px;
height:100%;
}
.border_br {
background:url(border_br.png);
position:absolute;
bottom:-6px; right:-6px;
width:6px;
height:6px;
}
.border_bl {
background:url(border_bl.png);
position:absolute;
bottom:-6px; left:-3px;
width:3px;
height:6px;
}
.border_tr {
background:url(border_tr.png);
position:absolute;
top:-2px; right:-5px;
width:5px;
height:2px;
}
.border_tl {
background:url(border_tl.png);
position:absolute;
top:-2px; left:-2px;
width:2px;
height:2px;
}
-->
</style>
As you can see, it perhaps isn't an optimal solution.
But is there a better way?
UPDATED: There is support for shadows in most browsers and versions, even if it is not one standard. Source using css-shadow: http://pastebin.com/LZHUQRW9
But my question relates not only to shadows.
Full source code: http://pastebin.com/wxFS2PHr
Have a look at http://css3pie.com
This will allow you to use CSS 3 elements in older browsers and should hopefully help to keep your markup cleaner.
You could also include some additional logic which will use CSS 3 for browsers that support it, and revert back to the CSS Pie functionality for other browsers.
You could try something like this: http://robertnyman.com/2010/03/16/drop-shadow-with-css-for-all-web-browsers/
I think there are many more libraries - JavaScript, .htc things, whatever... - to achieve the same.
Edit: I think you won't get around using 8 different pictures. But you could write a javascript that adds the required DIVs on the fly e.g. for each DIV with class border.
That would clean up your HTML markup - but the DOM remains complex..
Perhaps this article on css-tricks using border-image is what you're looking for? The interactive demo it links to seems to do what you ask for.
Of course this solution is only available in browsers that support css3 border-image. The demo above did work for me in FF and Chrome, but not in IE9. According to the Modernizr documentation it can be used to add support for border-image, but I haven't tried that for myself. Should that work then this would give you a relatively clean solution.
Related
What is the best perfomatic way to put a simple icon in HTML?
I'm working in a website with a mobile menu, so had to use a hamburger icon to it. I found 3 ways: Use FontAwesome or some lib like thatUse a SVGUse the three bars charactere I exclude from my options because Font awesome library is a very big lib to use just a simple icon. So I figure out this charactere: ≡. So I thought: "Omg, i just have to put it on my HTML and it's ok!" But I was thinking that it is maybe too easy to be true. So i had to come here and ask you guys: Is this correct? Is this supported by browsers in a large scale? Is there a better way to just use a simple icon? Thanks!
There is truly no right answer for this but here is a simple solution if you only need the three lines. .burger{ border-radius:0; border:0; width:15px; height:2px; background:rgb(0, 0, 0); position:relative; display:inline-block; padding:0; } .burger:after, .burger:before{ content:''; width:inherit; height:inherit; background:inherit; position:absolute; top:4px; left:0; } .burger:before{ top:-4px; } <button class='burger'></button>
Posts adjust Size and location. CSS
I am having an issue making an HTML page where I put posts of images of different sizes. Image width needs to be the same and height will be different. When the top post image is a smaller height I want the lower post image to move up to cover the blank space. You can better understand by looking at the below images: This is what I made (Check links of images please) http://i.stack.imgur.com/An2ca.png This is what I want http://i.stack.imgur.com/dQzFH.png I hope you can understand now. My CSS code is below: .posts{ width:80%; max-width:951px; min-height:990px; border:0px solid black; float:right; margin-left:10px; } .post{ width:300px; min-height:100px; float:left; margin-left:10px; margin-top:10px; border:0px solid green; vertical-align:text-top !important; } .post img{ width:100%; max-width:290px; height:auto; transition:all 1s ease; opacity:1.0; } .post img:hover{ opacity:0.7; }
I recommend that you look first at the basics of what Flexbox does for you by going here: Flexbox. That aside you can try to play around by setting your image holding classes to use display: flex. Fair warning, if you do care about cross browser functionality, make sure flex is supported for the audience you're trying to capture. You can look here: can I use. There are JS Libraries as suggested by the community (see your comments) that will leverage such functionality for you. I also will say that while these libraries may be robust, they will work as long as they achieve the functionality you desire, so take time to explore Masonry and Isotope to see if these provide a solution for you. I also recently found a good read regarding Flex Box: A Visual Guide to CSS3 Flexbox Properties box that you may find interesting and hopefully useful as well :) Otherwise let us know, we will try to continue to help you if you need more assistance.
Sudden cross-browser anomalies with absolute positioned widths
Consider the following code: <div> <input type='text' /> </div> ... div { width:300px; background:#DDD; position:relative; height:100px; } input { position:absolute; left:20px; right:20px; top:10px; bottom:10px; display:block; } A couple of months ago, this would have produced something like this fairly reliably: Now, the width looks a mess in the latest versions of Chrome & IE: http://jsfiddle.net/eY5uj/1/ However, this method still works fine cross-browser for many other elements like <p> for example: http://jsfiddle.net/eY5uj/2/ 2 questions: What is going on here? Is there any workaround (short of removing position:absolute and setting padding on the parent div?)
":hover to change z-index" not working in IE8
I have created a stack of div tags and used z-indexes to make them appear behind each other. They overlap enough for them all to be visible and mouse-overable. I then assigned a :hover to change the z-index and make the div tag which is being hovered over come to the top of the pile. An example of what I have would be... CSS #red-box { position:fixed; width:170px; height:210px; margin-left:70px; top:40px; background-color:red; z-index:3; } #red-box:hover { z-index:5; } #blue-box{ position:fixed; width:170px; height:210px; margin-left:150px; top:70px; background-color:blue; z-index:2; } #blue-box:hover{ z-index:5; } HTML <a id="red-box"></a> <a id="blue-box"></a> I have also created a jsFiddle to help highlight what's going on. This works great in the latest versions of all the browsers but the div tags' z-indexes do not change in IE8. Could anyone help me fix it?
This ought to fix it: http://brenelz.com/blog/squish-the-internet-explorer-z-index-bug/ Basically, just set it to a ridiculously high number. Edit: I just tested this in my version of IE on your JSFiddle, and it worked.
Why is the rendering of this CSS such a problem for IE 6?
I am rendering a small menu in the upper right-hand corner of a web site. There will always be a "Banner" graphic across the top of the page (1024x80 pixels) and the menu must render on top of it. In IE 7, IE 8, FireFox and Safari, the menu looks fine. In IE 6, however, the menu does appear sometimes and fails to appear at other times with no discernable pattern. In the CSS shown below, I placed the "z-index" in the class definitions after reading that this was a fix for some CSS problems of this type in IE 6. However, there is still no joy in Renderville. ANY help will be appreciated! Here is the CSS defined for the page, the header and the menu: Div.XPage { background-color: White; position:relative; width:1024px; border-left:1px solid #a4a4b1; border-right:1px solid #a4a4b1; margin:auto; text-align:left; z-index:10; } Div.XHeader { background-color: White; clear:both; padding:0px; margin:0px; z-index:2; } Div.XTopMenu { position:absolute; left:810px; top: 0px; width:214px; height:16px; background-color:#333333; z-index:3; } Div.XTopMenuItem { width:70px; height:14px; margin-bottom:3px; text-align:center; float:left; } Div.XTopMenuItem a { color: White; font-size:smaller; } Here is the HTML that uses these CSS classes: <div class="XPage"> <div class="XHeader"> <a href='/Home.aspx'><img src="/images/Header.png" alt="Banner Graphic" border="0" width="1024" height="80" /></a> </div> <div class="XTopMenu"> <div class='XTopMenuItem'><a href='/Home.aspx'>Home</a></div> <div class='XTopMenuItem'><a href='/Calendar.aspx'>Calendar</a></div> <div class='XTopMenuItem'><a href='/Logout.aspx'>Log Out</a></div> </div> ... </div>
IE6 has issues with z-indexing. In IE6 z-index is only respected for siblings. Things I'd try: Reverse the order of menu and header in the html Give the header a negative z-index Give the menu a more significant lead on z-index than the header, say z-index:999. Failing those you could try giving IE6 a different header image that didn't cover up where you want the menu to go. In many cases I've strived for IE6 to have degraded support, it's an ancient browser and has way too many problems. So if it's an option I'd consider letting it have a slightly different appearance. Of course that may not be up to you.
z-index only works on positioned elements (position:absolute, position:relative, or position:fixed). You do not have these on Div.XHeader thus that z-index is being ignored completely. Read more about z-index on w3schools
No directly useful info, I'm afraid, just another suggestion in case you're sufficiently desparate: Google has managed to solve this problem, and many more, in GWT. Hold off on the downvotes, please - I'm not sugesting rewriting the app in GWT. I am, however, recommending to look at their code. In their implementation of various components, they have sections, sometimes whole classes, dedicated to working around particular quirks in WebKit, Opera, IE6... Their code is very clear and well commented, often including not just "what" but also "why". How effective this approach is will depend on how good you are at zeroing in on relevant code (in Java), understanding it and moving the implementation of the essentials to your own code.
Why z-index 10 for the Page? It's supposed to be way in the background, right? Could you try setting its index to 0 or 1?