CSS border-radius Property Not Working [duplicate] - html

This question already has answers here:
Border-radius and padding not playing nice
(5 answers)
Closed 7 years ago.
I run my website on Tumblr. I just added the CSS Border-Radius property and set it to 20px for all images with a specific ID. However, as you can see, the corners on the right side of the image are not properly rounded, but the left side corners are.
The CSS code looks like this:
#articleimage {
float:left;
padding-right: 10px;
padding-bottom: 1px;
width: 400px;
border-radius:20px;
}
I've determined that the issue is caused by the padding to the right, but I require that CSS property. How can I stop this conflict?
View screenshot: http://i.stack.imgur.com/es0aa.png

try changing your padding for margin:
#articleimage {
float:left;
margin-right: 10px;
margin-bottom: 1px;
width: 400px;
border-radius:20px;
}

The problem may be due to the use of an <img> tag. The corners may be not fully rounded at the right because the image is prone to be distorted with width and the border-radius (i.e. the image may not fill the entire <img> element, therefore it seems that right border-radius is being "less rounded").
Margins or paddings do not affect, as you can see in the example below:
.cnt {
background-color: green; height: 700px; width: 600px
}
#articleimage,#articleimage2,#articleimage3,#articleimageAsBG {
display: block;
float: left;
width: 400px;
-moz-border-radius: 30px;
-webkit-border-radius: 30px;
border-radius: 30px;
}
#articleimage {
padding-right: 10px;
padding-bottom: 1px;
}
#articleimage2 {
margin-right: 10px;
margin-bottom: 1px;
}
#articleimage3 {
padding-right: 10px;
padding-bottom: 1px;
width: 100px;
}
#articleimageAsBG {
height: 192px;
background: url('http://i.stack.imgur.com/es0aa.png') no-repeat center black;
background-size: 98%;
}
<div class="cnt">
<img id="articleimage" src="http://i.stack.imgur.com/es0aa.png" />
<img id="articleimage2" src="http://i.stack.imgur.com/es0aa.png" />
<img id="articleimage3" src="http://i.stack.imgur.com/es0aa.png" />
<div id="articleimageAsBG">
</div>
</div>
You notice:
#articleimage is using padding and the right border-radius are slightly smaller.
#articleimage2 is using margin and the right border-radius are equally smaller.
#articleimage3 has reduced width (tiny image) so you can notice the difference.
The alternative, and solution I am suggesting to you, is to use another element (like a div) where you set that image to the background like the last one in the example (scroll down to see #articleimageAsBG), you just need to adjust its background-size property.
I also suggest that you add:
-moz-border-radius: 30px;
-webkit-border-radius: 30px;
For better browser compatibility. And maybe consider using display: inline-block instead of float. Hope it helps!

Related

HTML/CSS How to make such a button? [duplicate]

This question already has answers here:
Border-radius in percentage (%) and pixels (px) or em
(3 answers)
Closed 1 year ago.
I want to style a button and make it look like this:
I tried the code below, but it looks totally different.
button {
width: 500px;
height: 30px;
border-radius: 50%;
background-color: #f0f0f0;
}
I am interested in the border radius.
I suggest you to use something like this:
button {
width: 500px;
height: 30px;
border-radius: 15px; /* here we just change the radius to a fixed one, according to fixed height */
border: none; /* here we remove black border */
background-color: #f0f0f0;
}
For text, you can use text formating.
You have border-radius wrong. With your border-radius : 50%, will try to make the button oval kind of shape. So use px units to make rounded corners. Also there is default border on the button, so you have to set border to none. Below css would do the same thing :
button {
width: 500px;
height: 30px;
border-radius: 10px;
background-color: #f0f0f0;
border: none;
cursor: pointer;
font-family: monospace;
}
Set border-radius to px instead of %. Half of the height (30px) will make it nice and round. Anything less will make it less rounded.
button {
width: 500px;
height: 30px;
border-radius: 15px;
background-color: #f0f0f0;
}
button {
width: 500px;
height: 30px;
border-radius: 15px;
border: none;
background-color: #f0f0f0;
}
<button>text here</button>

input field won't size correctly

i have a really annoying issue with sizing an input field and i don't understand how it works.
I got this code. HTML:
<div class="container">
<div class="receipt">
<p class="location"></p>
<input type="text" id="checkoutField">
<div class="checkoutButton">
<a href="#/checkout">
<p>some button</p>
</a>
</div>
</div>
</div>
The container got a max width of 480px. And i want both the checkoutButton div and the input field to stretch out to that width limit, while also having a 20px margin on both sides. The elements should also be responsive, which is why they doesnt have a fixed size.
This works fine on the div, but i cant get the input field to work the same..
I made a jsfiddle that includes the CSS code aswell: jsfiddle
Why is the input behaving like this and how do i fix it?
Instead of calling margin left & right to individual items, it's better call padding for parent container.
Chk the Modified code - http://jsfiddle.net/k7vzod4y/3/
.receipt {
padding: 0 20px 24px;
}
.receipt .checkoutButton {
margin: 0;
}
.receipt #checkoutField {
width: 100%;
margin: 0;
}
Hope that helps.
It is possible easily using CSS3 calc function.
You could set you width to 100% - 40px to take care of your margins.
Something like this:
.receipt #checkoutField {
width: calc(100% - 40px);
border: 0;
height: 40px;
background-color: #35aba2;
border-radius: 4px;
margin-top: -6px;
margin-left: 20px;
}
You can see this in action in you updated fiddle
So after looking at your code example I would use the following method. I used the following on the JSFiddle you linked and it worked as you mentioned you wanted it to.
Set the width of the input field to 100%:
.receipt #checkoutField {
border: 0;
width: 100%;
height: 40px;
background-color: #35aba2;
border-radius: 4px;
margin-top: -6px;
margin-left: 20px;
margin-right: 20px;
}
And then 100% width on the checkout button as well:
.receipt .checkoutButton {
width: 100%;
height: 50px;
background-color: #35aba2;
border-radius: 4px;
margin-left: 20px;
margin-right: 20px;
cursor: pointer;
margin-top: -6px;
}
This method is also responsive because I have used percentages which are related units so they inherit from there parent. So a width of 100% will always stay at the full width of it's parent regardless of the viewport size.

Making a link in top bar full height

I am trying to place some buttons in my top bar where you can choose the display language. Those buttons should have full height in the top bar like the other buttons:
But for some reason I can't get those buttons to full height:
Here is a fiddle with my html and css setup: http://jsfiddle.net/gLgwm/1/
I tried using the following CSS which does not work:
#CtlLanguageSelection,
#CtlLanguageSelection a {
line-height: 35px !important;
padding-top: 0px;
padding-bottom: 0px;
padding-left: 5px;
padding-right: 5px;
}
Also tried setting min-height and height to 100% and 35px, did not work.
The a is an inline element, so it takes much space as the text. Make it an inline-block
http://jsfiddle.net/gLgwm/4/
#CtlLanguageSelection a {
line-height: 35px !important;
padding-top: 0px;
padding-bottom: 0px;
padding-left: 5px;
padding-right: 5px;
display: inline-block;
cursor:pointer;
}
Add this:
#CtlLanguageSelection a {
height: 100%;
display: inline-block;
}
If we set them to height: 100%; and give them display: inline-block it will work just fine.
DEMO HERE
Note: If you don't want that little gap between them (caused by inline-block) you can do this:
<a id="CtlChangeLanguageDE">de</a><a id="CtlChangeLanguageEN">en</a>
Just a little trick to sort it, there are many other ways so you can look them up if needed.
DEMO HERE

Correctly aligning image captions

How can I achieve a layout like this?
Right now I'm using this HTML:
<div class="image">
<img>
<div class="caption">
Caption Text
</div>
</div>
And this CSS:
.image {
background-color: #2A2A2A;
}
img {
max-width: 590px;
}
But the .image box is too big (since it expands to fit its parent):
The key is to not set a width for the img element, or the parent container. If the parent, .image is simply floated or in any other way adapted so that it shrinks to the size of its contents, this should work.
I used float to achieve the shrink-wrap aspect, but position: absolute; would do the same, as would display: inline-block;.
There's a demo over at JS Bin, which uses some jQuery to swap the images around, but it does nothing to the width of any elements. The CSS is reproduced below:
.image {
float: left; // for the shrink wrap
padding: 1em; // To achieve the bordered effect
background-color: #666; // just for contrast
-moz-border-radius: 2em; // for that web 2.0 goodness...
-webkit-border-radius: 2em;
border-radius: 2em;
}
.image img {
-moz-border-radius: 2em; // no width, anywhere. Presumably width: auto, would
-webkit-border-radius: 2em; // work, but that's redundant, since it's the default
border-radius: 2em;
}
.image img + .caption {
width: 100%; // forcing the .caption to take up 100% of the width
background-color: #ffa; // of its parent, except for the padding, so that it's
} // the same width as the image above.
As #Kyle said, block elements adjust their width to fit their parent's.
Setting a block element as inline though, is not the correct approach: what you need to do, is to set the .image div as a floating element, thus achieving a similar result, while keeping the features of a block element. The css to do the trick should be:
.image {
float: left;
display: inline; /* needed to fix the (IE <= 6) "3 pixels out of nowhere bug" */
/* whatever code you may find appropriate in order to render the rounded border */
}
.image .caption {
clear: left;
}
I left to you any further style improvement you may feel needed.
If you set the width of the .image box to the same width as the image, then apply padding to the .image box, you will get the border you are looking for because when you specify width, padding gets added to it.
So basically, you would need the following CSS:
.image {
padding: 10px;
width: 300px; /* assuming the picture is 300px */
}
Try the following:
.image {
position: relative;
width: 250px;
}
img {
border: 15px solid #777777;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
width: 100%;
}
.caption {
border-left: 15px solid #777777;
border-right: 15px solid #777777;
border-bottom: 15px solid #777777;
position: absolute;
width: 100%;
bottom: 0px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
}
<div class="image">
<img src="yourImage" height="150px" />
<div class="caption">
Caption TextCaption TextCaption TextCaption TextCaption Text
</div>
</div>
Now the reason I have applied 3 borders to the caption div is because you do not know the width of the image without the border, but you do know the width of the border for the image. Applying the same border to the caption will give the caption the same width. Of course you will need to adjust the width of .image and the height of the img tag (this can be done through css), but the rest will be done for you. Also the caption div will resize for larger captions.
Regards,
Richard
PS this code has been tried and tested in Chrome - it works fine.
Since divs are block-level elements, they expand to fit their parent.
It may not be the best solution, but if you don't know the size of the image ahead of time, you could do the below:
.image
{
padding: 10px;
max-width: 590px;
disply: inline;
}
.caption
{
background-color: #2A2A2A;
disply: inline;
}
The above will cause the img div to be rendered as an inline element which will shrink it to fit the content rather than its parent, and the padding will add the border.
I have come up with another solution. I dont believe David Thomas' answer makes the caption appear within the image (by all means correct me if I am wrong), so try the code below (I have used a combination of my code and Davids).
.image {
position: relative;
float: left;
border: 15px solid #777777;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
}
.caption {
position: absolute;
bottom: 5px;
left: 5px;
}
.image-container {
position: relative;
}
<div class="image">
<img src="/Images/header1.png" />
<div class="caption">
Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text Caption Text
</div>
</div>

Browser compliant rounded-corner navbar with CSS

I'm working on a browser-compatible navbar with rounded corners using DIVs and rounded images. I had it working perfectly in FireFox, only to discover that IE butchers it (of course).
The only problem I'm having now is getting my content DIV (navBody) to stretch to match the height of the side images. In both browsers now I have this:
http://img80.imageshack.us/img80/5088/40128898.jpg
<div class="navWrapper">
<div class="navLeft"></div>
<div class="navBody">
Login/Register
</div>
<div class="navRight"></div>
</div>
.navRight
{
float: left;
width: 12px;
height: 25px;
background: url('/images/nav/tabright_off.png');
}
.navLeft
{
float: left;
width: 12px;
height: 25px;
margin-left: 3px;
background: url('/images/nav/tableft_off.png');
}
.navBody
{
float: left;
background: #DDDDEE;
white-space: nowrap;
font: bold 12px Verdana, sans-serif;
padding-top: 5px;
overflow: hidden;
}
.navWrapper
{
float: left;
height: 25px;
display: inline;
}
I tried adding 5px padding-bottom to navBody, but this only works on FF and not IE due to box model issues. Setting navBody to a fixed height (tabs should be 20px high) seems to do nothing. Any ideas?
Try adding a
<br style="clear:both" />
To the bottom of the navBody div and see if that helps things.
Not sure why adding a height of 20px in the CSS isn't working (on navBody), that would be my first guess. You could instead try making it height: 25px (to match the sides) but then change the line-height to push your text down (instead of the padding-top).
Another option (that would change the actual design of your nav) would be to set a width on all the nav items. Rendering engines in general prefer to have width set on any floated elements.
Found the issue - FireFox was adding the padding-top (5px) to the 20px height I set to get a total of 25px; IE was not so the height stayed at 20px. Fixed it by making the height 25px by default and compensating in Firefox by cropping out the overflow in the wrapper div.