How to make a div with rounded corners - html

How do you make a div so that its border corners are rounded?

Here it is:
<style type="text/css">
div.test
{
width: 115px;
padding: 10px;
border: 2px solid #000;
border-radius: 15px;
-moz-border-radius: 15px;
}
</style>
<div class="test">This is some text!</div>

Use the border-radius property. The higher the specified amount (typically in px), the more rounded your shape. Example:
myDiv { border-radius:30px;}
Hope that helps.

add this css:
border-top-right-radius:15px;
border-top-left-radius:15px;
border-bottom-right-radius:15px;
border-bottom-left-radius:15px;

With CSS add the code: border-radius: 10px.
I use 10px for example, but you can experiment with however amount of pixels you like.

If you don't want to rely on pixels, you can always use %
border-radius: 50%;

Related

Basic 3-column layout in CSS

I'm doing an exercise to improve my coding and I'm trying to copy the layout from this page http://imgur.com/pM8owcj
I'm stuck with the 3-column section because each column overlaps the other.
I'm a beginner as you can see, I'll really appreciate any help.
Here is the link with the code: http://codepen.io/porpita/pen/ElKty
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="estilo.css" rel="stylesheet" type="text/css">
<title>
Ejercicio Multimedia
</title>
</head>
<body>
<section id="content">
<div id="header">
</div>
<div id="headermenu">
</div>
<div id="imagenprincipal">
</div>
<div id="espacio">
</div>
<div id="galeria">
<div id="columna">
<div id="movimientos"></div>
<div id="eventos"></div>
</div>
<div id="exposiciones"></div>
<div id="noticias"></div>
</div>
</section>
</body>
</html>
CSS
#content {
width: 1144px;
margin: 0 auto;
}
#header {
height: 140px;
border: 3px #000 solid;
}
#headermenu {
height: 40px;
border: 3px #000 solid;
}
#imagenprincipal {
height: 429px;
border: 3px #000 solid;
}
#espacio {
height: 69px;
border: 3px #000 solid;
}
#galeria {
height: 825px;
border: 3px #000 solid;
}
#columna {
width: 338px;
height: 825px;
border: 3px #000 solid;
float: left;
}
#movimientos {
width: 338px;
height: 353px;
border: 3px #000 solid;
}
#eventos {
width: 338px;
height: 472px;
border: 3px #000 solid;
}
#exposiciones {
width: 480px;
height: 825px;
}
#noticias {
width: 326px;
height: 825px;
border: 3px #000 solid;
}
Thanks a lot.
See the Updated Codepen Here: http://codepen.io/anon/pen/tmnrj
The borders you had around the inner elements (#columna, #exposiciones and #noticias) were adding to their widths, so 338px wide with a 3px border all around = 344px wide. You either need to reduce the width to compensate for the 6 pixels of border (3px each side) or set box-sizing: border-box; on the elements so they include the borders in their widths.
Reference on Box Sizing: http://css-tricks.com/box-sizing/
You were also only floating #columna. You need to float all three (#columna, #exposiciones and #noticias) and then set position: relative; overflow: hidden on #galeria to make sure it contains them or it will collapse.
Reference on floats: http://css-tricks.com/all-about-floats/
On a side note - Adding a background colour to the various elements can help identify what was happening with each as you can see in the above fiddle.
Hope that helps.
You're running into space issues because you are using thick pixel borders. Borders are applied to the outside of an element. This means if you set a width of 50px, then add a border of 3px all around, your final width is actually 56px, or 3px + 50px + 3px.
MDN has great documentation on how the CSS box model works, it'll save you hours of stuffing around.
Rather than trying to manually force things into place, it's often easier to allow the browser to calculate it for you by using percentages instead of fixed values like pixels.
It's also handy to test using background-color rather than border to identify elements, as the background color won't effect the actual width of your element.
Here's an example of your layout using percentages: Fiddle

Flexible Border Radius

I have some anchor tags. I am applying border radius to them so that they looks inside a round shape (CIRCLE). It must flexible to their text width. I am trying but not getting what to do.
FIDDLE
How can I achieve that? please any suggestion will be appreciated.
abc
abcdefgh
css:
a{
border:1px solid black;
padding:10px;
-webkit-border-radius: 100px;
-moz-border-radius: 100px;
border-radius: 100px;
}
You can give radius in percentage.
CSS:
a{
border:1px solid black;
padding:10px;
-webkit-border-radius: 10%;
-moz-border-radius: 10%;
border-radius: 10%;
}
Here is Updated fiddle
From border radius this is not possible to get same circle around anchor because it depends on anchor text size. if it is different it reflect circle also.
Update:
You can do this with div by giving then width and height: DEMO
Another Example

Inline-blocks won't align unless both contain text

Hi I've got two boxes of the same size, but for some reason they won't stay on the same "line", one box contains an image, another contains text. Replacing the Image with text causes them to be aligned the way I want them to be. However not having any text in the box cause them to be unaligned.
http://jsfiddle.net/U3b8r/1/
<div class="boxbox">
<sam-box class="box clickable no-padding" base="170" width="1" height="1">
<img src="http://placekitten.com/160/160">
</sam-box>
<sam-box class="box clickable no-padding" base="170" width="1" height="1">
This is a box
</sam-box>
I am using a sam box for custom measurements, I can assure you that the size of both elements is the same(174px to be exact).
You just need to usevertical-align:top; on inline-block elements. Oh, and 'custom' elements probably won't validate and you may have some x-browser issues too.
JSFiddle
CSS
.box {
padding: 10px;
background-color: transparent;
border-radius: 10px;
margin: 5px;
display: inline-block;
vertical-align:top;
font-weight: 400;
border: 2px solid transparent;
}
just add a float:left to your .box class
float:left;
FIDDLE
You might find that adding overflow:hidden; to your .box class fixes your problem - http://jsfiddle.net/U3b8r/4/
Add float:left for your boxes
.box {
float:left;
}
Use float left and set the width and height of the divs. Try like below and modified to your needs.
.box {
width:170px;
height:170px;
float:left;
border-radius: 10px;
margin: 5px;
font-weight: 400;
border: 2px solid transparent;
}
since you already give the Css property display: inline-block; then no need to defined the float: left property.
you only need to add below property in your box class
.box {
padding: 10px;
background-color: transparent;
border-radius: 10px;
margin: 5px;
display: inline-block;
vertical-align:top; /*only add this will work even if the image size is smaller*/
font-weight: 400;
border: 2px solid transparent;
}

Navbar div off centre by border

I have a navbar:
HTML:
<div id="navbar">
</div>
CSS:
#navbar{
width: 100%;
height: 50px;
background-color: #F0F0F0;
border-color: #B2B2B2;
border-style: solid;
-webkit-border-radius: 16px;
-moz-border-radius: 16px;
border-radius: 16px;
}
For some reason, the border makes it offcenter, there is always some space to the left of the div but not on the right. How do I fix this problem???
Remove width: 100% from your CSS. The div is block-level, so will take up all available horizontal space. Adding an explicit 100% just introduces problems when you then give it padding or, in this case, border
Add border-width and CSS3 box sizing to fix the issue.
border-width:2px;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
box-sizing:border-box;
http://jsfiddle.net/ADwwP/1/

Placing border inside of div and not on its edge

I have a <div> element and I want to put a border on it. I know I can write style="border: 1px solid black", but this adds 2px to either side of the div, which is not what I want.
I would rather have this border be -1px from the edge of the div. The div itself is 100px x 100px, and if I add a border, then I have to do some mathematics to make the border appear.
Is there any way that I can make the border appear, and ensure the box will still be 100px (including the border)?
Set box-sizing property to border-box:
div {
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
width: 100px;
height: 100px;
border: 20px solid #f00;
background: #00f;
margin: 10px;
}
div + div {
border: 10px solid red;
}
<div>Hello!</div>
<div>Hello!</div>
It works on IE8 & above.
You can also use box-shadow like this:
div{
-webkit-box-shadow:inset 0px 0px 0px 10px #f00;
-moz-box-shadow:inset 0px 0px 0px 10px #f00;
box-shadow:inset 0px 0px 0px 10px #f00;
}
Example here: http://jsfiddle.net/nVyXS/ (hover to view border)
This works in modern browsers only. For example: No IE 8 support.
See caniuse.com (box-shadow feature) for more info.
Probably it is belated answer, but I want to share with my findings. I found 2 new approaches to this problem that I have not found here in the answers:
Inner border through box-shadow css property
Yes, box-shadow is used to add box-shadows to the elements. But you can specify inset shadow, that would look like a inner border rather like a shadow. You just need to set horizontal and vertical shadows to 0px, and the "spread" property of the box-shadow to the width of the border you want to have. So for the 'inner' border of 10px you would write the following:
div{
width:100px;
height:100px;
background-color:yellow;
box-shadow:0px 0px 0px 10px black inset;
margin-bottom:20px;
}
Here is jsFiddle example that illustrates the difference between box-shadow border and 'normal' border. This way your border and the box width are of total 100px including the border.
More about box-shadow:here
Border through outline css property
Here is another approach, but this way the border would be outside of the box. Here is an example.
As follows from the example, you can use css outline property, to set the border that does not affect the width and height of the element. This way, the border width is not added to the width of an element.
div{
width:100px;
height:100px;
background-color:yellow;
outline:10px solid black;
}
More about outline: here
Yahoo! This is really possible. I found it.
For Bottom Border:
div {box-shadow: 0px -3px 0px red inset; }
For Top Border:
div {box-shadow: 0px 3px 0px red inset; }
You can use the properties outline and outline-offset with a negative value instead of using a regular border, works for me:
div{
height: 100px;
width: 100px;
background-color: grey;
margin-bottom: 10px;
}
div#border{
border: 2px solid red;
}
div#outline{
outline: 2px solid red;
outline-offset: -2px;
}
Using a regular border.
<div id="border"></div>
Using outline and outline-offset.
<div id="outline"></div>
Although this question has already been adequately answered with solutions using the box-shadow and outline properties, I would like to slightly expand on this
for all those who have landed here (like myself) searching for a solution for an inner border with an offset
So let's say you have a black 100px x 100px div and you need to inset it with a white border - which has an inner offset of 5px (say) - this can still be done with the above properties.
box-shadow
The trick here is to know that multiple box-shadows are allowed, where the first shadow is on top and subsequent shadows have lower z-ordering.
With that knowledge, the box-shadow declaration will be:
box-shadow: inset 0 0 0 5px black, inset 0 0 0 10px white;
div {
width: 100px;
height: 100px;
background: black;
box-shadow: inset 0 0 0 5px black, inset 0 0 0 10px white;
}
<div></div>
Basically, what that declaration is saying is: render the last (10px white) shadow first, then render the previous 5px black shadow above it.
outline with outline-offset
For the same effect as above the outline declarations would be:
outline: 5px solid white;
outline-offset: -10px;
div {
width: 100px;
height: 100px;
background: black;
outline: 5px solid white;
outline-offset: -10px;
}
<div></div>
NB: outline-offset isn't supported by IE if that's important to you.
Codepen demo
Use pseudo element:
.button {
background: #333;
color: #fff;
float: left;
padding: 20px;
margin: 20px;
position: relative;
}
.button::after {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
border: 5px solid #f00;
}
<div class='button'>Hello</div>
Using ::after you are styling the virtual last child of the selected element. content property creates an anonymous replaced element.
We are containing the pseudo element using absolute position relative to the parent. Then you have freedom to have whatever custom background and/or border in the background of your main element.
This approach does not affect placement of the contents of the main element, which is different from using box-sizing: border-box;.
Consider this example:
.parent {
width: 200px;
}
.button {
background: #333;
color: #fff;
padding: 20px;
border: 5px solid #f00;
border-left-width: 20px;
box-sizing: border-box;
}
<div class='parent'>
<div class='button'>Hello</div>
</div>
Here .button width is constrained using the parent element. Setting the border-left-width adjusts the content-box size and thus the position of the text.
.parent {
width: 200px;
}
.button {
background: #333;
color: #fff;
padding: 20px;
position: relative;
}
.button::after {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
border: 5px solid #f00;
border-left-width: 20px;
}
<div class='parent'>
<div class='button'>Hello</div>
</div>
Using the pseudo-element approach does not affect the content-box size.
Depending on the application, approach using a pseudo-element might or might not be a desirable behaviour.
I know this is somewhat older, but since the keywords "border inside" landed me directly here, I would like to share some findings that may be worth mentioning here.
When I was adding a border on the hover state, i got the effects that OP is talking about. The border ads pixels to the dimension of the box which made it jumpy.
There is two more ways one can deal with this that also work for IE7.
1)
Have a border already attached to the element and simply change the color. This way the mathematics are already included.
div {
width:100px;
height:100px;
background-color: #aaa;
border: 2px solid #aaa; /* notice the solid */
}
div:hover {
border: 2px dashed #666;
}
2 )
Compensate your border with a negative margin. This will still add the extra pixels, but the positioning of the element will not be jumpy on
div {
width:100px;
height:100px;
background-color: #aaa;
}
div:hover {
margin: -2px;
border: 2px dashed #333;
}
11 Years Later but heres the answer:
Just use outline:
outline: 0.2vw solid red;
I hope i can help someone who sees this question also 11 Yeas Later.
for consistent rendering between new and older browsers, add a double container, the outer with the width, the inner with the border.
<div style="width:100px;">
<div style="border:2px solid #000;">
contents here
</div>
</div>
this is obviously only if your precise width is more important than having extra markup!
If you use box-sizing: border-box means not only border,
padding,margin, etc. All element will come inside of the parent
element.
div p {
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
width: 150px;
height:100%;
border: 20px solid #f00;
background-color: #00f;
color:#fff;
padding: 10px;
}
<div>
<p>It was popularised in the 1960s with the release of Letraset sheets</p>
</div>
Best cross browser solution (mostly for IE support) like #Steve said is to make a div 98px in width and height than add a border 1px around it, or you could make a background image for div 100x100 px and draw a border on it.
You can look at outline with offset but this needs some padding to exists on your div. Or you can absolutely position a border div inside, something like
<div id='parentDiv' style='position:relative'>
<div id='parentDivsContent'></div>
<div id='fakeBordersDiv'
style='position: absolute;width: 100%;
height: 100%;
z-index: 2;
border: 2px solid;
border-radius: 2px;'/>
</div>
You might need to fiddle with margins on the fake borders div to fit it as you like.
A more modern solution might be to use css variables and calc. calc is widely supported but variables is not yet in IE11 (polyfills available).
:root {
box-width: 100px;
border-width: 1px;
}
#box {
width: calc(var(--box-width) - var(--border-width));
}
Although this does use some calculations, which the original questions was looking to avoid. I think this is an ok time to use calculations as they are controlled by the css itself. It also has no need for additional markup or misappropriating other css properties that may be needed later on.
This solution is only really useful if a fixed height isn't needed.
One solution I didn't see mentioned above is the case where you have padding on your input, which I do 99% of the time. You can do something along the lines of...
input {
padding: 8px;
}
input.invalid {
border: 2px solid red;
padding: 6px; // 8px - border or "calc(8px - 2px)"
}
What I like about this is that I have the full menu of border + padding + transition properties for each side.