I'm new to writing HTML and CSS, and I'm having trouble adjusting the text position.
This is what I expected to see:
But here is what I actually got:
and here is my code:
#box {
width: 330px;
height: 212px;
margin-top: 1px;
margin-left: 22.5px;
background-color: orange;
}
#box topic {
font-size: var(--text-big);
font-family: myFirstFont;
margin-top: 20px;
margin-top: 20px;
}
<section id=box>
<topic>XXX</topic>
</section>
You can add this lines to your box class to archive this.
border-radius:10px;
padding: 10px 8px;
box-sizing: border-box;
#box {
width: 330px;
height: 212px;
margin-top: 1px;
margin-left: 22.5px;
background-color: orange;
border-radius:10px;
padding: 10px 8px;
box-sizing: border-box;
}
#box topic {
font-size: var(--text-big);
font-family: myFirstFont;
margin-top: 20px;
margin-top: 20px;
}
<section id="box">
<topic>XXX</topic>
</section>
Add border-radius and padding to your section box
border-radius: 10px; /* this will make rounded edges*/
padding: 20px; /* this will give spacing */
Learn about Padding : https://developer.mozilla.org/en-US/docs/Web/CSS/padding
#box {
width: 330px;
height: 212px;
margin-top: 1px;
margin-left: 22.5px;
background-color: red;
border-radius: 10px;
padding: 20px;
}
#box topic {
font-size: var(--text-big);
font-family: myFirstFont;
margin-top: 20px;
margin-top: 20px;
}
<section id=box>
<topic>XXX</topic>
</section>
Just add some padding to your element (#box), for example padding: 12px; (adjust the value as needed). This will create an "inner distance" between border and contents.
#box {
width: 330px;
height: 212px;
margin-top: 1px;
margin-left: 22.5px;
background-color: #fc0;
padding: 12px;
}
<section id=box>
<topic>XXX</topic>
</section>
One option is as follows, among many others; explanatory comments are in the code:
/* defining custom properties, since you were
using them already: */
:root {
--text: 1em;
--text-big: 1.5em;
--spacing: 1em;
}
/* simple reset to normalise all elements to the same
means of width/height calculation, font, margin and
pading: */
*,
::before,
::after {
box-sizing: border-box;
font: normal 400 var(--text) / 1.5 sans-serif;
margin: 0;
padding: 0;
}
body {
background-color: lightgray;
}
#box {
/* unchanged: */
width: 330px;
height: 212px;
background-color: orange;
/* Added a 2px solid white border to replicate the
aesthetics from your question's 'expected output': */
border: 2px solid #fff;
/* used a custom CSS property to implement the
border-radius: */
border-radius: calc(var(--spacing));
/* Using logical properties to set the block-axis
margin (top and bottom, perpendicular to the
inline-axis, the writing-direction axis): */
margin-block: var(--spacing);
/* Again using logical properties to set the
inline-axis margin, which is - effectively,
but simplistically, the axis of writing-direction: */
margin-inline: var(--spacing);
/* logical properties again, for padding; though note
that I'm using the same --spacing custom property, along
with the CSS calc() function to reduce the size of the
padding to half that of the --spacing property: */
padding-block: calc(var(--spacing)/2);
padding-inline: calc(var(--spacing)/2);
}
#box article {
font-size: var(--text-big);
}
<section id=box>
<!-- Note that there is no <topic> element,
so I replaced that with an <article>
element instead: -->
<article>XXX</article>
</section>
JS Fiddle demo.
References:
calc() function.
CSS Logical Properties.
var() function.
Related
In the example below, is there a way to override the size of the h1 tags so they do not overflow (I do not want to just hide the overflow).
We really want the 'strictContainer' to be 100px of height and not bigger. I searched for an analoguous to size:auto but found nothing.
h1 {
height: 80px; border:solid black
}
.strictContainer {
height: 150px; border:solid gold
}
<div class="strictContainer">
<h1>Hello
</h1>
<h1>Hello
</h1>
</div>
Assuming you cannot modify the original CSS. You can override the size of h1 using a new rule make it have more precedence.
One way to fit both the h1 tags in the container is to make them have equal heights:
/* original rules */
h1 {
height: 80px;
border: 2px solid black
}
.strictContainer {
height: 150px;
border: 2px solid gold;
}
/* overrides */
.strictContainer>h1 {
/* need to override default margins */
margin: auto 0;
/* make them have equal heights*/
height: 50%;
box-sizing: border-box;
/* uncomment following to have auto height */
/*height: auto;*/
}
<div class="strictContainer">
<h1>Hello</h1>
<h1>Hello</h1>
</div>
User agents(browsers) put default styles on some html elements. For example, Chrome has following style on h1 tags by default:
h1 {
display: block;
font-size: 2em;
margin-block-start: 0.67em;
margin-block-end: 0.67em;
margin-inline-start: 0px;
margin-inline-end: 0px;
font-weight: bold;
}
In order to fit both the tags we need to override margins. I used margin: auto 0;. This means vertical margin is auto and horizontal margin is 0.
We've used box-sizing: border-box; to make borders part of the dimensions. Otherwise we would have to calculate height using height: calc(50% - 4px);
solution 2 You can make the container a flexbox:
/* original rules */
h1 {
height: 80px;
border: 2px solid black
}
.strictContainer {
height: 150px;
border: 2px solid gold;
}
/* overrides */
.strictContainer {
display: flex;
flex-direction: column;
justify-content: space-around; /* play with this */
}
.strictContainer>h1 {
/* need to override default margins */
margin: 0;
height: auto;
flex: 0 0 auto;
/* for 50% height use this */
/*flex: 1 0 auto;*/
}
<div class="strictContainer">
<h1>Hello</h1>
<h1>Hello</h1>
</div>
Note: In your code snippet you've used 150px height for the container so I've used the same. If your issue is with margins then you can use
You can use overflow: hidden to hide any content that may overflow, but I think you are asking for height: min-content.
.container {
width: 10em;
padding: 0.5em;
height: min-content;
background: peachpuff;
}
.content {
height: 5em;
border: 1px solid red;
}
<div class="container">
<div class="content">Hello this is some content la la la la la</div>
</div>
Overflow hidden is used to hide element that overflows
overflow:hidden
/**
other values are visible(default),scroll and auto
ww3schools.com/css/css_overflow.asp
**/
/*
you can remove the default setting
for box-sizing you can check out the following link
https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing
*/
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/*
I highly recommend you to use % or vh/vw instead of px
It makes your website responsive
*/
h1 {
height: 50%;
border: 2px solid black;
}
.strictContainer {
height: 150px;
border: 2px solid gold;
}
<div class="strictContainer">
<h1>Hello</h1>
<h1>Hello</h1>
</div>
Edit: here is a CodePen with CSS / HTML
I spend the weekend creating a CSS card for a website, only to realize that it's not responsive, at all. I'm not very well versed in CSS or responsive design, so I am hoping someone with more experience can help me out. So far, I've tried playing around with the #media tag, but I have not had any success. This is the relevant CSS:
#import url('https://fonts.googleapis.com/css?family=Muli&display=swap');
* {
box-sizing: border-box;
}
body {
background: #ffffff;
font-family: 'Muli', sans-serif;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
min-height: 100vh;
margin: 0;
}
.courses-container {
}
.course {
background-color: #fff;
border-radius: 10px;
box-shadow: 0 10px 10px rgba(0, 0, 0, 0.2);
display: flex;
max-width: 100%;
margin: 20px;
overflow: hidden;
width: 1300px;
}
.course h6 {
opacity: 0.6;
margin: 0;
letter-spacing: 1px;
text-transform: uppercase;
}
.course h2 {
letter-spacing: 1px;
margin: 10px 0;
}
.course-preview {
background-color: #2a265f;
color: #fff;
padding: 30px;
max-width: 250px;
}
.course-preview a {
color: #fff;
display: inline-block;
font-size: 12px;
opacity: 0.6;
margin-top: 30px;
text-decoration: none;
}
.course-info {
padding: 30px;
position: relative;
width: 100%;
}
.right-container {
padding: 30px;
background-color: #fff;
width: 30%;
line-height: 200%;
}
.progress-container {
position: absolute;
top: 30px;
right: 30px;
text-align: right;
width: 150px;
}
.progress {
background-color: #ddd;
border-radius: 3px;
height: 5px;
width: 100%;
}
.progress::after {
border-radius: 3px;
background-color: #2a265f;
content: '';
position: absolute;
top: 0;
left: 0;
height: 5px;
width: 10%;
}
.progress-text {
font-size: 10px;
opacity: 0.6;
letter-spacing: 1px;
}
This is a simple suggestion, using CSS Grid. It's a two column card (as yours): the left column width-fixed (300px), the right column width-fluid. I've applied a little gap between them just to make my example clearer.
.card {
max-width: 1000px;
display: grid;
grid-template: "left right" / 300px 1fr;
background-color: #fed330;
border-radius: 10px;
height: 300px;
}
.card>* {
padding: 20px;
}
.left {
grid-area: left;
}
.right {
grid-area: right;
}
#media screen and (max-width: 700px) {
.card {
grid-template: "left" "right" / 100%;
}
}
<div class="card">
<div class="left">
Lorem ipsum....
</div>
<div class="right">
Lorem ipsum...
</div>
</div>
It could be a useful starting point.
#gaston
A good way to test and learn about CSS is to use the browser's "Inspect" feature, with which you can test the css behavior in real time.
Activating, Deactivating features, changing values, and adding new ones.
You see the result in real time.
Then just adjust your code according to your tests.
Just right-click on the area you want to inspect. and then Inspect.
You will see an area with HTML and another with CSS.
Click on the areas in HTML and see the corresponding css.
***** Then just test to find the desired result.
That's how I found the solution in your code:
In the ".course" class of your css you added the "width" property twice.
"max-width: 100%;"
"width: 1000px;"
However, the last property entered has priority over the previous ones.
"width: 1000px;" is defining that your card will ALWAYS have 1000px.
SOLUTION:
Just remove: "max-width: 100%;"
And Modify "width: 1000px;" for "max-width: 1000px;"
So your card will have a maximum of 1000px, the minimum will be defined according to the width of the window
It will look like this:
.course {
background-color: #fff;
border-radius: 10px;
box-shadow: 0 10px 10px rgba (0, 0, 0, 0.2);
display: flex;
margin: 20px;
overflow: hidden;
max-width: 1000px;
}
The #media function will set the css when the screen is adjusted to a minimum or maximum width chosen by you.
What is defined within #media will have priority over other css. but only when the window meets the width you set.
You can use this to change the shape of your card completely to very small screens, placing the purple part on top of the card for example.
If you've solved your problem, mark the right answer to help others.
Good luck.
I have a div that contains two elements. An Icon <i> and a textarea <textarea>.
I know exactly how I want this to look and did a quick mock up with the dimensions figured out.
In case my mock up is too sloppy the layout is this.
The div .container has a width of 220px and a height of 35px width: 220px height:35px.
The two elements in the div are to be 4px apart and 4px from the border of the div. (Which I did with padding)
The icon .fa-plus-square has a height and width of 27px font-size: 27px. (I assumed this sets the width and height to be the same size, but I am unsure. This may be what is causing the problem)
The textarea .input has a width of 181px and a height of 27px width: 181px height: 27px.
Div Mock Up
Whats actually happening I have colored the add div blue to show you what the elements should be contained in.
Html Code
<div class="add">
<i class="far fa-plus-square" type="submit"></i>
<textarea class="input" placeholder="Add to To-Do list"></textarea>
</div>
Css
.add {
background-color: blue;
width: 220px;
height: 35px;
}
.fa-plus-square {
font-size: 27px;
padding-left: 4px;
padding-top: 4px;
padding-bottom: 4px;
color: rgb(254, 217, 67);
}
.input {
padding-left: 4px;
padding-top: 4px;
padding-bottom: 4px;
resize: none;
width: 181px;
height: 27px;
outline: none;
}
Let me know if more information is needed.
I don't have your icon, so I put an white border to simulate it. Here's the code to achieve something similar to your mock (read the comments):
.add * {
box-sizing: border-box; /* For precise calculations */
}
.add {
display: flex; /* Align items in a row, vertically centered */
align-items: center;
background-color: blue;
width: 220px;
height: 35px;
}
.fa-plus-square {
color: rgb(254, 217, 67);
width: 27px; /* The next properties are to make */
max-width: 27px; /* limit the size of the icon no matter */
height: 27px; /* how big the font size. */
max-height: 27px;
margin-left: 4px;
border: 1px solid white; /* So we can see the icon placeholder */
}
/*
Needed to add padding to
the text area
*/
.wrapper {
padding: 4px;
flex-grow: 1; /* Automatic adjustment */
height: 100%;
}
.input {
resize: none;
outline: none;
width: 100%; /* Adapts automatically in size */
height: 100%; /* to what we specified before */
}
<div class="add">
<i class="far fa-plus-square" type="submit"></i>
<div class="wrapper">
<textarea class="input" placeholder="Add to To-Do list"></textarea>
</div>
</div>
The results are precise:
The problem is that you didnt set the box-sizing property at all.
This is the 1st thing you do, always.
We use box-sizing: border-box 99% of the time, and in some very very rare cases we use others.
A fast fix to your layout.
* { box-sizing: border-box; }
.add {
background-color: blue;
max-width: 220px;
// padding here and dispaly
padding: 4px;
display: flex;
align-items: center;
}
.fa-plus-square {
margin-right: 4px; // NOTE HERE
font-size: 31px; // the actual size is 4px smaller // dont know why or how
color: rgb(254, 217, 67);
}
.input {
resize: none;
width: 100%;
height: 100%;
outline: none;
}
<div class="add">
<i class="far fa-plus-square" type="submit"></i>
<textarea class="input" placeholder="Add to To-Do list"></textarea>
</div>
https://codepen.io/ShadiMouma/pen/NWNyWXO?editors=1100
I followed this, this, this and this question. But the solution is not working because the problem is slightly different. I'm using a calendar component called <p-calendar> from primeng. It is a month-picker actually. This component has already a well defined styling in primeng.css. The component by default looks like this:
But I want to put my styling. I want it be to encircled by blue color on hover. I have achieved this:
.
But as you can see month name is pushed towards the top and not in the middle.
Here is primeng.css that I'm trying to modify.
CSS that was already there
.ui-datepicker {
width: 10px;
padding: .2em;
position: absolute;
}
.ui-datepicker.ui-datepicker-inline {
display: inline-block;
position: static;
}
.ui-datepicker .ui-datepicker-group {
border-left-width: 0;
border-right-width: 0;
border-top-width: 0;
border-bottom-width: 0;
}
/* Month Picker */
.ui-monthpicker .ui-monthpicker-month {
width: 33.3%;
display: inline-block;
text-align: center;
padding: .5em;
cursor: pointer;
}
.ui-datepicker-monthpicker select.ui-datepicker-year {
width: auto;
}
CSS that I added later on
.ui-monthpicker-month {
border-radius: 100% !important;
text-decoration: none;
margin-top: 10px;
color: #73767b;
font-family: 'Muli', sans-serif;
height: 50px !important;
width: 50px !important;
padding: 20px 20px 20px 20px;
}
.ui-monthpicker-month:hover {
background-color: #1474A4 !important;
color: white !important;
text-align: center !important;
}
I'm not very good at styling. Any advice and suggestions will be of great help.
PS: I also tried adding padding-top: 15px on :hover but then it started flickering.
Try to do a trick using percentage on the padding top and bottom and set the height element to zero.
.ui-monthpicker-month {
color: #333333;
border-radius: 100% !important;
height: 0 !important;
padding-top: 13% !important;
padding-bottom: 20% !important;
}
.ui-monthpicker-month:hover {
background-color: #1474A4 !important;
color: #fff;
}
And here is the screenshots:
------------------------------ PC ------------------------------
------------------------------ Tablet ------------------------------
------------------------------ Phone ------------------------------
Just add line-height as you need.
I added 2em you can change as required.
.ui-monthpicker .ui-monthpicker-month {
background-color:#3399cc;
width: 33.3%;
display: inline-block;
text-align: center;
padding: .5em;
cursor: pointer;
line-height:2em; // <-- Add line height
}
<div class="ui-monthpicker">
<div class="ui-monthpicker-month">Jan</div>
<div class="ui-monthpicker-month">Feb</div>
</div>
At the top level of my website layout are 4 div tags.
The first one is a full width header section, with css:
#header {
margin-top: 0px;
height: 70px;
border: 4px double rgb(255,255,255);
border-radius: 20px;
background: rgb(88,150,183) no-repeat fixed left top;
padding: 0px;
}
At the bottom is a full width footer:
#footer {
clear: both;
margin: 0px;
color:#cdcdcd;
padding: 10px;
text-align: center;
border: 4px double rgb(88,150,183);
border-radius: 20px;
}
On the left is my main menu section:
#categories {
float:left;
width:150px;
border: 4px double rgb(88,150,183);
border-radius: 20px;
}
All of those 3 elements work fine. They're in the right place and that doesn't change whatever screen resolution the user has on their monitor, or whether they view it on not maximum screen size.
My problem is with the main element of the page - where all the interesting stuff is. It's directly to the right of the menu div - or rather, it should be. My css is:
#main {
float:right;
min-height: 440px;
width: 80%;
margin-bottom: 20px;
padding:20px;
border: 4px double rgb(88,150,183);
border-radius: 20px;
}
width 80% works OK for most of my users, but for those with less resolution, the main element shifts below the menu, which is ghastly.
What I would ideally like is for the width set in the css #main to be something like (100% - 170px), thus leaving a nice margin between the menu and the main bit at all times and never pushing it below the menu. However, css standards don't fulfil that desire yet!
Could someone suggest how I amend my css to give me a nice clean page that's clean for all my users? Or do I need to go back to setting out my page using tables?
Using CSS3 flex
* { box-sizing: border-box; margin: 0; }
#parent{
display: flex;
}
#aside{
width: 170px; /* You, be fixed to 170 */
background: #1CEA6E;
padding: 24px;
}
#main{
flex: 1; /* You... fill the remaining space */
background: #C0FFEE;
padding: 24px;
}
<div id="parent">
<div id="aside">Aside</div>
<div id="main">Main</div>
</div>
Using CSS3 calc
width: calc(100% - 170px);
Example:
* { box-sizing: border-box; margin: 0; }
#aside {
background: #1CEA6E;
width: 170px;
float: left;
padding: 24px;
}
#main {
background: #C0FFEE;
width: calc(100% - 170px);
float: left;
padding: 24px;
}
<div id="aside">Aside</div>
<div id="main">Main</div>
Using float: left; and overflow
* { box-sizing: border-box; margin: 0; }
#aside{
width: 170px; /* You, be fixed to 170 */
float: left; /* and floated to the left */
padding: 24px;
background: #1CEA6E;
}
#main {
background: #C0FFEE;
padding: 24px;
overflow: auto; /* don't collapse spaces */
/* or you could use a .clearfix class (Google for it) */
}
<div id="aside">Aside</div>
<div id="main">Main</div>
Using style display: table;
* { box-sizing: border-box; margin: 0; }
#parent{
display: table;
border-collapse: collapse;
width: 100%;
}
#parent > div {
display: table-cell;
}
#aside{
width: 170px; /* You, be fixed to 170 */
background: #1CEA6E;
padding: 24px;
}
#main{
background: #C0FFEE;
padding: 24px;
}
<div id="parent">
<div id="aside">Aside</div>
<div id="main">Main</div>
</div>
Is this what you are looking for? You don't need any css3
Dont need any css3
.wrapper {
width: 800px;
height: 800px;
background-color: blue;
}
.content {
width: auto;
height: 100%;
background-color: yellow;
}
.menu {
width: 170px;
height: 100%;
float: left;
background-color: red;
}
<div class="wrapper">
<div class="menu">Menu</div>
<div class="content">
Aside
</div>
</div>
You can use 'calc' function supported by all modern browsers and IE9+, or switch to flexbox (supported by IE11+)
See this pen: https://codepen.io/neutrico/pen/MyXmxa
width: calc(100% - 170px);
Keep in mind that all borders matter unless you set 'box-sizing' to 'border-box' (or just remove these borders and apply them on child elements).